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

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

sdkrystian wrote:

The expression type need to be set to `ASTContext::DependentTy` (currently, 
it's set to `ASTContext::OverloadTy`) if we find a member of a base class (as 
it is for direct members). Should be an easy fix, so I'm thinking of not 
reverting and just opening a PR that will address this.

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


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

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

sdkrystian wrote:

@rupprecht looks like the issue is with non-static members in base classes:
```cpp
struct B
{
int z;
void h(int);
};

template 
struct A : B
{
int y;
void g(int);

template
void f(U); 

template<>
void f(int x)
{
x;
y;
z; // error: reference to overloaded function could not be resolved
g(x);
g(y);
g(z);
g(0);
h(x); // error: call to non-static member function without an object 
argument
h(y);
h(z); // error: no matching function for call to 'h'
h(0); // error: call to non-static member function without an object 
argument
}
};

template struct A; 
```
I'll look into this and see whether it's a trivial fix. If not, I'll revert.

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


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

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

rupprecht wrote:

Still seeing a few more failures, although the original repro is now working.

```c++
struct Base {
int BaseFunc(int x) { return 0; }
int val;
};

template 
struct Foo : Base {
template 
int bar(X x) {
return BaseFunc(val);  // OK
}

template <>
int bar(double x) {
return BaseFunc(0);  // error: call to non-static member function 
without an object argument
}

template <>
int bar(int x) {
return BaseFunc(val);  // error: no matching function for call to 
'BaseFunc'
// note: candidate function not viable: no overload of 'val' matching 
'int' for 1st argument
}
};
```

https://godbolt.org/z/Yn1jbGqW3

We also see a `reference to overloaded function could not be resolved; did you 
mean to call it?`, but I haven't been able to reduce that yet.

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


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

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

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


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

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

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

