Skip to content

Unsoundness from mixing trait impls #9745

Closed
@alexcrichton

Description

@alexcrichton

This code compiles successfully:

trait A {}                                
impl<'self> A for &'self mut A {}         

fn foo(t: &mut A) -> ~A: {                
    ~t as ~A:                             
}                                         

struct B;                                 
impl A for B {}                           

impl Drop for B {                         
    fn drop(&mut self) {                  
        println!("dropping");             
    }                                     
}                                         

fn main() {                               
    let _tmp = {                          
        let mut b = B;                    
        foo(&mut b as &mut A)             
    };                                    
    println!("after conversion");         
}                                         

and yields the output of

$ ./foo       
dropping
after conversion

The value b has been dropped by the time that after conversion is printed, yet we still have a reference to it via the _tmp handle.

Nominating for backwards-compatibility

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions