Aliénore Bouttefeux | c64a2ed | 2021-04-15 14:58:17 | [diff] [blame] | 1 | // Test for issue 81576 |
| 2 | // Remove generic arguments if no method is found for all possible generic argument |
| 3 | |
Aliénore Bouttefeux | 120691c | 2021-05-22 10:03:37 | [diff] [blame] | 4 | use std::marker::PhantomData; |
Aliénore Bouttefeux | c64a2ed | 2021-04-15 14:58:17 | [diff] [blame] | 5 | |
| 6 | struct Wrapper2<'a, T, const C: usize> { |
| 7 | x: &'a T, |
| 8 | } |
| 9 | |
| 10 | impl<'a, const C: usize> Wrapper2<'a, i8, C> { |
| 11 | fn method(&self) {} |
| 12 | } |
| 13 | |
| 14 | impl<'a, const C: usize> Wrapper2<'a, i16, C> { |
| 15 | fn method(&self) {} |
| 16 | } |
| 17 | |
| 18 | impl<'a, const C: usize> Wrapper2<'a, i32, C> { |
| 19 | fn method(&self) {} |
| 20 | } |
| 21 | struct Wrapper<T>(T); |
| 22 | |
Aliénore Bouttefeux | c64a2ed | 2021-04-15 14:58:17 | [diff] [blame] | 23 | impl Wrapper<i8> { |
| 24 | fn method(&self) {} |
| 25 | } |
| 26 | |
| 27 | impl Wrapper<i16> { |
| 28 | fn method(&self) {} |
| 29 | } |
| 30 | |
| 31 | impl Wrapper<i32> { |
| 32 | fn method(&self) {} |
| 33 | } |
| 34 | |
| 35 | impl Wrapper<i64> { |
| 36 | fn method(&self) {} |
| 37 | } |
| 38 | |
| 39 | impl Wrapper<u8> { |
| 40 | fn method(&self) {} |
| 41 | } |
| 42 | |
| 43 | impl Wrapper<u16> { |
| 44 | fn method(&self) {} |
| 45 | } |
| 46 | |
| 47 | struct Point<T> { |
| 48 | x: T, |
| 49 | y: T, |
| 50 | } |
| 51 | |
| 52 | impl Point<f64> { |
| 53 | fn distance(&self) -> f64 { |
| 54 | self.x.hypot(self.y) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | struct Other; |
| 59 | |
| 60 | impl Other { |
| 61 | fn other(&self) {} |
| 62 | } |
| 63 | |
Artur Sinila | c39826e | 2022-07-18 23:25:14 | [diff] [blame] | 64 | struct Struct<T> { |
| 65 | _phatom: PhantomData<T>, |
Aliénore Bouttefeux | 120691c | 2021-05-22 10:03:37 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | impl<T> Default for Struct<T> { |
| 69 | fn default() -> Self { |
Artur Sinila | c39826e | 2022-07-18 23:25:14 | [diff] [blame] | 70 | Self { _phatom: PhantomData } |
Aliénore Bouttefeux | 120691c | 2021-05-22 10:03:37 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | impl<T: Clone + Copy + PartialEq + Eq + PartialOrd + Ord> Struct<T> { |
| 75 | fn method(&self) {} |
| 76 | } |
| 77 | |
Aliénore Bouttefeux | c64a2ed | 2021-04-15 14:58:17 | [diff] [blame] | 78 | fn main() { |
Artur Sinila | c39826e | 2022-07-18 23:25:14 | [diff] [blame] | 79 | let point_f64 = Point { x: 1_f64, y: 1_f64 }; |
Aliénore Bouttefeux | c64a2ed | 2021-04-15 14:58:17 | [diff] [blame] | 80 | let d = point_f64.distance(); |
Artur Sinila | c39826e | 2022-07-18 23:25:14 | [diff] [blame] | 81 | let point_i32 = Point { x: 1_i32, y: 1_i32 }; |
Aliénore Bouttefeux | c64a2ed | 2021-04-15 14:58:17 | [diff] [blame] | 82 | let d = point_i32.distance(); |
| 83 | //~^ ERROR no method named `distance` found for struct `Point<i32> |
| 84 | let d = point_i32.other(); |
Aliénore Bouttefeux | 6efa14b | 2021-05-21 12:01:38 | [diff] [blame] | 85 | //~^ ERROR no method named `other` found for struct `Point |
Michael Goulet | 0fabceb | 2023-04-26 21:48:17 | [diff] [blame] | 86 | let v = vec![1, 2, 3]; |
| 87 | v.iter().map(Box::new(|x| x * x) as Box<dyn Fn(&i32) -> i32>).extend(std::iter::once(100)); |
Aliénore Bouttefeux | 6efa14b | 2021-05-21 12:01:38 | [diff] [blame] | 88 | //~^ ERROR no method named `extend` found for struct `Map |
Aliénore Bouttefeux | c64a2ed | 2021-04-15 14:58:17 | [diff] [blame] | 89 | let wrapper = Wrapper(true); |
| 90 | wrapper.method(); |
| 91 | //~^ ERROR no method named `method` found for struct `Wrapper<bool> |
| 92 | wrapper.other(); |
Aliénore Bouttefeux | 6efa14b | 2021-05-21 12:01:38 | [diff] [blame] | 93 | //~^ ERROR no method named `other` found for struct `Wrapper |
Aliénore Bouttefeux | c64a2ed | 2021-04-15 14:58:17 | [diff] [blame] | 94 | let boolean = true; |
Artur Sinila | c39826e | 2022-07-18 23:25:14 | [diff] [blame] | 95 | let wrapper = Wrapper2::<'_, _, 3> { x: &boolean }; |
Aliénore Bouttefeux | c64a2ed | 2021-04-15 14:58:17 | [diff] [blame] | 96 | wrapper.method(); |
Artur Sinila | c39826e | 2022-07-18 23:25:14 | [diff] [blame] | 97 | //~^ ERROR no method named `method` found for struct `Wrapper2<'_, bool, 3> |
Aliénore Bouttefeux | c64a2ed | 2021-04-15 14:58:17 | [diff] [blame] | 98 | wrapper.other(); |
Aliénore Bouttefeux | 6efa14b | 2021-05-21 12:01:38 | [diff] [blame] | 99 | //~^ ERROR no method named `other` found for struct `Wrapper2 |
Aliénore Bouttefeux | c64a2ed | 2021-04-15 14:58:17 | [diff] [blame] | 100 | let a = vec![1, 2, 3]; |
| 101 | a.not_found(); |
Aliénore Bouttefeux | 6efa14b | 2021-05-21 12:01:38 | [diff] [blame] | 102 | //~^ ERROR no method named `not_found` found for struct `Vec |
Aliénore Bouttefeux | 120691c | 2021-05-22 10:03:37 | [diff] [blame] | 103 | let s = Struct::<f64>::default(); |
| 104 | s.method(); |
| 105 | //~^ ERROR the method `method` exists for struct `Struct<f64>`, but its trait bounds were not satisfied |
Aliénore Bouttefeux | c64a2ed | 2021-04-15 14:58:17 | [diff] [blame] | 106 | } |