[clang] [Clang][Sema] Diagnose friend function specialization definitions (PR #72863)

Krystian Stasiowski via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 27 05:00:33 PST 2023


sdkrystian wrote:

@erichkeane the most recent commit results in the following:
```cpp
template<typename T> void f(T);
void g() { }

template<typename T>
struct A {
  // error: cannot declare an explicit specialization in a friend
  template<> friend void f<>(T) { }
  // error: friend function specialization cannot be defined
  friend void f<>(T) { }
  // error: cannot declare an explicit specialization in a friend
  template<> friend void ::g() { }
  // error: friend function definition cannot be qualified with '::'
  friend void ::g() { }
};

void h() {
  void i();
  struct B {
    // error: templates can only be declared in namespace or class scope
    // error: cannot declare an explicit specialization in a friend
    template<> friend void i() { }
    // error: friend function cannot be defined in a local class
    friend void i() { }
  };
}
```
Unrelated, but it also seems that `template<> friend void f<>(T) { }` causes [this assert](https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/SemaDecl.cpp#L10477) to fire. This happens because we clear `HasExplicitTemplateArgs` for invalid declarations [here](https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/SemaDecl.cpp#L10427). I could include a fix, but it might fall outside the scope of this PR.

I also added a release note as requested.

https://github.com/llvm/llvm-project/pull/72863


More information about the cfe-commits mailing list