Skip to content

Owned trait objects are erroneously permitted to contain references #11971

Closed
@chris-morgan

Description

@chris-morgan

(pnkfelix: Here is a test case. This behavior is not sound.)

pub trait IntMaker { fn i(&self) -> int; }

impl<'a> IntMaker for &'a int {
    fn i(&self) -> int { let & &x = self; x }
}

#[cfg(does_not_and_should_not_compile)]
fn main_forbidden_soundly() -> ~IntMaker {
    let x = 2;
    ~&x as ~IntMaker
}

fn main_unsound() -> ~IntMaker: {
    let x = 3;
    ~&x as ~IntMaker:
}

pub fn main() {
    let m2 = main_unsound();
    println!("m2: {}", m2.i());
}

Transcript of a run:

% rustc --version
/Users/fklock/opt/rust-dbg/bin/rustc 0.10-pre (caf17fe 2014-03-21 02:21:50 -0700)
host: x86_64-apple-darwin
% rustc  /tmp/demo.rs && ./demo
m2: 140306048614800

Original bug report follows

Consider the errors you get with trying to use references inside a managed trait object such as @Any: (remove the : and you get a 'static bound failure error plus the same error):

<anon>:1:38: 1:40 error: value may contain references
<anon>:1 #[feature(managed_boxes)];fn main(){@&2 as @Any:;}
                                              ^~
error: aborting due to previous error

Now, the problem: owned trait objects do not have this contains-references check.

With the default constraints, ~&2 as ~Any does not compile, because something with references is not Send. This is good and proper.

On the other hand, ~&2 as ~Any: does compile successfully. (Complete example: fn main() { ~&2 as ~Any:; }, should not compile.) This is unsound and should be forbidden in the same way that @T: forbids references.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions