Skip to content

Commit

Permalink
[Pattern Matching] Fix bug causing incorrect diagnostic
Browse files Browse the repository at this point in the history
Incorrect diagnostic can be emitted when using statement
expressions as a pattern substatement in an inspect with
a trailing `void` return type.

Add a specific check for the void return type. All other
cases are handled when we check for convertibility later
in semantic analysis.

fixes llvm#19
  • Loading branch information
dansarginson authored and Jari Ronkainen committed Mar 26, 2024
1 parent d50d93e commit 611bd6e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16744,8 +16744,17 @@ ExprResult Sema::ActOnStmtExprResult(ExprResult ER) {
// to perform implicit conversion during the copy initialization.
if (getCurScope()->isPatternScope()) {
InspectExpr *IE = getCurFunction()->InspectStack.back().getPointer();
if (IE->hasExplicitResultType())
T = IE->getType();
if (IE->hasExplicitResultType()) {
QualType ER = IE->getType();

// Catch this edge case where we can't make a valid
// statement expression result from a void type.
// All other cases should be caught elsewhere when
// we explore convertibility between the pattern
// substatement and the inspect return type.
if (!ER.getTypePtr()->isVoidType())
T = ER;
}
}

// FIXME: Provide a better location for the initialization.
Expand Down
2 changes: 1 addition & 1 deletion clang/test/SemaCXX/inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void a2() {

void b(int x) {
inspect(x) -> void {
__ => 3; // expected-error {{cannot initialize statement expression result of type 'void' with an rvalue of type 'int'}}
__ => 3; // expected-error {{resulting expression type 'int' must match trailing result type 'void'}}
};
}

Expand Down

0 comments on commit 611bd6e

Please sign in to comment.