Skip to content

[Clang] Don't allow implicit this access when checking function constraints #151276

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
Jul 30, 2025

Conversation

zyn0217
Copy link
Contributor

@zyn0217 zyn0217 commented Jul 30, 2025

We allowed implicit this access when checking associated constraints after CWG2369. As a result, some of the invalid function call expressions were not properly SFINAE'ed out and ended up as hard errors at evaluation time.

We tried fixing that by mucking around the CurContext, but that spawned additional breakages and I think it's probably safe to revert to the previous behavior to avoid churns.

Though there is CWG2589, which justifies the previous change, it's not what we're pursuing now.

Fixes #151271
Fixes #145505

Reopens #115838

…raints

We allowed implicit this access when checking associated constraints after
CWG2369. As a result, some of the invalid function call expressions were
not properly SFINAE'ed out and ended up as hard errors at evaluation time.

We tried fixing that by mucking around the CurContext, but that spawned
additional breakages and I think it's probably safe to revert to the
previous behavior to avoid churns.

Though there is CWG2589, which justifies the previous change, it's not
what we're pursuing now.
@zyn0217 zyn0217 requested a review from cor3ntin July 30, 2025 05:31
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 30, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 30, 2025

@llvm/pr-subscribers-clang

Author: Younan Zhang (zyn0217)

Changes

We allowed implicit this access when checking associated constraints after CWG2369. As a result, some of the invalid function call expressions were not properly SFINAE'ed out and ended up as hard errors at evaluation time.

We tried fixing that by mucking around the CurContext, but that spawned additional breakages and I think it's probably safe to revert to the previous behavior to avoid churns.

Though there is CWG2589, which justifies the previous change, it's not what we're pursuing now.

Fixes #151271

Reopens #115838


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

