Skip to content

Trait objects can be created with lifetime bounds that ignore input lifetimes #18055

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

Closed
lifthrasiir opened this issue Oct 15, 2014 · 5 comments · Fixed by #18105
Closed

Trait objects can be created with lifetime bounds that ignore input lifetimes #18055

lifthrasiir opened this issue Oct 15, 2014 · 5 comments · Fixed by #18105
Assignees
Milestone

Comments

@lifthrasiir
Copy link
Contributor

fn foo() -> Box<Reader + 'static> {
    let v = [1, 2, 3];
    box std::io::BufReader::new(v)
}

fn main() {
    let mut foo = foo();
    println!("{}", foo.read_to_end());
}

This should not compile, since v is &'a [u8] for some non-'static 'a and thus BufReader<'a> will contain &'a [u8] as well. It compiles fine however and results in the varying output depending on the optimization level (which shows that this is an UB out of the "safe" code).

Tested with rustc 0.13.0-nightly (1c3ddd297 2014-10-13 23:27:46 +0000) and playpen.

@zwarich
Copy link

zwarich commented Oct 15, 2014

A more self-contained example that shouldn't type-check:

fn f(v: &[u8]) -> Box<Clone + 'static> {
    box v
}

fn main() { }

@zwarich zwarich changed the title Invalid object lifetime bounds: Box<BufReader<'a>> coerces to Box<Reader+'static> Trait objects can be created with lifetime bounds that ignore input lifetimes Oct 15, 2014
@bkoropoff
Copy link
Contributor

It seems that this has been possible since generalized lifetime bounds went in.

@nikomatsakis
Copy link
Contributor

Huh. This was supposed to be fixed. I'll take a look.

@nikomatsakis nikomatsakis self-assigned this Oct 16, 2014
@pnkfelix
Copy link
Member

P-backcompat-lang, 1.0

@pnkfelix pnkfelix added this to the 1.0 milestone Oct 16, 2014
@nikomatsakis
Copy link
Contributor

OK, simple oversight. PR coming soon. Amazing we have no test for this. It occurs only with coercions. (In general, this code path could be cleaned up, and will be as part of implementing generalized where clauses, making these kinds of oversights harder to do.)

bors added a commit that referenced this issue Oct 18, 2014
Check object lifetime bounds in coercions, not just trait bounds.  Fixes #18055.

r? @pcwalton 

This is a [breaking change]. Change code like this:

    fn foo(v: &[u8]) -> Box<Clone+'static> { ... }

to make the lifetimes agree:

    // either...
    fn foo(v: &'static[u8]) -> Box<Clone+'static> { box v }

    // or ...
    fn foo<'a>(v: &'a [u8]) -> Box<Clone+'a> { box v }
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 a pull request may close this issue.

5 participants