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
Tracking issue for RFC 2341, Allow locals and destructuring in const fn #48821
It's a bit trickier, because rustc_mir::transform::qualify_consts needs to track local variables just like it does only temporaries right now, but it shouldn't
Also, was let in other const contexts in the RFC, i.e. const X: i32 = { let x = 2 + 3; x * x};?
A question about the "Drawbacks" section from the original RFC:
You can create mutable locals in constants and then actually modify them. This has no real impact on the constness, as the mutation happens entirely at compile time and results in an immutable value.
Can you elaborate on this? Is this just referring to something like const foo: i32 = { let mut x = 0; x += 1; x };? That's my straightforward reading of the text, but it also seems completely expected so I'm not sure whether there's a more pernicious drawback I ought to be seeing.
Not a drawback really, but a note that this is new behaviour, because before you could never modify the same memory twice, only emulate the same program behaviour via const fn.
This works now on nightly (behind a feature gate). Please play around with this feature liberally and report any findings! Any help in documenting and pushing it over the stabilization line is appreciated.
We could stabilize let bindings in const fn (but not in const/static), because const functions do not have the issue that && and || short circuit (since these operations are illegal in const functions until control flow in general is allowed).
Due to backwards compatibility constraints we cannot make && and || illegal in constants and statics.
@oli-obk Hmm... there are a bunch of bug reports cited in #48821 (comment)... we'd have to feel comfortable with the implementation before stabilizing.
However, my main question re. stabilizing is whether stabilizing let without control flow would bring much benefit without any control flow? What is it that we "unlock" by stabilizing in const fn only?
However, my main question re. stabilizing is whether stabilizing let without control flow would bring much benefit without any control flow? What is it that we "unlock" by stabilizing in const fn only?
Or just generally make your code more readable in case the return expression is very complex.
I somehow doubt expressions can become complex without any sort of control flow but I suppose it doesn't hurt. Clearly enough time has passed since it was landed in nightly...
Could you? prepare:
a special new issue or alternatively a stabilization PR (the latter is my preference)
with a report that points out:
0. when the initial implementation landed in nightly
relevant tests
what exactly is being stabilized
what is not being stabilized
and why
any divergences from the RFC (there are such because of c) )
change this tracking issue with a checkbox pointing out the remaining work after the initial stabilization
Activity
eddyb commentedon Mar 7, 2018
It's a bit trickier, because
rustc_mir::transform::qualify_constsneeds to track local variables just like it does only temporaries right now, but it shouldn'tAlso, was
letin other const contexts in the RFC, i.e.const X: i32 = { let x = 2 + 3; x * x};?Centril commentedon Mar 7, 2018
@eddyb Feel free to edit the issue description =)
Yes, because (quoting the summary):
bstrie commentedon Mar 8, 2018
A question about the "Drawbacks" section from the original RFC:
Can you elaborate on this? Is this just referring to something like
const foo: i32 = { let mut x = 0; x += 1; x };? That's my straightforward reading of the text, but it also seems completely expected so I'm not sure whether there's a more pernicious drawback I ought to be seeing.oli-obk commentedon Mar 8, 2018
Not a drawback really, but a note that this is new behaviour, because before you could never modify the same memory twice, only emulate the same program behaviour via const fn.
Auto merge of #49172 - oli-obk:const_let, r=eddyb
oli-obk commentedon Jun 2, 2018
This works now on nightly (behind a feature gate). Please play around with this feature liberally and report any findings! Any help in documenting and pushing it over the stabilization line is appreciated.
nexplor commentedon Jul 24, 2018
oli-obk commentedon Jul 25, 2018
Imo it would be fine if added behind its own feature gate. So if you're looking for something easy to do:
const_letand copy all the defining parts)3 remaining items
qwerty19106 commentedon Sep 30, 2018
oli-obk commentedon Nov 21, 2018
We could stabilize
letbindings inconst fn(but not inconst/static), becauseconstfunctions do not have the issue that&&and||short circuit (since these operations are illegal inconstfunctions until control flow in general is allowed).Due to backwards compatibility constraints we cannot make
&&and||illegal in constants and statics.cc @Centril
Centril commentedon Nov 22, 2018
@oli-obk Hmm... there are a bunch of bug reports cited in #48821 (comment)... we'd have to feel comfortable with the implementation before stabilizing.
However, my main question re. stabilizing is whether stabilizing
letwithout control flow would bring much benefit without any control flow? What is it that we "unlock" by stabilizing inconst fnonly?oli-obk commentedon Nov 22, 2018
I have a fix ready for these bugs in #56070
You can do things like
Or just generally make your code more readable in case the return expression is very complex.
Centril commentedon Nov 22, 2018
@oli-obk
Cool. :)
I somehow doubt expressions can become complex without any sort of control flow but I suppose it doesn't hurt. Clearly enough time has passed since it was landed in nightly...
Could you? prepare:
0. when the initial implementation landed in nightly
Centril commentedon Nov 22, 2018
oli-obk commentedon Nov 22, 2018
Centril commentedon Nov 22, 2018
mjbshaw commentedon Dec 29, 2018
Does this tracking issue include mutable references? Or is that a separate RFC/tracking issue?
oli-obk commentedon Dec 29, 2018
Mutable references require their own full rfc imo. There are so many edge cases. I have started tracking some in rust-lang/const-eval#16
Centril commentedon Dec 30, 2018
Stabilization proposed: #57175 (comment)
Centril commentedon Jan 12, 2019
Stabilized in #57175; Documentation issue: rust-lang/reference#506
Everything is done so closing therefore.