[clang] Enable AST mutation in the constant evaluator (PR #115168)
Richard Smith via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 25 15:46:13 PST 2024
zygoloid wrote:
> I think there's a contradiction between what `define_aggregate` needs and that hypothetical `write_char_to_file` needs from the same language construct (which is currently called _plainly constant-evaluated expression_). If we say that we're not going to guarantee idempotency for any function that can have side effects during constant evaluation, then it seems that it just can't work. What do you think of this?
This behavior of `define_aggregate` seems strange to me -- I wonder if it's trying to work around a "the same constant expression may be evaluated more than once" concern, and if we instead guaranteed that doesn't happen, they could avoid the complexity and instead say that it's an error to define a type more than once.
Alternatively, something like:
```c++
consteval int f() {
if (!is_defined(^^S)) {
define_aggregate(^^S, {});
}
write_char_to_file("output.txt", 'a');
return 0;
}
```
seems like it could be workable. (Though that doesn't have the same "check the definition is the same each time" property.)
Note that idempotency is *not* composable if you can observe whether the idempotent action has already happened. For example:
```
struct A;
struct B;
consteval void f() {
if (!is_defined(^^A)) { define_aggregate(^^A, {}); }
else { define_aggregate(^^B, {}); }
}
```
Here, `f()` is not idempotent, even though all the pieces that it's constructed from are. Evaluating a call to `f()` twice is not the same as evaluating a call to it once.
https://github.com/llvm/llvm-project/pull/115168
More information about the cfe-commits
mailing list