[clang] Enable AST mutation in the constant evaluator (PR #115168)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 27 10:12:27 PST 2024


AaronBallman wrote:

> Hey Richard - We added idempotency to define_aggregate in response to concern from Clang maintainers that it could interact poorly with clang-repl. The argument was that idempotency would provide a better experience for a user that wanted to re-evaluate an expression that was previously submitted for evaluation. I think, for define_aggregate, it's a reasonable choice (but no, this is not a case of P2996 authors trying to work around our own model - the original behavior was indeed non-idempotent).

I think I was the maintainer who mentioned that. :-)

I think we want consteval function calls to be idempotent because it means we can memoize calls for improved performance and it matches existing implementation/mental models of what constant evaluation means. Certainly that helps with things like clang-repl too, but clang-repl isn't standards conforming anyway so I don't think it should push the design of the standards feature too much.

I think there's no way we can make consteval function calls themselves idempotent once C++ has reflection with side effects because that introduces observable effects outside of the value computations. Same inputs can lead to different outputs.

So whether define_aggregate itself is idempotent is kind of moot. What we want is for `consteval` functions to produce the same outputs when given the same inputs, but I believe there's no reasonable way to accomplish that. So I think we can make it non-idempotent without really losing anything more.

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


More information about the cfe-commits mailing list