[clang] [clang] CTAD: Fix require-clause is not transformed. (PR #89378)

2024-04-22 Thread Haojian Wu via cfe-commits

hokein wrote:

> This seems to have broken the bot: #89476 (you should have had an email?)
> 
> I reverted in #89476

sorry, and thanks for the revert. I will take a further look. 

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


[clang] [clang] CTAD: Fix require-clause is not transformed. (PR #89378)

2024-04-19 Thread Mehdi Amini via cfe-commits

joker-eph wrote:

This seems to have broken the bot: 
https://github.com/llvm/llvm-project/pull/89476 (you should have had an email?)

I reverted in https://github.com/llvm/llvm-project/pull/89476

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


[clang] [clang] CTAD: Fix require-clause is not transformed. (PR #89378)

2024-04-19 Thread Haojian Wu via cfe-commits

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


[clang] [clang] CTAD: Fix require-clause is not transformed. (PR #89378)

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

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


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


[clang] [clang] CTAD: Fix require-clause is not transformed. (PR #89378)

2024-04-19 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Haojian Wu (hokein)


Changes

Fixes https://github.com/llvm/llvm-project/issues/89013

When building the deduction guide, we use the TemplateArgsForBuildingFPrime to 
transform the require-clause from the underlying class deduction guide. 
However, we do this at the wrong place where not all elements of 
TemplateArgsForBuildingFPrime are initialized. The fix involves rearranging the 
transformRequireClause call to the correct location.

As part of the fix, we extend the TemplateInstantiator to support more types in 
the template-rewrite mode. Otherwise, we will encounter an assertion error when 
attempting to rewrite the template type parameter type like D with a complex 
type like DerivedU.

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


4 Files Affected:

- (modified) clang/lib/Sema/SemaTemplate.cpp (+14-13) 
- (modified) clang/lib/Sema/SemaTemplateInstantiate.cpp (+1-4) 
- (modified) clang/test/SemaCXX/cxx20-ctad-type-alias.cpp (+18) 
- (modified) clang/test/SemaTemplate/deduction-guide.cpp (+28) 


``diff
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index d4976f9d0d11d8..4bda31ba67c02d 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -2962,19 +2962,6 @@ void DeclareImplicitDeductionGuidesForTypeAlias(
   Context.getCanonicalTemplateArgument(
   Context.getInjectedTemplateArg(NewParam));
 }
-// Substitute new template parameters into requires-clause if present.
-Expr *RequiresClause =
-transformRequireClause(SemaRef, F, TemplateArgsForBuildingFPrime);
-// FIXME: implement the is_deducible constraint per C++
-// [over.match.class.deduct]p3.3:
-//... and a constraint that is satisfied if and only if the arguments
-//of A are deducible (see below) from the return type.
-auto *FPrimeTemplateParamList = TemplateParameterList::Create(
-Context, AliasTemplate->getTemplateParameters()->getTemplateLoc(),
-AliasTemplate->getTemplateParameters()->getLAngleLoc(),
-FPrimeTemplateParams,
-AliasTemplate->getTemplateParameters()->getRAngleLoc(),
-/*RequiresClause=*/RequiresClause);
 
 // To form a deduction guide f' from f, we leverage clang's instantiation
 // mechanism, we construct a template argument list where the template
@@ -3020,6 +3007,20 @@ void DeclareImplicitDeductionGuidesForTypeAlias(
 F, TemplateArgListForBuildingFPrime, AliasTemplate->getLocation(),
 Sema::CodeSynthesisContext::BuildingDeductionGuides)) {
   auto *GG = cast(FPrime);
+  // Substitute new template parameters into requires-clause if present.
+  Expr *RequiresClause =
+  transformRequireClause(SemaRef, F, TemplateArgsForBuildingFPrime);
+  // FIXME: implement the is_deducible constraint per C++
+  // [over.match.class.deduct]p3.3:
+  //... and a constraint that is satisfied if and only if the arguments
+  //of A are deducible (see below) from the return type.
+  auto *FPrimeTemplateParamList = TemplateParameterList::Create(
+  Context, AliasTemplate->getTemplateParameters()->getTemplateLoc(),
+  AliasTemplate->getTemplateParameters()->getLAngleLoc(),
+  FPrimeTemplateParams,
+  AliasTemplate->getTemplateParameters()->getRAngleLoc(),
+  /*RequiresClause=*/RequiresClause);
+
   buildDeductionGuide(SemaRef, AliasTemplate, FPrimeTemplateParamList,
   GG->getCorrespondingConstructor(),
   GG->getExplicitSpecifier(), GG->getTypeSourceInfo(),
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp 
b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 7cd428de0bb32d..63894cc3cdd538 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -2501,10 +2501,7 @@ 
TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder ,
   assert(Arg.getKind() == TemplateArgument::Type &&
  "unexpected nontype template argument kind in template rewrite");
   QualType NewT = Arg.getAsType();
-  assert(isa(NewT) &&
- "type parm not rewritten to type parm");
-  auto NewTL = TLB.push(NewT);
-  NewTL.setNameLoc(TL.getNameLoc());
+  TLB.pushTrivial(SemaRef.Context, NewT, TL.getNameLoc());
   return NewT;
 }
 
diff --git a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp 
b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
index 6f04264a655ad5..508a3a5da76a91 100644
--- a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
+++ b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
@@ -289,3 +289,21 @@ using String = Array;
 // Verify no crash on constructing the aggregate deduction guides.
 String s("hello");
 } // namespace test21
+
+// GH89013
+namespace test22 {
+class Base {};
+template 
+class Derived final : public Base {};
+
+template 
+requires 

[clang] [clang] CTAD: Fix require-clause is not transformed. (PR #89378)

2024-04-19 Thread Haojian Wu via cfe-commits

https://github.com/hokein created 
https://github.com/llvm/llvm-project/pull/89378

Fixes https://github.com/llvm/llvm-project/issues/89013

When building the deduction guide, we use the TemplateArgsForBuildingFPrime to 
transform the require-clause from the underlying class deduction guide. 
However, we do this at the wrong place where not all elements of 
TemplateArgsForBuildingFPrime are initialized. The fix involves rearranging the 
transformRequireClause call to the correct location.

As part of the fix, we extend the TemplateInstantiator to support more types in 
the template-rewrite mode. Otherwise, we will encounter an assertion error when 
attempting to rewrite the template type parameter type like D with a complex 
type like Derived.

>From 0476e5f735a220c8c991d88db0b4ce2d37f50383 Mon Sep 17 00:00:00 2001
From: Haojian Wu 
Date: Fri, 19 Apr 2024 14:18:47 +0200
Subject: [PATCH] [clang] CTAD: Fix require-clause is not transformed.

Fixes https://github.com/llvm/llvm-project/issues/89013

When building the deduction guide, we use the TemplateArgsForBuildingFPrime to
transform the require-clause from the underlying class deduction guide. However,
we do this at the wrong place where not all elements of 
TemplateArgsForBuildingFPrime
are initialized. The fix involves rearranging the transformRequireClause call to
the correct location.

As part of the fix, we extend the TemplateInstantiator to support more types in
the template-rewrite mode. Otherwise, we will encounter an assertion error when
attempting to rewrite the template type parameter type like D with a complex 
type
like Derived.
---
 clang/lib/Sema/SemaTemplate.cpp  | 27 ++-
 clang/lib/Sema/SemaTemplateInstantiate.cpp   |  5 +---
 clang/test/SemaCXX/cxx20-ctad-type-alias.cpp | 18 +
 clang/test/SemaTemplate/deduction-guide.cpp  | 28 
 4 files changed, 61 insertions(+), 17 deletions(-)

diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index d4976f9d0d11d8..4bda31ba67c02d 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -2962,19 +2962,6 @@ void DeclareImplicitDeductionGuidesForTypeAlias(
   Context.getCanonicalTemplateArgument(
   Context.getInjectedTemplateArg(NewParam));
 }
-// Substitute new template parameters into requires-clause if present.
-Expr *RequiresClause =
-transformRequireClause(SemaRef, F, TemplateArgsForBuildingFPrime);
-// FIXME: implement the is_deducible constraint per C++
-// [over.match.class.deduct]p3.3:
-//... and a constraint that is satisfied if and only if the arguments
-//of A are deducible (see below) from the return type.
-auto *FPrimeTemplateParamList = TemplateParameterList::Create(
-Context, AliasTemplate->getTemplateParameters()->getTemplateLoc(),
-AliasTemplate->getTemplateParameters()->getLAngleLoc(),
-FPrimeTemplateParams,
-AliasTemplate->getTemplateParameters()->getRAngleLoc(),
-/*RequiresClause=*/RequiresClause);
 
 // To form a deduction guide f' from f, we leverage clang's instantiation
 // mechanism, we construct a template argument list where the template
@@ -3020,6 +3007,20 @@ void DeclareImplicitDeductionGuidesForTypeAlias(
 F, TemplateArgListForBuildingFPrime, AliasTemplate->getLocation(),
 Sema::CodeSynthesisContext::BuildingDeductionGuides)) {
   auto *GG = cast(FPrime);
+  // Substitute new template parameters into requires-clause if present.
+  Expr *RequiresClause =
+  transformRequireClause(SemaRef, F, TemplateArgsForBuildingFPrime);
+  // FIXME: implement the is_deducible constraint per C++
+  // [over.match.class.deduct]p3.3:
+  //... and a constraint that is satisfied if and only if the arguments
+  //of A are deducible (see below) from the return type.
+  auto *FPrimeTemplateParamList = TemplateParameterList::Create(
+  Context, AliasTemplate->getTemplateParameters()->getTemplateLoc(),
+  AliasTemplate->getTemplateParameters()->getLAngleLoc(),
+  FPrimeTemplateParams,
+  AliasTemplate->getTemplateParameters()->getRAngleLoc(),
+  /*RequiresClause=*/RequiresClause);
+
   buildDeductionGuide(SemaRef, AliasTemplate, FPrimeTemplateParamList,
   GG->getCorrespondingConstructor(),
   GG->getExplicitSpecifier(), GG->getTypeSourceInfo(),
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp 
b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 7cd428de0bb32d..63894cc3cdd538 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -2501,10 +2501,7 @@ 
TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder ,
   assert(Arg.getKind() == TemplateArgument::Type &&
  "unexpected nontype template argument kind in template