llvm-project/clang/test/SemaTemplate/deduction-crash.cpp
Matheus Izvekov 91cdd35008
[clang] Improve nested name specifier AST representation (#147835)
This is a major change on how we represent nested name qualifications in
the AST.

* The nested name specifier itself and how it's stored is changed. The
prefixes for types are handled within the type hierarchy, which makes
canonicalization for them super cheap, no memory allocation required.
Also translating a type into nested name specifier form becomes a no-op.
An identifier is stored as a DependentNameType. The nested name
specifier gains a lightweight handle class, to be used instead of
passing around pointers, which is similar to what is implemented for
TemplateName. There is still one free bit available, and this handle can
be used within a PointerUnion and PointerIntPair, which should keep
bit-packing aficionados happy.
* The ElaboratedType node is removed, all type nodes in which it could
previously apply to can now store the elaborated keyword and name
qualifier, tail allocating when present.
* TagTypes can now point to the exact declaration found when producing
these, as opposed to the previous situation of there only existing one
TagType per entity. This increases the amount of type sugar retained,
and can have several applications, for example in tracking module
ownership, and other tools which care about source file origins, such as
IWYU. These TagTypes are lazily allocated, in order to limit the
increase in AST size.

This patch offers a great performance benefit.

It greatly improves compilation time for
[stdexec](https://github.com/NVIDIA/stdexec). For one datapoint, for
`test_on2.cpp` in that project, which is the slowest compiling test,
this patch improves `-c` compilation time by about 7.2%, with the
`-fsyntax-only` improvement being at ~12%.

This has great results on compile-time-tracker as well:

![image](https://github.com/user-attachments/assets/700dce98-2cab-4aa8-97d1-b038c0bee831)

This patch also further enables other optimziations in the future, and
will reduce the performance impact of template specialization resugaring
when that lands.

It has some other miscelaneous drive-by fixes.

About the review: Yes the patch is huge, sorry about that. Part of the
reason is that I started by the nested name specifier part, before the
ElaboratedType part, but that had a huge performance downside, as
ElaboratedType is a big performance hog. I didn't have the steam to go
back and change the patch after the fact.

There is also a lot of internal API changes, and it made sense to remove
ElaboratedType in one go, versus removing it from one type at a time, as
that would present much more churn to the users. Also, the nested name
specifier having a different API avoids missing changes related to how
prefixes work now, which could make existing code compile but not work.

How to review: The important changes are all in
`clang/include/clang/AST` and `clang/lib/AST`, with also important
changes in `clang/lib/Sema/TreeTransform.h`.

The rest and bulk of the changes are mostly consequences of the changes
in API.

PS: TagType::getDecl is renamed to `getOriginalDecl` in this patch, just
for easier to rebasing. I plan to rename it back after this lands.

Fixes #136624
Fixes https://github.com/llvm/llvm-project/issues/43179
Fixes https://github.com/llvm/llvm-project/issues/68670
Fixes https://github.com/llvm/llvm-project/issues/92757
2025-08-09 05:06:53 -03:00

175 lines
4.5 KiB
C++

// RUN: %clang_cc1 -fsyntax-only %s -std=c++1z -verify
// PR7511
template<a> // expected-error +{{}}
struct int_;
template<a> // expected-error +{{}}
template<int,typename T1,typename>
struct ac
{
typedef T1 ae
};
template<class>struct aaa
{
typedef ac<1,int,int>::ae ae // expected-error +{{}}
};
template<class>
struct state_machine
{
typedef aaa<int>::ae aaa;
int start()
{
ant(0);
}
template<class>
struct region_processing_helper
{
template<class,int=0>
struct In;
template<int my>
struct In<a::int_<aaa::a>,my>; // expected-error +{{}}
template<class Event>
int process(Event)
{
In<a::int_<0> > a; // expected-error +{{}}
}
} // expected-error +{{}}
template<class Event>
int ant(Event)
{
region_processing_helper<int>* helper;
helper->process(0) // expected-error +{{}}
}
};
int a()
{
state_machine<int> p;
p.ant(0);
}
// PR9974
template <int> struct enable_if;
template <class > struct remove_reference ;
template <class _Tp> struct remove_reference<_Tp&> ;
template <class > struct __tuple_like;
template <class _Tp, class _Up, int = __tuple_like<typename remove_reference<_Tp>::type>::value>
struct __tuple_convertible;
struct pair
{
template<class _Tuple, int = enable_if<__tuple_convertible<_Tuple, pair>::value>::type>
pair(_Tuple&& );
};
template <class> struct basic_ostream;
template <int>
void endl( ) ;
extern basic_ostream<char> cout;
int operator<<( basic_ostream<char> , pair ) ; // expected-note +{{}}
void register_object_imp ( )
{
cout << endl<1>; // expected-error +{{}}
}
// PR12933
namespace PR12933 {
template<typename S> // expected-error +{{}}
template<typename T>
void function(S a, T b) {}
int main() {
function(0, 1); // expected-error +{{}}
return 0;
}
}
// A buildbot failure from libcxx
namespace libcxx_test {
template <class _Ptr, bool> struct __pointer_traits_element_type;
template <class _Ptr> struct __pointer_traits_element_type<_Ptr, true>;
template <template <class, class...> class _Sp, class _Tp, class ..._Args> struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true> {
typedef char type;
};
template <class T> struct B {};
__pointer_traits_element_type<B<int>, true>::type x;
}
namespace PR14281_part1 {
template <class P, int> struct A;
template <class P> struct A<P, 1>;
template <template <class, int> class S, class T> struct A<S<T, 1>, 1> {
typedef char type;
};
template <class T, int i> struct B {};
A<B<int, 1>, 1>::type x;
}
namespace PR14281_part2 {
typedef decltype(nullptr) nullptr_t;
template <class P, nullptr_t> struct A;
template <class P> struct A<P, nullptr>;
template <template <class, nullptr_t> class S, class T> struct A<S<T, nullptr>, nullptr> {
typedef char type;
};
template <class T, nullptr_t i> struct B {};
A<B<int, nullptr>, nullptr>::type x;
}
namespace PR14281_part3 {
extern int some_decl;
template <class P, int*> struct A;
template <class P> struct A<P, &some_decl>;
template <template <class, int*> class S, class T> struct A<S<T, &some_decl>, &some_decl> {
typedef char type;
};
template <class T, int* i> struct B {};
A<B<int, &some_decl>, &some_decl>::type x;
}
namespace var_template_partial_spec_incomplete {
template<typename T> int n;
template<typename T, typename U = void> int n<T *>; // expected-error +{{}} expected-note {{}}
int k = n<void *>;
}
namespace deduceFunctionSpecializationForInvalidOutOfLineFunction {
template <typename InputT, typename OutputT>
struct SourceSelectionRequirement {
template<typename T>
OutputT evaluateSelectionRequirement(InputT &&Value) {
}
};
template <typename InputT, typename OutputT>
OutputT SourceSelectionRequirement<InputT, OutputT>::
evaluateSelectionRequirement<void>(InputT &&Value) { // expected-error {{cannot specialize a member of an unspecialized template}}
return Value;
}
}
namespace PR51872_part1 {
template<int> class T1 { template <struct U1> T1(); };
// expected-error@-1 {{non-type template parameter has incomplete type 'struct U1'}}
// expected-note@-2 {{forward declaration of 'PR51872_part1::U1'}}
// expected-note@-3 {{implicit deduction guide declared as 'template <int> T1(PR51872_part1::T1<value-parameter-0-0>) -> PR51872_part1::T1<value-parameter-0-0>'}}
T1 t1 = 0;
// expected-error@-1 {{no viable constructor or deduction guide for deduction of template arguments of 'T1'}}
// expected-note@-7 {{candidate template ignored: could not match 'PR51872_part1::T1<value-parameter-0-0>' against 'int'}}
}