Fix incorrect TypeId retrieval in reflect component clone
#22656
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.
Objective
Fix incorrect
TypeIdretrieval inComponentCloneCtx::write_target_component_reflect().When cloning reflected components, the code incorrectly called
.type_id()on aBox<dyn Reflect>, which returns the container'sTypeIdrather than the underlying concrete type'sTypeId.For example:
See Rust documentation: https://doc.rust-lang.org/std/any/index.html#smart-pointers-and-dyn-any.
Solution
Dereference the
Box<dyn Reflect>before calling.type_id()to obtain the concrete type's identifier.Changed from:
To:
Testing
cargo fmt --checkcargo test -p bevy_reflectcargo test -p bevy_ecsAdditional Notes
This pattern is error-prone. A safer long-term solution would be for
Reflecttrait to provide an unambiguous method liketypeid()orreflect_type_id()that clearly indicates it returns the underlying type's identifier, not the trait object's.Code Change
View the specific change
File:
crates/bevy_ecs/src/entity/clone_entities.rspub fn write_target_component_reflect(&mut self, component: Box<dyn bevy_reflect::Reflect>) { /* ... */ let source_type_id = self .component_info .type_id() .expect("Source component must have TypeId"); - let component_type_id = component.type_id(); + let component_type_id = (*component).type_id(); if source_type_id != component_type_id { panic!("Passed component TypeId does not match source component TypeId") } /* ... */ }