Skip to content

Back-compat risk: same label can be attached to distinct blocks in the same scope #21633

Closed

Description

@pnkfelix

The current Rust alpha allows one to attach two lifetimes with the same name to two distinct loop blocks.

This is a backwards compatibility risk for hypothesized future versions of Rust where the lifetime labels can be used in e.g. type annotations (rather than solely used for labelled break/continue statements).

Example:

fn main() {
    let d = 3;
    'a: for i in (0..d).rev() {
        println!("i: {}", i);
    }

    'a: for i in (d..d*2).rev() {
        println!("i: {}", i);
    }
}

Activity

  1. pnkfelix commented on Jan 25, 2015

    @pnkfelix
    ContributorAuthor

    Nominating for P-backcompat-lang, 1.0 beta.

  2. arielb1 commented on Jan 28, 2015

    @arielb1
    Contributor

    isn't this just ordinary shadowing?

  3. pnkfelix commented on Jan 29, 2015

    @pnkfelix
    ContributorAuthor

    @arielb1 in my opinion ordinary shadowing would be when an nested scope is labeled with the same lifetime as a scope is nested within.

    I see the two scopes here as siblings, at least in terms of what we might use 'a for in type annotations.

    (Having said that, there is probably a coherent semantics that allows this based on shadowing.)

  4. arielb1 commented on Jan 29, 2015

    @arielb1
    Contributor

    The 'a's scope is the for loop's, just like the i.

  5. pnkfelix commented on Jan 29, 2015

    @pnkfelix
    ContributorAuthor

    @arielb1 ah. okay yes, that is another coherent semantics, I think. Not sure if its the one I would pick, but I do not see any obvious holes in it.

  6. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    on Jan 29, 2015
  7. pnkfelix commented on Jan 29, 2015

    @pnkfelix
    ContributorAuthor

    Polish issue for 1.0, P-low.

  8. added this to the 1.0 milestone on Jan 29, 2015
  9. self-assigned this
    on Apr 2, 2015
  10. pnkfelix commented on Apr 3, 2015

    @pnkfelix
    ContributorAuthor

    (i spent a little while looking into this. It may be a little tricky to do at certain phases of the compiler, because the current for-loop expansion moves the label into the inside of a block (basically implicitly building in the scoping semantics outlined by @arielb1 above), so I don't think I can meaningfully do the analysis post expansion, at least, not without knowing enough to skip over such blocks as not introducing an inner scope.)

    current branch is here: pnkfelix@fsk-detect-duplicate-loop-labels


    semi-ironically, I think the fact that the loop-label ends up inside a block in the expansion is my own fault; due to #21984

  11. pnkfelix commented on Apr 7, 2015

    @pnkfelix
    ContributorAuthor

    @nikomatsakis suggests that we strengthen this to simply disallow any duplicate labels in the body of an entire function. That would certainly resolve my above problem.

    Update: posted general warning that we'll be adopting this strategy to internals: http://internals.rust-lang.org/t/psa-rejecting-duplicate-loop-labels/1833

  12. added a commit that references this issue on Apr 10, 2015
    b64b055
  13. added a commit that references this issue on Apr 21, 2015
    75f3565
  14. added a commit that references this issue on Feb 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.P-lowLow priority

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions