diff options
| author | jam1garner <8260240+jam1garner@users.noreply.github.com> | 2021-05-27 00:30:55 -0400 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2021-06-14 13:27:24 -0400 |
| commit | 35af38353e928c0d244e9bf0daaeaea8bf064fbf (patch) | |
| tree | 3a7a23f8b22890fb3fc527fc7f5d92b7da38c720 | |
| parent | c341d5b9d7ebf0f2a2000de81fe236ae2f3445b0 (diff) | |
| download | rust-35af38353e928c0d244e9bf0daaeaea8bf064fbf.tar.gz rust-35af38353e928c0d244e9bf0daaeaea8bf064fbf.zip | |
Add UI tests for `future_prelude_collision` lint
| -rw-r--r-- | compiler/rustc_typeck/src/check/method/mod.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/lint/future-prelude-collision.fixed | 57 | ||||
| -rw-r--r-- | src/test/ui/lint/future-prelude-collision.rs | 57 | ||||
| -rw-r--r-- | src/test/ui/lint/future-prelude-collision.stderr | 28 |
4 files changed, 143 insertions, 1 deletions
diff --git a/compiler/rustc_typeck/src/check/method/mod.rs b/compiler/rustc_typeck/src/check/method/mod.rs index 1960e757bad..1d9ee4595d1 100644 --- a/compiler/rustc_typeck/src/check/method/mod.rs +++ b/compiler/rustc_typeck/src/check/method/mod.rs @@ -535,7 +535,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let trait_name = tcx.def_path_str(pick.item.container.assert_trait()); let mut lint = lint.build(&format!( - "trait method `{}` will become ambiguous in Rust 2021", + "trait-associated function `{}` will become ambiguous in Rust 2021", method_name.name )); diff --git a/src/test/ui/lint/future-prelude-collision.fixed b/src/test/ui/lint/future-prelude-collision.fixed new file mode 100644 index 00000000000..06d0bb01538 --- /dev/null +++ b/src/test/ui/lint/future-prelude-collision.fixed @@ -0,0 +1,57 @@ +// run-rustfix +// edition:2018 +// check-pass + +trait TryIntoU32 { + fn try_into(self) -> Result<u32, ()>; +} + +impl TryIntoU32 for u8 { + fn try_into(self) -> Result<u32, ()> { + Ok(self as u32) + } +} + +trait TryFromU8: Sized { + fn try_from(x: u8) -> Result<Self, ()>; +} + +impl TryFromU8 for u32 { + fn try_from(x: u8) -> Result<Self, ()> { + Ok(x as u32) + } +} + +trait FromByteIterator { + fn from_iter<T>(iter: T) -> Self + where T: Iterator<Item = u8>; +} + +impl FromByteIterator for Vec<u8> { + fn from_iter<T>(iter: T) -> Self + where T: Iterator<Item = u8> + { + iter.collect() + } +} + +fn main() { + // test dot-call that will break in 2021 edition + let _: u32 = TryIntoU32::try_into(3u8).unwrap(); + //~^ WARNING trait method `try_into` will become ambiguous in Rust 2021 + + // test associated function call that will break in 2021 edition + let _ = <u32 as TryFromU8>::try_from(3u8).unwrap(); + //~^ WARNING trait-associated function `try_from` will become ambiguous in Rust 2021 + + // test reverse turbofish too + let _ = <Vec<u8> as FromByteIterator>::from_iter(vec![1u8, 2, 3, 4, 5, 6].into_iter()); + //~^ WARNING trait-associated function `from_iter` will become ambiguous in Rust 2021 + + // negative testing lint (this line should *not* emit a warning) + let _: u32 = TryFromU8::try_from(3u8).unwrap(); + + // test type omission + let _: u32 = <_ as TryFromU8>::try_from(3u8).unwrap(); + //~^ WARNING trait-associated function `try_from` will become ambiguous in Rust 2021 +} diff --git a/src/test/ui/lint/future-prelude-collision.rs b/src/test/ui/lint/future-prelude-collision.rs new file mode 100644 index 00000000000..61658ac2725 --- /dev/null +++ b/src/test/ui/lint/future-prelude-collision.rs @@ -0,0 +1,57 @@ +// run-rustfix +// edition:2018 +// check-pass + +trait TryIntoU32 { + fn try_into(self) -> Result<u32, ()>; +} + +impl TryIntoU32 for u8 { + fn try_into(self) -> Result<u32, ()> { + Ok(self as u32) + } +} + +trait TryFromU8: Sized { + fn try_from(x: u8) -> Result<Self, ()>; +} + +impl TryFromU8 for u32 { + fn try_from(x: u8) -> Result<Self, ()> { + Ok(x as u32) + } +} + +trait FromByteIterator { + fn from_iter<T>(iter: T) -> Self + where T: Iterator<Item = u8>; +} + +impl FromByteIterator for Vec<u8> { + fn from_iter<T>(iter: T) -> Self + where T: Iterator<Item = u8> + { + iter.collect() + } +} + +fn main() { + // test dot-call that will break in 2021 edition + let _: u32 = 3u8.try_into().unwrap(); + //~^ WARNING trait method `try_into` will become ambiguous in Rust 2021 + + // test associated function call that will break in 2021 edition + let _ = u32::try_from(3u8).unwrap(); + //~^ WARNING trait-associated function `try_from` will become ambiguous in Rust 2021 + + // test reverse turbofish too + let _ = <Vec<u8>>::from_iter(vec![1u8, 2, 3, 4, 5, 6].into_iter()); + //~^ WARNING trait-associated function `from_iter` will become ambiguous in Rust 2021 + + // negative testing lint (this line should *not* emit a warning) + let _: u32 = TryFromU8::try_from(3u8).unwrap(); + + // test type omission + let _: u32 = <_>::try_from(3u8).unwrap(); + //~^ WARNING trait-associated function `try_from` will become ambiguous in Rust 2021 +} diff --git a/src/test/ui/lint/future-prelude-collision.stderr b/src/test/ui/lint/future-prelude-collision.stderr new file mode 100644 index 00000000000..79f4f1412d2 --- /dev/null +++ b/src/test/ui/lint/future-prelude-collision.stderr @@ -0,0 +1,28 @@ +warning: trait method `try_into` will become ambiguous in Rust 2021 + --> $DIR/future-prelude-collision.rs:40:18 + | +LL | let _: u32 = 3u8.try_into().unwrap(); + | ^^^^^^^^^^^^^^ help: disambiguate the associated function: `TryIntoU32::try_into(3u8)` + | + = note: `#[warn(future_prelude_collision)]` on by default + +warning: trait-associated function `try_from` will become ambiguous in Rust 2021 + --> $DIR/future-prelude-collision.rs:44:13 + | +LL | let _ = u32::try_from(3u8).unwrap(); + | ^^^^^^^^^^^^^ help: disambiguate the associated function: `<u32 as TryFromU8>::try_from` + +warning: trait-associated function `from_iter` will become ambiguous in Rust 2021 + --> $DIR/future-prelude-collision.rs:48:13 + | +LL | let _ = <Vec<u8>>::from_iter(vec![1u8, 2, 3, 4, 5, 6].into_iter()); + | ^^^^^^^^^^^^^^^^^^^^ help: disambiguate the associated function: `<Vec<u8> as FromByteIterator>::from_iter` + +warning: trait-associated function `try_from` will become ambiguous in Rust 2021 + --> $DIR/future-prelude-collision.rs:55:18 + | +LL | let _: u32 = <_>::try_from(3u8).unwrap(); + | ^^^^^^^^^^^^^ help: disambiguate the associated function: `<_ as TryFromU8>::try_from` + +warning: 4 warnings emitted + |
