Skip to content

[Clang] Fix an incorrect assumption on getTemplatedDecl() #131559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2025

Conversation

zyn0217
Copy link
Contributor

@zyn0217 zyn0217 commented Mar 17, 2025

Since a68d20e, we've been calling HandleDelayedAccessCheck() for concept declarations when the declaration contains invalid member accesses.

However, a concept declaration is TemplateDecl such that doesn't contain any TemplatedDecl.

(No release note because I think it'd be better to get backported to 20)

Fixes #131530

Since a68d20e, we've been calling HandleDelayedAccessCheck() for concept
declarations when the declaration contains invalid member accesses.

However, a concept declaration is TemplateDecl such that doesn't contain any
TemplatedDecl.
@zyn0217 zyn0217 requested a review from cor3ntin March 17, 2025 04:52
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 17, 2025

@llvm/pr-subscribers-clang

Author: Younan Zhang (zyn0217)

Changes

Since a68d20e, we've been calling HandleDelayedAccessCheck() for concept declarations when the declaration contains invalid member accesses.

However, a concept declaration is TemplateDecl such that doesn't contain any TemplatedDecl.

Fixes #131530


Full diff: https://github.com/llvm/llvm-project/pull/131559.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaAccess.cpp (+2-2)
  • (modified) clang/test/SemaCXX/concept-crash-on-diagnostic.cpp (+12)
diff --git a/clang/lib/Sema/SemaAccess.cpp b/clang/lib/Sema/SemaAccess.cpp
index f79d9a758e7af..6813786df3fc4 100644
--- a/clang/lib/Sema/SemaAccess.cpp
+++ b/clang/lib/Sema/SemaAccess.cpp
@@ -1518,8 +1518,8 @@ void Sema::HandleDelayedAccessCheck(DelayedDiagnostic &DD, Decl *D) {
   } else if (FunctionDecl *FN = dyn_cast<FunctionDecl>(D)) {
     DC = FN;
   } else if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D)) {
-    if (isa<DeclContext>(TD->getTemplatedDecl()))
-      DC = cast<DeclContext>(TD->getTemplatedDecl());
+    if (auto *D = dyn_cast_if_present<DeclContext>(TD->getTemplatedDecl()))
+      DC = D;
   } else if (auto *RD = dyn_cast<RequiresExprBodyDecl>(D)) {
     DC = RD;
   }
diff --git a/clang/test/SemaCXX/concept-crash-on-diagnostic.cpp b/clang/test/SemaCXX/concept-crash-on-diagnostic.cpp
index 71e55c8290ee4..bd7b11a32e174 100644
--- a/clang/test/SemaCXX/concept-crash-on-diagnostic.cpp
+++ b/clang/test/SemaCXX/concept-crash-on-diagnostic.cpp
@@ -36,3 +36,15 @@ void function() {
 // expected-note@#4 {{candidate template ignored: constraints not satisfied [with IteratorL = Object *, IteratorR = Object *]}}
 // We don't know exactly the substituted type for `lhs == rhs`, thus a placeholder 'expr-type' is emitted.
 // expected-note@#3 {{because 'convertible_to<expr-type, bool>' would be invalid}}
+
+namespace GH131530 {
+
+class foo {
+  struct bar {}; // expected-note {{implicitly declared private}}
+};
+
+template <typename T>
+concept is_foo_concept = __is_same(foo::bar, T);
+// expected-error@-1 {{'bar' is a private member of 'foo'}}
+
+}

@zyn0217 zyn0217 merged commit d911085 into llvm:main Mar 17, 2025
11 checks passed
@zyn0217 zyn0217 deleted the 131530 branch March 17, 2025 08:54
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the quick fix, I would have also added the test that did not crash but issued a diagnostic from the issue as well: #131530

unless we have a similar test already.

@zyn0217
Copy link
Contributor Author

zyn0217 commented Mar 18, 2025

Thank you for the quick fix, I would have also added the test that did not crash but issued a diagnostic from the issue as well: #131530

unless we have a similar test already.

Yeah, probably it merits a followup NFC patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang] ICE in clang 19.1.0, 20.1.0, trunk when accessing private member in concept
4 participants