4 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (-1)
  • (modified) clang/lib/Sema/SemaConcept.cpp (-4)
  • (modified) clang/lib/Sema/SemaTemplate.cpp (-2)
  • (modified) clang/test/SemaTemplate/concepts.cpp (+17-17)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index fcd3887ec7a09..8cf8589005654 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -957,7 +957,6 @@ Bug Fixes to C++ Support
 - Fixed an access checking bug when initializing non-aggregates in default arguments (#GH62444), (#GH83608)
 - Fixed a pack substitution bug in deducing class template partial specializations. (#GH53609)
 - Fixed a crash when constant evaluating some explicit object member assignment operators. (#GH142835)
-- Fixed an access checking bug when substituting into concepts (#GH115838)
 - Fix a bug where private access specifier of overloaded function not respected. (#GH107629)
 - Correctly handles calling an explicit object member function template overload set
   through its address (``(&Foo::bar<baz>)()``).
diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp
index 834417f8e15ac..20567a6d9d1a8 100644
--- a/clang/lib/Sema/SemaConcept.cpp
+++ b/clang/lib/Sema/SemaConcept.cpp
@@ -1097,10 +1097,6 @@ static bool CheckFunctionConstraintsWithoutInstantiation(
   }
 
   Sema::ContextRAII SavedContext(SemaRef, FD);
-  std::optional<Sema::CXXThisScopeRAII> ThisScope;
-  if (auto *Method = dyn_cast<CXXMethodDecl>(FD))
-    ThisScope.emplace(SemaRef, /*Record=*/Method->getParent(),
-                      /*ThisQuals=*/Method->getMethodQualifiers());
   return SemaRef.CheckConstraintSatisfaction(
       Template, TemplateAC, MLTAL, PointOfInstantiation, Satisfaction);
 }
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index b76619fc50268..f067aedf1cc9f 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -4749,8 +4749,6 @@ Sema::CheckConceptTemplateId(const CXXScopeSpec &SS,
   EnterExpressionEvaluationContext EECtx{
       *this, ExpressionEvaluationContext::Unevaluated, CSD};
 
-  ContextRAII CurContext(*this, CSD->getDeclContext(),
-                         /*NewThisContext=*/false);
   if (!AreArgsDependent &&
       CheckConstraintSatisfaction(
           NamedConcept, AssociatedConstraint(NamedConcept->getConstraintExpr()),
diff --git a/clang/test/SemaTemplate/concepts.cpp b/clang/test/SemaTemplate/concepts.cpp
index 62a4f95d79c74..6d6052791507d 100644
--- a/clang/test/SemaTemplate/concepts.cpp
+++ b/clang/test/SemaTemplate/concepts.cpp
@@ -1228,25 +1228,25 @@ template <KnownKind T> struct KnownType {
 
 }
 
-namespace GH115838 {
+namespace CWG2369_Regression_2 {
 
-template<typename T> concept has_x = requires(T t) {{ t.x };};
-
-class Publ { public:    int x = 0; };
-class Priv { private:   int x = 0; };
-class Prot { protected: int x = 0; };
-class Same { protected: int x = 0; };
-
-template<typename T> class D;
-template<typename T> requires ( has_x<T>) class D<T>: public T { public: static constexpr bool has = 1; };
-template<typename T> requires (!has_x<T>) class D<T>: public T { public: static constexpr bool has = 0; };
+template <typename T>
+concept HasFastPropertyForAttribute =
+    requires(T element, int name) { element.propertyForAttribute(name); };
+
+template <typename OwnerType>
+struct SVGPropertyOwnerRegistry {
+  static int fastAnimatedPropertyLookup() {
+    static_assert (HasFastPropertyForAttribute<OwnerType>);
+    return 1;
+  }
+};
 
-// "Same" is identical to "Prot" but queried before used.
-static_assert(!has_x<Same>,  "Protected should be invisible.");
-static_assert(!D<Same>::has, "Protected should be invisible.");
+class SVGCircleElement {
+  friend SVGPropertyOwnerRegistry<SVGCircleElement>;
+  void propertyForAttribute(int);
+};
 
-static_assert( D<Publ>::has, "Public should be visible.");
-static_assert(!D<Priv>::has, "Private should be invisible.");
-static_assert(!D<Prot>::has, "Protected should be invisible.");
+int i = SVGPropertyOwnerRegistry<SVGCircleElement>::fastAnimatedPropertyLookup();
 
 }

@zyn0217
Copy link
Contributor Author

zyn0217 commented Jul 30, 2025

@tstellar Can you help us check if clang compiles webkitgtk with this patch? thanks!

@zyn0217 zyn0217 merged commit a9d491b into llvm:main Jul 30, 2025
9 checks passed
@zyn0217 zyn0217 deleted the 151271 branch July 30, 2025 06:07
@zyn0217
Copy link
Contributor Author

zyn0217 commented Jul 30, 2025

(I'll issue a backport in a few days)

zyn0217 added a commit to zyn0217/llvm-project that referenced this pull request Aug 5, 2025
…raints (llvm#151276)

We allowed implicit this access when checking associated constraints
after CWG2369. As a result, some of the invalid function call
expressions were not properly SFINAE'ed out and ended up as hard errors
at evaluation time.

We tried fixing that by mucking around the CurContext, but that spawned
additional breakages and I think it's probably safe to revert to the
previous behavior to avoid churns.

Though there is CWG2589, which justifies the previous change, it's not
what we're pursuing now.

Fixes llvm#151271
Fixes llvm#145505
@zyn0217
Copy link
Contributor Author

zyn0217 commented Aug 5, 2025

#152073

tru pushed a commit that referenced this pull request Aug 5, 2025
…raints (#151276)

We allowed implicit this access when checking associated constraints
after CWG2369. As a result, some of the invalid function call
expressions were not properly SFINAE'ed out and ended up as hard errors
at evaluation time.

We tried fixing that by mucking around the CurContext, but that spawned
additional breakages and I think it's probably safe to revert to the
previous behavior to avoid churns.

Though there is CWG2589, which justifies the previous change, it's not
what we're pursuing now.

Fixes #151271
Fixes #145505
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.

Regression caused by #143096 [clang] friend declaration does not apply during constraint validation
3 participants