fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
3.0 KiB98 linesraw
1// run-rustfix
2// edition:2018
3// check-pass
4#![warn(rust_2021_prelude_collisions)]
5
6trait TryIntoU32 {
7    fn try_into(self) -> Result<u32, ()>;
8}
9
10impl TryIntoU32 for u8 {
11    fn try_into(self) -> Result<u32, ()> {
12        Ok(self as u32)
13    }
14}
15
16// needed for autoref test
17impl TryIntoU32 for &f32 {
18    fn try_into(self) -> Result<u32, ()> {
19        Ok(*self as u32)
20    }
21}
22
23trait TryFromU8: Sized {
24    fn try_from(x: u8) -> Result<Self, ()>;
25}
26
27impl TryFromU8 for u32 {
28    fn try_from(x: u8) -> Result<Self, ()> {
29        Ok(x as u32)
30    }
31}
32
33impl TryIntoU32 for *const u16 {
34    fn try_into(self) -> Result<u32, ()> {
35        Ok(unsafe { *self } as u32)
36    }
37}
38
39trait FromByteIterator {
40    fn from_iter<T>(iter: T) -> Self
41    where
42        T: Iterator<Item = u8>;
43}
44
45impl FromByteIterator for Vec<u8> {
46    fn from_iter<T>(iter: T) -> Self
47    where
48        T: Iterator<Item = u8>,
49    {
50        iter.collect()
51    }
52}
53
54fn main() {
55    // test dot-call that will break in 2021 edition
56    let _: u32 = TryIntoU32::try_into(3u8).unwrap();
57    //~^ WARNING trait method `try_into` will become ambiguous in Rust 2021
58    //~^^ WARNING this is accepted in the current edition
59
60    // test associated function call that will break in 2021 edition
61    let _ = <u32 as TryFromU8>::try_from(3u8).unwrap();
62    //~^ WARNING trait-associated function `try_from` will become ambiguous in Rust 2021
63    //~^^ WARNING this is accepted in the current edition
64
65    // test reverse turbofish too
66    let _ = <Vec<u8> as FromByteIterator>::from_iter(vec![1u8, 2, 3, 4, 5, 6].into_iter());
67    //~^ WARNING trait-associated function `from_iter` will become ambiguous in Rust 2021
68    //~^^ WARNING this is accepted in the current edition
69
70    // negative testing lint (this line should *not* emit a warning)
71    let _: u32 = TryFromU8::try_from(3u8).unwrap();
72
73    // test type omission
74    let _: u32 = <_ as TryFromU8>::try_from(3u8).unwrap();
75    //~^ WARNING trait-associated function `try_from` will become ambiguous in Rust 2021
76    //~^^ WARNING this is accepted in the current edition
77
78    // test autoderef
79    let _: u32 = TryIntoU32::try_into(*(&3u8)).unwrap();
80    //~^ WARNING trait method `try_into` will become ambiguous in Rust 2021
81    //~^^ WARNING this is accepted in the current edition
82
83    // test autoref
84    let _: u32 = TryIntoU32::try_into(&3.0).unwrap();
85    //~^ WARNING trait method `try_into` will become ambiguous in Rust 2021
86    //~^^ WARNING this is accepted in the current edition
87
88    let mut data = 3u16;
89    let mut_ptr = std::ptr::addr_of_mut!(data);
90    let _: u32 = TryIntoU32::try_into(mut_ptr as *const _).unwrap();
91    //~^ WARNING trait method `try_into` will become ambiguous in Rust 2021
92    //~^^ WARNING this is accepted in the current edition
93
94    type U32Alias = u32;
95    let _ = <U32Alias as TryFromU8>::try_from(3u8).unwrap();
96    //~^ WARNING trait-associated function `try_from` will become ambiguous in Rust 2021
97    //~^^ WARNING this is accepted in the current edition
98}