Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Mismatched assumptions for interaction between associated type and lifetime in trait #24622

Copy link
Copy link
@huonw

Description

@huonw
Issue body actions
pub trait Foo<'a> {
    type Bar: std::fmt::Debug;
    fn baz(&'a self) -> Self::Bar;
}

pub struct X {
    name : String,
}

impl<'a> Foo<'a> for X {
    type Bar = &'a String;
    fn baz(&'a self) -> &'a String {
        &self.name
    }
}

pub fn foo<T: for<'a> Foo<'a>>(x: T) {
    let y = x.baz();
    drop(x);
    // for T = X, this is a &String pointing to dropped memory
    println!("{:?}", y); 
}

pub fn main() {
    foo(X { name: "foo".to_string() });
}

This segfaults.

The impl is allowing &'a String because 'a is in scope, but the use in foo assumes that Bar is independent of 'a.

(Noticed in http://stackoverflow.com/q/29745134/1256624 .)

Metadata

Metadata

Assignees

Labels

A-associated-itemsArea: Associated items (types, constants & functions)Area: Associated items (types, constants & functions)A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-trait-systemArea: Trait systemArea: Trait systemP-highHigh priorityHigh priorityT-langRelevant to the language teamRelevant to the language team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.