https://github.com/Sirraide commented:

I think this unfortunately isn’t this simple: I can think of a few problems 
this runs into and I the solutions I’ve managed to think of are:

First, we could use `Sema::CheckConstexprFunctionDefinition()` (see also the 
call to that function in `Sema::BuildLambda()`, which handles making lambdas 
implicitly `constexpr`) to make sure a function is even allowed to be 
`constexpr`; however, that needs to happen after we parse the function body.

This, however, might have some unfortunate consequences for function 
declarations without a body; it would be a bit janky to have to remove the 
`constexpr` specifier again once we see the definition and realise it can’t 
actually be `constexpr`, but not making it `constexpr` if there is no body runs 
into problems with code like:
```c++
int foo();
int bar() { return foo(); }
int foo() { return 3; }
static_assert(bar() == 3);
```
When we encounter `bar()` here, we can only make it implicitly `constexpr` if 
we tread `foo()` as `constexpr` as well even though we haven’t seen its body 
yet and hence can’t actually check whether it can actually be `constexpr`. GCC 
accepts this code if we declare `foo()` and `bar()` as `inline`.

An alternative approach might be to just *not* make functions `constexpr` in 
the AST and also never call `CheckConstexprFunctionDefinition()` for such 
functions in this mode and instead just allow calling functions that are not 
declared `constexpr` if the lang opt is set. That however runs into the problem 
that the constant evaluator needs to be able to diagnose any invalid constructs 
it runs into w/o crashing, and I’m not sure it’s currently set up for that 
given that `CheckConstexprFunctionDefinitions()` rejects e.g. `goto`s before we 
ever get to the constant evaluator. So that definitely requires more tests for 
all things we might run into there.



https://github.com/llvm/llvm-project/pull/136436
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to