Skip to content

Commit 3d4f761

Browse files
committed
yeast: Only clone the user context
1 parent 81057bd commit 3d4f761

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎shared/yeast-macros/src/lower.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,9 @@ fn rust_block(block: &syn::Block) -> TokenStream {
641641
fn isolated_interpolation(uses_ctx: bool, body: TokenStream) -> TokenStream {
642642
if uses_ctx {
643643
quote! {{
644-
let __saved_user_ctx = (*ctx).clone();
644+
let __saved_user_ctx = (*ctx.user_ctx).clone();
645645
let __result = { #body };
646-
*ctx = __saved_user_ctx;
646+
*ctx.user_ctx = __saved_user_ctx;
647647
__result
648648
}}
649649
} else {

‎shared/yeast/tests/test.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,33 @@ fn test_interpolation_preserves_user_context_assignment() {
10441044
);
10451045
}
10461046

1047+
#[test]
1048+
fn test_interpolation_accepts_borrowed_build_ctx() {
1049+
fn build_identifier(
1050+
ctx: &mut yeast::build::BuildCtx<'_, InterpolationContext>,
1051+
) -> yeast::OutputId {
1052+
yeast::tree!((identifier #{
1053+
ctx.counter += 1;
1054+
ctx.counter
1055+
}))
1056+
}
1057+
1058+
let input = "x = 1";
1059+
let input_ast = parse_input(input);
1060+
let mut ast = empty_output_ast(input);
1061+
let mut user_ctx = InterpolationContext::default();
1062+
let mut ctx = yeast::build::BuildCtx::new(&input_ast, &mut ast, &mut user_ctx);
1063+
1064+
let node = build_identifier(&mut ctx);
1065+
assert_eq!(ctx.counter, 0);
1066+
assert_dump_eq(
1067+
&dump_ast(ctx.output, node, input),
1068+
r#"
1069+
identifier "1"
1070+
"#,
1071+
);
1072+
}
1073+
10471074
#[derive(Clone, Default)]
10481075
struct BorrowedInterpolationContext {
10491076
locations: Vec<yeast::OutputId>,

0 commit comments

Comments
 (0)