-
Notifications
You must be signed in to change notification settings - Fork 32
/
inherent-dyn-collision.rs
77 lines (67 loc) · 1.94 KB
/
inherent-dyn-collision.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Test case where the method we want is an inherent method on a
// dyn Trait. In that case, the fix is to insert `*` on the receiver.
//
//@ check-pass
//@ run-rustfix
//@ edition:2018
#![warn(rust_2021_prelude_collisions)]
trait TryIntoU32 {
fn try_into(&self) -> Result<u32, ()>;
}
impl TryIntoU32 for u8 {
// note: &self
fn try_into(&self) -> Result<u32, ()> {
Ok(22)
}
}
mod inner {
use super::get_dyn_trait;
// note: this does nothing, but is copying from ffishim's problem of
// having a struct of the same name as the trait in-scope, while *also*
// implementing the trait for that struct but **without** importing the
// trait itself into scope
struct TryIntoU32;
impl super::TryIntoU32 for TryIntoU32 {
fn try_into(&self) -> Result<u32, ()> {
Ok(0)
}
}
// this is where the gross part happens. since `get_dyn_trait` returns
// a Box<dyn Trait>, it can still call the method for `dyn Trait` without
// `Trait` being in-scope. it might even be possible to make the trait itself
// entirely unreference-able from the callsite?
pub fn test() -> u32 {
get_dyn_trait().try_into().unwrap()
//~^ WARNING trait method `try_into` will become ambiguous
//~| WARNING this is accepted in the current edition
}
}
fn get_dyn_trait() -> Box<dyn TryIntoU32> {
Box::new(3u8) as Box<dyn TryIntoU32>
}
fn main() {
dbg!(inner::test());
}
// ferrocene-annotations: fls_85vx1qfa061i
// Traits
//
// ferrocene-annotations: fls_fk2m2irwpeof
// Implementations
//
// ferrocene-annotations: fls_e9hwvqsib5d5
// Modules
//
// ferrocene-annotations: fls_9gprp17h6t1q
// Use Imports
//
// ferrocene-annotations: fls_xa4nbfas01cj
// Call Expressions
//
// ferrocene-annotations: fls_z7q8kbjwdc7g
// Method Call Expressions
//
// ferrocene-annotations: fls_qa98qdi42orq
// Trait Object Types
//
// ferrocene-annotations: fls_wqazkzle0ix9
// Method Resolution