[clang] 801c78d - Revert "Reland "[clang][Sema] Use original template pattern when declaring implicit deduction guides for nested template classes" (#69676)"

2023-11-01 Thread Antonio Abbatangelo via cfe-commits

Author: Antonio Abbatangelo
Date: 2023-11-01T18:10:02-04:00
New Revision: 801c78d5b474c2319aa8ead44db7ba8cacac4714

URL: 
https://github.com/llvm/llvm-project/commit/801c78d5b474c2319aa8ead44db7ba8cacac4714
DIFF: 
https://github.com/llvm/llvm-project/commit/801c78d5b474c2319aa8ead44db7ba8cacac4714.diff

LOG: Revert "Reland "[clang][Sema] Use original template pattern when declaring 
implicit deduction guides for nested template classes" (#69676)"

This reverts commit f418319730341e9d41ce8ead6fbfe5603c343985.

Failing test case: 
https://github.com/llvm/llvm-project/pull/69676#issuecomment-1789255366

Added: 


Modified: 
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaTemplate.cpp
clang/test/SemaTemplate/nested-deduction-guides.cpp

Removed: 
clang/test/SemaTemplate/nested-implicit-deduction-guides.cpp



diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3198d6bfe75a2e8..4696836b3a00caa 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -665,11 +665,6 @@ Bug Fixes to C++ Support
   declaration definition. Fixes:
   (`#61763 `_)
 
-- Fix a bug where implicit deduction guides are not correctly generated for 
nested template
-  classes. Fixes:
-  (`#46200 `_)
-  (`#57812 `_)
-
 - Diagnose use of a variable-length array in a coroutine. The design of
   coroutines is such that it is not possible to support VLA use. Fixes:
   (`#65858 `_)

