← Back to Pull requests

[clang][Sema] Fix access checking in nested concept-id substitution #246811

Open
ShashwatAgrawal20 opened this pull request with 35 lines diff across 3 files
View on GitHub
ShashwatAgrawal20 ShashwatAgrawal20 commented on Nov 28, 2026 07:17 UTC
Author

Summary

fixes #246008

Access-control failures during concept satisfaction are diagnosed as SFINAE and recorded on the active SFINAETrap, but
Sema::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).

Diff: 35 lines (+34 / -1) across 3 files Reviewer: zyn0217 Review duration: 7773m
All Vouch review confidence checks passed
Residual risk 0.02 = Change Risk (0.27) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.27 · 35 lines diff across 3 files
0.27
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.91 · 5 substantive comment(s)
0.91
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
91%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.214
Real PR Conversation Timeline
14 items
github-actions[bot] github-actions[bot] commented on Nov 28, 2026 07:17 UTC
Bot

<!--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.

  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.
  • 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

    llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Nov 28, 2026 07:18 UTC
    Bot

    <!--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, but
    Sema::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:

  • (modified) clang/lib/Sema/SemaConcept.cpp (+6-1)

  • (added) clang/test/SemaCXX/concept-access-in-concept-id.cpp (+26)
  • ``````

    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>

    daniel-petrovic daniel-petrovic commented on code on Nov 29, 2026 08:16 UTC
    Code Review
    clang/lib/Sema/SemaConcept.cpp:1
    @@ -812,7 +812,12 @@ ConstraintSatisfactionChecker::SubstitutionInTemplateArguments(
       // mappings to preserve the outer depths.
       if (S.SubstTemplateArgumentsInParameterMapping(
               Constraint.getParameterMapping(), Constraint.getBeginLoc(), MLTAL,
    -          SubstArgs)) {
    +          SubstArgs) ||
    +      Trap.hasErrorOccurred()) {

    just an observation: having SubstTemplateArgumentsInParameterMapping(...) be able to return true while an error has occurred feels a bit odd

    ShashwatAgrawal20 ShashwatAgrawal20 commented on code on Nov 29, 2026 09:53 UTC
    Code Review
    clang/lib/Sema/SemaConcept.cpp:1
    @@ -812,7 +812,12 @@ ConstraintSatisfactionChecker::SubstitutionInTemplateArguments(
       // mappings to preserve the outer depths.
       if (S.SubstTemplateArgumentsInParameterMapping(
               Constraint.getParameterMapping(), Constraint.getBeginLoc(), MLTAL,
    -          SubstArgs)) {
    +          SubstArgs) ||
    +      Trap.hasErrorOccurred()) {

    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.

    zyn0217 zyn0217 commented on code on Nov 30, 2026 11:03 UTC
    Code Review
    clang/lib/Sema/SemaConcept.cpp:1
    @@ -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.

    zyn0217 zyn0217 commented on code on Nov 30, 2026 11:05 UTC
    Code Review
    clang/lib/Sema/SemaConcept.cpp:1
    @@ -812,7 +812,12 @@ ConstraintSatisfactionChecker::SubstitutionInTemplateArguments(
       // mappings to preserve the outer depths.
       if (S.SubstTemplateArgumentsInParameterMapping(
               Constraint.getParameterMapping(), Constraint.getBeginLoc(), MLTAL,
    -          SubstArgs)) {
    +          SubstArgs) ||
    +      Trap.hasErrorOccurred()) {

    This is clang's old habit. In Sema a value true usually suggests 'the result contains errors'.

    zyn0217 zyn0217 commented on code on Nov 30, 2026 11:06 UTC
    Code Review
    clang/test/SemaCXX/concept-access-in-concept-id.cpp:1
    @@ -0,0 +1,26 @@
    +// RUN: %clang_cc1 -std=c++20 -fsyntax-only %s

    Can you move the test to existing file, e.g. SemaTemplate/concepts.cpp, thanks.

    zyn0217 zyn0217 commented on code on Nov 30, 2026 11:09 UTC
    Code Review
    clang/lib/Sema/SemaConcept.cpp:1
    @@ -812,7 +812,12 @@ ConstraintSatisfactionChecker::SubstitutionInTemplateArguments(
       // mappings to preserve the outer depths.
       if (S.SubstTemplateArgumentsInParameterMapping(
               Constraint.getParameterMapping(), Constraint.getBeginLoc(), MLTAL,
    -          SubstArgs)) {
    +          SubstArgs) ||
    +      Trap.hasErrorOccurred()) {

    Can you also add an explanation along the lines of

    // Note that sometimes the substitution succeeds even with a SFINAE failure.
    

    zyn0217 zyn0217 approved these changes on Nov 30, 2026 11:09 UTC
    Approved Reviewer

    LGTM with some nits

    zyn0217 zyn0217 commented on Nov 30, 2026 11:10 UTC
    Contributor

    Please also add a release note, thanks

    zyn0217 zyn0217 commented on code on Dec 01, 2026 08:05 UTC
    Code Review
    clang/lib/Sema/SemaConcept.cpp:1
    @@ -810,9 +810,14 @@ ConstraintSatisfactionChecker::SubstitutionInTemplateArguments(
     
       // We don't want the template argument substitution into parameter
       // mappings to preserve the outer depths.
    +  // Note that sometimes the substitution succeeds even with a SFINAE failure.
    +  // Access-control failures are diagnosed as SFINAE, so check the trap as well
    +  // to avoid treating the constraint as satisfied.
    +  // See https://github.com/llvm/llvm-project/issues/246008.

    ShashwatAgrawal20 ShashwatAgrawal20 commented on code on Dec 01, 2026 08:10 UTC
    Code Review
    clang/lib/Sema/SemaConcept.cpp:1
    @@ -810,9 +810,14 @@ ConstraintSatisfactionChecker::SubstitutionInTemplateArguments(
     
       // We don't want the template argument substitution into parameter
       // mappings to preserve the outer depths.
    +  // Note that sometimes the substitution succeeds even with a SFINAE failure.
    +  // Access-control failures are diagnosed as SFINAE, so check the trap as well
    +  // to avoid treating the constraint as satisfied.
    +  // See https://github.com/llvm/llvm-project/issues/246008.

    removed, thanks!

    ShashwatAgrawal20 ShashwatAgrawal20 commented on Dec 02, 2026 17:16 UTC
    Author

    @mikekazakov @ldionne @nickdesaulniers

    Reviewers
    Assignees
    ShashwatAgrawal20 ShashwatAgrawal20
    Labels
    risk: low confidence: 91.3% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 27.4% (LOW) · Higher = larger blast radius
    27.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 91.3% · Lower = rushed or shallow review
    91.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 2.4% · Safe to merge
    2.4%