[clang] [Clang][Sema] Fix crash when 'this' is used in a dependent class scope function template specialization that instantiates to a static member function (PR #87541)

2024-04-10 Thread Krystian Stasiowski via cfe-commits

sdkrystian wrote:

@rupprecht That's unintended, will revert and address it.

https://github.com/llvm/llvm-project/pull/87541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][Sema] Fix crash when 'this' is used in a dependent class scope function template specialization that instantiates to a static member function (PR #87541)

2024-04-09 Thread Jordan Rupprecht via cfe-commits

rupprecht wrote:

This commit appears to regress an example like this:

```c++
template 
struct Foo {
  template 
  int bar(X x) {
return 0;
  }

  template <>
  int bar(int x) {
return bar(5.0);
  }
};

void call() {
  Foo f;
  f.bar(1);
}
```

Used to compile, now results in an error:
```
PR87541.cpp:10:12: error: call to non-static member function without an object 
argument
   10 | return bar(5.0);
  |^~~
PR87541.cpp:16:5: note: in instantiation of function template specialization 
'Foo::bar' requested here
   16 |   f.bar(1);
  | ^
```

Is that intended?

https://github.com/llvm/llvm-project/pull/87541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][Sema] Fix crash when 'this' is used in a dependent class scope function template specialization that instantiates to a static member function (PR #87541)

2024-04-09 Thread Krystian Stasiowski via cfe-commits

https://github.com/sdkrystian closed 
https://github.com/llvm/llvm-project/pull/87541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][Sema] Fix crash when 'this' is used in a dependent class scope function template specialization that instantiates to a static member function (PR #87541)

2024-04-09 Thread Krystian Stasiowski via cfe-commits

https://github.com/sdkrystian updated 
https://github.com/llvm/llvm-project/pull/87541

>From 6ad6b5e698c3ae6fd8e881582fcfa4c8bb231da4 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski 
Date: Wed, 3 Apr 2024 14:33:27 -0400
Subject: [PATCH 1/4] [Clang][Sema] Fix crash when 'this' is used in a
 dependent class scope function template specialization that instantiates to a
 static member function

---
 clang/include/clang/Sema/Sema.h   |  8 +++-
 clang/lib/Sema/SemaExpr.cpp   |  5 ++-
 clang/lib/Sema/SemaExprCXX.cpp| 44 +--
 clang/lib/Sema/SemaExprMember.cpp | 44 ++-
 .../lib/Sema/SemaTemplateInstantiateDecl.cpp  |  8 
 clang/lib/Sema/TreeTransform.h|  7 +--
 ...ms-function-specialization-class-scope.cpp | 38 ++--
 7 files changed, 118 insertions(+), 36 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 56d66a4486e0e74..57117963d84f1d2 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5428,7 +5428,8 @@ class Sema final : public SemaBase {
 
   ExprResult BuildDeclarationNameExpr(const CXXScopeSpec &SS, LookupResult &R,
   bool NeedsADL,
-  bool AcceptInvalidDecl = false);
+  bool AcceptInvalidDecl = false,
+  bool NeedUnresolved = false);
   ExprResult BuildDeclarationNameExpr(
   const CXXScopeSpec &SS, const DeclarationNameInfo &NameInfo, NamedDecl 
*D,
   NamedDecl *FoundD = nullptr,
@@ -6580,7 +6581,10 @@ class Sema final : public SemaBase {
 SourceLocation RParenLoc);
 
    ActOnCXXThis -  Parse 'this' pointer.
-  ExprResult ActOnCXXThis(SourceLocation loc);
+  ExprResult ActOnCXXThis(SourceLocation Loc);
+
+  /// Check whether the type of 'this' is valid in the current context.
+  bool CheckCXXThisType(SourceLocation Loc, QualType Type);
 
   /// Build a CXXThisExpr and mark it referenced in the current context.
   Expr *BuildCXXThisExpr(SourceLocation Loc, QualType Type, bool IsImplicit);
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 8db4fffeecfe35f..7b91bbe0b2054d3 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -3442,10 +3442,11 @@ static bool 
ShouldLookupResultBeMultiVersionOverload(const LookupResult &R) {
 
 ExprResult Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS,
   LookupResult &R, bool NeedsADL,
-  bool AcceptInvalidDecl) {
+  bool AcceptInvalidDecl,
+  bool NeedUnresolved) {
   // If this is a single, fully-resolved result and we don't need ADL,
   // just build an ordinary singleton decl ref.
-  if (!NeedsADL && R.isSingleResult() &&
+  if (!NeedUnresolved && !NeedsADL && R.isSingleResult() &&
   !R.getAsSingle() &&
   !ShouldLookupResultBeMultiVersionOverload(R))
 return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), 
R.getFoundDecl(),
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index cfb5c6b6f283373..d84c66e5969f74a 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -1415,26 +1415,42 @@ bool Sema::CheckCXXThisCapture(SourceLocation Loc, 
const bool Explicit,
 }
 
 ExprResult Sema::ActOnCXXThis(SourceLocation Loc) {
-  /// C++ 9.3.2: In the body of a non-static member function, the keyword this
-  /// is a non-lvalue expression whose value is the address of the object for
-  /// which the function is called.
+  // C++20 [expr.prim.this]p1:
+  //   The keyword this names a pointer to the object for which an
+  //   implicit object member function is invoked or a non-static
+  //   data member's initializer is evaluated.
   QualType ThisTy = getCurrentThisType();
 
-  if (ThisTy.isNull()) {
-DeclContext *DC = getFunctionLevelDeclContext();
+  if (CheckCXXThisType(Loc, ThisTy))
+return ExprError();
 
-if (const auto *Method = dyn_cast(DC);
-Method && Method->isExplicitObjectMemberFunction()) {
-  return Diag(Loc, diag::err_invalid_this_use) << 1;
-}
+  return BuildCXXThisExpr(Loc, ThisTy, /*IsImplicit=*/false);
+}
 
-if (isLambdaCallWithExplicitObjectParameter(CurContext))
-  return Diag(Loc, diag::err_invalid_this_use) << 1;
+bool Sema::CheckCXXThisType(SourceLocation Loc, QualType Type) {
+  if (!Type.isNull())
+return false;
 
-return Diag(Loc, diag::err_invalid_this_use) << 0;
+  // C++20 [expr.prim.this]p3:
+  //   If a declaration declares a member function or member function template
+  //   of a class X, the expression this is a prvalue of type
+  //   "pointer to cv-qualifier-seq X" wherever X is the current class between
+  //   the optional cv-qualif

[clang] [Clang][Sema] Fix crash when 'this' is used in a dependent class scope function template specialization that instantiates to a static member function (PR #87541)

2024-04-09 Thread via cfe-commits

https://github.com/cor3ntin approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/87541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][Sema] Fix crash when 'this' is used in a dependent class scope function template specialization that instantiates to a static member function (PR #87541)

2024-04-08 Thread Erich Keane via cfe-commits

erichkeane wrote:

I'm OK with this, but would like @cor3ntin to take another look since he 
engaged earlier.


https://github.com/llvm/llvm-project/pull/87541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][Sema] Fix crash when 'this' is used in a dependent class scope function template specialization that instantiates to a static member function (PR #87541)

2024-04-08 Thread Krystian Stasiowski via cfe-commits

sdkrystian wrote:

Ping @erichkeane :)

https://github.com/llvm/llvm-project/pull/87541
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits