You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#![feature(const_fn)]constfnf(x:usize) -> A{A{field: x }}structA{field:usize,}fnmain(){let _ = [0;f(5).field];}
The problem is that the A { field: x } expression is lazily evaluated. So we evaluate it when we access field in f(5).field. But now we already lost the function argument x since we are no longer evaluating the const fn. When we try to access x in the const evaluator it bails out with non-const expr. The easy fix would be to evaluate everything eagerly (as it is done in trans/consts). But then we get breaking changes left and right.
So we can't make the const evaluator eager without introducing breaking changes.
As a remedy we can make it eager, but report errors lazily: const_eval evaluates everything eagerly, but stores a Result<ConstVal, ConstEvalErr> instead of a ConstVal wherever we currently allow lazy evaluation.
Another alternative is to make const fn lazy, too. So we just pass in the expression that generated the function argument, instead of evaluating the function argument. Then we do the same for structs and tuples, so instead of just lazily storing the expression that generates them, we also store the expressions of their fields.
imo this is not a nice solution, and probably the most complex implementation-wise
The text was updated successfully, but these errors were encountered:
This seems fixed, though I'm uncertain. Please reopen if I misjudged the issue and provide an example we should next gate closing this issue upon, if possible. Closing at least for now though.
Compiles successfully:
#![feature(const_fn)]constfnf(x:usize) -> A{A{field: x }}structA{field:usize,}fnmain(){let _ = [0;f(5).field];}
The following code fails to compile
The problem is that the
A { field: x }
expression is lazily evaluated. So we evaluate it when we accessfield
inf(5).field
. But now we already lost the function argumentx
since we are no longer evaluating the const fn. When we try to accessx
in the const evaluator it bails out withnon-const expr
. The easy fix would be to evaluate everything eagerly (as it is done intrans/consts
). But then we get breaking changes left and right.Example: the following is legal (see #28189):
But we don't support closures. In fact, you can write arbitrary expression that typecheck and they will compile:
So we can't make the const evaluator eager without introducing breaking changes.
Result<ConstVal, ConstEvalErr>
instead of aConstVal
wherever we currently allow lazy evaluation.The text was updated successfully, but these errors were encountered: