Skip to content

Commit 8473e3f

Browse files
Make sure that we suggest turbofishing the right type arg
1 parent 81eef2d commit 8473e3f

5 files changed

+122
-10
lines changed

compiler/rustc_hir_typeck/src/fallback.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,11 @@ impl<'tcx> AnnotateUnitFallbackVisitor<'_, 'tcx> {
621621
.iter()
622622
.filter(|arg| matches!(arg.unpack(), ty::GenericArgKind::Type(_)))
623623
.count();
624-
for (idx, arg) in args.iter().enumerate() {
624+
for (idx, arg) in args
625+
.iter()
626+
.filter(|arg| matches!(arg.unpack(), ty::GenericArgKind::Type(_)))
627+
.enumerate()
628+
{
625629
if let Some(ty) = arg.as_type()
626630
&& let Some(vid) = self.fcx.root_vid(ty)
627631
&& self.reachable_vids.contains(&vid)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//@ revisions: e2021 e2024
2+
//
3+
//@[e2021] edition: 2021
4+
//@[e2024] edition: 2024
5+
//@[e2024] compile-flags: -Zunstable-options
6+
//
7+
//@[e2021] run-pass
8+
//@[e2021] run-rustfix
9+
//@[e2024] check-fail
10+
11+
fn main() {
12+
m();
13+
q();
14+
let _ = meow();
15+
}
16+
17+
fn m() {
18+
//[e2021]~^ this function depends on never type fallback being `()`
19+
//[e2021]~| this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
20+
let x: () = match true {
21+
true => Default::default(),
22+
//[e2024]~^ error: the trait bound `!: Default` is not satisfied
23+
false => panic!("..."),
24+
};
25+
26+
dbg!(x);
27+
}
28+
29+
fn q() -> Option<()> {
30+
//[e2021]~^ this function depends on never type fallback being `()`
31+
//[e2021]~| this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
32+
fn deserialize<T: Default>() -> Option<T> {
33+
Some(T::default())
34+
}
35+
36+
deserialize::<()>()?;
37+
//[e2024]~^ error: the trait bound `!: Default` is not satisfied
38+
39+
None
40+
}
41+
42+
// Make sure we turbofish the right argument
43+
fn help<'a: 'a, T: Into<()>, U>(_: U) -> Result<T, ()> {
44+
Err(())
45+
}
46+
fn meow() -> Result<(), ()> {
47+
//[e2021]~^ this function depends on never type fallback being `()`
48+
//[e2021]~| this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
49+
help::<(), _>(1)?;
50+
//[e2024]~^ error: the trait bound `(): From<!>` is not satisfied
51+
Ok(())
52+
}

tests/ui/editions/never-type-fallback-breaking.e2021.stderr

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: this function depends on never type fallback being `()`
2-
--> $DIR/never-type-fallback-breaking.rs:15:1
2+
--> $DIR/never-type-fallback-breaking.rs:17:1
33
|
44
LL | fn m() {
55
| ^^^^^^
@@ -8,7 +8,7 @@ LL | fn m() {
88
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
99
= help: specify the types explicitly
1010
note: in edition 2024, the requirement `!: Default` will fail
11-
--> $DIR/never-type-fallback-breaking.rs:19:17
11+
--> $DIR/never-type-fallback-breaking.rs:21:17
1212
|
1313
LL | true => Default::default(),
1414
| ^^^^^^^^^^^^^^^^^^
@@ -19,7 +19,7 @@ LL | let x: () = match true {
1919
| ++++
2020

2121
warning: this function depends on never type fallback being `()`
22-
--> $DIR/never-type-fallback-breaking.rs:27:1
22+
--> $DIR/never-type-fallback-breaking.rs:29:1
2323
|
2424
LL | fn q() -> Option<()> {
2525
| ^^^^^^^^^^^^^^^^^^^^
@@ -28,7 +28,7 @@ LL | fn q() -> Option<()> {
2828
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
2929
= help: specify the types explicitly
3030
note: in edition 2024, the requirement `!: Default` will fail
31-
--> $DIR/never-type-fallback-breaking.rs:34:5
31+
--> $DIR/never-type-fallback-breaking.rs:36:5
3232
|
3333
LL | deserialize()?;
3434
| ^^^^^^^^^^^^^
@@ -37,5 +37,24 @@ help: use `()` annotations to avoid fallback changes
3737
LL | deserialize::<()>()?;
3838
| ++++++
3939

40-
warning: 2 warnings emitted
40+
warning: this function depends on never type fallback being `()`
41+
--> $DIR/never-type-fallback-breaking.rs:46:1
42+
|
43+
LL | fn meow() -> Result<(), ()> {
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
45+
|
46+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
47+
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
48+
= help: specify the types explicitly
49+
note: in edition 2024, the requirement `(): From<!>` will fail
50+
--> $DIR/never-type-fallback-breaking.rs:49:5
51+
|
52+
LL | help(1)?;
53+
| ^^^^^^^
54+
help: use `()` annotations to avoid fallback changes
55+
|
56+
LL | help::<(), _>(1)?;
57+
| +++++++++
58+
59+
warning: 3 warnings emitted
4160

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: the trait bound `!: Default` is not satisfied
2-
--> $DIR/never-type-fallback-breaking.rs:19:17
2+
--> $DIR/never-type-fallback-breaking.rs:21:17
33
|
44
LL | true => Default::default(),
55
| ^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `!`
@@ -8,19 +8,42 @@ LL | true => Default::default(),
88
= help: did you intend to use the type `()` here instead?
99

1010
error[E0277]: the trait bound `!: Default` is not satisfied
11-
--> $DIR/never-type-fallback-breaking.rs:34:5
11+
--> $DIR/never-type-fallback-breaking.rs:36:5
1212
|
1313
LL | deserialize()?;
1414
| ^^^^^^^^^^^^^ the trait `Default` is not implemented for `!`
1515
|
1616
= note: this error might have been caused by changes to Rust's type-inference algorithm (see issue #48950 <https://github.com/rust-lang/rust/issues/48950> for more information)
1717
= help: did you intend to use the type `()` here instead?
1818
note: required by a bound in `deserialize`
19-
--> $DIR/never-type-fallback-breaking.rs:30:23
19+
--> $DIR/never-type-fallback-breaking.rs:32:23
2020
|
2121
LL | fn deserialize<T: Default>() -> Option<T> {
2222
| ^^^^^^^ required by this bound in `deserialize`
2323

24-
error: aborting due to 2 previous errors
24+
error[E0277]: the trait bound `(): From<!>` is not satisfied
25+
--> $DIR/never-type-fallback-breaking.rs:49:5
26+
|
27+
LL | help(1)?;
28+
| ^^^^^^^ the trait `From<!>` is not implemented for `()`
29+
|
30+
= help: the following other types implement trait `From<T>`:
31+
`(T, T)` implements `From<[T; 2]>`
32+
`(T, T, T)` implements `From<[T; 3]>`
33+
`(T, T, T, T)` implements `From<[T; 4]>`
34+
`(T, T, T, T, T)` implements `From<[T; 5]>`
35+
`(T, T, T, T, T, T)` implements `From<[T; 6]>`
36+
`(T, T, T, T, T, T, T)` implements `From<[T; 7]>`
37+
`(T, T, T, T, T, T, T, T)` implements `From<[T; 8]>`
38+
`(T, T, T, T, T, T, T, T, T)` implements `From<[T; 9]>`
39+
and 4 others
40+
= note: required for `!` to implement `Into<()>`
41+
note: required by a bound in `help`
42+
--> $DIR/never-type-fallback-breaking.rs:43:20
43+
|
44+
LL | fn help<'a: 'a, T: Into<()>, U>(_: U) -> Result<T, ()> {
45+
| ^^^^^^^^ required by this bound in `help`
46+
47+
error: aborting due to 3 previous errors
2548

2649
For more information about this error, try `rustc --explain E0277`.

tests/ui/editions/never-type-fallback-breaking.rs

+14
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
//@[e2024] compile-flags: -Zunstable-options
66
//
77
//@[e2021] run-pass
8+
//@[e2021] run-rustfix
89
//@[e2024] check-fail
910

1011
fn main() {
1112
m();
1213
q();
14+
let _ = meow();
1315
}
1416

1517
fn m() {
@@ -36,3 +38,15 @@ fn q() -> Option<()> {
3638

3739
None
3840
}
41+
42+
// Make sure we turbofish the right argument
43+
fn help<'a: 'a, T: Into<()>, U>(_: U) -> Result<T, ()> {
44+
Err(())
45+
}
46+
fn meow() -> Result<(), ()> {
47+
//[e2021]~^ this function depends on never type fallback being `()`
48+
//[e2021]~| this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
49+
help(1)?;
50+
//[e2024]~^ error: the trait bound `(): From<!>` is not satisfied
51+
Ok(())
52+
}

0 commit comments

Comments
 (0)