diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -1275,7 +1275,7 @@ Result.resolveKind(); bool ADL = UseArgumentDependentLookup(SS, Result, NextToken.is(tok::l_paren)); - return BuildDeclarationNameExpr(SS, Result, ADL); + return BuildDeclarationNameExpr(SS, Result, ADL, /*AcceptInvalidDecl=*/true); } ExprResult Sema::ActOnNameClassifiedAsOverloadSet(Scope *S, Expr *E) { 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 @@ -3183,8 +3183,9 @@ /// as an expression. This is only actually called for lookups that /// were not overloaded, and it doesn't promise that the declaration /// will in fact be used. -static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { - if (D->isInvalidDecl()) +static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D, + bool AcceptInvalid) { + if (D->isInvalidDecl() && !AcceptInvalid) return true; if (isa(D)) { @@ -3230,7 +3231,8 @@ // result, because in the overloaded case the results can only be // functions and function templates. if (R.isSingleResult() && !ShouldLookupResultBeMultiVersionOverload(R) && - CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) + CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl(), + AcceptInvalidDecl)) return ExprError(); // Otherwise, just build an unresolved lookup expression. Suppress @@ -3262,7 +3264,7 @@ "Cannot refer unambiguously to a function template"); SourceLocation Loc = NameInfo.getLoc(); - if (CheckDeclInExpr(*this, Loc, D)) { + if (CheckDeclInExpr(*this, Loc, D, AcceptInvalidDecl)) { // Recovery from invalid cases (e.g. D is an invalid Decl). // We use the dependent type for the RecoveryExpr to prevent bogus follow-up // diagnostics, as invalid decls use int as a fallback type. @@ -3494,9 +3496,16 @@ break; } - return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS, FoundD, - /*FIXME: TemplateKWLoc*/ SourceLocation(), - TemplateArgs); + auto *E = + BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS, FoundD, + /*FIXME: TemplateKWLoc*/ SourceLocation(), TemplateArgs); + // Clang AST consumers assume a DeclRefExpr refers to a valid decl. We + // wrap a DeclRefExpr referring to an invalid decl with a dependent-type + // RecoveryExpr to avoid follow-up semantic analysis (thus prevent bogus + // diagnostics). + if (VD->isInvalidDecl() && E) + return CreateRecoveryExpr(E->getBeginLoc(), E->getEndLoc(), {E}); + return E; } static void ConvertUTF8ToWideString(unsigned CharByteWidth, StringRef Source, 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 @@ -406,8 +406,17 @@ InvalidDecl + 1; // CHECK: BinaryOperator {{.*}} // CHECK-NEXT: |-RecoveryExpr {{.*}} '' + // CHECK-NEXT: | | `-DeclRefExpr {{.*}} 'InvalidDecl' 'int' // CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 1 InvalidDecl(); // CHECK: CallExpr {{.*}} // CHECK-NEXT: `-RecoveryExpr {{.*}} '' } + +void RecoverToAnInvalidDecl() { + Unknown* foo; // invalid decl + goo; // the typo was correct to the invalid foo. + // Verify that RecoveryExpr has an inner DeclRefExpr. + // CHECK: RecoveryExpr {{.*}} '' contains-errors lvalue + // CHECK-NEXT: `-DeclRefExpr {{.*}} 'foo' 'int *' +} diff --git a/clang/test/SemaTemplate/constraints.cpp b/clang/test/SemaTemplate/constraints.cpp --- a/clang/test/SemaTemplate/constraints.cpp +++ b/clang/test/SemaTemplate/constraints.cpp @@ -15,12 +15,11 @@ template constexpr int test = 0; template requires C constexpr int test = 1; - template requires (B && C) || (X::value && C) constexpr int test = 2; // expected-error {{non-constant expression}} expected-note {{subexpression}} expected-note {{instantiation of}} expected-note {{while substituting}} + template requires (B && C) || (X::value && C) constexpr int test = 2; // expected-note {{instantiation of}} expected-note {{while substituting}} static_assert(test == 2); static_assert(test == 2); static_assert(test == 2); // satisfaction of second term of || not considered static_assert(test == 1); static_assert(test == 2); // constraints are partially ordered - // FIXME: These diagnostics are excessive. - static_assert(test == 1); // expected-note 2{{while}} expected-note 2{{during}} + static_assert(test == 1); // expected-note {{while}} expected-note {{during}} } diff --git a/clang/test/SemaTemplate/cxx2a-constraint-exprs.cpp b/clang/test/SemaTemplate/cxx2a-constraint-exprs.cpp --- a/clang/test/SemaTemplate/cxx2a-constraint-exprs.cpp +++ b/clang/test/SemaTemplate/cxx2a-constraint-exprs.cpp @@ -26,28 +26,22 @@ namespace constant_evaluated { template requires f struct S {}; - // expected-note@-1{{in instantiation of}} expected-note@-1{{while substituting}} \ - expected-error@-1{{substitution into constraint expression resulted in a non-constant expression}} \ - expected-note@-1{{subexpression not valid}} + // expected-note@-1{{in instantiation of}} expected-note@-1{{while substituting}} using s = S; - // expected-note@-1 2{{while checking}} + // expected-note@-1 {{while checking}} template void foo() requires f { }; // expected-note@-1{{in instantiation}} expected-note@-1{{while substituting}} \ - expected-note@-1{{candidate template ignored}} expected-note@-1{{subexpression not valid}} \ - expected-error@-1{{substitution into constraint expression resulted in a non-constant expression}} + expected-note@-1{{candidate template ignored}} int a = (foo(), 0); - // expected-note@-1 2{{while checking}} expected-error@-1{{no matching function}} \ - expected-note@-1 2{{in instantiation}} + // expected-note@-1 {{while checking}} expected-error@-1{{no matching function}} \ + expected-note@-1 {{in instantiation}} template void bar() requires requires { requires f; } { }; - // expected-note@-1{{in instantiation}} expected-note@-1{{subexpression not valid}} \ + // expected-note@-1{{in instantiation}} \ expected-note@-1{{while substituting}} \ - expected-error@-1{{substitution into constraint expression resulted in a non-constant expression}} \ - expected-note@-1 2{{while checking the satisfaction of nested requirement}} + expected-note@-1 {{while checking the satisfaction of nested requirement}} int b = (bar(), 0); template struct M { static void foo() requires f { }; }; - // expected-note@-1{{in instantiation}} expected-note@-1{{subexpression not valid}} \ - expected-note@-1{{while substituting}} \ - expected-error@-1{{substitution into constraint expression resulted in a non-constant expression}} + // expected-note@-1{{in instantiation}} expected-note@-1{{while substituting}} int c = (M::foo(), 0); - // expected-note@-1 2{{while checking}} + // expected-note@-1 {{while checking}} } diff --git a/clang/test/SemaTemplate/instantiate-var-template.cpp b/clang/test/SemaTemplate/instantiate-var-template.cpp --- a/clang/test/SemaTemplate/instantiate-var-template.cpp +++ b/clang/test/SemaTemplate/instantiate-var-template.cpp @@ -28,7 +28,7 @@ template constexpr T b = a; // expected-error {{invalid application of 'sizeof' to an incomplete type 'void'}} static_assert(b == 1, ""); - static_assert(b == 1, ""); // expected-note {{in instantiation of}} expected-error {{not an integral constant}} + static_assert(b == 1, ""); // expected-note {{in instantiation of}} template void f() { static_assert(a == 0, ""); // expected-error {{static assertion failed due to requirement 'a == 0'}} \