Skip to content

Emit a suggestion to cast the never type into a concrete type when it fails to satisfy an impl Trait bound#157966

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Muhtasim-Rasheed:issue-157923-suggestion-cast-never-type
Jul 6, 2026
Merged

Emit a suggestion to cast the never type into a concrete type when it fails to satisfy an impl Trait bound#157966
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Muhtasim-Rasheed:issue-157923-suggestion-cast-never-type

Conversation

@Muhtasim-Rasheed

@Muhtasim-Rasheed Muhtasim-Rasheed commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #157923

@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 Jun 16, 2026
@rustbot

rustbot commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

r? @mati865

rustbot has assigned @mati865.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 20 candidates

@qaijuang qaijuang 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.

Took a quick glance 👀

View changes since this review

Comment on lines +4490 to +4496
let snippet = tcx.sess.source_map().span_to_snippet(span).unwrap();
if span != expr.span {
err.help(format!(
"`!` can be coerced to any type; consider casting to a concrete type that implements the trait, e.g. \
`{} as SomeType`", snippet
));
}

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.

Suggested change
let snippet = tcx.sess.source_map().span_to_snippet(span).unwrap();
if span != expr.span {
err.help(format!(
"`!` can be coerced to any type; consider casting to a concrete type that implements the trait, e.g. \
`{} as SomeType`", snippet
));
}
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(span)
&& span != expr.span
{
err.help(format!(
"`!` can be coerced to any type; consider casting to a concrete type that implements the trait, e.g. \
`{snippet} as SomeType`"
));
}

@mati865

mati865 commented Jun 17, 2026

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned davidtwco and unassigned mati865 Jun 17, 2026

@davidtwco davidtwco 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.

This seems reasonable to me, one comment

View changes since this review

Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 2, 2026
@rustbot

rustbot commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

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

@Muhtasim-Rasheed Muhtasim-Rasheed force-pushed the issue-157923-suggestion-cast-never-type branch from e0182f3 to c9ad0e5 Compare July 5, 2026 13:56
@rustbot

rustbot commented Jul 5, 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.

@Muhtasim-Rasheed

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@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 Jul 5, 2026
@davidtwco

Copy link
Copy Markdown
Member

@bors r+ rollup

@rust-bors

rust-bors Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

📌 Commit c9ad0e5 has been approved by davidtwco

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 6, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 6, 2026
…estion-cast-never-type, r=davidtwco

Emit a suggestion to cast the never type into a concrete type when it fails to satisfy an `impl Trait` bound

Fixes rust-lang#157923
rust-bors Bot pushed a commit that referenced this pull request Jul 6, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - #156976 (enable eager `param_env` norm in new solver)
 - #158537 (Add `std::io::cursor::WriteThroughCursor`)
 - #158540 (Move `std::io::Seek` to `core::io`)
 - #157820 (consider subtyping when checking if an infer var is sized)
 - #158505 (Update POSIX edition links)
 - #158853 (Fix typo for link on nto-qnx.md)
 - #157466 (Better error message when bare type in impl parameter list)
 - #157966 (Emit a suggestion to cast the never type into a concrete type when it fails to satisfy an `impl Trait` bound)
 - #158381 (Expose debug scope of statement and terminator in rustc_public)
 - #158405 (rustc_target: Add ARMv8-M related target features)
 - #158770 (Weaken guarantee for `From<legacy::RangeInclusive> for RangeInclusive`)
 - #158820 (Fix rustdoc ICE on deprecated note in inlined re-export chain)
@rust-bors rust-bors Bot merged commit 1c30ae0 into rust-lang:main Jul 6, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 6, 2026
rust-timer added a commit that referenced this pull request Jul 6, 2026
Rollup merge of #157966 - Muhtasim-Rasheed:issue-157923-suggestion-cast-never-type, r=davidtwco

Emit a suggestion to cast the never type into a concrete type when it fails to satisfy an `impl Trait` bound

Fixes #157923
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Emit a suggestion to cast ! to a concrete type when ! is infered as the hidden type for impl Trait and ! doesn't implement Trait

5 participants