Skip to content

Commit 879893c

Browse files
authored
fix insert_reflect panic caused by clone_value (#10627)
# Objective - `insert_reflect` relies on `reflect_type_path`, which doesn't gives the actual type path for object created by `clone_value`, leading to an unexpected panic. This is a workaround for it. - Fix #10590 ## Solution - Tries to get type path from `get_represented_type_info` if get failed from `reflect_type_path`. --- ## Defect remaining - `get_represented_type_info` implies a shortage on performance than using `TypeRegistry`.
1 parent cc6c4d6 commit 879893c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

crates/bevy_ecs/src/reflect/entity_commands.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,18 @@ fn insert_reflect(
189189
type_registry: &TypeRegistry,
190190
component: Box<dyn Reflect>,
191191
) {
192-
let type_info = component.reflect_type_path();
192+
let type_info = component
193+
.get_represented_type_info()
194+
.expect("component should represent a type.");
195+
let type_path = type_info.type_path();
193196
let Some(mut entity) = world.get_entity_mut(entity) else {
194-
panic!("error[B0003]: Could not insert a reflected component (of type {}) for entity {entity:?} because it doesn't exist in this World.", component.reflect_type_path());
197+
panic!("error[B0003]: Could not insert a reflected component (of type {type_path}) for entity {entity:?} because it doesn't exist in this World.");
195198
};
196-
let Some(type_registration) = type_registry.get_with_type_path(type_info) else {
197-
panic!("Could not get type registration (for component type {}) because it doesn't exist in the TypeRegistry.", component.reflect_type_path());
199+
let Some(type_registration) = type_registry.get_with_type_path(type_path) else {
200+
panic!("Could not get type registration (for component type {type_path}) because it doesn't exist in the TypeRegistry.");
198201
};
199202
let Some(reflect_component) = type_registration.data::<ReflectComponent>() else {
200-
panic!("Could not get ReflectComponent data (for component type {}) because it doesn't exist in this TypeRegistration.", component.reflect_type_path());
203+
panic!("Could not get ReflectComponent data (for component type {type_path}) because it doesn't exist in this TypeRegistration.");
201204
};
202205
reflect_component.insert(&mut entity, &*component);
203206
}
@@ -346,17 +349,22 @@ mod tests {
346349
let mut commands = system_state.get_mut(&mut world);
347350

348351
let entity = commands.spawn_empty().id();
352+
let entity2 = commands.spawn_empty().id();
349353

350354
let boxed_reflect_component_a = Box::new(ComponentA(916)) as Box<dyn Reflect>;
355+
let boxed_reflect_component_a_clone = boxed_reflect_component_a.clone_value();
351356

352357
commands
353358
.entity(entity)
354359
.insert_reflect(boxed_reflect_component_a);
360+
commands
361+
.entity(entity2)
362+
.insert_reflect(boxed_reflect_component_a_clone);
355363
system_state.apply(&mut world);
356364

357365
assert_eq!(
358366
world.entity(entity).get::<ComponentA>(),
359-
Some(&ComponentA(916))
367+
world.entity(entity2).get::<ComponentA>()
360368
);
361369
}
362370

0 commit comments

Comments
 (0)