Skip to content

Commit

Permalink
Fix field visibility for read-only WorldQuery types (#8163)
Browse files Browse the repository at this point in the history
When using the `#[derive(WorldQuery)]` macro, the `ReadOnly` struct
generated has default (private) visibility for each field, regardless of
the visibility of the original field.

For each field of a read-only `WorldQuery` variant, use the visibility
of the associated field defined on the original struct.
  • Loading branch information
JoJoJet authored and mockersf committed Mar 27, 2023
1 parent d6aba58 commit 673a627
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/bevy_ecs/macros/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> 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,)*
}

Expand Down
34 changes: 34 additions & 0 deletions crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1642,3 +1642,37 @@ unsafe impl<Q: WorldQuery> WorldQuery for NopWorldQuery<Q> {

/// SAFETY: `NopFetch` never accesses any data
unsafe impl<Q: WorldQuery> ReadOnlyWorldQuery for NopWorldQuery<Q> {}

#[cfg(test)]
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<private::Q>) {
for q in &query {
let _ = &q.a;
}
}

crate::system::assert_is_system(my_system);
}
}

0 comments on commit 673a627

Please sign in to comment.