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

Skip virtual dispatches when collecting default impl mono items#158822
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.

Problem

Under eager mono item collection (-Clink-dead-code), create_mono_items_for_default_impls
walks a trait's provided methods for each impl. When a trait is implemented for (something that
normalizes to) its own trait object type, e.g. impl Trait for dyn Trait spelled through a
lazy_type_alias or an associated type projection, resolving a provided method with the trait
object as the Self type yields an InstanceKind::Virtual rather than a concrete Item.

That virtual dispatch was pushed as a mono item and later fed to instance_mir, which bugs:

error: internal compiler error: virtual dispatches have no instance MIR
...
query stack during panic:
#0 [items_of_instance] collecting items used by `<dyn Trait as Trait>::a - virtual#3`
#1 [collect_and_partition_mono_items]
#2 [exported_generic_symbols]

These impls are accepted by coherence (only the syntactic impl Trait for dyn Trait is rejected
with E0371; the alias and projection forms are allowed, and are relied upon by the run-pass test
tests/ui/traits/object/ambiguity-vtable-segfault.rs), so the program should compile.

Fix

Skip the item when the resolved instance is a virtual dispatch. A virtual dispatch has no
standalone MIR body to codegen, so there is nothing to collect. This mirrors the normal
use-collection path visit_instance_use, which already skips Virtual/Intrinsic instances.

Tests

tests/ui/codegen/mono-item-collector-dyn-self-default-impl.rs, a build-pass test compiled with
-Clink-dead-code, covering:

  • a lazy_type_alias self type (the original report),
  • an associated type projection self type (this form reproduces with no feature gate),
  • an impl with an overridden method alongside an inherited provided one, and
  • an auto-trait object type (dyn Qux + Send).

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

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

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: virtual dispatches have no instance MIR

4 participants