Skip to content

Skip virtual dispatches when collecting default impl mono items - #158822

Open
peterphitran wants to merge 2 commits into
rust-lang:mainfrom
peterphitran:fix-ice-158411
Open

peterphitran wants to merge 2 commits into
rust-lang:mainfrom
peterphitran:fix-ice-158411

Conversation

@peterphitran

@peterphitran peterphitran commented Jul 5, 2026 •

Copy link
Copy Markdown

Fixes #158411. Fixes #114198.

With -Clink-dead-code, create_mono_items_for_default_impls resolves each inherited provided method of a trait impl. When the impl's self type normalizes to the trait's own object type (via a type alias or projection, which coherence accepts, see #57893), resolution picks the builtin object candidate and returns InstanceKind::Virtual, which was pushed as a mono item and ICEd in instance_mir. This skips Virtual instances there, as visit_instance_use already does on the lazy path. Nothing reachable is lost: calls on dyn Trait always go through the vtable. Rejecting these impls instead would break tests/ui/traits/object/ambiguity-vtable-segfault.rs. Reproduces on nightly without feature gates (impl Trait for <Ty as Owner>::Struct with Struct = dyn Trait).

Test plan (build-pass with -Clink-dead-code):

  • Lazy type alias and associated type projection self types both compile: the two ways to reach the ICE.
  • An overridden method in the same impl is still collected: the skip must not drop real items.
  • dyn Trait + Send compiles: auto traits in the object type take the same path.
  • tests/crashes/114198*.rs are removed since they now compile; their cases are in the new test.

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

rustbot commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @TaKO8Ki (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

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

@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

This PR changes a file inside tests/crashes. If a crash was fixed, please move into the corresponding ui subdir and add 'Fixes #' to the PR description to autoclose the issue upon merge.

@rust-bors

rust-bors Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #159407) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

@fmease fmease added the F-checked_type_aliases `#![feature(checked_type_aliases)]` (formerly: `lazy_type_alias`) label Aug 22, 2026
@fmease fmease moved this to In Progress in Checked Type Aliases (CTA) Aug 22, 2026
@apiraino

apiraino commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

@rustbot reroll

@rustbot rustbot assigned jieyouxu and unassigned TaKO8Ki Oct 1, 2026
@rust-lang rust-lang deleted a comment from rustbot Oct 1, 2026
@jieyouxu

jieyouxu commented Oct 1, 2026

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned folkertdev and unassigned jieyouxu Oct 1, 2026
@folkertdev

Copy link
Copy Markdown
Contributor

r? oli-obk (or at least you might know someone specific that is able to review, instead of random chance)

@rustbot rustbot assigned oli-obk and unassigned folkertdev Oct 1, 2026
@oli-obk

oli-obk commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Please state how and where an LLM was used in the process of creating this PR

@peterphitran

peterphitran commented Oct 3, 2026 •

Copy link
Copy Markdown
Author

@oli-obk

  • Recreated and validated this issue myself watching for the crash, from there went back and forth with LLM evaluating root cause, blast radius, and iterating through possible fixes.
  • Landed on this fix from the LLM to skip at the item collection list.
  • From there had it implement and create the tests where I reviewed and then had the LLM create the pr.
  • Had the LLM trace back into the history of where this issue came from
  • Also I have noticed the new LLM policy announced which this PR predates so let me know if needed I can look to start fresh requesting to work with a mentor

Side note:

  • Had the LLM run variant scenarios and found that crashing can occur when a trait has where Self: Sized default method it will return None and expect_resolve resulting in an ICE this is another issue coming from the same root cause as the current issue with -Clink-dead-code and dyn-self impl
  • I asked it about just skiping both Virtual and None and then LLM said it was an option but also brought up another route where can return early from create_mono_items_for_default_impls when its the same or a parent trait and its arguments match dyn type since it is the built in object choice instead of impl

@oli-obk oli-obk added the llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. label Oct 5, 2026
@oli-obk

oli-obk commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Since the PR change is rather trivial, and you predate the policy, I'll give it a shot, but if this results in you just running the LLM for me, then please just close the PR.

Why do we land in this situation, but not for impls that are directly on dyn Trait? Can we instead make it so that there is no difference when resolving method calls for dyn Trait impls compared to impls for type aliases/projections that normalize to dyn Trait?

@oli-obk oli-obk 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 Oct 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-checked_type_aliases `#![feature(checked_type_aliases)]` (formerly: `lazy_type_alias`) llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

Status: In Progress

9 participants