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

behavior of '_ around dyn Trait is wrong #48468

Closed
nikomatsakis opened this issue Feb 23, 2018 · 1 comment
Closed

behavior of '_ around dyn Trait is wrong #48468

nikomatsakis opened this issue Feb 23, 2018 · 1 comment
Labels
A-type-system Area: Type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

Generally speaking, '_ is equivalent to "eliding" a lifetime completely. That is, &'_ T and &T are equivalent, as are Foo<'_> and Foo (unfortunately). However, there are two cases where this is not true:

  • dyn Trait + '_ -- the object-lifetime-defaulting rules are different, and '_ needs to interact with them (same for Trait, of course)
  • impl Trait + '_ -- same, but for different reasons

@cramertj pointed out that our current behavior around dyn Trait is not what we really want:

#![feature(dyn_trait)]
#![feature(underscore_lifetimes)]

// These two give the same error that the iterator must be valid for the 'static lifetime:
fn a<T>(items: &[T]) -> Box<dyn Iterator<Item=&T>> {
    Box::new(items.iter()) //~ ERROR cannot infer an appropriate lifetime
}

fn b<T>(items: &[T]) -> Box<dyn Iterator<Item=&T> + '_> {
    Box::new(items.iter()) // *Should* be OK, currently errors
}

// But this one works:
fn c<'a, T>(items: &'a [T]) -> Box<dyn Iterator<Item=&'a T> + 'a> {
    Box::new(items.iter()) // OK
}

fn main() { }
@nikomatsakis nikomatsakis added A-type-system Area: Type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 23, 2018
@ExpHP
Copy link
Contributor

ExpHP commented Feb 23, 2018

fn b<T>(items: &[T]) -> Box<dyn Iterator<Item=&T> + '_>;

I find this fascinating. Who would have predicted prior to implementation that introducing '_ would allow us to elide even more lifetimes?

nikomatsakis added a commit to nikomatsakis/rust that referenced this issue Mar 14, 2018
kennytm added a commit to kennytm/rust that referenced this issue Mar 17, 2018
…elision, r=cramertj

resolve `'_` in `dyn Trait` just like ordinary elision

r? @cramertj

Fixes rust-lang#48468
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants