Skip to content

Fix trait method resolution on an adjusted never type#156047

Open
JonathanBrouwer wants to merge 2 commits into
rust-lang:mainfrom
JonathanBrouwer:never-ty-method
Open

Fix trait method resolution on an adjusted never type#156047
JonathanBrouwer wants to merge 2 commits into
rust-lang:mainfrom
JonathanBrouwer:never-ty-method

Conversation

@JonathanBrouwer

@JonathanBrouwer JonathanBrouwer commented May 1, 2026

Copy link
Copy Markdown
Contributor

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 1, 2026
@rustbot

rustbot commented May 1, 2026

Copy link
Copy Markdown
Collaborator

WaffleLapkin is not on the review rotation at the moment.
They may take a while to respond.

@JonathanBrouwer JonathanBrouwer May 1, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this PR allows more code to compile on stable, for example see this test.

View changes since the review

@JonathanBrouwer JonathanBrouwer May 3, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a problem we could delay the problem by putting this change behind the never_type feature gate

Comment thread compiler/rustc_hir_typeck/src/method/probe.rs Outdated

@lcnr lcnr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this a reasonable change, I would expect us to emit a FCW here, I don't want to support this permanently or more importantly, this may overlap with other kinds of inference fallback in the future, so we generally don't want users to rely on this

View changes since this review

@WaffleLapkin WaffleLapkin added the F-never_type `#![feature(never_type)]` label May 1, 2026
@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin WaffleLapkin added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. T-types Relevant to the types team, which will review and decide on the PR/issue. labels May 1, 2026
@JonathanBrouwer

JonathanBrouwer commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

This PR is now based on #156048, still need to add the FCW lint, I'll do that later.
@rustbot author

Is "Trait method call on adjusted never type" the correct terminology? I'm not super familiar with the type system yet

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 2, 2026
@rustbot

rustbot commented May 2, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 2, 2026
@rust-bors

This comment has been minimized.

@rustbot

rustbot commented May 3, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

}

declare_lint! {
/// The `trait_method_on_coerced_never_type` lint detects situations in which a never type, which

@JonathanBrouwer JonathanBrouwer May 3, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really unsure of what the correct terminology is, both for the lint definition and the diagnostic, so please give feedback on this

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the proper way to say it would be "method_call_on_diverging_infer_variable" and "detects situations in which a method is called on a value resulting from a never-to-any coercion, without necessary information to infer a type for it", but I'm not sure how much we want to surface this internal language...

"detects trait method calls on an coerced never type",
@future_incompatible = FutureIncompatibleInfo {
reason: fcw!(FutureReleaseError #156047),
report_in_deps: true,

@JonathanBrouwer JonathanBrouwer May 3, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided on a lint with level Warn with report_in_deps: true. If something else is desired, let me know

View changes since the review

@JonathanBrouwer

JonathanBrouwer commented May 3, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

I rebased on #156079 and #156048 so the diff is nicely small now

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 3, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin WaffleLapkin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code changes look good to me. This is blocked on T-types FCP (also the wording in the lint could be improved).

View changes since this review

}

declare_lint! {
/// The `trait_method_on_coerced_never_type` lint detects situations in which a never type, which

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the proper way to say it would be "method_call_on_diverging_infer_variable" and "detects situations in which a method is called on a value resulting from a never-to-any coercion, without necessary information to infer a type for it", but I'm not sure how much we want to surface this internal language...

Comment on lines +5731 to +5736
/// Calling trait methods on a coerced `!` was previously disallowed for the never type,
/// but it did work for empty enums such as `Infallible` since these don't coerce.
/// This means that changing the definition of `Infallible` to become a type alias to `!` (a long-term goal),
/// would break code that called a trait method on `Infallible`, in such a way that the `!` would coerce.
///
/// Therefore, to aid in the transition of changing `Infallible` to a type alias, this is temporarily allowed with a FCW.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a note that calling a method on ! is never useful (it's unreachable code) and propose removing the method call / using the never to any coercion.

Also mention that if people want to support MSRVs before Infallible = !, the proper fix is to match e {} as DesiredType.

@WaffleLapkin WaffleLapkin added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 4, 2026
@JonathanBrouwer JonathanBrouwer added the I-types-nominated Nominated for discussion during a types team meeting. label May 8, 2026
@JonathanBrouwer JonathanBrouwer added the S-waiting-on-t-types Status: Awaiting decision from T-types label May 29, 2026
@lcnr

lcnr commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Code changes look good to me. This is blocked on T-types FCP (also the wording in the lint could be improved).

Can one of you writeup a quick proposal of what this is doing and why?

@WaffleLapkin

Copy link
Copy Markdown
Member

Proposal

When trying to call methods on a type containing diverging inference variables, fallback them to ! and emit a FCW.

Motivation

#155924 describes current breakage from making Infallible = !. One of the issues (number 1 in the list there) is code that tries to convert Result<_, Infallible> to Result<_, anyhow::Error> with .map_err(|e| anyhow::anyhow!(e)).

The problem is that anyhow! does auto-ref based specialization on the error, in the form of

let error = match $err {
    error => (&error).anyhow_kind().new(error),
};

match scrutinee is a coercion point, so error happens to be a diverging inference variable and method selection currently fails.

I would like to avoid this breakage.

FCW?

I personally think that one ought to be able to call a method on a diverging inference variable regardless. Otherwise you end up with code breaking in confusing situations, see #143349 for example.

However if I recall correctly @lcnr was against having this hack permanently. So we can report an FCW and eventually remove this hack.

@lcnr

lcnr commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

@rfcbot fcp merge types

@rust-rfcbot

rust-rfcbot commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

@lcnr has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. F-never_type `#![feature(never_type)]` I-types-nominated Nominated for discussion during a types team meeting. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. S-waiting-on-t-types Status: Awaiting decision from T-types T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent method lookup on never type

6 participants