[clang] [Clang][Sema] Fix lookup of dependent operator= named by using-declaration (PR #91503)
Krystian Stasiowski via cfe-commits
cfe-commits at lists.llvm.org
Wed May 8 10:02:29 PDT 2024
https://github.com/sdkrystian created https://github.com/llvm/llvm-project/pull/91503
Fixes [this bug](https://github.com/llvm/llvm-project/pull/90152#issuecomment-2100932093) caused by #90152.
Will add tests shortly.
>From 60d2030216403c7cfa8272396497d31aed314288 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski <sdkrystian at gmail.com>
Date: Wed, 8 May 2024 12:18:49 -0400
Subject: [PATCH 1/2] [Clang][Sema] Fix lookup of dependent operator= outside
of complete-class contexts
---
clang/lib/Sema/SemaLookup.cpp | 28 +++++++++++++---------------
clang/lib/Sema/SemaTemplate.cpp | 7 ++-----
2 files changed, 15 insertions(+), 20 deletions(-)
diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index e63da5875d2c9..6bd5932212b92 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -1269,19 +1269,19 @@ struct FindLocalExternScope {
};
} // end anonymous namespace
+static bool isDependentAssignmentOperator(DeclarationName Name,
+ DeclContext *LookupContext) {
+ auto *LookupRecord = dyn_cast_if_present<CXXRecordDecl>(LookupContext);
+ return Name.getCXXOverloadedOperator() == OO_Equal && LookupRecord &&
+ !LookupRecord->isBeingDefined() && LookupRecord->isDependentContext();
+}
+
bool Sema::CppLookupName(LookupResult &R, Scope *S) {
assert(getLangOpts().CPlusPlus && "Can perform only C++ lookup");
DeclarationName Name = R.getLookupName();
Sema::LookupNameKind NameKind = R.getLookupKind();
- // If this is the name of an implicitly-declared special member function,
- // go through the scope stack to implicitly declare
- if (isImplicitlyDeclaredMemberFunctionName(Name)) {
- for (Scope *PreS = S; PreS; PreS = PreS->getParent())
- if (DeclContext *DC = PreS->getEntity())
- DeclareImplicitMemberFunctionsWithName(*this, Name, R.getNameLoc(), DC);
- }
// C++23 [temp.dep.general]p2:
// The component name of an unqualified-id is dependent if
// - it is a conversion-function-id whose conversion-type-id
@@ -1299,9 +1299,8 @@ bool Sema::CppLookupName(LookupResult &R, Scope *S) {
if (isImplicitlyDeclaredMemberFunctionName(Name)) {
for (Scope *PreS = S; PreS; PreS = PreS->getParent())
if (DeclContext *DC = PreS->getEntity()) {
- if (DC->isDependentContext() && isa<CXXRecordDecl>(DC) &&
- Name.getCXXOverloadedOperator() == OO_Equal &&
- !R.isTemplateNameLookup()) {
+ if (!R.isTemplateNameLookup() &&
+ isDependentAssignmentOperator(Name, DC)) {
R.setNotFoundInCurrentInstantiation();
return false;
}
@@ -2472,8 +2471,6 @@ bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx,
}
} QL(LookupCtx);
- bool TemplateNameLookup = R.isTemplateNameLookup();
- CXXRecordDecl *LookupRec = dyn_cast<CXXRecordDecl>(LookupCtx);
if (!InUnqualifiedLookup && !R.isForRedeclaration()) {
// C++23 [temp.dep.type]p5:
// A qualified name is dependent if
@@ -2486,13 +2483,14 @@ bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx,
if (DeclarationName Name = R.getLookupName();
(Name.getNameKind() == DeclarationName::CXXConversionFunctionName &&
Name.getCXXNameType()->isDependentType()) ||
- (Name.getCXXOverloadedOperator() == OO_Equal && LookupRec &&
- LookupRec->isDependentContext() && !TemplateNameLookup)) {
+ (!R.isTemplateNameLookup() &&
+ isDependentAssignmentOperator(Name, LookupCtx))) {
R.setNotFoundInCurrentInstantiation();
return false;
}
}
+ CXXRecordDecl *LookupRec = dyn_cast<CXXRecordDecl>(LookupCtx);
if (LookupDirect(*this, R, LookupCtx)) {
R.resolveKind();
if (LookupRec)
@@ -2604,7 +2602,7 @@ bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx,
// template, and if the name is used as a template-name, the
// reference refers to the class template itself and not a
// specialization thereof, and is not ambiguous.
- if (TemplateNameLookup)
+ if (R.isTemplateNameLookup())
if (auto *TD = getAsTemplateNameDecl(ND))
ND = TD;
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 7e57fa0696725..480bc74c2001a 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -726,7 +726,7 @@ Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
const DeclarationNameInfo &NameInfo,
bool isAddressOfOperand,
const TemplateArgumentListInfo *TemplateArgs) {
- DeclContext *DC = getFunctionLevelDeclContext();
+ QualType ThisType = getCurrentThisType();
// C++11 [expr.prim.general]p12:
// An id-expression that denotes a non-static data member or non-static
@@ -748,10 +748,7 @@ Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
IsEnum = isa_and_nonnull<EnumType>(NNS->getAsType());
if (!MightBeCxx11UnevalField && !isAddressOfOperand && !IsEnum &&
- isa<CXXMethodDecl>(DC) &&
- cast<CXXMethodDecl>(DC)->isImplicitObjectMemberFunction()) {
- QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType().getNonReferenceType();
-
+ !ThisType.isNull()) {
// Since the 'this' expression is synthesized, we don't need to
// perform the double-lookup check.
NamedDecl *FirstQualifierInScope = nullptr;
>From af3a77c52c0b488ab3fc2fdf16db07a77fe8fcd5 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski <sdkrystian at gmail.com>
Date: Wed, 8 May 2024 12:59:24 -0400
Subject: [PATCH 2/2] [Clang][Sema] Fix lookup of dependent operator= named by
using-declaration
---
clang/lib/Sema/SemaDeclCXX.cpp | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 157d42c09cfcd..91c83564b567e 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -12963,14 +12963,15 @@ NamedDecl *Sema::BuildUsingDeclaration(
return nullptr;
}
- DeclContext *LookupContext = computeDeclContext(SS);
NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
- if (!LookupContext || EllipsisLoc.isValid()) {
- NamedDecl *D;
+ DeclContext *LookupContext = computeDeclContext(SS);
+
+ auto BuildDependent = [&] {
+ NamedDecl *D = nullptr;
// Dependent scope, or an unexpanded pack
if (!LookupContext && CheckUsingDeclQualifier(UsingLoc, HasTypenameKeyword,
SS, NameInfo, IdentLoc))
- return nullptr;
+ return D;
if (HasTypenameKeyword) {
// FIXME: not all declaration name kinds are legal here
@@ -12987,7 +12988,7 @@ NamedDecl *Sema::BuildUsingDeclaration(
CurContext->addDecl(D);
ProcessDeclAttributeList(S, D, AttrList);
return D;
- }
+ };
auto Build = [&](bool Invalid) {
UsingDecl *UD =
@@ -13002,6 +13003,9 @@ NamedDecl *Sema::BuildUsingDeclaration(
auto BuildInvalid = [&]{ return Build(true); };
auto BuildValid = [&]{ return Build(false); };
+ if (!LookupContext || EllipsisLoc.isValid())
+ return BuildDependent();
+
if (RequireCompleteDeclContext(SS, LookupContext))
return BuildInvalid();
@@ -13024,6 +13028,9 @@ NamedDecl *Sema::BuildUsingDeclaration(
LookupQualifiedName(R, LookupContext);
+ if (R.wasNotFoundInCurrentInstantiation())
+ return BuildDependent();
+
// Validate the context, now we have a lookup
if (CheckUsingDeclQualifier(UsingLoc, HasTypenameKeyword, SS, NameInfo,
IdentLoc, &R))
More information about the cfe-commits
mailing list