Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault on Mac when function cast to borrowed trait invokes self in its impl #4775

Closed
pnkfelix opened this issue Feb 3, 2013 · 2 comments
Closed
Labels
A-codegen Area: Code generation I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Feb 3, 2013

% uname -a
Darwin fklockii-Eris.local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64

% rustc --version
rustc 0.6 (77ee858 2013-02-02 11:54:29 +0100)
host: x86_64-apple-darwin

% cat bug5.rs
fn echo<T>(x:T) { io::println(fmt!("%?", x)); }

pub trait OpInt { fn call(&self, int, int) -> int; }

impl fn(int,int) -> int : OpInt {
    fn call(&self, a:int, b:int) -> int {
        echo("I dont wanna die!");
        (*self)(a,b)
    }
}

fn squarei(x:int, op:&OpInt) -> int       { op.call(x, x) }

fn muli(x:int, y:int) -> int {
    echo("You will never get here.");
    x * y
}


fn main() {
    echo("Entered main");
    let r = squarei(3, (|x,y|muli(x,y)) as &OpInt); echo(r);
}

% rustc bug5.rs && ./bug5
warning: no debug symbols in executable (-arch x86_64)
"Entered main"
"I dont wanna die!"
Segmentation fault: 11
@alexcrichton
Copy link
Member

I tried to update this to current syntax to make sure it still does fail, but I wasn't even able to get this to compile. The closest I could get was:

fn echo<T>(x:T) { io::println(fmt!("%?", x)); }

pub trait OpInt {
    fn call(&self, int, int) -> int;
}

impl<'self> OpInt for &'self fn(int,int) -> int {
    fn call(&self, a:int, b:int) -> int {
        echo("I dont wanna die!");
        (*self)(a,b)
    }
}

fn squarei(x:int, op:&OpInt) -> int { op.call(x, x) }

fn muli(x:int, y:int) -> int {
    echo("You will never get here.");
    x * y
}


fn main() {
    echo("Entered main");
    let f: &fn(int, int) -> int = |x, y| muli(x, y);
    let r = squarei(3, &f as &OpInt);
    echo(r);
}

which yielded the error:

./foo.rs:25:24: 25:25 error: illegal borrow: borrowed value does not live long enough
./foo.rs:25     let r = squarei(3, &f as &OpInt);
                                    ^
note: borrowed pointer must be valid for the static lifetime...
./foo.rs:22:10: 27:1 note: ...but borrowed value is only valid for the block at 22:10
./foo.rs:22 fn main() {
./foo.rs:23     echo("Entered main");
./foo.rs:24     let f: &fn(int, int) -> int = |x, y| muli(x, y);
./foo.rs:25     let r = squarei(3, &f as &OpInt);
./foo.rs:26     echo(r);
./foo.rs:27 }
error: aborting due to previous error

There's probably a few things here. I've heard that the trait system isn't quite up to par of what it could be, and this may be both in terms of this being able to compile and for at runtime as well. Perhaps this should be blocked on #5527?

@pnkfelix
Copy link
Member Author

pnkfelix commented May 1, 2013

Lets close this; this code was when I was experimenting with the language and does not represent any particular use case. It's entirely possible that whatever hole was there (that the segfault is exploiting) has since been filled/fixed by changes to the type+borrow checkers.

@pnkfelix pnkfelix closed this as completed May 1, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.
Projects
None yet
Development

No branches or pull requests

2 participants