>From eb389e142b18d1a14d23d9fadea3c503331c2f73 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski 
Date: Tue, 9 Apr 2024 08:31:52 -0400
Subject: [PATCH 1/6] Reapply "[Clang][Sema] Fix crash when 'this' is used in a
 dependent class scope function template specialization that instantiates to a
 static member function (#87541)"

This patch fixes a crash that happens when '`this`' is referenced
(implicitly or explicitly) in a dependent class scope function template
specialization that instantiates to a static member function. For
example:
```
template
struct A
{
template
static void f();

template<>
void f()
{
this; // causes crash during instantiation
}
};

template struct A;
```
This happens because during instantiation of the function body,
`Sema::getCurrentThisType` will return a null `QualType` which we
rebuild the `CXXThisExpr` with. A similar problem exists for implicit
class member access expressions in such contexts (which shouldn't really
happen within templates anyways per [class.mfct.non.static]
p2, but changing that is non-trivial). This patch fixes the crash by building
`UnresolvedLookupExpr`s instead of `MemberExpr`s for these implicit
member accesses, which will then be correctly rebuilt as `MemberExpr`s
during instantiation.
---
 clang/docs/ReleaseNotes.rst   |  2 +
 clang/include/clang/Sema/Sema.h   |  8 ++-
 clang/lib/Sema/SemaExpr.cpp   |  5 +-
 clang/lib/Sema/SemaExprCXX.cpp| 44 +-
 clang/lib/Sema/SemaExprMember.cpp | 42 +
 .../lib/Sema/SemaTemplateInstantiateDecl.cpp  |  8 +++
 clang/lib/Sema/TreeTransform.h| 53 ++---
 ...ms-function-specialization-class-scope.cpp | 59 ++-
 8 files changed, 181 insertions(+), 40 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f5359afe1f0999..6bff80ed4d210b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -520,6 +520,8 @@ Bug Fixes to C++ Support
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
 - Fixed a bug that prevented member function templates of class templates 
declared with a deduced return type
   from being explicitly specialized for a given implicit instantiation of the 
class template.
+- Fixed a crash when ``this`` is used in a dependent class scope function 
template specialization
+  that instantiates to a static member function.
 
 - Fix crash when inheriting from a cv-qualified type. Fixes:
   (`#35603 `_)
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 9769d36900664c..f311f9f3743454 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5439,7 +5439,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,
@@ -6591,7 +6592,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 594c11788f4e7e..45acbf197ea6b4 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() &&
   !ShouldL

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

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

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

>From eb389e142b18d1a14d23d9fadea3c503331c2f73 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski 
Date: Tue, 9 Apr 2024 08:31:52 -0400
Subject: [PATCH 1/5] Reapply "[Clang][Sema] Fix crash when 'this' is used in a
 dependent class scope function template specialization that instantiates to a
 static member function (#87541)"

This patch fixes a crash that happens when '`this`' is referenced
(implicitly or explicitly) in a dependent class scope function template
specialization that instantiates to a static member function. For
example:
```
template
struct A
{
template
static void f();

template<>
void f()
{
this; // causes crash during instantiation
}
};

template struct A;
```
This happens because during instantiation of the function body,
`Sema::getCurrentThisType` will return a null `QualType` which we
rebuild the `CXXThisExpr` with. A similar problem exists for implicit
class member access expressions in such contexts (which shouldn't really
happen within templates anyways per [class.mfct.non.static]
p2, but changing that is non-trivial). This patch fixes the crash by building
`UnresolvedLookupExpr`s instead of `MemberExpr`s for these implicit
member accesses, which will then be correctly rebuilt as `MemberExpr`s
during instantiation.
---
 clang/docs/ReleaseNotes.rst   |  2 +
 clang/include/clang/Sema/Sema.h   |  8 ++-
 clang/lib/Sema/SemaExpr.cpp   |  5 +-
 clang/lib/Sema/SemaExprCXX.cpp| 44 +-
 clang/lib/Sema/SemaExprMember.cpp | 42 +
 .../lib/Sema/SemaTemplateInstantiateDecl.cpp  |  8 +++
 clang/lib/Sema/TreeTransform.h| 53 ++---
 ...ms-function-specialization-class-scope.cpp | 59 ++-
 8 files changed, 181 insertions(+), 40 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f5359afe1f0999..6bff80ed4d210b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -520,6 +520,8 @@ Bug Fixes to C++ Support
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
 - Fixed a bug that prevented member function templates of class templates 
declared with a deduced return type
   from being explicitly specialized for a given implicit instantiation of the 
class template.
+- Fixed a crash when ``this`` is used in a dependent class scope function 
template specialization
+  that instantiates to a static member function.
 
 - Fix crash when inheriting from a cv-qualified type. Fixes:
   (`#35603 `_)
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 9769d36900664c..f311f9f3743454 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5439,7 +5439,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,
@@ -6591,7 +6592,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 594c11788f4e7e..45acbf197ea6b4 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() &&
   !ShouldL

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

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

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

>From eb389e142b18d1a14d23d9fadea3c503331c2f73 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski 
Date: Tue, 9 Apr 2024 08:31:52 -0400
Subject: [PATCH 1/4] Reapply "[Clang][Sema] Fix crash when 'this' is used in a
 dependent class scope function template specialization that instantiates to a
 static member function (#87541)"

This patch fixes a crash that happens when '`this`' is referenced
(implicitly or explicitly) in a dependent class scope function template
specialization that instantiates to a static member function. For
example:
```
template
struct A
{
template
static void f();

template<>
void f()
{
this; // causes crash during instantiation
}
};

template struct A;
```
This happens because during instantiation of the function body,
`Sema::getCurrentThisType` will return a null `QualType` which we
rebuild the `CXXThisExpr` with. A similar problem exists for implicit
class member access expressions in such contexts (which shouldn't really
happen within templates anyways per [class.mfct.non.static]
p2, but changing that is non-trivial). This patch fixes the crash by building
`UnresolvedLookupExpr`s instead of `MemberExpr`s for these implicit
member accesses, which will then be correctly rebuilt as `MemberExpr`s
during instantiation.
---
 clang/docs/ReleaseNotes.rst   |  2 +
 clang/include/clang/Sema/Sema.h   |  8 ++-
 clang/lib/Sema/SemaExpr.cpp   |  5 +-
 clang/lib/Sema/SemaExprCXX.cpp| 44 +-
 clang/lib/Sema/SemaExprMember.cpp | 42 +
 .../lib/Sema/SemaTemplateInstantiateDecl.cpp  |  8 +++
 clang/lib/Sema/TreeTransform.h| 53 ++---
 ...ms-function-specialization-class-scope.cpp | 59 ++-
 8 files changed, 181 insertions(+), 40 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f5359afe1f0999..6bff80ed4d210b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -520,6 +520,8 @@ Bug Fixes to C++ Support
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
 - Fixed a bug that prevented member function templates of class templates 
declared with a deduced return type
   from being explicitly specialized for a given implicit instantiation of the 
class template.
+- Fixed a crash when ``this`` is used in a dependent class scope function 
template specialization
+  that instantiates to a static member function.
 
 - Fix crash when inheriting from a cv-qualified type. Fixes:
   (`#35603 `_)
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 9769d36900664c..f311f9f3743454 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5439,7 +5439,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,
@@ -6591,7 +6592,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 594c11788f4e7e..45acbf197ea6b4 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() &&
   !ShouldL

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

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

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

Can you add the example that caused the revert to the tests?

Else, LGTM.

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


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

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

sdkrystian wrote:

@erichkeane Apologies, I should have structured the commits differently so it's 
clear what the new changes are...

I added an `IsAddressOfOperand` parameter to 
`TreeTransform::TransformUnresolvedLookupExpr` and changed the conditions under 
which `Sema::BuildPossibleImplicitMemberExpr` is called to match those in 
`Sema::ActOnIdExpression`. 

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


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

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

https://github.com/erichkeane commented:

Can you point out the 'diff' from the last patch for me? 

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


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

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

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

>From eb389e142b18d1a14d23d9fadea3c503331c2f73 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski 
Date: Tue, 9 Apr 2024 08:31:52 -0400
Subject: [PATCH 1/3] Reapply "[Clang][Sema] Fix crash when 'this' is used in a
 dependent class scope function template specialization that instantiates to a
 static member function (#87541)"

This patch fixes a crash that happens when '`this`' is referenced
(implicitly or explicitly) in a dependent class scope function template
specialization that instantiates to a static member function. For
example:
```
template
struct A
{
template
static void f();

template<>
void f()
{
this; // causes crash during instantiation
}
};

template struct A;
```
This happens because during instantiation of the function body,
`Sema::getCurrentThisType` will return a null `QualType` which we
rebuild the `CXXThisExpr` with. A similar problem exists for implicit
class member access expressions in such contexts (which shouldn't really
happen within templates anyways per [class.mfct.non.static]
p2, but changing that is non-trivial). This patch fixes the crash by building
`UnresolvedLookupExpr`s instead of `MemberExpr`s for these implicit
member accesses, which will then be correctly rebuilt as `MemberExpr`s
during instantiation.
---
 clang/docs/ReleaseNotes.rst   |  2 +
 clang/include/clang/Sema/Sema.h   |  8 ++-
 clang/lib/Sema/SemaExpr.cpp   |  5 +-
 clang/lib/Sema/SemaExprCXX.cpp| 44 +-
 clang/lib/Sema/SemaExprMember.cpp | 42 +
 .../lib/Sema/SemaTemplateInstantiateDecl.cpp  |  8 +++
 clang/lib/Sema/TreeTransform.h| 53 ++---
 ...ms-function-specialization-class-scope.cpp | 59 ++-
 8 files changed, 181 insertions(+), 40 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f5359afe1f0999..6bff80ed4d210b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -520,6 +520,8 @@ Bug Fixes to C++ Support
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
 - Fixed a bug that prevented member function templates of class templates 
declared with a deduced return type
   from being explicitly specialized for a given implicit instantiation of the 
class template.
+- Fixed a crash when ``this`` is used in a dependent class scope function 
template specialization
+  that instantiates to a static member function.
 
 - Fix crash when inheriting from a cv-qualified type. Fixes:
   (`#35603 `_)
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 9769d36900664c..f311f9f3743454 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5439,7 +5439,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,
@@ -6591,7 +6592,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 594c11788f4e7e..45acbf197ea6b4 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() &&
   !ShouldL

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

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

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

>From eb389e142b18d1a14d23d9fadea3c503331c2f73 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski 
Date: Tue, 9 Apr 2024 08:31:52 -0400
Subject: [PATCH 1/3] Reapply "[Clang][Sema] Fix crash when 'this' is used in a
 dependent class scope function template specialization that instantiates to a
 static member function (#87541)"

This patch fixes a crash that happens when '`this`' is referenced
(implicitly or explicitly) in a dependent class scope function template
specialization that instantiates to a static member function. For
example:
```
template
struct A
{
template
static void f();

template<>
void f()
{
this; // causes crash during instantiation
}
};

template struct A;
```
This happens because during instantiation of the function body,
`Sema::getCurrentThisType` will return a null `QualType` which we
rebuild the `CXXThisExpr` with. A similar problem exists for implicit
class member access expressions in such contexts (which shouldn't really
happen within templates anyways per [class.mfct.non.static]
p2, but changing that is non-trivial). This patch fixes the crash by building
`UnresolvedLookupExpr`s instead of `MemberExpr`s for these implicit
member accesses, which will then be correctly rebuilt as `MemberExpr`s
during instantiation.
---
 clang/docs/ReleaseNotes.rst   |  2 +
 clang/include/clang/Sema/Sema.h   |  8 ++-
 clang/lib/Sema/SemaExpr.cpp   |  5 +-
 clang/lib/Sema/SemaExprCXX.cpp| 44 +-
 clang/lib/Sema/SemaExprMember.cpp | 42 +
 .../lib/Sema/SemaTemplateInstantiateDecl.cpp  |  8 +++
 clang/lib/Sema/TreeTransform.h| 53 ++---
 ...ms-function-specialization-class-scope.cpp | 59 ++-
 8 files changed, 181 insertions(+), 40 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f5359afe1f0999..6bff80ed4d210b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -520,6 +520,8 @@ Bug Fixes to C++ Support
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
 - Fixed a bug that prevented member function templates of class templates 
declared with a deduced return type
   from being explicitly specialized for a given implicit instantiation of the 
class template.
+- Fixed a crash when ``this`` is used in a dependent class scope function 
template specialization
+  that instantiates to a static member function.
 
 - Fix crash when inheriting from a cv-qualified type. Fixes:
   (`#35603 `_)
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 9769d36900664c..f311f9f3743454 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5439,7 +5439,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,
@@ -6591,7 +6592,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 594c11788f4e7e..45acbf197ea6b4 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() &&
   !ShouldL

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

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

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

>From eb389e142b18d1a14d23d9fadea3c503331c2f73 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski 
Date: Tue, 9 Apr 2024 08:31:52 -0400
Subject: [PATCH 1/3] Reapply "[Clang][Sema] Fix crash when 'this' is used in a
 dependent class scope function template specialization that instantiates to a
 static member function (#87541)"

This patch fixes a crash that happens when '`this`' is referenced
(implicitly or explicitly) in a dependent class scope function template
specialization that instantiates to a static member function. For
example:
```
template
struct A
{
template
static void f();

template<>
void f()
{
this; // causes crash during instantiation
}
};

template struct A;
```
This happens because during instantiation of the function body,
`Sema::getCurrentThisType` will return a null `QualType` which we
rebuild the `CXXThisExpr` with. A similar problem exists for implicit
class member access expressions in such contexts (which shouldn't really
happen within templates anyways per [class.mfct.non.static]
p2, but changing that is non-trivial). This patch fixes the crash by building
`UnresolvedLookupExpr`s instead of `MemberExpr`s for these implicit
member accesses, which will then be correctly rebuilt as `MemberExpr`s
during instantiation.
---
 clang/docs/ReleaseNotes.rst   |  2 +
 clang/include/clang/Sema/Sema.h   |  8 ++-
 clang/lib/Sema/SemaExpr.cpp   |  5 +-
 clang/lib/Sema/SemaExprCXX.cpp| 44 +-
 clang/lib/Sema/SemaExprMember.cpp | 42 +
 .../lib/Sema/SemaTemplateInstantiateDecl.cpp  |  8 +++
 clang/lib/Sema/TreeTransform.h| 53 ++---
 ...ms-function-specialization-class-scope.cpp | 59 ++-
 8 files changed, 181 insertions(+), 40 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f5359afe1f0999..6bff80ed4d210b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -520,6 +520,8 @@ Bug Fixes to C++ Support
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
 - Fixed a bug that prevented member function templates of class templates 
declared with a deduced return type
   from being explicitly specialized for a given implicit instantiation of the 
class template.
+- Fixed a crash when ``this`` is used in a dependent class scope function 
template specialization
+  that instantiates to a static member function.
 
 - Fix crash when inheriting from a cv-qualified type. Fixes:
   (`#35603 `_)
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 9769d36900664c..f311f9f3743454 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5439,7 +5439,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,
@@ -6591,7 +6592,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 594c11788f4e7e..45acbf197ea6b4 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() &&
   !ShouldL

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

2024-04-10 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Krystian Stasiowski (sdkrystian)


Changes

Reapplies #87541 and addresses the bug which caused expressions naming 
overload sets to be incorrectly rebuilt. 

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


8 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+2) 
- (modified) clang/include/clang/Sema/Sema.h (+6-2) 
- (modified) clang/lib/Sema/SemaExpr.cpp (+3-2) 
- (modified) clang/lib/Sema/SemaExprCXX.cpp (+30-14) 
- (modified) clang/lib/Sema/SemaExprMember.cpp (+30-12) 
- (modified) clang/lib/Sema/SemaTemplateInstantiateDecl.cpp (+8) 
- (modified) clang/lib/Sema/TreeTransform.h (+46-7) 
- (modified) clang/test/SemaTemplate/ms-function-specialization-class-scope.cpp 
(+56-3) 


``diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f5359afe1f0999..6bff80ed4d210b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -520,6 +520,8 @@ Bug Fixes to C++ Support
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
 - Fixed a bug that prevented member function templates of class templates 
declared with a deduced return type
   from being explicitly specialized for a given implicit instantiation of the 
class template.
+- Fixed a crash when ``this`` is used in a dependent class scope function 
template specialization
+  that instantiates to a static member function.
 
 - Fix crash when inheriting from a cv-qualified type. Fixes:
   (`#35603 `_)
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 9769d36900664c..f311f9f3743454 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5439,7 +5439,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,
@@ -6591,7 +6592,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 594c11788f4e7e..45acbf197ea6b4 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 7b9b8f149d9edd..9822477260e592 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->i

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

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

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

>From eb389e142b18d1a14d23d9fadea3c503331c2f73 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski 
Date: Tue, 9 Apr 2024 08:31:52 -0400
Subject: [PATCH] Reapply "[Clang][Sema] Fix crash when 'this' is used in a
 dependent class scope function template specialization that instantiates to a
 static member function (#87541)"

This patch fixes a crash that happens when '`this`' is referenced
(implicitly or explicitly) in a dependent class scope function template
specialization that instantiates to a static member function. For
example:
```
template
struct A
{
template
static void f();

template<>
void f()
{
this; // causes crash during instantiation
}
};

template struct A;
```
This happens because during instantiation of the function body,
`Sema::getCurrentThisType` will return a null `QualType` which we
rebuild the `CXXThisExpr` with. A similar problem exists for implicit
class member access expressions in such contexts (which shouldn't really
happen within templates anyways per [class.mfct.non.static]
p2, but changing that is non-trivial). This patch fixes the crash by building
`UnresolvedLookupExpr`s instead of `MemberExpr`s for these implicit
member accesses, which will then be correctly rebuilt as `MemberExpr`s
during instantiation.
---
 clang/docs/ReleaseNotes.rst   |  2 +
 clang/include/clang/Sema/Sema.h   |  8 ++-
 clang/lib/Sema/SemaExpr.cpp   |  5 +-
 clang/lib/Sema/SemaExprCXX.cpp| 44 +-
 clang/lib/Sema/SemaExprMember.cpp | 42 +
 .../lib/Sema/SemaTemplateInstantiateDecl.cpp  |  8 +++
 clang/lib/Sema/TreeTransform.h| 53 ++---
 ...ms-function-specialization-class-scope.cpp | 59 ++-
 8 files changed, 181 insertions(+), 40 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f5359afe1f0999..6bff80ed4d210b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -520,6 +520,8 @@ Bug Fixes to C++ Support
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
 - Fixed a bug that prevented member function templates of class templates 
declared with a deduced return type
   from being explicitly specialized for a given implicit instantiation of the 
class template.
+- Fixed a crash when ``this`` is used in a dependent class scope function 
template specialization
+  that instantiates to a static member function.
 
 - Fix crash when inheriting from a cv-qualified type. Fixes:
   (`#35603 `_)
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 9769d36900664c..f311f9f3743454 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5439,7 +5439,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,
@@ -6591,7 +6592,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 594c11788f4e7e..45acbf197ea6b4 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() &&
   !ShouldLooku

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

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

https://github.com/sdkrystian created 
https://github.com/llvm/llvm-project/pull/88311

Reapplies #87541 and addresses the bug which caused expressions naming overload 
sets to be incorrectly rebuilt. 

>From f6d703151ec06cec140c4d425dc2bbd8b7ac4f93 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski 
Date: Tue, 9 Apr 2024 08:31:52 -0400
Subject: [PATCH] Reapply "[Clang][Sema] Fix crash when 'this' is used in a
 dependent class scope function template specialization that instantiates to a
 static member function (#87541)"

This patch fixes a crash that happens when '`this`' is referenced
(implicitly or explicitly) in a dependent class scope function template
specialization that instantiates to a static member function. For
example:
```
template
struct A
{
template
static void f();

template<>
void f()
{
this; // causes crash during instantiation
}
};

template struct A;
```
This happens because during instantiation of the function body,
`Sema::getCurrentThisType` will return a null `QualType` which we
rebuild the `CXXThisExpr` with. A similar problem exists for implicit
class member access expressions in such contexts (which shouldn't really
happen within templates anyways per [class.mfct.non.static]
p2, but changing that is non-trivial). This patch fixes the crash by building
`UnresolvedLookupExpr`s instead of `MemberExpr`s for these implicit
member accesses, which will then be correctly rebuilt as `MemberExpr`s
during instantiation.
---
 clang/docs/ReleaseNotes.rst   |  2 +
 clang/include/clang/Sema/Sema.h   |  8 ++-
 clang/lib/Sema/SemaExpr.cpp   |  5 +-
 clang/lib/Sema/SemaExprCXX.cpp| 44 +-
 clang/lib/Sema/SemaExprMember.cpp | 42 +
 .../lib/Sema/SemaTemplateInstantiateDecl.cpp  |  8 +++
 clang/lib/Sema/TreeTransform.h| 44 --
 ...ms-function-specialization-class-scope.cpp | 59 ++-
 8 files changed, 174 insertions(+), 38 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f5359afe1f0999..6bff80ed4d210b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -520,6 +520,8 @@ Bug Fixes to C++ Support
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
 - Fixed a bug that prevented member function templates of class templates 
declared with a deduced return type
   from being explicitly specialized for a given implicit instantiation of the 
class template.
+- Fixed a crash when ``this`` is used in a dependent class scope function 
template specialization
+  that instantiates to a static member function.
 
 - Fix crash when inheriting from a cv-qualified type. Fixes:
   (`#35603 `_)
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 9769d36900664c..f311f9f3743454 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5439,7 +5439,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,
@@ -6591,7 +6592,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 594c11788f4e7e..45acbf197ea6b4 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(