backend: Fix incorrect impl block selection#4660
Merged
Merged
Conversation
philberty
reviewed
Jun 29, 2026
baacbf4 to
eb82d6a
Compare
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>
eb82d6a to
f0a1d3b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
During vtable generation in
compute_address_for_trait_item, the compiler was picking the firstimplblock that satisfied the trait predicate, without verifying if theimplblock 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
receivertype to ensure we compute the address of the correct associated function.Fixes #4600
gcc/rust/ChangeLog:
gcc/testsuite/ChangeLog: