diff --git a/crates/bevy_ecs/macros/src/fetch.rs b/crates/bevy_ecs/macros/src/fetch.rs index 3ae328e952..a72b4d10f3 100644 --- a/crates/bevy_ecs/macros/src/fetch.rs +++ b/crates/bevy_ecs/macros/src/fetch.rs @@ -348,7 +348,7 @@ pub fn derive_world_query_impl(input: TokenStream) -> TokenStream { #[doc = "`]."] #[automatically_derived] #visibility struct #read_only_struct_name #user_impl_generics #user_where_clauses { - #( #field_idents: #read_only_field_types, )* + #( #field_visibilities #field_idents: #read_only_field_types, )* #(#(#ignored_field_attrs)* #ignored_field_visibilities #ignored_field_idents: #ignored_field_types,)* } diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index 17aa0203ed..c9a18d1081 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -1394,6 +1394,34 @@ mod tests { use super::*; use crate::{self as bevy_ecs, system::Query}; + #[derive(Component)] + pub struct A; + + // Ensures that each field of a `WorldQuery` struct's read-only variant + // has the same visibility as its corresponding mutable field. + #[test] + fn read_only_field_visibility() { + mod private { + use super::*; + + #[derive(WorldQuery)] + #[world_query(mutable)] + pub struct Q { + pub a: &'static mut A, + } + } + + let _ = private::QReadOnly { a: &A }; + + fn my_system(query: Query) { + for q in &query { + let _ = &q.a; + } + } + + crate::system::assert_is_system(my_system); + } + // Ensures that metadata types generated by the WorldQuery macro // do not conflict with user-defined types. // Regression test for https://github.com/bevyengine/bevy/issues/8010.