Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve: Fix #23880, a scoping bug #31105

Merged
merged 1 commit into from
Jan 26, 2016

Conversation

jseyfried
Copy link
Contributor

This fixes #23880, a scoping bug in which items in a block are shadowed by local variables and type parameters that are in scope.

After this PR, an item in a block will shadow any local variables or type parameters above the item in the scope hierarchy. Items in a block will continue to be shadowed by local variables in the same block (even if the item is defined after the local variable).

This is a [breaking-change]. For example, the following code breaks:

fn foo() {
    let mut f = 1;
    {
        fn f() {}
        f += 1; // This will resolve to the function instead of the local variable
    }
}

@rust-highfive
Copy link
Collaborator

r? @alexcrichton

(rust_highfive has picked a reviewer for you, use r? to override)

@jseyfried
Copy link
Contributor Author

r? @nrc (or @nikomatsakis)

@rust-highfive rust-highfive assigned nrc and unassigned alexcrichton Jan 22, 2016
@oli-obk
Copy link
Contributor

oli-obk commented Jan 22, 2016

your example code that's broken by your changes could be a compile-fail test

// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(unused)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment describing what is being tested please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

@jseyfried
Copy link
Contributor Author

@oli-obk true, but the run-pass test is testing the same thing

@jseyfried
Copy link
Contributor Author

@nrc I added a comment describing the test and improved the commit message.

@nrc
Copy link
Member

nrc commented Jan 25, 2016

Could you add [breaking-change] to the commit message please?

@nrc
Copy link
Member

nrc commented Jan 25, 2016

r+ with the change to the commit message

@jseyfried jseyfried force-pushed the fix_lexical_scoping branch from 1c07493 to 5821219 Compare January 26, 2016 01:35
@jseyfried
Copy link
Contributor Author

I updated the commit message.

@nrc
Copy link
Member

nrc commented Jan 26, 2016

@jseyfried sorry, I should have been clearer. The subject line can stay the same, but you should add [breaking-change] to the body of the commit message. You should add an explanation of the breaking change, perhaps including the example, and any advice for how to fix any bustage.

@jseyfried jseyfried force-pushed the fix_lexical_scoping branch from 5821219 to e1dd02c Compare January 26, 2016 04:07
This fixes a bug in which items in a block are shadowed by local variables and type parameters that are in scope.
It is a [breaking-change]. For example, the following code breaks:

```rust
fn foo() {
    let mut f = 1;
    {
        fn f() {}
        f += 1; // This will now resolve to the function instead of the local variable
    }
}
```

Any breakage can be fixed by renaming the item that is no longer shadowed.
@jseyfried jseyfried force-pushed the fix_lexical_scoping branch from e1dd02c to faf0852 Compare January 26, 2016 04:21
@jseyfried
Copy link
Contributor Author

@nrc Ok.

I'm a little unclear on what should go in the merge commit message for a PR (in general) and what should go in the individual commit message(s).

Sampling past PRs, including ones with breaking changes, it looks pretty common to have a detailed merge commit message and a simple one-liner for the individual commit(s). Is this bad practice?

@nrc
Copy link
Member

nrc commented Jan 26, 2016

@jseyfried thanks, I am a little confused too - I didn't know bors uses the PR message as the merge commit message, so it would have been fine, I guess. I personally prefer having the breaking-change message in the commit which makes the change so that it is easy to find the relevant code, but I think official policy is just that breaking-change has to be in a commit message.

@nrc
Copy link
Member

nrc commented Jan 26, 2016

@bors: r+

@bors
Copy link
Contributor

bors commented Jan 26, 2016

📌 Commit faf0852 has been approved by nrc

bors added a commit that referenced this pull request Jan 26, 2016
This fixes #23880, a scoping bug in which items in a block are shadowed by local variables and type parameters that are in scope.

After this PR, an item in a block will shadow any local variables or type parameters above the item in the scope hierarchy. Items in a block will continue to be shadowed by local variables in the same block (even if the item is defined after the local variable).

This is a [breaking-change]. For example, the following code breaks:
```rust
fn foo() {
    let mut f = 1;
    {
        fn f() {}
        f += 1; // This will resolve to the function instead of the local variable
    }
}
@bors
Copy link
Contributor

bors commented Jan 26, 2016

⌛ Testing commit faf0852 with merge 43c1a17...

@bors bors merged commit faf0852 into rust-lang:master Jan 26, 2016
bors added a commit that referenced this pull request Feb 26, 2016
This fixes a bug (#31845) introduced in #31105 in which lexical scopes contain items from all anonymous module ancestors, even if the path to the anonymous module includes a normal module:
```rust
fn f() {
    fn g() {}
    mod foo {
        fn h() {
           g(); // This erroneously resolves on nightly
        }
    }
}
```

This is a [breaking-change] on nightly but not on stable or beta.
r? @nikomatsakis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Items with 'lexical' scope have poor scoping
6 participants