Skip to content

Tracking issue for RFC 2341, Allow locals and destructuring in const fn #48821

Closed

Description

@Centril

This is a tracking issue for the RFC "Allow locals and destructuring in const fn" (rust-lang/rfcs#2341).

Steps:

Unresolved questions:

None.

Activity

  1. added
    T-langRelevant to the language team
    B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Mar 7, 2018
  2. eddyb commented on Mar 7, 2018

    @eddyb
    Contributor

    just remove the few checks

    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};?

  3. Centril commented on Mar 7, 2018

    @Centril
    ContributorAuthor

    @eddyb Feel free to edit the issue description =)

    Also, was let in other const contexts in the RFC, i.e. const X: i32 = { let x = 2 + 3; x * x}; ?

    Yes, because (quoting the summary):

    Allow let bindings in the body of constants and const fns. Additionally enable destructuring in let bindings and const fn arguments.

  4. added
    B-RFC-approvedBlocker: Approved by a merged RFC but not yet implemented.
    and removed
    B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.
    on Mar 7, 2018
  5. bstrie commented on Mar 8, 2018

    @bstrie
    Contributor

    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.

  6. oli-obk commented on Mar 8, 2018

    @oli-obk
    Contributor

    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.

  7. added a commit that references this issue on May 22, 2018
  8. added
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    on Jun 2, 2018
  9. oli-obk commented on Jun 2, 2018

    @oli-obk
    Contributor

    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.

  10. nexplor commented on Jul 24, 2018

    @nexplor
  11. oli-obk commented on Jul 25, 2018

    @oli-obk
    Contributor

    Imo it would be fine if added behind its own feature gate. So if you're looking for something easy to do:

    1. add a new feature gate (just search for const_let and copy all the defining parts)
    2. wrap https://github.com/rust-lang/rust/blob/master/src/librustc_mir/transform/qualify_consts.rs#L276 in a feature gate check (so if the feature gate is active, then that line needs to be not called)
    3. Add regression tests
  12. 3 remaining items

  13. qwerty19106 commented on Sep 30, 2018

    @qwerty19106
  14. oli-obk commented on Nov 21, 2018

    @oli-obk
    Contributor

    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.

    cc @Centril

  15. Centril commented on Nov 22, 2018

    @Centril
    ContributorAuthor

    @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?

  16. oli-obk commented on Nov 22, 2018

    @oli-obk
    Contributor

    I have a fix ready for these bugs in #56070

    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?

    You can do things like

    fn bar() -> [u32; 500] {
        let mut foo = [0; 500];
        foo[3] = 42;
        foo
    }

    Or just generally make your code more readable in case the return expression is very complex.

  17. Centril commented on Nov 22, 2018

    @Centril
    ContributorAuthor

    @oli-obk

    I have a fix ready for these bugs in #56070

    Cool. :)

    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:

    1. a special new issue or alternatively a stabilization PR (the latter is my preference)
    2. with a report that points out:
      0. when the initial implementation landed in nightly
      1. relevant tests
      2. what exactly is being stabilized
      3. what is not being stabilized
        1. and why
      4. any divergences from the RFC (there are such because of c) )
    3. change this tracking issue with a checkbox pointing out the remaining work after the initial stabilization
  18. Centril commented on Nov 22, 2018

    @Centril
    Author
  19. oli-obk commented on Nov 22, 2018

    @oli-obk
  20. Centril commented on Nov 22, 2018

    @Centril
    Author
  21. mjbshaw commented on Dec 29, 2018

    @mjbshaw
    Contributor

    Does this tracking issue include mutable references? Or is that a separate RFC/tracking issue?

  22. oli-obk commented on Dec 29, 2018

    @oli-obk
    Contributor

    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

  23. Centril commented on Dec 30, 2018

    @Centril
    ContributorAuthor

    Stabilization proposed: #57175 (comment)

  24. added
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    on Dec 31, 2018
  25. Centril commented on Jan 12, 2019

    @Centril
    ContributorAuthor

    Stabilized in #57175; Documentation issue: rust-lang/reference#506

    Everything is done so closing therefore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)B-RFC-approvedBlocker: Approved by a merged RFC but not yet implemented.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCE-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-langRelevant to the language team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions