Skip to content

[Clang][Sema] Use correct TemplateName when transforming TemplateSpecializationType #93411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -7216,9 +7216,30 @@ TreeTransform<Derived>::TransformElaboratedType(TypeLocBuilder &TLB,
return QualType();
}

QualType NamedT = getDerived().TransformType(TLB, TL.getNamedTypeLoc());
if (NamedT.isNull())
return QualType();
QualType NamedT;
if (SemaRef.getLangOpts().CPlusPlus20 && QualifierLoc &&
isa<TemplateSpecializationType>(TL.getNamedTypeLoc().getType())) {
TemplateSpecializationTypeLoc SpecTL =
TL.getNamedTypeLoc().castAs<TemplateSpecializationTypeLoc>();
const TemplateSpecializationType *TST =
SpecTL.getType()->castAs<TemplateSpecializationType>();
Comment on lines +7220 to +7225
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (QualType QT = TL.getNamedTypeLoc().getType(); ... && isa<...>(QT)) {
  auto SpecTL = ...;
  auto *TST = QT.getTypePtr();
}

Probably looks better.

CXXScopeSpec SS;
SS.Adopt(QualifierLoc);
if (TemplateDecl *TD = TST->getTemplateName().getAsTemplateDecl()) {
TemplateName InstName = getDerived().RebuildTemplateName(
SS, TL.getTemplateKeywordLoc(), *TD->getIdentifier(),
TL.getNamedTypeLoc().getBeginLoc(), /*ObjectType=*/QualType(),
/*FirstQualifierInScope=*/nullptr, /*AllowInjectedClassName=*/false);
if (InstName.isNull())
return QualType();
NamedT = TransformTemplateSpecializationType(TLB, SpecTL, InstName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean getDerived().TransformTemplateSpecializationType()?

}
}
if (NamedT.isNull()) {
NamedT = getDerived().TransformType(TLB, TL.getNamedTypeLoc());
if (NamedT.isNull())
return QualType();
}

// C++0x [dcl.type.elab]p2:
// If the identifier resolves to a typedef-name or the simple-template-id
Expand Down
14 changes: 14 additions & 0 deletions clang/test/SemaCXX/PR91677.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s
// expected-no-diagnostics

template <typename> struct t1 {
template <typename>
struct t2 {};
};

template <typename T>
t1<T>::template t2<T> f1();

void f2() {
f1<bool>();
}
7 changes: 4 additions & 3 deletions clang/test/SemaTemplate/typename-specifier-3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ namespace PR12884_original {
typedef int arg;
};
struct C {
typedef B::X<typename B::arg> x; // precxx17-warning{{missing 'typename' prior to dependent type name B::X; implicit 'typename' is a C++20 extension}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A drive-by comment: The test PR12884_original is still crashing on trunk but it slips through our testing probably because we somehow stop after errors above - if we move the whole namespace to the top, then we'd immediately run into an assert saying Unable to find instantiation of declaration!.

typedef B::X<typename B::arg> x; // precxx17-warning{{missing 'typename' prior to dependent type name B::X; implicit 'typename' is a C++20 extension}} \
cxx17-error{{typename specifier refers to non-type member 'arg' in 'PR12884_original::A<int>::B'}}
};
};

template <> struct A<int>::B {
template <int N> struct X {};
static const int arg = 0;
static const int arg = 0; // cxx17-note{{referenced member 'arg' is declared here}}
};

A<int>::C::x a;
A<int>::C::x a; // cxx17-note{{in instantiation of member class 'PR12884_original::A<int>::C' requested here}}
}
namespace PR12884_half_fixed {
template <typename T> struct A {
Expand Down
Loading