|
1 | | -//@ revisions: nofallback fallback |
2 | | -//@[fallback] edition: 2024 |
3 | | -//@[nofallback] run-pass |
4 | | -//@[fallback] check-fail |
5 | | - |
| 1 | +// Test diagnostic for the case where a trait is not implemented for `!`. If it is implemented |
| 2 | +// for `()`, we want to add a note saying that this might be caused by a breaking change in the |
| 3 | +// compiler. |
| 4 | +// |
| 5 | +//@ revisions: e2021 e2024 |
| 6 | +//@[e2021] edition: 2021 |
| 7 | +//@[e2024] edition: 2024 |
| 8 | +//@[e2021] run-pass |
6 | 9 | #![expect(dependency_on_unit_never_type_fallback, unused)] |
7 | 10 |
|
8 | | -trait Deserialize: Sized { |
9 | | - fn deserialize() -> Result<Self, String>; |
10 | | -} |
| 11 | +trait OnlyUnit {} |
11 | 12 |
|
12 | | -impl Deserialize for () { |
13 | | - fn deserialize() -> Result<(), String> { |
14 | | - Ok(()) |
15 | | - } |
16 | | -} |
| 13 | +impl OnlyUnit for () {} |
| 14 | +//[e2024]~^ help: trait `OnlyUnit` is implemented for `()` |
17 | 15 |
|
18 | | -trait ImplementedForUnitButNotNever {} |
19 | | - |
20 | | -impl ImplementedForUnitButNotNever for () {} //[fallback]~ HELP trait `ImplementedForUnitButNotNever` is implemented for `()` |
21 | | - |
22 | | -fn foo<T: ImplementedForUnitButNotNever>(_t: T) {} |
23 | | -//[fallback]~^ note: required by this bound in `foo` |
24 | | -//[fallback]~| note: required by a bound in `foo` |
25 | | -fn smeg() { |
26 | | - let _x = return; |
27 | | - foo(_x); |
28 | | - //[fallback]~^ ERROR the trait bound |
29 | | - //[fallback]~| NOTE the trait `ImplementedForUnitButNotNever` is not implemented |
30 | | - //[fallback]~| NOTE this error might have been caused |
31 | | - //[fallback]~| NOTE required by a bound introduced by this call |
32 | | - //[fallback]~| HELP you might have intended to use the type `()` |
33 | | -} |
| 16 | +fn requires_unit(_: impl OnlyUnit) {} |
| 17 | +//[e2024]~^ note: required by this bound in `requires_unit` |
| 18 | +//[e2024]~| note: required by a bound in `requires_unit` |
| 19 | + |
| 20 | + |
| 21 | +trait OnlyU32 {} |
| 22 | + |
| 23 | +impl OnlyU32 for u32 {} |
| 24 | +//[e2024]~^ help: the trait `OnlyU32` is implemented for `u32` |
| 25 | + |
| 26 | +fn requires_u32(_: impl OnlyU32) {} |
| 27 | +//[e2024]~^ note: required by this bound in `requires_u32` |
| 28 | +//[e2024]~| note: required by a bound in `requires_u32` |
| 29 | + |
| 30 | + |
| 31 | +trait Nothing {} |
| 32 | +//[e2024]~^ help: this trait has no implementations, consider adding one |
| 33 | + |
| 34 | +fn requires_nothing(_: impl Nothing) {} |
| 35 | +//[e2024]~^ note: required by this bound in `requires_nothing` |
| 36 | +//[e2024]~| note: required by a bound in `requires_nothing` |
34 | 37 |
|
35 | 38 | fn main() { |
36 | | - smeg(); |
| 39 | + let x = return; |
| 40 | + requires_unit(x); |
| 41 | + //[e2024]~^ error: the trait bound `!: OnlyUnit` is not satisfied |
| 42 | + //[e2024]~| note: the trait `OnlyUnit` is not implemented for `!` |
| 43 | + //[e2024]~| note: this error might have been caused by changes to Rust's type-inference algorithm (see issue #148922 <https://github.com/rust-lang/rust/issues/148922> for more information) |
| 44 | + //[e2024]~| note: required by a bound introduced by this call |
| 45 | + //[e2024]~| help: you might have intended to use the type `()` |
| 46 | + |
| 47 | + #[cfg(e2024)] |
| 48 | + requires_u32(x); |
| 49 | + //[e2024]~^ error: the trait bound `!: OnlyU32` is not satisfied |
| 50 | + //[e2024]~| note: the trait `OnlyU32` is not implemented for `!` |
| 51 | + //[e2024]~| note: required by a bound introduced by this call |
| 52 | + |
| 53 | + #[cfg(e2024)] |
| 54 | + requires_nothing(x); |
| 55 | + //[e2024]~^ error: the trait bound `!: Nothing` is not satisfied |
| 56 | + //[e2024]~| note: the trait `Nothing` is not implemented for `!` |
| 57 | + //[e2024]~| note: required by a bound introduced by this call |
37 | 58 | } |
0 commit comments