Skip to content

Unexplained lifetime error in closure type #11740

Closed
@nikomatsakis

Description

@nikomatsakis

In the following test, removing the type annotation from attr produces lifetime errors:

#[feature(managed_boxes)];

use std::cast;
use std::unstable::raw::Box;

struct Attr {
    name: ~str,
    value: ~str,
}

struct Element {
    attrs: ~[@mut Attr],
}

impl Element {
    pub unsafe fn get_attr<'a>(&'a self, name: &str) {
        self.attrs.iter().find(|attr| { // To remove error: |attr: & & 'a @mut Attr| {
                let attr: ***Box<Attr> = cast::transmute(attr);
                true
        });
    }
}

pub fn main() {
    let element = Element {
        attrs: ~[],
    };
    let _ = unsafe { element.get_attr("foo") };
}

Error message:

Running /home/nmatsakis/versioned/rust-1/build/i686-unknown-linux-gnu/stage2/bin/rustc:
/home/nmatsakis/tmp/foo.rs:17:9: 17:27 error: cannot infer an appropriate lifetime for autoref due to conflicting requirements
/home/nmatsakis/tmp/foo.rs:17         self.attrs.iter().find(|attr| { // |attr: & & 'a @Attr| {
                                      ^~~~~~~~~~~~~~~~~~
/home/nmatsakis/tmp/foo.rs:17:9: 17:19 note: first, the lifetime cannot outlive the expression at 17:8...
/home/nmatsakis/tmp/foo.rs:17         self.attrs.iter().find(|attr| { // |attr: & & 'a @Attr| {
                                      ^~~~~~~~~~
/home/nmatsakis/tmp/foo.rs:17:9: 17:19 note: ...so that automatically reference is valid at the time of borrow
/home/nmatsakis/tmp/foo.rs:17         self.attrs.iter().find(|attr| { // |attr: & & 'a @Attr| {
                                      ^~~~~~~~~~
/home/nmatsakis/tmp/foo.rs:17:9: 17:27 note: but, the lifetime must be valid for the method call at 17:8...
/home/nmatsakis/tmp/foo.rs:17         self.attrs.iter().find(|attr| { // |attr: & & 'a @Attr| {
                                      ^~~~~~~~~~~~~~~~~~
/home/nmatsakis/tmp/foo.rs:17:9: 17:19 note: ...so that method receiver is valid for the method call
/home/nmatsakis/tmp/foo.rs:17         self.attrs.iter().find(|attr| { // |attr: & & 'a @Attr| {
                                      ^~~~~~~~~~
error: aborting due to previous error
task 'rustc' failed at 'explicit failure', /home/nmatsakis/versioned/rust-1/src/libsyntax/diagnostic.rs:75
task '<main>' failed at 'explicit failure', /home/nmatsakis/versioned/rust-1/src/librustc/lib.rs:448

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions