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
{{ message }}
Repository navigation
lazy const evaluation incompatible with const fn #29928
#![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
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];}
Activity
Mark-Simulacrum commentedon May 2, 2017
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:
Does not compile:
oli-obk commentedon May 2, 2017
Was fixed in #38813