[all-commits] [llvm/llvm-project] 1156bb: [Clang][Sema] Diagnose use of template keyword aft...

Krystian Stasiowski via All-commits all-commits at lists.llvm.org
Fri Feb 2 10:15:53 PST 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 1156bbc5b1837e688b0e5d6952f1a900aca29062
      https://github.com/llvm/llvm-project/commit/1156bbc5b1837e688b0e5d6952f1a900aca29062
  Author: Krystian Stasiowski <sdkrystian at gmail.com>
  Date:   2024-02-02 (Fri, 02 Feb 2024)

  Changed paths:
    M clang/docs/ReleaseNotes.rst
    M clang/include/clang/AST/TypeLoc.h
    M clang/include/clang/Basic/DiagnosticSemaKinds.td
    M clang/include/clang/Sema/Sema.h
    M clang/lib/AST/TypeLoc.cpp
    M clang/lib/Parse/ParseDecl.cpp
    M clang/lib/Sema/SemaDecl.cpp
    M clang/lib/Sema/SemaDeclCXX.cpp
    M clang/lib/Sema/SemaTemplate.cpp
    M clang/lib/Sema/TreeTransform.h
    M clang/test/CXX/drs/dr23xx.cpp
    M clang/test/CXX/drs/dr7xx.cpp
    M clang/test/CXX/temp/temp.decls/temp.class/temp.mem.func/p1.cpp
    A clang/test/CXX/temp/temp.names/p5.cpp
    M clang/test/CXX/temp/temp.spec/part.spec.cpp
    M clang/test/SemaCXX/static-assert.cpp
    M clang/test/SemaTemplate/class-template-spec.cpp

  Log Message:
  -----------
  [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).




More information about the All-commits mailing list