Skip to content

Commit 8ff5c2a

Browse files
Use sub_unification_table_root_var instead of root_var to check that the ty vars are equal
1 parent 056df74 commit 8ff5c2a

4 files changed

Lines changed: 87 additions & 8 deletions

File tree

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
501501
// We demand the type to be equal to the never type, so we can probe the never type for methods
502502
// (see https://github.com/rust-lang/rust/issues/143349)
503503
} else if let ty::Infer(ty::TyVar(ty_id)) = *ty.kind()
504-
&& let ty_id = self.root_var(ty_id)
504+
&& let ty_id = self.sub_unification_table_root_var(ty_id)
505505
&& let root_ty = Ty::new_var(self.tcx, ty_id)
506506
&& self
507507
.diverging_type_vars
508508
.borrow()
509509
.iter()
510-
.any(|&candidate_id| self.root_var(candidate_id) == ty_id)
510+
.any(|&candidate_id| self.sub_unification_table_root_var(candidate_id) == ty_id)
511511
{
512512
self.tcx.emit_node_span_lint(
513513
TRAIT_METHOD_ON_COERCED_NEVER_TYPE,

compiler/rustc_infer/src/infer/type_variable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
246246
}
247247

248248
/// Returns the "root" variable of `vid` in the `sub_unification_table`
249-
/// equivalence table. All type variables that have been are related via
249+
/// equivalence table. All type variables that have been related via
250250
/// equality or subtyping will yield the same root variable (per the
251251
/// union-find algorithm), so `sub_unification_table_root_var(a)
252252
/// == sub_unification_table_root_var(b)` implies that:

tests/ui/never_type/basic/method-on-never.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ impl Trait for ! {
1212
}
1313
}
1414

15+
struct Adhoc;
16+
struct Error;
17+
18+
#[doc(hidden)]
19+
trait AdhocKind: Sized {
20+
#[inline]
21+
fn anyhow_kind(&self) -> Adhoc {
22+
Adhoc
23+
}
24+
}
25+
26+
impl<T> AdhocKind for &T where T: ?Sized + Send + Sync + 'static {}
27+
28+
impl Adhoc {
29+
#[cold]
30+
fn new<M>(self, message: M) -> Error
31+
where
32+
M: Send + Sync + 'static,
33+
{
34+
Error
35+
}
36+
}
37+
1538
fn main() {
1639
let x = loop {};
1740
x.method();
@@ -21,4 +44,16 @@ fn main() {
2144
{ loop {} }.method();
2245
//~^ WARN [trait_method_on_coerced_never_type]
2346
//~| WARN previously accepted
47+
48+
let e = match loop {} {
49+
y => y.method(),
50+
//~^ WARN [trait_method_on_coerced_never_type]
51+
//~| WARN previously accepted
52+
};
53+
54+
let error = match loop {} {
55+
error => (&error).anyhow_kind().new(error),
56+
//~^ WARN [trait_method_on_coerced_never_type]
57+
//~| WARN previously accepted
58+
};
2459
}

tests/ui/never_type/basic/method-on-never.stderr

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: trait method call on a coerced never type
2-
--> $DIR/method-on-never.rs:17:7
2+
--> $DIR/method-on-never.rs:40:7
33
|
44
LL | x.method();
55
| ^^^^^^
@@ -10,7 +10,7 @@ LL | x.method();
1010
= note: `#[warn(trait_method_on_coerced_never_type)]` (part of `#[warn(future_incompatible)]`) on by default
1111

1212
warning: trait method call on a coerced never type
13-
--> $DIR/method-on-never.rs:21:17
13+
--> $DIR/method-on-never.rs:44:17
1414
|
1515
LL | { loop {} }.method();
1616
| ^^^^^^
@@ -19,11 +19,31 @@ LL | { loop {} }.method();
1919
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2020
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
2121

22-
warning: 2 warnings emitted
22+
warning: trait method call on a coerced never type
23+
--> $DIR/method-on-never.rs:49:16
24+
|
25+
LL | y => y.method(),
26+
| ^^^^^^
27+
|
28+
= help: consider providing a type annotation
29+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
30+
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
31+
32+
warning: trait method call on a coerced never type
33+
--> $DIR/method-on-never.rs:55:27
34+
|
35+
LL | error => (&error).anyhow_kind().new(error),
36+
| ^^^^^^^^^^^
37+
|
38+
= help: consider providing a type annotation
39+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
40+
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
41+
42+
warning: 4 warnings emitted
2343

2444
Future incompatibility report: Future breakage diagnostic:
2545
warning: trait method call on a coerced never type
26-
--> $DIR/method-on-never.rs:17:7
46+
--> $DIR/method-on-never.rs:40:7
2747
|
2848
LL | x.method();
2949
| ^^^^^^
@@ -35,7 +55,7 @@ LL | x.method();
3555

3656
Future breakage diagnostic:
3757
warning: trait method call on a coerced never type
38-
--> $DIR/method-on-never.rs:21:17
58+
--> $DIR/method-on-never.rs:44:17
3959
|
4060
LL | { loop {} }.method();
4161
| ^^^^^^
@@ -45,3 +65,27 @@ LL | { loop {} }.method();
4565
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
4666
= note: `#[warn(trait_method_on_coerced_never_type)]` (part of `#[warn(future_incompatible)]`) on by default
4767

68+
Future breakage diagnostic:
69+
warning: trait method call on a coerced never type
70+
--> $DIR/method-on-never.rs:49:16
71+
|
72+
LL | y => y.method(),
73+
| ^^^^^^
74+
|
75+
= help: consider providing a type annotation
76+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
77+
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
78+
= note: `#[warn(trait_method_on_coerced_never_type)]` (part of `#[warn(future_incompatible)]`) on by default
79+
80+
Future breakage diagnostic:
81+
warning: trait method call on a coerced never type
82+
--> $DIR/method-on-never.rs:55:27
83+
|
84+
LL | error => (&error).anyhow_kind().new(error),
85+
| ^^^^^^^^^^^
86+
|
87+
= help: consider providing a type annotation
88+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
89+
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
90+
= note: `#[warn(trait_method_on_coerced_never_type)]` (part of `#[warn(future_incompatible)]`) on by default
91+

0 commit comments

Comments
 (0)