Skip to content

Report an error when lifetime parameters are shadowed. #19777

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

Merged
merged 2 commits into from
Dec 16, 2014

Conversation

nikomatsakis
Copy link
Contributor

per rfc 459
cc #19390

One question is: should we start by warning, and only switch to hard error later? I think we discussed something like this in the meeting.

r? @alexcrichton

@alexcrichton
Copy link
Member

I'm ok with a hard error here for now, the error message should be pretty helpful in fixing it.

r=me with a compile-fail test using //~ ERROR and //~ HELP to ensure we generate errors in this case.

@nikomatsakis nikomatsakis force-pushed the warn-on-shadowing branch 2 times, most recently from a8a2438 to 784c486 Compare December 13, 2014 10:36
@nikomatsakis
Copy link
Contributor Author

I decided to go with a warning; it seems kinder, and also it will make it easier to land the patch. It does mean we'll need a separate PR to make this a hard error.

@flaper87
Copy link
Contributor

FWIW, I think a hard error is fine

@nikomatsakis
Copy link
Contributor Author

@flaper87 honestly my biggest motivation is that I do not want this patch to bounce because of some obscure shadowed lifetime in a test somewhere, or in some windows-specific code

@nikomatsakis
Copy link
Contributor Author

so being kind to myself is also kind to others (a warning makes for an easier transition, whether it's for our code or others') :)

@nikomatsakis nikomatsakis force-pushed the warn-on-shadowing branch 3 times, most recently from 6f61604 to 6b7a38d Compare December 14, 2014 16:16
This is not technically a [breaking-change], but it will be soon, so
you should update your code. Typically, shadowing is accidental, and
the shadowing lifetime can simply be removed. This frequently occurs
in constructor patterns:

```rust
// Old:
impl<'a> SomeStruct<'a> { fn new<'a>(..) -> SomeStruct<'a> { ... } }

// Should be:
impl<'a> SomeStruct<'a> { fn new(..) -> SomeStruct<'a> { ... } }
```

Otherwise, you should rename the inner lifetime to something
else. Note though that lifetime elision frequently applies:

```rust
// Old
impl<'a> SomeStruct<'a> {
    fn get<'a>(x: &'a self) -> &'a T { &self.field }
}

// Should be:
impl<'a> SomeStruct<'a> {
    fn get(x: &self) -> &T { &self.field }
}
``
bors added a commit that referenced this pull request Dec 16, 2014
per rfc 459
cc #19390

One question is: should we start by warning, and only switch to hard error later? I think we discussed something like this in the meeting. 

r? @alexcrichton
@bors bors merged commit 1718cd6 into rust-lang:master Dec 16, 2014
@nikomatsakis nikomatsakis deleted the warn-on-shadowing branch March 30, 2016 16:17
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.

4 participants