Skip to content

Segfault with iterators and vector of borrowed structs #7573

Closed
@catamorphism

Description

@catamorphism

The following code segfaults when run:

use std::io;

pub struct PkgId {
    local_path: ~str,
    junk: ~str
}

impl PkgId {
    fn new(s: &str) -> PkgId {
        PkgId {
            local_path: s.to_owned(),
            junk: ~"wutevs"
        }
    }
}

pub fn remove_package_from_database(id: &PkgId) {
    let mut lines_to_use: ~[&PkgId] = ~[];
    let push_id = |installed_id: &PkgId| {
        lines_to_use.push(installed_id);
    };
    list_database(push_id);

    for lines_to_use.iter().advance() |l| {
        io::stdout().write_line(l.local_path);
    }

}

pub fn list_database(f: &fn(&PkgId)) {
    let stuff = ["foo", "bar"];

    for stuff.iter().advance() |l| {
        f(&PkgId::new(*l));
    }
}

fn main() {
    remove_package_from_database(&PkgId::new("foo"));
}

This is with 040ac2a. It's suspicious to me that the compiler even accepts the ~[&PkgId] type (shouldn't the references need a lifetime?)

If I remove the junk field, or change io::stdout().write_line to io::println, then instead of a segfault I get a failure in the io library.

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions