Closed
Description
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.