[clang] [Clang] Instantiate functions from constant evaluation. (PR #205557)
Daniel M. Katz via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 17 13:03:40 PDT 2026
katzdm wrote:
@efriedma-quic I've looked into ~40 or so of the 50 failures that result from commenting the block described above. From these, the primary conclusion that I draw is that instantiation during constant evaluation, even once all cases that folks wish to see handled are implemented, is unlikely to be sufficient for us to remove early instantiation of constexpr functions. Many early diagnostic checks depend on constant folding that is not mandated by the language; deferring instantiation of constexpr functions until the evaluation of the expression required by the language means that we miss chances to perform such early checks. Here are some examples:
- Clang tentatively evaluates the operand of a 'switch' statement to see if it happens to be a constant expression; if it is, it warns if the result is not covered by any `case` statement. Without eager instantiation of referenced constexpr functions, that evaluation (which is not typically manifestly constant-evaluated and should not have side effects) cannot instantiate constexpr functions declared therein; the expression fails to be constant, and the diagnostic cannot be issued.
- Without eager instantiation of referenced constexpr functions, it cannot be ascertained whether a variable's initialization by way of a heretofore-uninstantiated constexpr constructor has side-effects. Such a variable therefore cannot be safely diagnosed as "unused".
- The code for checking whether the sanity of a constant array size first checks for narrowing conversion. The narrowing conversion check cannot instantiate, so it fails to produce a constant expression. The array size check can instantiate, so it correctly diagnoses an invalid array size - but interprets it as INT_MAX rather than -1, and so the diagnostic is pessimized.
- Narrowing conversions are not treated as manifestly constant-evaluated expressions, and therefore cannot instantiate during evaluation. If we don't early-instantiate constexpr functions, then a currently-working narrowing conversion can break if it refers to a constexpr functions that is not instantiated earlier in the TU.
As I've looked into the vast majority of the failing tests resulting from this hypothetical patch, now having reached the conclusion that the patch is not a wise course of action at this time, I'm going to conclude my investigation into these failures. I'll start moving forward with the plan outlined [above](https://github.com/llvm/llvm-project/pull/205557#issuecomment-5701363903), hopefully tomorrow.
https://github.com/llvm/llvm-project/pull/205557
More information about the cfe-commits
mailing list