Skip to content

bevy_reflect: ignored fields are not ignored by FromReflect #5101

@nicopap

Description

@nicopap

bevy version: main as of 1bd33ca

When deriving FromReflect on tuple structs, the field index of fields declared after a field marked with #[reflect(ignore)] are not as one would expect them.

Here is an example:

#[derive(Reflect, FromReflect)]
struct Foo(
  u32, // :0
  #[reflect(ignore)]
  f32, // :1
  String, // :2
);

let mut tuple_struct = DynamicTupleStruct::default();
tuple_struct.set_name("Foo".to_owned());
tuple_struct.insert(22_u32);
tuple_struct.insert(3432423234234_i128);
tuple_struct.insert("hello world".to_owned());

let foo = Foo::from_reflect(&tuple_struct).unwrap();

println!(
    "tuple_struct\n[0]: {:?}\n[1]: {:?}\n[2]: {:?}\n",
    tuple_struct.field(0),
    tuple_struct.field(1),
    tuple_struct.field(2),
);
println!(
    "foo\n[0]: {:?}\n[1]: {:?}\n[2]: {:?}\n",
    foo.field(0),
    foo.field(1),
    foo.field(2).
);

Prints the following:

tuple_struct
[0]: Some(22)
[1]: Some(3432423234234)
[2]: Some("hello world")

foo
[0]: Some(22)
[1]: Some("hello world")
[2]: None

The unexpected behavior is here:

let mut tuple_struct = DynamicTupleStruct::default();
tuple_struct.set_name("Foo".to_owned());
tuple_struct.insert(22_u32);
// vvv required, otherwise Foo::from_reflect returns None
tuple_struct.insert(3432423234234_i128);
tuple_struct.insert("hello world".to_owned());

let foo = Foo::from_reflect(&tuple_struct).unwrap();

You'd expect to be able to build Foo from a DynamicTupleStruct with exactly two fields, not three. The correct behavior would be to account for it in the FromReflect implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ReflectionRuntime information about typesC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions