Files
clang-p2996/clang/test/CXX/temp/temp.decls/temp.class/temp.mem.func/p1.cpp
Krystian Stasiowski 1156bbc5b1 [Clang][Sema] Diagnose use of template keyword after declarative nested-name-specifiers (#78595)
According to [temp.names] p5:
> The keyword template shall not appear immediately after a declarative nested-name-specifier.

[expr.prim.id.qual] p2 defines a declarative nested-name-specifier as follows:
> A nested-name-specifier is declarative if it is part of
> - a class-head-name,
> - an enum-head-name,
> - a qualified-id that is the id-expression of a declarator-id, or
> - a declarative nested-name-specifier.

Note: I believe this definition is defective as it doesn't include _nested-name-specifiers_ appearing in _elaborated-type-specifiers_ that declare partial/explicit specializations and explicit instantiations. See my post to the core reflector. Minus a few bugs that are addressed by this PR, this is how we implement it.

This means that declarations like:
```
template<typename>
struct A
{
    template<typename> 
    struct B
   {
        void f();
   };
};

template<typename T>
template<typename U>
void A<T>::template B<U>::f() { } // error: 'template' cannot be used after a declarative nested name specifier
```
are ill-formed. This PR add diagnostics for such declarations. The name of the diagnostic group is `template-in-declaration-name`.

Regarding the aforementioned "few bugs that are addressed by this PR" in order to correctly implement this:
- `CheckClassTemplate` did not call `diagnoseQualifiedDeclaration` when the semantic context was dependent. This allowed for constructs like:
```
struct A
{
    template<typename T>
    struct B
    {
        template<typename U>
        struct C;
    };
};

template<typename T>
template<typename U>
struct decltype(A())::B<T>::C { };
```
- `ActOnClassTemplateSpecialization` did not call `diagnoseQualifiedDeclaration` at all, allowing for qualified partial/explicit specializations at class scope and other related nonsense
- `TreeTransform::TransformNestedNameSpecifierLoc` would rebuild a `NestedNameSpecifier::TypeSpecWithTemplate` as a `NestedNameSpecifier::TypeSpec`
- `TemplateSpecializationTypeLoc::initializeLocal` would set the `template` keyword `SourceLocation` to the provided `Loc` parameter, which would result in a `TemplateSpecializationTypeLoc` obtained via `ASTContext::getTrivialTypeSourceInfo` being displayed as always having a `template` prefix (since the presence of the keyword is not stored anywhere else).
2024-02-02 13:15:41 -05:00

101 lines
2.1 KiB
C++

// RUN: %clang_cc1 -fsyntax-only -verify %s
template<typename T, typename U> // expected-note{{previous template}}
class X0 {
public:
typedef int size_type;
X0(int);
~X0();
void f0(const T&, const U&);
T& operator[](int i) const;
void f1(size_type) const;
void f2(size_type) const;
void f3(size_type) const;
void f4() ;
operator T*() const;
T value;
};
template<typename T, typename U>
void X0<T, U>::f0(const T&, const U&) { // expected-note{{previous definition}}
}
template<class X, class Y>
X& X0<X, Y>::operator[](int i) const {
(void)i;
return value;
}
template<class X, class Y>
void X0<X, Y>::f1(int) const { }
template<class X, class Y>
void X0<X, Y>::f2(size_type) const { }
template<class X, class Y, class Z> // expected-error{{too many template parameters}}
void X0<X, Y>::f3(size_type) const {
}
template<class X, class Y>
void X0<Y, X>::f4() { } // expected-error{{does not refer}}
// FIXME: error message should probably say, "redefinition of 'X0<T, U>::f0'"
// rather than just "redefinition of 'f0'"
template<typename T, typename U>
void X0<T, U>::f0(const T&, const U&) { // expected-error{{redefinition}}
}
// Test out-of-line constructors, destructors
template<typename T, typename U>
X0<T, U>::X0(int x) : value(x) { }
template<typename T, typename U>
X0<T, U>::~X0() { }
// Test out-of-line conversion functions.
template<typename T, typename U>
X0<T, U>::operator T*() const {
return &value;
}
namespace N { template <class X> class A {void a();}; }
namespace N { template <class X> void A<X>::a() {} }
// PR5566
template<typename T>
struct X1 {
template<typename U>
struct B { void f(); };
};
template<typename T>
template<typename U>
void X1<T>::template B<U>::f() { } // expected-warning{{'template' cannot be used after a declarative}}
// PR5527
template <template <class> class T>
class X2 {
template <class F>
class Bar {
void Func();
};
};
template <template <class> class T>
template <class F>
void X2<T>::Bar<F>::Func() {}
// PR5528
template <template <class> class T>
class X3 {
void F();
};
template <template <class> class T>
void X3<T>::F() {}