Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 35af383

Browse files
jam1garnernikomatsakis
authored andcommitted
Add UI tests for future_prelude_collision lint
1 parent c341d5b commit 35af383

File tree

4 files changed

+143
-1
lines changed

4 files changed

+143
-1
lines changed

compiler/rustc_typeck/src/check/method/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
535535
let trait_name = tcx.def_path_str(pick.item.container.assert_trait());
536536

537537
let mut lint = lint.build(&format!(
538-
"trait method `{}` will become ambiguous in Rust 2021",
538+
"trait-associated function `{}` will become ambiguous in Rust 2021",
539539
method_name.name
540540
));
541541

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// run-rustfix
2+
// edition:2018
3+
// check-pass
4+
5+
trait TryIntoU32 {
6+
fn try_into(self) -> Result<u32, ()>;
7+
}
8+
9+
impl TryIntoU32 for u8 {
10+
fn try_into(self) -> Result<u32, ()> {
11+
Ok(self as u32)
12+
}
13+
}
14+
15+
trait TryFromU8: Sized {
16+
fn try_from(x: u8) -> Result<Self, ()>;
17+
}
18+
19+
impl TryFromU8 for u32 {
20+
fn try_from(x: u8) -> Result<Self, ()> {
21+
Ok(x as u32)
22+
}
23+
}
24+
25+
trait FromByteIterator {
26+
fn from_iter<T>(iter: T) -> Self
27+
where T: Iterator<Item = u8>;
28+
}
29+
30+
impl FromByteIterator for Vec<u8> {
31+
fn from_iter<T>(iter: T) -> Self
32+
where T: Iterator<Item = u8>
33+
{
34+
iter.collect()
35+
}
36+
}
37+
38+
fn main() {
39+
// test dot-call that will break in 2021 edition
40+
let _: u32 = TryIntoU32::try_into(3u8).unwrap();
41+
//~^ WARNING trait method `try_into` will become ambiguous in Rust 2021
42+
43+
// test associated function call that will break in 2021 edition
44+
let _ = <u32 as TryFromU8>::try_from(3u8).unwrap();
45+
//~^ WARNING trait-associated function `try_from` will become ambiguous in Rust 2021
46+
47+
// test reverse turbofish too
48+
let _ = <Vec<u8> as FromByteIterator>::from_iter(vec![1u8, 2, 3, 4, 5, 6].into_iter());
49+
//~^ WARNING trait-associated function `from_iter` will become ambiguous in Rust 2021
50+
51+
// negative testing lint (this line should *not* emit a warning)
52+
let _: u32 = TryFromU8::try_from(3u8).unwrap();
53+
54+
// test type omission
55+
let _: u32 = <_ as TryFromU8>::try_from(3u8).unwrap();
56+
//~^ WARNING trait-associated function `try_from` will become ambiguous in Rust 2021
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// run-rustfix
2+
// edition:2018
3+
// check-pass
4+
5+
trait TryIntoU32 {
6+
fn try_into(self) -> Result<u32, ()>;
7+
}
8+
9+
impl TryIntoU32 for u8 {
10+
fn try_into(self) -> Result<u32, ()> {
11+
Ok(self as u32)
12+
}
13+
}
14+
15+
trait TryFromU8: Sized {
16+
fn try_from(x: u8) -> Result<Self, ()>;
17+
}
18+
19+
impl TryFromU8 for u32 {
20+
fn try_from(x: u8) -> Result<Self, ()> {
21+
Ok(x as u32)
22+
}
23+
}
24+
25+
trait FromByteIterator {
26+
fn from_iter<T>(iter: T) -> Self
27+
where T: Iterator<Item = u8>;
28+
}
29+
30+
impl FromByteIterator for Vec<u8> {
31+
fn from_iter<T>(iter: T) -> Self
32+
where T: Iterator<Item = u8>
33+
{
34+
iter.collect()
35+
}
36+
}
37+
38+
fn main() {
39+
// test dot-call that will break in 2021 edition
40+
let _: u32 = 3u8.try_into().unwrap();
41+
//~^ WARNING trait method `try_into` will become ambiguous in Rust 2021
42+
43+
// test associated function call that will break in 2021 edition
44+
let _ = u32::try_from(3u8).unwrap();
45+
//~^ WARNING trait-associated function `try_from` will become ambiguous in Rust 2021
46+
47+
// test reverse turbofish too
48+
let _ = <Vec<u8>>::from_iter(vec![1u8, 2, 3, 4, 5, 6].into_iter());
49+
//~^ WARNING trait-associated function `from_iter` will become ambiguous in Rust 2021
50+
51+
// negative testing lint (this line should *not* emit a warning)
52+
let _: u32 = TryFromU8::try_from(3u8).unwrap();
53+
54+
// test type omission
55+
let _: u32 = <_>::try_from(3u8).unwrap();
56+
//~^ WARNING trait-associated function `try_from` will become ambiguous in Rust 2021
57+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
warning: trait method `try_into` will become ambiguous in Rust 2021
2+
--> $DIR/future-prelude-collision.rs:40:18
3+
|
4+
LL | let _: u32 = 3u8.try_into().unwrap();
5+
| ^^^^^^^^^^^^^^ help: disambiguate the associated function: `TryIntoU32::try_into(3u8)`
6+
|
7+
= note: `#[warn(future_prelude_collision)]` on by default
8+
9+
warning: trait-associated function `try_from` will become ambiguous in Rust 2021
10+
--> $DIR/future-prelude-collision.rs:44:13
11+
|
12+
LL | let _ = u32::try_from(3u8).unwrap();
13+
| ^^^^^^^^^^^^^ help: disambiguate the associated function: `<u32 as TryFromU8>::try_from`
14+
15+
warning: trait-associated function `from_iter` will become ambiguous in Rust 2021
16+
--> $DIR/future-prelude-collision.rs:48:13
17+
|
18+
LL | let _ = <Vec<u8>>::from_iter(vec![1u8, 2, 3, 4, 5, 6].into_iter());
19+
| ^^^^^^^^^^^^^^^^^^^^ help: disambiguate the associated function: `<Vec<u8> as FromByteIterator>::from_iter`
20+
21+
warning: trait-associated function `try_from` will become ambiguous in Rust 2021
22+
--> $DIR/future-prelude-collision.rs:55:18
23+
|
24+
LL | let _: u32 = <_>::try_from(3u8).unwrap();
25+
| ^^^^^^^^^^^^^ help: disambiguate the associated function: `<_ as TryFromU8>::try_from`
26+
27+
warning: 4 warnings emitted
28+

0 commit comments

Comments
 (0)