Skip to content

Lifetimes can escape in traits / objects #5723

Closed
@nikomatsakis

Description

@nikomatsakis

This code from the io library is definitely wrong as the lifetime of the parameter s is lost:

pub fn with_str_reader<T>(s: &str, f: &fn(@Reader) -> T) -> T {
    str::byte_slice(s, |bytes| with_bytes_reader(bytes, f))
}

The lifetime of the s is lost. I have a fix for this in a branch I want to land (currently commented out with a FIXME) but I don't want to turn on the check because to do this right we need & objects working. I'm not even sure where to insert strategic transmutes to get with_str_reader working.

This signature for example should really be:

pub fn with_str_reader<T>(s: &str, f: &fn(&Reader) -> T) -> T {...}

or even

pub fn str_reader<'a, T>(s: &'a str) -> &'a Reader {...}

There is a test that was supposed to be defending against this case, but it was basically accidentally reporting an error.

Metadata

Metadata

Assignees

Labels

A-lifetimesArea: Lifetimes / regionsI-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.P-mediumMedium priority

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions