Skip to content

Commit 4a2f29d

Browse files
authored
[clang] don't print redundant context notes when instantiating alias templates (#161986)
The redundant notes were introduced with the workaround for finding the template instantiationa args for lambdas inside template type aliases. This removes the notes for the cases where we are simply instantiating an outer template, and when diagnosing uses of the alias template. Also adds comments calling the workaround explicitly.
1 parent c793782 commit 4a2f29d

File tree

5 files changed

+39
-23
lines changed

5 files changed

+39
-23
lines changed

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3822,14 +3822,19 @@ QualType Sema::CheckTemplateIdType(ElaboratedTypeKeyword Keyword,
38223822
AliasTemplate->getTemplateParameters()->getDepth());
38233823

38243824
LocalInstantiationScope Scope(*this);
3825-
InstantiatingTemplate Inst(
3826-
*this, /*PointOfInstantiation=*/TemplateLoc,
3827-
/*Entity=*/AliasTemplate,
3828-
/*TemplateArgs=*/TemplateArgLists.getInnermost());
38293825

38303826
// Diagnose uses of this alias.
38313827
(void)DiagnoseUseOfDecl(AliasTemplate, TemplateLoc);
38323828

3829+
// FIXME: The TemplateArgs passed here are not used for the context note,
3830+
// nor they should, because this note will be pointing to the specialization
3831+
// anyway. These arguments are needed for a hack for instantiating lambdas
3832+
// in the pattern of the alias. In getTemplateInstantiationArgs, these
3833+
// arguments will be used for collating the template arguments needed to
3834+
// instantiate the lambda.
3835+
InstantiatingTemplate Inst(*this, /*PointOfInstantiation=*/TemplateLoc,
3836+
/*Entity=*/AliasTemplate,
3837+
/*TemplateArgs=*/CTAI.SugaredConverted);
38333838
if (Inst.isInvalid())
38343839
return QualType();
38353840

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,12 @@ void Sema::PrintInstantiationStack(InstantiationContextDiagFuncRef DiagFunc) {
12711271
PDiag(diag::note_building_deduction_guide_here));
12721272
break;
12731273
case CodeSynthesisContext::TypeAliasTemplateInstantiation:
1274+
// Workaround for a workaround: don't produce a note if we are merely
1275+
// instantiating some other template which contains this alias template.
1276+
// This would be redundant either with the error itself, or some other
1277+
// context note attached to it.
1278+
if (Active->NumTemplateArgs == 0)
1279+
break;
12741280
DiagFunc(Active->PointOfInstantiation,
12751281
PDiag(diag::note_template_type_alias_instantiation_here)
12761282
<< cast<TypeAliasTemplateDecl>(Active->Entity)

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,17 +1580,19 @@ Decl *TemplateDeclInstantiator::InstantiateTypeAliasTemplateDecl(
15801580
if (!InstParams)
15811581
return nullptr;
15821582

1583-
TypeAliasDecl *Pattern = D->getTemplatedDecl();
1584-
Sema::InstantiatingTemplate InstTemplate(
1585-
SemaRef, D->getBeginLoc(), D,
1586-
D->getTemplateDepth() >= TemplateArgs.getNumLevels()
1587-
? ArrayRef<TemplateArgument>()
1588-
: (TemplateArgs.begin() + TemplateArgs.getNumLevels() - 1 -
1589-
D->getTemplateDepth())
1590-
->Args);
1583+
// FIXME: This is a hack for instantiating lambdas in the pattern of the
1584+
// alias. We are not really instantiating the alias at its template level,
1585+
// that only happens in CheckTemplateId, this is only for outer templates
1586+
// which contain it. In getTemplateInstantiationArgs, the template arguments
1587+
// used here would be used for collating the template arguments needed to
1588+
// instantiate the lambda. Pass an empty argument list, so this workaround
1589+
// doesn't get confused if there is an outer alias being instantiated.
1590+
Sema::InstantiatingTemplate InstTemplate(SemaRef, D->getBeginLoc(), D,
1591+
ArrayRef<TemplateArgument>());
15911592
if (InstTemplate.isInvalid())
15921593
return nullptr;
15931594

1595+
TypeAliasDecl *Pattern = D->getTemplatedDecl();
15941596
TypeAliasTemplateDecl *PrevAliasTemplate = nullptr;
15951597
if (getPreviousDeclForInstantiation<TypedefNameDecl>(Pattern)) {
15961598
DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());

clang/test/SemaTemplate/alias-template-deprecated.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,19 @@ using UsingInstWithCPPAttr [[deprecated("Do not use this")]] = NoAttr<int>;
4646
void bar() {
4747
NoAttr<int> obj; // Okay
4848

49-
// expected-warning@+2 {{'UsingWithAttr' is deprecated}}
50-
// expected-note@+1 {{in instantiation of template type alias 'UsingWithAttr' requested here}}
49+
// expected-warning@+1 {{'UsingWithAttr' is deprecated}}
5150
UsingWithAttr<int> objUsingWA;
5251

53-
// expected-warning@+2 {{'UsingWithAttr' is deprecated}}
54-
// expected-note@+1 {{in instantiation of template type alias 'UsingWithAttr' requested here}}
52+
// expected-warning@+1 {{'UsingWithAttr' is deprecated}}
5553
NoAttr<UsingWithAttr<int>> s;
5654

5755
// expected-note@+1 {{'DepInt' has been explicitly marked deprecated here}}
5856
using DepInt [[deprecated]] = int;
59-
// expected-warning@+3 {{'UsingWithAttr' is deprecated}}
60-
// expected-warning@+2 {{'DepInt' is deprecated}}
61-
// expected-note@+1 {{in instantiation of template type alias 'UsingWithAttr' requested here}}
57+
// expected-warning@+2 {{'UsingWithAttr' is deprecated}}
58+
// expected-warning@+1 {{'DepInt' is deprecated}}
6259
using X = UsingWithAttr<DepInt>;
6360

64-
// expected-warning@+2 {{'UsingWithAttr' is deprecated}}
65-
// expected-note@+1 {{in instantiation of template type alias 'UsingWithAttr' requested here}}
61+
// expected-warning@+1 {{'UsingWithAttr' is deprecated}}
6662
UsingWithAttr<int>().foo();
6763

6864
// expected-warning@+1 {{'UsingInstWithAttr' is deprecated}}
@@ -74,8 +70,7 @@ void bar() {
7470
// expected-warning@+1 {{'UsingTDWithAttr' is deprecated}}
7571
UsingTDWithAttr objUTDWA;
7672

77-
// expected-warning@+2 {{'UsingWithCPPAttr' is deprecated}}
78-
// expected-note@+1 {{in instantiation of template type alias 'UsingWithCPPAttr' requested here}}
73+
// expected-warning@+1 {{'UsingWithCPPAttr' is deprecated}}
7974
UsingWithCPPAttr<int> objUsingWCPPA;
8075

8176
// expected-warning@+1 {{'UsingInstWithCPPAttr' is deprecated: Do not use this}}

clang/test/SemaTemplate/alias-templates.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,11 @@ namespace resolved_nttp {
312312

313313
using TC2 = decltype(C<bool, 2, 3>::p); // expected-note {{instantiation of}}
314314
}
315+
316+
namespace OuterSubstFailure {
317+
template <class T> struct A {
318+
template <class> using B = T&;
319+
// expected-error@-1 {{cannot form a reference to 'void'}}
320+
};
321+
template struct A<void>; // expected-note {{requested here}}
322+
} // namespace OuterSubstFailure

0 commit comments

Comments
 (0)