Skip to content

backend: Fix incorrect impl block selection#4660

Merged
philberty merged 1 commit into
Rust-GCC:masterfrom
nsvke:fix-vtable-impl-selection
Jun 30, 2026
Merged

backend: Fix incorrect impl block selection#4660
philberty merged 1 commit into
Rust-GCC:masterfrom
nsvke:fix-vtable-impl-selection

Conversation

@nsvke

@nsvke nsvke commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

During vtable generation in compute_address_for_trait_item, the compiler was picking the first impl block that satisfied the trait predicate, without verifying if the impl block actually belonged to the target receiver type. This caused dynamic dispatch to call the wrong methods when multiple types implemented the same trait in the same scope.

This patch adds a comparison between the impl block's resolved type and the receiver type to ensure we compute the address of the correct associated function.

Fixes #4600

gcc/rust/ChangeLog:

* backend/rust-compile.cc (HIRCompileBase::compute_address_for_trait_item): Ensure the impl block type matches the receiver type.

gcc/testsuite/ChangeLog:

* rust/execute/trait-multi-impl.rs: New test.

Comment thread gcc/rust/backend/rust-compile.cc
@nsvke nsvke force-pushed the fix-vtable-impl-selection branch 2 times, most recently from baacbf4 to eb82d6a Compare June 29, 2026 11:26
During vtable generation in `compute_address_for_trait_item`, the
compiler was picking the first `impl` block that satisfied the trait
predicate, without verifying if the `impl` block actually belonged to
the target receiver type. This caused dynamic dispatch to call the
wrong methods when multiple types implemented the same trait in the
same scope.

This patch adds a comparison between the impl block's resolved type and
the `receiver` type to ensure we compute the address of the correct
associated function.

gcc/rust/ChangeLog:

	* backend/rust-compile.cc
	(HIRCompileBase::compute_address_for_trait_item): Ensure the
	impl block type matches the receiver type.

gcc/testsuite/ChangeLog:

	* rust/execute/trait-multi-impl.rs: New test.
	* rust/execute/trait-multi-impl-generic.rs: New test.
	* rust/execute/trait-multi-impl-generic2.rs: New test.

Signed-off-by: Enes Cevik <enes@nsvke.com>
@nsvke nsvke force-pushed the fix-vtable-impl-selection branch from eb82d6a to f0a1d3b Compare June 30, 2026 07:29

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

LGTM

@philberty philberty added this pull request to the merge queue Jun 30, 2026
Merged via the queue into Rust-GCC:master with commit b634446 Jun 30, 2026
13 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in libcore 1.49 Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Trait resolution mixes up methods for same-named structs in different modules

2 participants