llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang] Clang accepts explicit instantiation of a function template with deduced return type and explicit trailing return type (non‑standard)

Open Attacker23 opened this issue 1 year ago • 4 comments

I suspect that Clang currently accepts a non‑standard form of explicit instantiation that other major compilers (GCC, MSVC, EDG) correctly reject:

struct A {
  char g() { return 'a'; }
  template<class T> auto f(T t) {
    return t + g();
  }
};

template auto A::f<int>(int t) -> decltype(t + g());
// The correct code should probably be:
// template auto A::f<int>(int t);

https://godbolt.org/z/cdz4Y3Tea

What’s puzzling is the version history:

  • Clang ≤ 16.0.0 correctly rejected this code.
  • Clang 17.0.1 – 18.1.0 caused a crash.
  • Clang 19.1.0+ now accepts the code silently.

It would be helpful to know which change or patch introduced this behavior,
and whether the current acceptance is intentional or a missing diagnostic.

Attacker23 avatar Oct 21 '25 11:10 Attacker23

@llvm/issue-subscribers-clang-frontend

Author: None (Attacker23)

I suspect that **Clang currently accepts a non‑standard form of explicit instantiation** that other major compilers (GCC, MSVC, EDG) correctly reject:
struct A {
  char g() { return 'a'; }
  template&lt;class T&gt; auto f(T t) {
    return t + g();
  }
};

template auto A::f&lt;int&gt;(int t) -&gt; decltype(t + g());
// The correct code should probably be:
// template auto A::f&lt;int&gt;(int t);

https://godbolt.org/z/cdz4Y3Tea

What’s puzzling is the version history:

  • Clang ≤ 16.0.0 correctly rejected this code.
  • Clang 17.0.1 – 18.1.0 caused a crash.
  • Clang 19.1.0+ now accepts the code silently.

It would be helpful to know which change or patch introduced this behavior,
and whether the current acceptance is intentional or a missing diagnostic.

llvmbot avatar Oct 21 '25 14:10 llvmbot

Local bisection: Silently accepts the explicit instantiation → Diagnoses the explicit instantiation at https://github.com/llvm/llvm-project/commit/d2f75f2fe3261264bb4692368aab64aaafb30f08 (f1bf0b02ae25 → d2f75f2fe326).

CE comparison: Clang 21.1.0 (assertions) → Clang 22.1.0 (assertions), showing the same transition.

Current CE trunk: Diagnoses the same construct with invalid use of 'this' outside of a non-static member function.

This reports the observed behavior, without asserting which behavior is required.

Prepared with assistance from Codex.

hongtaihu avatar Oct 29 '26 07:10 hongtaihu

4. Local bisection: Silently accepts the explicit instantiation → Diagnoses the explicit instantiation at d2f75f2 (f1bf0b02ae25 → d2f75f2fe326). CE comparison: Clang 21.1.0 (assertions) → Clang 22.1.0 (assertions), showing the same transition. Current CE trunk: Diagnoses the same construct with invalid use of 'this' outside of a non-static member function. This reports the observed behavior, without asserting which behavior is required. Prepared with assistance from Codex.

@shafik Thanks a lot!

Attacker23 avatar Oct 29 '26 07:10 Attacker23

@mizvekov

shafik avatar Nov 03 '26 01:11 shafik