Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions crates/bevy_scene/macros/src/bsn/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,17 @@ impl BsnType {
return Ok(quote! {#(#type_assigns)*});
}

if let Some(
value @ (BsnValue::Ident(_)
| BsnValue::Expr(_)
| BsnValue::Closure(_)
| BsnValue::Tuple(_)),
) = value
{
let ident = ctx.hoisted_expressions.hoist(value);
return Ok(quote! { *#bind_name = #ident; });
}

// NOTE: It is very important to still produce outputs for None field values. This is what
// enables field autocomplete in Rust Analyzer
value
Expand Down Expand Up @@ -995,6 +1006,49 @@ mod tests {
.contains("Duplicate field `x` found in BSN enum variant"));
}

#[test]
fn enum_variant_expr_is_hoisted() {
let mut refs = EntityRefs::default();
let paths = TestPaths::new();
let mut exprs = HoistedExpressions::default();
let mut ctx = paths.ctx(&mut refs, &mut exprs);
let mut assignments = vec![];
let handle = BsnType {
path: parse_quote!(FontSourceTemplate),
enum_variant: Some(parse_quote!(Handle)),
fields: BsnFields::Tuple(vec![BsnUnnamedField {
value: BsnValue::Expr(quote!(some_borrow.clone())),
}]),
};

let res = handle.push_enum_patch(
&mut ctx,
&parse_quote!(Handle),
&mut assignments,
PatchTarget {
path: &[],
is_ref: false,
},
);

assert!(res.is_ok());
assert_eq!(ctx.errors.len(), 0);
assert_eq!(exprs.expressions.len(), 1);
assert_eq!(
exprs.expressions[0].to_string(),
"let _expr0 = { some_borrow . clone () } . into () ;"
);
let assignment_output: String = assignments.iter().map(|t| t.to_string()).collect();
assert!(
assignment_output.contains("_expr0"),
"expected hoisted ident in assignment output: {assignment_output}"
);
assert!(
!assignment_output.contains("some_borrow"),
"borrow should not appear inline in assignment output: {assignment_output}"
);
}

#[test]
fn bsn_root_preserves_inference_on_error() {
// Arrange
Expand Down
42 changes: 42 additions & 0 deletions crates/bevy_scene/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2881,6 +2881,48 @@ mod tests {
assert_eq!(entity.get::<TextFont>().unwrap().font_size, FontSize(24));
}

#[test]
fn enum_variant_subexpressions_are_hoisted() {
#[derive(Component, FromTemplate, PartialEq, Eq, Debug, Clone)]
enum FontSource {
#[default]
Handle { value: String },
}

struct Config {
value: String,
}

fn make_scene(config: &Config) -> impl Scene {
bsn! {
Children [
(FontSource::Handle { value: { config.value.clone() } }),
(FontSource::Handle { value: { config.value.clone() } }),
]
}
}

let mut app = test_app();
let world = app.world_mut();
let config = Config {
value: "test".to_string(),
};
let entity = world.spawn_scene(make_scene(&config)).unwrap().id();
let children = world.entity(entity).get::<Children>().unwrap();
assert_eq!(
world.entity(children[0]).get::<FontSource>().unwrap(),
&FontSource::Handle {
value: "test".to_string(),
}
);
assert_eq!(
world.entity(children[1]).get::<FontSource>().unwrap(),
&FontSource::Handle {
value: "test".to_string(),
}
);
}

#[test]
fn field_name_shorthand() {
let mut app = test_app();
Expand Down
Loading