diff  --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 59721c8dc664aa9..9044400fbb1f3f3 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -2253,7 +2253,6 @@ struct ConvertConstructorToDeductionGuideTransform {
 
   Sema &SemaRef;
   ClassTemplateDecl *Template;
-  ClassTemplateDecl *NestedPattern = nullptr;
 
   DeclContext *DC = Template->getDeclContext();
   CXXRecordDecl *Primary = Template->getTemplatedDecl();
@@ -2333,9 +2332,6 @@ struct ConvertConstructorToDeductionGuideTransform {
   Args.addOuterRetainedLevel();
 }
 
-if (NestedPattern)
-  Args.addOuterRetainedLevels(NestedPattern->getTemplateDepth());
-
 FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc()
.getAsAdjusted();
 assert(FPTL && "no prototype for constructor declaration");
@@ -2445,17 +2441,10 @@ struct ConvertConstructorToDeductionGuideTransform {
 SmallVector ParamTypes;
 const FunctionProtoType *T = TL.getTypePtr();
 
-MultiLevelTemplateArgumentList OuterInstantiationArgs;
-if (NestedPattern)
-  OuterInstantiationArgs = SemaRef.getTemplateInstantiationArgs(Template);
-
 //-- The types of the function parameters are those of the constructor.
 for (auto *OldParam : TL.getParams()) {
   ParmVarDecl *NewParam =
   transformFunctionTypeParam(OldParam, Args, MaterializedTypedefs);
-  if (NestedPattern && NewParam)
-NewParam = transformFunctionTypeParam(NewParam, OuterInstantiationArgs,
-  MaterializedTypedefs);
   if (!NewParam)
 return QualType();
   ParamTypes.push_back(NewParam->getType());
@@ -2661,24 +2650,13 @@ void Sema::DeclareImplicitDeductionGuides(TemplateDecl 
*Template,
   if (BuildingDeductionGuides.isInvalid())
 return;
 
-  // If the template is nested, then we need to use the original
-  // pattern to iterate over the constructors.
-  ClassTemplateDecl *Pattern = Transform.Template;
-  while (Pattern->getInstantiatedFromMemberTemplate()) {
-if (Pattern->isMemberSpecialization())
-  break;
-Pattern = Pattern->getInstantiatedFromMemberTemplate();
-Transform.NestedPattern = Pattern;
-  }
-
   // Convert declared constructors into deduction guide templates.
   // FIXME: Skip constructors for which deduction must necessarily fail (those
   // for which some class template parameter without a default argument never
   // appears in a deduced context).
-  ContextRAII SavedContext(*this, Pattern->getTemplatedDecl());
   llvm::SmallPtrSet ProcessedCtors;
   bool AddedAny = false;
-  for (NamedDecl *D : LookupConstructors(Pattern->getTemplatedDecl())) {
+  for (NamedDecl *D : LookupConstructors(Transform.Primary)) {
 D = D->getUnderlyingDecl();
 if (D->isInvalidDecl() || D->isImplicit())
   continue;
@@ -2724,8 +2702,6 @@ void Sema::DeclareImplicitDeductionGuides(TemplateDecl 
*Template,
   Transform.buildSimpleDeductionGuide(Transform.DeducedType))
   ->getTemplatedDecl())
   ->setDeductionCandidateKind(DeductionCandidate::Copy);
-
-  SavedContext.pop();
 }
 
 /// Diagnose the presence of a default template argument on a

diff  --git a/clang/test/S

[clang] ce9eaf0 - Revert "[clang][Sema] Use original template pattern when declaring implicit deduction guides for nested template classes (#68379)"

2023-10-16 Thread Antonio Abbatangelo via cfe-commits

Author: Antonio Abbatangelo
Date: 2023-10-16T22:16:09-04:00
New Revision: ce9eaf0360d9f528ab061bcdbcf81c5b2155f098

URL: 
https://github.com/llvm/llvm-project/commit/ce9eaf0360d9f528ab061bcdbcf81c5b2155f098
DIFF: 
https://github.com/llvm/llvm-project/commit/ce9eaf0360d9f528ab061bcdbcf81c5b2155f098.diff

LOG: Revert "[clang][Sema] Use original template pattern when declaring 
implicit deduction guides for nested template classes (#68379)"

This reverts commit dd0fba11690f9fef304d5f48cde646e5eca8d3c0.

It fails on nested classes that have both an explicit deduction guide and
a constructor that has an argument of the same type as the class (i.e. a copy 
constructor).

Added: 


Modified: 
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaTemplate.cpp

Removed: 
clang/test/SemaTemplate/nested-implicit-deduction-guides.cpp



diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3f83cd71e64cbce..99525b00239a4ca 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -516,11 +516,6 @@ Bug Fixes to C++ Support
   rather than prefer the non-templated constructor as specified in
   [standard.group]p3.
 
-- Fix a bug where implicit deduction guides are not correctly generated for 
nested template
-  classes. Fixes:
-  (`#46200 `_)
-  (`#57812 `_)
-
 Bug Fixes to AST Handling
 ^
 - Fixed an import failure of recursive friend class template.

diff  --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index fba5b2213917065..ff370dd1e080b2b 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -2250,7 +2250,6 @@ struct ConvertConstructorToDeductionGuideTransform {
 
   Sema &SemaRef;
   ClassTemplateDecl *Template;
-  ClassTemplateDecl *NestedPattern = nullptr;
 
   DeclContext *DC = Template->getDeclContext();
   CXXRecordDecl *Primary = Template->getTemplatedDecl();
@@ -2328,8 +2327,6 @@ struct ConvertConstructorToDeductionGuideTransform {
 if (FTD) {
   Args.addOuterTemplateArguments(SubstArgs);
   Args.addOuterRetainedLevel();
-  if (NestedPattern)
-Args.addOuterRetainedLevels(NestedPattern->getTemplateDepth());
 }
 
 FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc()
@@ -2441,17 +2438,10 @@ struct ConvertConstructorToDeductionGuideTransform {
 SmallVector ParamTypes;
 const FunctionProtoType *T = TL.getTypePtr();
 
-MultiLevelTemplateArgumentList OuterInstantiationArgs;
-if (NestedPattern)
-  OuterInstantiationArgs = SemaRef.getTemplateInstantiationArgs(Template);
-
 //-- The types of the function parameters are those of the constructor.
 for (auto *OldParam : TL.getParams()) {
   ParmVarDecl *NewParam =
   transformFunctionTypeParam(OldParam, Args, MaterializedTypedefs);
-  if (NestedPattern && NewParam)
-NewParam = transformFunctionTypeParam(NewParam, OuterInstantiationArgs,
-  MaterializedTypedefs);
   if (!NewParam)
 return QualType();
   ParamTypes.push_back(NewParam->getType());
@@ -2657,23 +2647,13 @@ void Sema::DeclareImplicitDeductionGuides(TemplateDecl 
*Template,
   if (BuildingDeductionGuides.isInvalid())
 return;
 
-  // If the template is nested, then we need to use the original
-  // pattern to iterate over the constructors.
-  ClassTemplateDecl *Pattern = Transform.Template;
-  while (Pattern->getInstantiatedFromMemberTemplate()) {
-if (Pattern->isMemberSpecialization())
-  break;
-Pattern = Pattern->getInstantiatedFromMemberTemplate();
-Transform.NestedPattern = Pattern;
-  }
-
   // Convert declared constructors into deduction guide templates.
   // FIXME: Skip constructors for which deduction must necessarily fail (those
   // for which some class template parameter without a default argument never
   // appears in a deduced context).
   llvm::SmallPtrSet ProcessedCtors;
   bool AddedAny = false;
-  for (NamedDecl *D : LookupConstructors(Pattern->getTemplatedDecl())) {
+  for (NamedDecl *D : LookupConstructors(Transform.Primary)) {
 D = D->getUnderlyingDecl();
 if (D->isInvalidDecl() || D->isImplicit())
   continue;

diff  --git a/clang/test/SemaTemplate/nested-implicit-deduction-guides.cpp 
b/clang/test/SemaTemplate/nested-implicit-deduction-guides.cpp
deleted file mode 100644
index 4915c687cf4c4ef..000
--- a/clang/test/SemaTemplate/nested-implicit-deduction-guides.cpp
+++ /dev/null
@@ -1,12 +0,0 @@
-// RUN: %clang_cc1 -std=c++17 -verify %s
-// expected-no-diagnostics
-
-template struct S {
-template struct N {
-N(T) {}
-N(T, U) {}
-template N(V, U) {}
-};
-};
-
-S::N x{"a", 1};



___
cfe-commits