diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -18466,9 +18466,12 @@ Cond = CheckSwitchCondition(Loc, SubExpr); break; } - if (Cond.isInvalid()) - return ConditionError(); - + if (Cond.isInvalid()) { + Cond = CreateRecoveryExpr(SubExpr->getBeginLoc(), SubExpr->getEndLoc(), + {SubExpr}); + if (!Cond.get()) + return ConditionError(); + } // FIXME: FullExprArg doesn't have an invalid bit, so check nullness instead. FullExprArg FullExpr = MakeFullExpr(Cond.get(), Loc); if (!FullExpr.get()) diff --git a/clang/test/AST/ast-dump-recovery.cpp b/clang/test/AST/ast-dump-recovery.cpp --- a/clang/test/AST/ast-dump-recovery.cpp +++ b/clang/test/AST/ast-dump-recovery.cpp @@ -211,3 +211,30 @@ } NoCrashOnInvalidInitList = { .abc = nullptr, }; + +void InvalidCondition() { + // CHECK: IfStmt {{.*}} + // CHECK-NEXT: |-RecoveryExpr {{.*}} '' contains-errors + // CHECK-NEXT: | `-UnresolvedLookupExpr {{.*}} + if (invalid()) {} + + // CHECK: WhileStmt {{.*}} + // CHECK-NEXT: |-RecoveryExpr {{.*}} '' contains-errors + // CHECK-NEXT: | `-UnresolvedLookupExpr {{.*}} + while (invalid()) {} + + // CHECK: SwitchStmt {{.*}} + // CHECK-NEXT: |-RecoveryExpr {{.*}} '' contains-errors + // CHECK-NEXT: | `-UnresolvedLookupExpr {{.*}} + switch(invalid()) { + case 1: + break; + } + // FIXME: figure out why the type of ConditionalOperator is not int. + // CHECK: ConditionalOperator {{.*}} '' contains-errors + // CHECK-NEXT: |-RecoveryExpr {{.*}} '' contains-errors + // CHECK-NEXT: | `-UnresolvedLookupExpr {{.*}} + // CHECK-NEXT: |-IntegerLiteral {{.*}} 'int' 1 + // CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 2 + invalid() ? 1 : 2; +} diff --git a/clang/test/SemaTemplate/instantiate-expr-3.cpp b/clang/test/SemaTemplate/instantiate-expr-3.cpp --- a/clang/test/SemaTemplate/instantiate-expr-3.cpp +++ b/clang/test/SemaTemplate/instantiate-expr-3.cpp @@ -65,7 +65,7 @@ void f(T t) { (void)({ if (t) // expected-error{{contextually convertible}} - t = t + 17; + t = t + 17; // expected-error {{invalid operands to binary expression ('N1::X' and 'int')}} t + 12; // expected-error{{invalid operands}} }); }