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

Unsoundness from mixing trait impls #9745

Closed
alexcrichton opened this issue Oct 7, 2013 · 7 comments
Closed

Unsoundness from mixing trait impls #9745

alexcrichton opened this issue Oct 7, 2013 · 7 comments

Comments

@alexcrichton
Copy link
Member

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

@alexcrichton
Copy link
Member Author

cc @nikomatsakis

@catamorphism
Copy link
Contributor

1.0, high

@nikomatsakis
Copy link
Contributor

Dup of #5723

@alexcrichton
Copy link
Member Author

I'd like to close this, but 5723 isn't nominated/triaged, so I'm going to leave this open until that bug is milestoned. In the meantime I will nominate #5723

@alexcrichton
Copy link
Member Author

denominating because 5723 has been nominated.

dmski added a commit to dmski/rust that referenced this issue Apr 8, 2014
Regionck didn't do any checks for casts/coercions to an owned trait,
which resulted in lifetimes of the source pointer
to be ignored in the result of such cast.

This fix constraints all regions of the source type of the cast/coercion to be superregions
of each region of the target type (if target trait definition has some lifetime params),
or of 'static lifetime (if there're no lifetime params in target trait's definition).

Closes rust-lang#5723
Closes rust-lang#9745
Closes rust-lang#11971
@ghost
Copy link

ghost commented Sep 16, 2014

Can close now, I think?

@alexcrichton
Copy link
Member Author

Indeed it can, thanks @jakub-!

flip1995 pushed a commit to flip1995/rust that referenced this issue Dec 1, 2022
…od-calls-suggestion, r=flip1995

Fix `redundant_closure_for_method_calls` suggestion

Fixes rust-lang#7746. The issue turns out to be more general than raw pointers. The `redundant_closure_for_method_calls` lint produces incorrect suggestions when the method is associated with a type that must be enclosed in angle brackets or must be written with generic arguments substituted. For example:

```rust
fn main() {
    // Clippy's suggestion: [T; N]::as_slice
    // Correct suggestion:  <[u8; 3]>::as_slice
    let array_opt: Option<&[u8; 3]> = Some(&[4, 8, 7]);
    array_opt.map(|a| a.as_slice());

    // Clippy's suggestion: [T]::len
    // Correct suggestion:  <[u8]>::len
    let slice_opt: Option<&[u8]> = Some(b"slice");
    slice_opt.map(|s| s.len());

    // Clippy's suggestion: *const T::is_null
    // Correct suggestion:  <*const usize>::is_null
    let ptr_opt: Option<*const usize> = Some(&487);
    ptr_opt.map(|p| p.is_null());

    // Clippy's suggestion: dyn TestTrait::method_on_dyn
    // Correct suggestion:  <dyn TestTrait>::method_on_dyn
    let test_struct = TestStruct {};
    let dyn_opt: Option<&dyn TestTrait> = Some(&test_struct);
    dyn_opt.map(|d| d.method_on_dyn());
}

// For the trait object example:
trait TestTrait {}
struct TestStruct {}
impl TestTrait for TestStruct {}

impl dyn TestTrait + '_ {
    fn method_on_dyn(&self) -> bool {
        false
    }
}
```

The issue also affects references and tuples, though I had to patch the standard library with non-trait methods for those types to test that. Just in case, I also included handling for `!`, since it appeared to be possible to call methods on it with angle brackets. I just couldn't verify the resulting suggestion, since dead-code analysis eliminates the code first.

This is my first exposure to Rust compiler internals, so please let me know if I'm taking the wrong approach here!

changelog: [`redundant_closure_for_method_calls`]: add angle brackets and substitute generic arguments in suggestion when needed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants