Skip to content

Commit d45bcfd

Browse files
Wuketukealice-i-cecileMrGVSV
authored
improved the error message by insert_boxed (issue #13646) (again) (#13706)
previously I worked on fixing issue #13646, back when the error message did not include the type at all. But that error message had room for improvement, so I included the feedback of @alice-i-cecile and @MrGVSV. The error message will now read `the given key (of type bevy_reflect::tests::Foo) does not support hashing` or 'the given key (of type bevy_reflect::DynamicStruct) does not support hashing' in case of a dynamic struct that represents a hashable struct i also added a new unit test for this new behaviour (`reflect_map_no_hash_dynamic`). Fixes #13646 (again) --------- Co-authored-by: Alice Cecile <[email protected]> Co-authored-by: Gino Valente <[email protected]>
1 parent b17292f commit d45bcfd

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

crates/bevy_reflect/src/lib.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ mod tests {
596596
any::TypeId,
597597
borrow::Cow,
598598
fmt::{Debug, Formatter},
599+
hash::Hash,
599600
marker::PhantomData,
600601
};
601602

@@ -756,19 +757,60 @@ mod tests {
756757
}
757758

758759
#[test]
759-
#[should_panic(expected = "the given key bevy_reflect::tests::Foo does not support hashing")]
760+
#[should_panic(
761+
expected = "the given key of type `bevy_reflect::tests::Foo` does not support hashing"
762+
)]
760763
fn reflect_map_no_hash() {
761764
#[derive(Reflect)]
762765
struct Foo {
763766
a: u32,
764767
}
765768

766769
let foo = Foo { a: 1 };
770+
assert!(foo.reflect_hash().is_none());
767771

768772
let mut map = DynamicMap::default();
769773
map.insert(foo, 10u32);
770774
}
771775

776+
#[test]
777+
#[should_panic(
778+
expected = "the dynamic type `bevy_reflect::DynamicStruct` (representing `bevy_reflect::tests::Foo`) does not support hashing"
779+
)]
780+
fn reflect_map_no_hash_dynamic_representing() {
781+
#[derive(Reflect, Hash)]
782+
#[reflect(Hash)]
783+
struct Foo {
784+
a: u32,
785+
}
786+
787+
let foo = Foo { a: 1 };
788+
assert!(foo.reflect_hash().is_some());
789+
let dynamic = foo.clone_dynamic();
790+
791+
let mut map = DynamicMap::default();
792+
map.insert(dynamic, 11u32);
793+
}
794+
795+
#[test]
796+
#[should_panic(
797+
expected = "the dynamic type `bevy_reflect::DynamicStruct` does not support hashing"
798+
)]
799+
fn reflect_map_no_hash_dynamic() {
800+
#[derive(Reflect, Hash)]
801+
#[reflect(Hash)]
802+
struct Foo {
803+
a: u32,
804+
}
805+
806+
let mut dynamic = DynamicStruct::default();
807+
dynamic.insert("a", 4u32);
808+
assert!(dynamic.reflect_hash().is_none());
809+
810+
let mut map = DynamicMap::default();
811+
map.insert(dynamic, 11u32);
812+
}
813+
772814
#[test]
773815
fn reflect_ignore() {
774816
#[derive(Reflect)]

crates/bevy_reflect/src/map.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,26 @@ impl MapInfo {
197197
#[macro_export]
198198
macro_rules! hash_error {
199199
( $key:expr ) => {{
200-
let type_name = match (*$key).get_represented_type_info() {
201-
None => "Unknown",
202-
Some(s) => s.type_path(),
203-
};
204-
format!("the given key {} does not support hashing", type_name).as_str()
205-
}};
200+
let type_path = (*$key).reflect_type_path();
201+
if !$key.is_dynamic() {
202+
format!(
203+
"the given key of type `{}` does not support hashing",
204+
type_path
205+
)
206+
} else {
207+
match (*$key).get_represented_type_info() {
208+
// Handle dynamic types that do not represent a type (i.e a plain `DynamicStruct`):
209+
None => format!("the dynamic type `{}` does not support hashing", type_path),
210+
// Handle dynamic types that do represent a type (i.e. a `DynamicStruct` proxying `Foo`):
211+
Some(s) => format!(
212+
"the dynamic type `{}` (representing `{}`) does not support hashing",
213+
type_path,
214+
s.type_path()
215+
),
216+
}
217+
}
218+
.as_str()
219+
}}
206220
}
207221

208222
/// An ordered mapping between reflected values.

0 commit comments

Comments
 (0)