Skip to content

Commit 186c09a

Browse files
committed
add test for dyn collisions
1 parent 3dc47e2 commit 186c09a

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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(future_prelude_collision)]
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 was previously accepted
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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(future_prelude_collision)]
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 was previously accepted
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
warning: trait method `try_into` will become ambiguous in Rust 2021
2+
--> $DIR/inherent-dyn-collision.rs:41:9
3+
|
4+
LL | get_dyn_trait().try_into().unwrap()
5+
| ^^^^^^^^^^^^^^^ help: disambiguate the method call: `(&*get_dyn_trait())`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/inherent-dyn-collision.rs:8:9
9+
|
10+
LL | #![warn(future_prelude_collision)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^
12+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2021 edition!
13+
= note: for more information, see issue #85684 <https://github.com/rust-lang/rust/issues/85684>
14+
15+
warning: 1 warning emitted
16+

0 commit comments

Comments
 (0)