Repository navigation
yeast: Isolate interpolation contexts from each other #22964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tausbn
wants to merge
5
commits into
main
Choose a base branch
from
tausbn/yeast-isolate-interpolation-contexts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+252
−63
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
452ab94
yeast: Isolate interpolation contexts
tausbn b672a33
yeast: Test interpolation context isolation
tausbn 161ee6c
Unified: Use isolated interpolation contexts
tausbn ee41076
yeast: Only clone the user context
tausbn e6205e0
yeast: Document interpolation context detection
tausbn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -943,6 +943,153 @@ fn test_choice_fallback_can_reuse_consumed_splice() { | |||||
| ); | ||||||
| } | ||||||
|
|
||||||
| #[derive(Clone, Default)] | ||||||
| struct InterpolationContext { | ||||||
| counter: usize, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think using a normal unsigned integer would be more natural here.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in e6205e0. |
||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn test_output_template_interpolations_clone_user_context() { | ||||||
| let input = "x = 1"; | ||||||
| let input_ast = parse_input(input); | ||||||
| let mut ast = empty_output_ast(input); | ||||||
| let mut user_ctx = InterpolationContext::default(); | ||||||
| let mut ctx = yeast::build::BuildCtx::new(&input_ast, &mut ast, &mut user_ctx); | ||||||
| let source = yeast::tree!((identifier "source")); | ||||||
|
|
||||||
| let nodes = yeast::trees!( | ||||||
| (call | ||||||
| location: { | ||||||
| ctx.counter += 1; | ||||||
| source | ||||||
| } | ||||||
| method: { | ||||||
| ctx.counter += 1; | ||||||
| let value = ctx.counter.to_string(); | ||||||
| ctx.literal("identifier", &value) | ||||||
| } | ||||||
| receiver: (identifier #{ | ||||||
| ctx.counter += 1; | ||||||
| ctx.counter | ||||||
| }) | ||||||
| ) | ||||||
| { | ||||||
| ctx.counter += 1; | ||||||
| let value = ctx.counter.to_string(); | ||||||
| [ctx.literal("identifier", &value)] | ||||||
| } | ||||||
| ); | ||||||
|
|
||||||
| assert_eq!(ctx.counter, 0); | ||||||
| assert_eq!(nodes.len(), 2); | ||||||
| assert_dump_eq( | ||||||
| &dump_ast(ctx.output, nodes[0], input), | ||||||
| r#" | ||||||
| call | ||||||
| method: identifier "1" | ||||||
| receiver: identifier "1" | ||||||
| "#, | ||||||
| ); | ||||||
| assert_dump_eq( | ||||||
| &dump_ast(ctx.output, nodes[1], input), | ||||||
| r#" | ||||||
| identifier "1" | ||||||
| "#, | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn test_interpolation_preserves_build_ctx_method_borrowing() { | ||||||
| let input = "x = 1"; | ||||||
| let input_ast = parse_input(input); | ||||||
| let mut ast = empty_output_ast(input); | ||||||
| let mut user_ctx = InterpolationContext::default(); | ||||||
| let mut ctx = yeast::build::BuildCtx::new(&input_ast, &mut ast, &mut user_ctx); | ||||||
| let source = yeast::tree!((identifier "source")); | ||||||
|
|
||||||
| let node = yeast::tree!((call | ||||||
| method: { | ||||||
| ctx.literal("identifier", &ctx.source_text(source)) | ||||||
| } | ||||||
| )); | ||||||
|
|
||||||
| assert_dump_eq( | ||||||
| &dump_ast(ctx.output, node, input), | ||||||
| r#" | ||||||
| call | ||||||
| method: identifier "source" | ||||||
| "#, | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn test_interpolation_preserves_user_context_assignment() { | ||||||
| let input = "x = 1"; | ||||||
| let input_ast = parse_input(input); | ||||||
| let mut ast = empty_output_ast(input); | ||||||
| let mut user_ctx = InterpolationContext::default(); | ||||||
| let mut ctx = yeast::build::BuildCtx::new(&input_ast, &mut ast, &mut user_ctx); | ||||||
|
|
||||||
| let node = yeast::tree!((identifier #{ | ||||||
| *ctx = InterpolationContext { counter: 1 }; | ||||||
| ctx.counter | ||||||
| })); | ||||||
|
|
||||||
| assert_eq!(ctx.counter, 0); | ||||||
| assert_dump_eq( | ||||||
| &dump_ast(ctx.output, node, input), | ||||||
| r#" | ||||||
| identifier "1" | ||||||
| "#, | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn test_interpolation_accepts_borrowed_build_ctx() { | ||||||
| fn build_identifier( | ||||||
| ctx: &mut yeast::build::BuildCtx<'_, InterpolationContext>, | ||||||
| ) -> yeast::OutputId { | ||||||
| yeast::tree!((identifier #{ | ||||||
| ctx.counter += 1; | ||||||
| ctx.counter | ||||||
| })) | ||||||
| } | ||||||
|
|
||||||
| let input = "x = 1"; | ||||||
| let input_ast = parse_input(input); | ||||||
| let mut ast = empty_output_ast(input); | ||||||
| let mut user_ctx = InterpolationContext::default(); | ||||||
| let mut ctx = yeast::build::BuildCtx::new(&input_ast, &mut ast, &mut user_ctx); | ||||||
|
|
||||||
| let node = build_identifier(&mut ctx); | ||||||
| assert_eq!(ctx.counter, 0); | ||||||
| assert_dump_eq( | ||||||
| &dump_ast(ctx.output, node, input), | ||||||
| r#" | ||||||
| identifier "1" | ||||||
| "#, | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| #[derive(Clone, Default)] | ||||||
| struct BorrowedInterpolationContext { | ||||||
| locations: Vec<yeast::OutputId>, | ||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn test_interpolation_accepts_value_borrowed_from_context() { | ||||||
| let input = "x = 1"; | ||||||
| let input_ast = parse_input(input); | ||||||
| let mut ast = empty_output_ast(input); | ||||||
| let mut user_ctx = BorrowedInterpolationContext::default(); | ||||||
| let mut ctx = yeast::build::BuildCtx::new(&input_ast, &mut ast, &mut user_ctx); | ||||||
| let location = yeast::tree!((identifier "location")); | ||||||
| ctx.locations.push(location); | ||||||
| let locations = ctx.locations.iter(); | ||||||
|
|
||||||
| yeast::tree!((call location: {locations})); | ||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn test_root_choice_returns_an_optional_node() { | ||||||
| let input = "x = 1"; | ||||||
|
|
||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe at a doc comment for this function that also mentions that this is an imperfect check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in e6205e0.