]> git.lizzy.rs Git - rust.git/blob - tests/ui/rust-2021/inherent-dyn-collision.rs
Rollup merge of #106766 - GuillaumeGomez:rm-stripper-dead-code, r=notriddle
[rust.git] / tests / ui / rust-2021 / inherent-dyn-collision.rs
1 // Test case where the method we want is an inherent method on a
2 // dyn Trait. In that case, the fix is to insert `*` on the receiver.
3 //
4 // check-pass
5 // run-rustfix
6 // edition:2018
7
8 #![warn(rust_2021_prelude_collisions)]
9
10 trait TryIntoU32 {
11     fn try_into(&self) -> Result<u32, ()>;
12 }
13
14 impl TryIntoU32 for u8 {
15     // note: &self
16     fn try_into(&self) -> Result<u32, ()> {
17         Ok(22)
18     }
19 }
20
21 mod inner {
22     use super::get_dyn_trait;
23
24     // note: this does nothing, but is copying from ffishim's problem of
25     // having a struct of the same name as the trait in-scope, while *also*
26     // implementing the trait for that struct but **without** importing the
27     // trait itself into scope
28     struct TryIntoU32;
29
30     impl super::TryIntoU32 for TryIntoU32 {
31         fn try_into(&self) -> Result<u32, ()> {
32             Ok(0)
33         }
34     }
35
36     // this is where the gross part happens. since `get_dyn_trait` returns
37     // a Box<dyn Trait>, it can still call the method for `dyn Trait` without
38     // `Trait` being in-scope. it might even be possible to make the trait itself
39     // entirely unreference-able from the callsite?
40     pub fn test() -> u32 {
41         get_dyn_trait().try_into().unwrap()
42         //~^ WARNING trait method `try_into` will become ambiguous
43         //~| WARNING this is accepted in the current edition
44     }
45 }
46
47 fn get_dyn_trait() -> Box<dyn TryIntoU32> {
48     Box::new(3u8) as Box<dyn TryIntoU32>
49 }
50
51 fn main() {
52     dbg!(inner::test());
53 }