[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype template parameter (PR #90376)

2024-04-30 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype template parameter (PR #90376)

2024-04-30 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/90376

>From c5c67ed879fc58e5371de6fc8296b7b6f653a072 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 28 Apr 2024 14:24:30 +0800
Subject: [PATCH] [Clang][Sema] Fix a bug on template partial specialization
 with issue on deduction of nontype tempalte parameter

---
 clang/docs/ReleaseNotes.rst  |  2 ++
 clang/include/clang/Sema/Sema.h  | 10 ++
 clang/lib/Sema/SemaTemplate.cpp  | 19 +++
 clang/lib/Sema/SemaTemplateDeduction.cpp | 10 ++
 clang/test/SemaCXX/PR68885.cpp   | 21 +
 5 files changed, 50 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR68885.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 98c80b6017f604..1abc00a25f1f42 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -612,6 +612,8 @@ Bug Fixes to C++ Support
 - Fix CTAD for ``std::initializer_list``. This allows 
``std::initializer_list{1, 2, 3}`` to be deduced as
   ``std::initializer_list`` as intended.
 - Fix a bug on template partial specialization whose template parameter is 
`decltype(auto)`.
+- Fix a bug on template partial specialization with issue on deduction of 
nontype template parameter
+  whose type is `decltype(auto)`. Fixes (#GH68885).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 1ca523ec88c2f9..809b9c4498f697 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -9249,7 +9249,8 @@ class Sema final : public SemaBase {
   void NoteTemplateParameterLocation(const NamedDecl );
 
   ExprResult BuildExpressionFromDeclTemplateArgument(
-  const TemplateArgument , QualType ParamType, SourceLocation Loc);
+  const TemplateArgument , QualType ParamType, SourceLocation Loc,
+  NamedDecl *TemplateParam = nullptr);
   ExprResult
   BuildExpressionFromNonTypeTemplateArgument(const TemplateArgument ,
  SourceLocation Loc);
@@ -9572,9 +9573,10 @@ class Sema final : public SemaBase {
 
   bool isSameOrCompatibleFunctionType(QualType Param, QualType Arg);
 
-  TemplateArgumentLoc getTrivialTemplateArgumentLoc(const TemplateArgument 
,
-QualType NTTPType,
-SourceLocation Loc);
+  TemplateArgumentLoc
+  getTrivialTemplateArgumentLoc(const TemplateArgument , QualType NTTPType,
+SourceLocation Loc,
+NamedDecl *TemplateParam = nullptr);
 
   /// Get a template argument mapping the given template parameter to itself,
   /// e.g. for X in \c template, this would return an expression 
template
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index ed5507c0ec0100..3c2a5a4ac47e69 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -8438,10 +8438,9 @@ void Sema::NoteTemplateParameterLocation(const NamedDecl 
) {
 /// declaration and the type of its corresponding non-type template
 /// parameter, produce an expression that properly refers to that
 /// declaration.
-ExprResult
-Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument ,
-  QualType ParamType,
-  SourceLocation Loc) {
+ExprResult Sema::BuildExpressionFromDeclTemplateArgument(
+const TemplateArgument , QualType ParamType, SourceLocation Loc,
+NamedDecl *TemplateParam) {
   // C++ [temp.param]p8:
   //
   //   A non-type template-parameter of type "array of T" or
@@ -8508,6 +8507,18 @@ Sema::BuildExpressionFromDeclTemplateArgument(const 
TemplateArgument ,
   } else {
 assert(ParamType->isReferenceType() &&
"unexpected type for decl template argument");
+if (NonTypeTemplateParmDecl *NTTP =
+dyn_cast_if_present(TemplateParam)) {
+  QualType TemplateParamType = NTTP->getType();
+  const AutoType *AT = TemplateParamType->getAs();
+  if (AT && AT->isDecltypeAuto()) {
+RefExpr = new (getASTContext()) SubstNonTypeTemplateParmExpr(
+ParamType->getPointeeType(), RefExpr.get()->getValueKind(),
+RefExpr.get()->getExprLoc(), RefExpr.get(), VD, NTTP->getIndex(),
+/*PackIndex=*/std::nullopt,
+/*RefParam=*/true);
+  }
+}
   }
 
   // At this point we should have the right value category.
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp 
b/clang/lib/Sema/SemaTemplateDeduction.cpp
index c3815bca038554..e93f7bd842e444 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -2639,7 +2639,8 @@ static bool isSameTemplateArg(ASTContext ,
 /// argument.
 TemplateArgumentLoc
 

[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype template parameter (PR #90376)

2024-04-29 Thread via cfe-commits

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


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


[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype template parameter (PR #90376)

2024-04-29 Thread via cfe-commits

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