<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/109359>109359</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang][C++] Confusing error message after triggering a hard error in a function template specialization
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          frederick-vs-ja
      </td>
    </tr>
</table>

<pre>
    Currently, when the same ill-formed function template specialization is instantiated more than once, Clang emits strange error messages. [Godbolt link](https://godbolt.org/z/9sdTK37YP).

```C++
#include <new>

namespace fvs {   
template<class T, class...Args,
 class Ret = decltype(::new(static_cast<void*>(nullptr)) T(*static_cast<Args&&(*)()>(nullptr)()...))>
Ret construct_at(T* loc, Args&&... args)
{
    // intentionally no const_cast, see https://cplusplus.github.io/LWG/issue3870
    return ::new (static_cast<void*>(loc)) T(static_cast<Args&&>(args)...);
}


template<class T, class...Args,
    class Ret = decltype(::new(static_cast<void*>(nullptr)) T(*static_cast<Args&&(*)()>(nullptr)()...))>
Ret construct_at_v2(T* loc, Args&&... args)
{
    return fvs::construct_at(loc, static_cast<Args&&>(args)...);
}
}

int main()
{
    int n{};
    const int* p = &n;
    fvs::construct_at(p);
 fvs::construct_at_v2(p);
}
```

Error messages:
```
<source>:9:19: error: static_cast from 'const int *' to 'void *' is not allowed
    9 |     return ::new (static_cast<void*>(loc)) T(static_cast<Args&&>(args)...);
      | ^~~~~~~~~~~~~~~~~~~~~~~
<source>:25:10: note: in instantiation of function template specialization 'fvs::construct_at<const int, const int *>' requested here
   25 |     fvs::construct_at(p);
      | ^
<source>:17:12: error: no matching function for call to 'construct_at'
   17 |     return fvs::construct_at(loc, static_cast<Args&&>(args)...);
      | ^~~~~~~~~~~~~~~~~
<source>:26:10: note: in instantiation of function template specialization 'fvs::construct_at_v2<const int, const int *>' requested here
   26 |     fvs::construct_at_v2(p);
      | ^
<source>:6:5: note: candidate template ignored: substitution failure [with T = const int, Args = <>, Ret = decltype(::new (static_cast<void *>(nullptr)) const int(/*implicit*/(const int)0))]
    6 | Ret construct_at(T* loc, Args&&... args)
      |     ^
2 errors generated.
Compiler returned: 1
```

It seems that Clang treats the ill-formedness after the first instantiation failure as something like substitution failure.

Clang started to behave like this since 3.4, while MSVC and GCC don't do so. It's unclear to me whether this is intended.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUV02P2zYQ_TX0ZbCCRFmWdPDBK8dB0BYo2kWLngKaGktMKNIlKS82h_72YiT5K_HutgmCooEje8nRDN97w-FQeK8ag7hk2T3L1jPRh9a65c5hjU7Jj3cHf_dBzLa2flpWvXNogn5ivILHFg2EFsGLDkFpfbezrsMadr2RQVkDAbu9FgHB71EqodUnMYwrD8r4IExQImANnXUIoRUGrJFIvistTAPYqeDBBydMg4DOWQcdei8a9BGw7P6trbdWB9DKfGTZmvGiDWHvWbpifMP4phnnI-saxjefGN-Uvn74Ic3_-JnxMmLxmsWr6bmIx0_F-D19xlGeKiN1XyOwtDL4yNI3l28Z0aHfC4mwO3hg-T0AjDNH7CytpBbewwPhGn5GUbRyjWe8Gk3HUfgFA7B0DTVKHZ72yHhBQNIVheWFDyIo-V4KH1haHayqGV_Rcnhheq33wTFeMl5SoILx1bX9GHAxfGh2sC3o-YUHGo2iaPR2wkurk9b44HoZ3ovAePHA-Aq0lQTs7D-KIhDDX-XEVD6RCUTOoAsoE9BQLgitn8DY0fW4WF6BR4RrJeVe957-R40Kbb-NlGV88-PvbxnfKO97TIs8PodxGHpn4MQfvEzgAOJE3rPMDbYTtomh9Jgo-foqm_51DgD8L9Pg_YF_ZSZMEu0OfoT3WXJN3r5Ni2tRlAnQCWUmxF8uiQwMjeTrk7NBGFoazRLM_aAO4wtzZfMcjv3lym5bjRzub0M4lqVLIG-uKiE5vGmbVt72TiJxla5Klq4SeoyFlH5ckAs7ZztgPD9hhSE9cgiWh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