aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaConcept.cpp
diff options
context:
space:
mode:
authorYounan Zhang <zyn7109@gmail.com>2024-09-02 13:42:42 +0800
committerGitHub <noreply@github.com>2024-09-02 13:42:42 +0800
commit358165ded3c45115ce587d56ef792a9e7c0214ea (patch)
tree63fe73e19bd0c34ad181b8df5e138268655cf3d4 /clang/lib/Sema/SemaConcept.cpp
parent647f892a7281e99c4209cee07097f6a052ed474f (diff)
downloadllvm-358165ded3c45115ce587d56ef792a9e7c0214ea.zip
llvm-358165ded3c45115ce587d56ef792a9e7c0214ea.tar.gz
llvm-358165ded3c45115ce587d56ef792a9e7c0214ea.tar.bz2
[Clang][Concepts] Correct the CurContext for friend declarations (#106890)
`FindInstantiatedDecl()` relies on the `CurContext` to find the corresponding class template instantiation for a class template declaration. Previously, we pushed the semantic declaration context for constraint comparison, which is incorrect for constraints on friend declarations. In issue #78101, the semantic context of the friend is the TU, so we missed the implicit template specialization `Template<void, 4>` when looking for the instantiation of the primary template `Template` at the time of checking the member instantiation; instead, we mistakenly picked up the explicit specialization `Template<float, 5>`, hence the error. As a bonus, this also fixes a crash when diagnosing constraints. The DeclarationName is not necessarily an identifier, so it's incorrect to call `getName()` on e.g. overloaded operators. Since the DiagnosticBuilder has correctly handled Decl printing, we don't need to find the printable name ourselves. Fixes https://github.com/llvm/llvm-project/issues/78101
Diffstat (limited to 'clang/lib/Sema/SemaConcept.cpp')
-rw-r--r--clang/lib/Sema/SemaConcept.cpp9
1 files changed, 8 insertions, 1 deletions
diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp
index 86d6f30..6a1b325 100644
--- a/clang/lib/Sema/SemaConcept.cpp
+++ b/clang/lib/Sema/SemaConcept.cpp
@@ -1012,7 +1012,14 @@ static const Expr *SubstituteConstraintExpressionWithoutSatisfaction(
// possible that e.g. constraints involving C<Class<T>> and C<Class> are
// perceived identical.
std::optional<Sema::ContextRAII> ContextScope;
- if (auto *RD = dyn_cast<CXXRecordDecl>(DeclInfo.getDeclContext())) {
+ const DeclContext *DC = [&] {
+ if (!DeclInfo.getDecl())
+ return DeclInfo.getDeclContext();
+ return DeclInfo.getDecl()->getFriendObjectKind()
+ ? DeclInfo.getLexicalDeclContext()
+ : DeclInfo.getDeclContext();
+ }();
+ if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
ThisScope.emplace(S, const_cast<CXXRecordDecl *>(RD), Qualifiers());
ContextScope.emplace(S, const_cast<DeclContext *>(cast<DeclContext>(RD)),
/*NewThisContext=*/false);