You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
fnecho<T>(x:T){ io::println(fmt!("%?", x));}pubtraitOpInt{fncall(&self,int,int) -> int;}impl<'self>OpIntfor&'self fn(int,int) -> int{fncall(&self,a:int,b:int) -> int{echo("I dont wanna die!");(*self)(a,b)}}fnsquarei(x:int,op:&OpInt) -> int{ op.call(x, x)}fnmuli(x:int,y:int) -> int{echo("You will never get here.");
x * y
}fnmain(){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?
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.
The text was updated successfully, but these errors were encountered: