[clang][Sema] Fix access checking in nested concept-id substitution #246811
<!--LLVM NEW CONTRIBUTOR COMMENT-->
Hello @ShashwatAgrawal20 :wave:
Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.
All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.
---
Frequently asked questions
How do I add reviewers?
This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.
You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.
What if there are no comments?
If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.
Are any special GitHub settings required to contribute to LLVM?
We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.
---
If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.
Thank you,
The LLVM Community
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Shashwat Agrawal (ShashwatAgrawal20)
<details>
<summary>Changes</summary>
Summary
fixes #<!-- -->246008
Access-control failures during concept satisfaction are diagnosed as SFINAE and recorded on the active SFINAETrap, butSema::SubstTemplateArgumentsInParameterMapping can still return success. In ConstraintSatisfactionChecker::SubstitutionInTemplateArguments, the trap was only checked when substitution returned failure. As a result, a private-member access used in a nested concept-id argument could be ignored, and the enclosing concept could be incorrectly satisfied.
Check Trap.hasErrorOccurred() after parameter-mapping substitution and consume the SFINAE diagnostic, matching the existing atomic-constraint handling.
This regressed in Clang 22 after constraint normalization (#<!-- -->161671).
---
Full diff: https://github.com/llvm/llvm-project/pull/246811.diff
2 Files Affected:
````````````diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp
index 72d2ad4a782e20..86538f484b5f7d 100644
--- a/clang/lib/Sema/SemaConcept.cpp
+++ b/clang/lib/Sema/SemaConcept.cpp
@@ -812,7 +812,12 @@ ConstraintSatisfactionChecker::SubstitutionInTemplateArguments(
// mappings to preserve the outer depths.
if (S.SubstTemplateArgumentsInParameterMapping(
Constraint.getParameterMapping(), Constraint.getBeginLoc(), MLTAL,
- SubstArgs)) {
+ SubstArgs) ||
+ Trap.hasErrorOccurred()) {
+ // Access-control failures are diagnosed as SFINAE, so they don't make
+ // SubstTemplateArgumentsInParameterMapping return true. Check the trap
+ // as well, otherwise the constraint is incorrectly treated as satisfied.
+ // See https://github.com/llvm/llvm-project/issues/246008.
Satisfaction.IsSatisfied = false;
if (Trap.hasErrorOccurred())
consumeSFINAEFailure(Info, Satisfaction);
diff --git a/clang/test/SemaCXX/concept-access-in-concept-id.cpp b/clang/test/SemaCXX/concept-access-in-concept-id.cpp
new file mode 100644
index 00000000000000..4e8f2e40955be8
--- /dev/null
+++ b/clang/test/SemaCXX/concept-access-in-concept-id.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only %s
+
+template <class T, class U> concept same_as = __is_same(T, U);
+template <class T> T &&declval();
+
+template <class T>
+concept has_int_x = same_as<decltype(declval<T>().x), int>;
+
+template <class T>
+using x_t = decltype(declval<T>().x);
+
+template <class T>
+concept has_int_x_via_alias = same_as<x_t<T>, int>;
+
+class A {
+ int x;
+};
+class B {
+public:
+ int x;
+};
+
+static_assert(!has_int_x<A>);
+static_assert(has_int_x<B>);
+static_assert(!has_int_x_via_alias<A>);
+static_assert(has_int_x_via_alias<B>);
</details>
just an observation: having SubstTemplateArgumentsInParameterMapping(...) be able to return true while an error has occurred feels a bit odd
It is a bit odd. SubstTemplateArgumentsInParameterMapping reports failures through its return value, but access-control diagnostics are SFINAE_AccessControl: Sema::EmitDiagnostic records them on the active SFINAETrap and suppresses them, so the transform can complete and return success (false).
I checked the trap at the call site to match the existing patterns in this file: EvaluateAtomicConstraint (isInvalid() || Trap.hasErrorOccurred()) and EvaluateSlow(ConceptIdConstraint) (SubstTemplateArguments(...) || Trap.hasErrorOccurred()). I didn't move it into the helper because SubstituteParameterMappings also calls it during normalization under Sema::NonSFINAEContext, where there's no trap and these diagnostics should remain hard errors.
Happy to leave it as-is with the comment, or fold the check into the helper behind an active-SFINAE guard if you'd prefer that shape.
This is clang's old habit. In Sema a value true usually suggests 'the result contains errors'.
Can you move the test to existing file, e.g. SemaTemplate/concepts.cpp, thanks.
Can you also add an explanation along the lines of
// Note that sometimes the substitution succeeds even with a SFINAE failure.
LGTM with some nits
Please also add a release note, thanks
removed, thanks!
@mikekazakov @ldionne @nickdesaulniers
zyn0217
ShashwatAgrawal20
Summary
fixes #246008
Access-control failures during concept satisfaction are diagnosed as SFINAE and recorded on the active
SFINAETrap, butSema::SubstTemplateArgumentsInParameterMappingcan still return success. InConstraintSatisfactionChecker::SubstitutionInTemplateArguments, the trap was only checked when substitution returned failure. As a result, a private-member access used in a nested concept-id argument could be ignored, and the enclosing concept could be incorrectly satisfied.Check
Trap.hasErrorOccurred()after parameter-mapping substitution and consume the SFINAE diagnostic, matching the existing atomic-constraint handling.This regressed in Clang 22 after constraint normalization (#161671).