Rollup merge of #163450 - JonathanBrouwer:fix-never, r=lcnr

Force the correct type variable to never for method resolution on an adjusted never type

Fixes the problem described in https://rust-lang.zulipchat.com/#narrow/channel/618216-t-types.2Fcall-for-participation/topic/method.20call.20on.20diverging.20infer.20doesn.27t.20constrain.20to.20never/near/627440568

The reason that the old code didn't work, as far as I understand, is that the subtype constraint `root_ty <: ty` is only evaluated after `ty` is unioned with `&_`, causing a `mismatched type` error when `&_` is then given the subtype constraint.

This is fixed by demanding `ty`, rather than `root_ty` to be equal to never.

I asked lcnr whether they think there's a way to reproduce this on the old trait solver, they were unsure and I also am.
I think to be safe we should backport this to 1.100, since the never type stabilization is in 1.100 and there might be a way to break this code then.

r? @lcnr
cc @WaffleLapkin
This commit is contained in:
Jonathan Brouwer
2026-09-29 09:59:41 +02:00
committed by GitHub
4 changed files with 126 additions and 12 deletions

View File

@@ -524,8 +524,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span,
MethodCallOnDivergingInferenceVariable,
);
let root_ty = Ty::new_var(self.tcx, ty_id);
self.demand_eqtype(span, root_ty, self.tcx.types.never);
self.demand_eqtype(span, ty, self.tcx.types.never);
} else {
let guar = match *ty.kind() {
_ if let Some(guar) = self.tainted_by_errors() => guar,

View File

@@ -1,5 +1,5 @@
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:42:7
--> $DIR/method-on-never.rs:44:7
|
LL | x.method();
| ^^^^^^
@@ -10,7 +10,7 @@ LL | x.method();
= note: `#[warn(method_call_on_diverging_infer_var)]` (part of `#[warn(future_incompatible)]`) on by default
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:46:17
--> $DIR/method-on-never.rs:48:17
|
LL | { loop {} }.method();
| ^^^^^^
@@ -20,7 +20,7 @@ LL | { loop {} }.method();
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:51:16
--> $DIR/method-on-never.rs:53:16
|
LL | y => y.method(),
| ^^^^^^
@@ -30,7 +30,7 @@ LL | y => y.method(),
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:57:27
--> $DIR/method-on-never.rs:59:27
|
LL | error => (&error).anyhow_kind().new(error),
| ^^^^^^^^^^^
@@ -40,7 +40,7 @@ LL | error => (&error).anyhow_kind().new(error),
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:63:9
--> $DIR/method-on-never.rs:65:9
|
LL | res.method();
| ^^^^^^
@@ -53,7 +53,7 @@ warning: 5 warnings emitted
Future incompatibility report: Future breakage diagnostic:
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:42:7
--> $DIR/method-on-never.rs:44:7
|
LL | x.method();
| ^^^^^^
@@ -65,7 +65,7 @@ LL | x.method();
Future breakage diagnostic:
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:46:17
--> $DIR/method-on-never.rs:48:17
|
LL | { loop {} }.method();
| ^^^^^^
@@ -77,7 +77,7 @@ LL | { loop {} }.method();
Future breakage diagnostic:
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:51:16
--> $DIR/method-on-never.rs:53:16
|
LL | y => y.method(),
| ^^^^^^
@@ -89,7 +89,7 @@ LL | y => y.method(),
Future breakage diagnostic:
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:57:27
--> $DIR/method-on-never.rs:59:27
|
LL | error => (&error).anyhow_kind().new(error),
| ^^^^^^^^^^^
@@ -101,7 +101,7 @@ LL | error => (&error).anyhow_kind().new(error),
Future breakage diagnostic:
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:63:9
--> $DIR/method-on-never.rs:65:9
|
LL | res.method();
| ^^^^^^

View File

@@ -0,0 +1,113 @@
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:44:7
|
LL | x.method();
| ^^^^^^
|
= help: consider providing a type annotation
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
= note: `#[warn(method_call_on_diverging_infer_var)]` (part of `#[warn(future_incompatible)]`) on by default
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:48:17
|
LL | { loop {} }.method();
| ^^^^^^
|
= help: consider providing a type annotation
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:53:16
|
LL | y => y.method(),
| ^^^^^^
|
= help: consider providing a type annotation
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:59:27
|
LL | error => (&error).anyhow_kind().new(error),
| ^^^^^^^^^^^
|
= help: consider providing a type annotation
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:65:9
|
LL | res.method();
| ^^^^^^
|
= help: consider providing a type annotation
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
warning: 5 warnings emitted
Future incompatibility report: Future breakage diagnostic:
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:44:7
|
LL | x.method();
| ^^^^^^
|
= help: consider providing a type annotation
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
= note: `#[warn(method_call_on_diverging_infer_var)]` (part of `#[warn(future_incompatible)]`) on by default
Future breakage diagnostic:
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:48:17
|
LL | { loop {} }.method();
| ^^^^^^
|
= help: consider providing a type annotation
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
= note: `#[warn(method_call_on_diverging_infer_var)]` (part of `#[warn(future_incompatible)]`) on by default
Future breakage diagnostic:
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:53:16
|
LL | y => y.method(),
| ^^^^^^
|
= help: consider providing a type annotation
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
= note: `#[warn(method_call_on_diverging_infer_var)]` (part of `#[warn(future_incompatible)]`) on by default
Future breakage diagnostic:
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:59:27
|
LL | error => (&error).anyhow_kind().new(error),
| ^^^^^^^^^^^
|
= help: consider providing a type annotation
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
= note: `#[warn(method_call_on_diverging_infer_var)]` (part of `#[warn(future_incompatible)]`) on by default
Future breakage diagnostic:
warning: method call on a diverging inference variable
--> $DIR/method-on-never.rs:65:9
|
LL | res.method();
| ^^^^^^
|
= help: consider providing a type annotation
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #156047 <https://github.com/rust-lang/rust/issues/156047>
= note: `#[warn(method_call_on_diverging_infer_var)]` (part of `#[warn(future_incompatible)]`) on by default

View File

@@ -1,3 +1,5 @@
//@ revisions: current next
//@[next] compile-flags: -Znext-solver
//@ check-pass
// Regression test for https://github.com/rust-lang/rust/issues/143349