Repository navigation
yeast: Remove eager translation - #23302
Conversation
Translate captured input nodes only when they are structurally consumed. This removes `TranslatedCaptures` and gives rule bodies raw `Id` values consistently.
Explicitly translate captures used as stored output nodes or inspected by Rust control flow. Keep subscript inputs raw until each emitted method consumes them.
All captures now bind raw input IDs, so the separate raw-capture spelling no longer carries any distinction.
There was a problem hiding this comment.
🔵 Needs a closer look
Changes to capture binding and translation timing interact across macros, runtime behavior, and context-sensitive Swift rules, requiring domain review to confirm semantic preservation.
No open issues 🎉
What changed in this PR
This PR simplifies YEAST capture handling by making all @ captures raw input IDs and translating them when consumed. It removes the eager-translation machinery and adapts the Swift extractor to the new behavior.
Changes:
- Remove
@@syntax and eager capture translation from parsing, macro expansion, and runtime support. - Update Swift rules to use interpolation-time translation, with explicit translation where needed.
- Refresh documentation and add tests for context-sensitive translation and repeated interpolation.
| File | Description |
|---|---|
unified/extractor/src/languages/swift/swift.rs |
Migrate Swift rules and helpers to raw captures. |
shared/yeast/tests/test.rs |
Update captures and test interpolation-time translation. |
shared/yeast/src/lib.rs |
Remove eager translation support and update API documentation. |
shared/yeast/src/captures.rs |
Remove translated-capture storage and helpers. |
shared/yeast/src/build.rs |
Add translate_one for translations requiring exactly one result. |
shared/yeast/doc/yeast.md |
Document unified captures and translation behavior. |
shared/yeast-macros/src/rule_parse.rs |
Reject @@ and allow raw root captures with @. |
shared/yeast-macros/src/lower.rs |
Bind all captures as input IDs without eager translation. |
shared/yeast-macros/src/lib.rs |
Update macro documentation and examples. |
shared/yeast-macros/src/ast.rs |
Remove the raw/eager distinction from capture representation. |
Review effort: Balanced (adaptive)
paldepind
left a comment
There was a problem hiding this comment.
Overall this looks nice and great for simplicity.
Are the new tests some that you feel are necessary? Or are they just some that Copilot produced? I'm a bit suspicious whether removing a feature really requires this much additional testing?
| /// Recursively translate `id`, requiring exactly one resulting node. | ||
| pub fn translate_one(&mut self, id: Id) -> OutputId { | ||
| let translated = self.translate(id); | ||
| match translated.as_slice() { |
There was a problem hiding this comment.
This can use single_id from #23292 (once rebased or merged with main).
There was a problem hiding this comment.
I will merge in main and address this in the merge commit.
| parameter: {ctx.reset(); ctx.subscript = Some(sub_ctx); params} | ||
| return_type: {ctx.reset(); return_type} | ||
| body: {ctx.reset(); body} | ||
| body: [(block) if { body.is_none() }])) |
There was a problem hiding this comment.
Why this changes in subscript_method? I think all the ctx.reset()s makes this harder to read and the old approach would still work.
There was a problem hiding this comment.
The diff with the old approach that still works:
diff --git a/unified/extractor/src/languages/swift/swift.rs b/unified/extractor/src/languages/swift/swift.rs
index b153cfd8ed0..7f77792b6f2 100644
--- a/unified/extractor/src/languages/swift/swift.rs
+++ b/unified/extractor/src/languages/swift/swift.rs
@@ -215,56 +215,54 @@ fn subscript_method(
) -> yeast::OutputId {
let is_chained = ctx.is_chained;
let sub_ctx = sub.clone();
- let SubscriptContext {
- keyword: kw,
- modifiers,
- type_params,
- params,
- return_type,
- } = sub;
- let SubscriptAccessor {
- modifiers: accessor_modifiers,
- new_value_param,
- body,
- } = accessor;
- if let Some((param_name, spec)) = new_value_param {
+ let (mods, type_params, params, element_type, body) = ctx.scoped(|ctx| {
+ ctx.reset();
+ let mods = ctx.translate(sub.modifiers);
+ let type_params = ctx.translate(sub.type_params);
+ let element_type = ctx.translate(sub.return_type).into_iter().next();
+ let body = ctx.translate(accessor.body).into_iter().next();
+ ctx.subscript = Some(sub_ctx);
+ let params = ctx.translate(sub.params);
+ (mods, type_params, params, element_type, body)
+ });
+ let body = body.unwrap_or_else(|| tree!((block)));
+ let kw = sub.keyword;
+ if let Some((param_name, spec)) = accessor.new_value_param {
let implicit_param_name = param_name.is_none().then_some("newValue");
tree!((function_declaration
- modifier: {ctx.reset(); modifiers}
- modifier: {ctx.reset(); accessor_modifiers}
- modifier: [(modifier "chained_declaration") if { is_chained }]
- name_node: (identifier #{kw})
- type_parameter: {ctx.reset(); type_params}
- parameter: {ctx.reset(); ctx.subscript = Some(sub_ctx); params}
- // The new value is passed as a named parameter with the reserved
- // external name `#newValue`, so it can't clash with the index
- // parameters (including omitted defaults).
- parameter: [
- (parameter
- external_name_node: (identifier "#newValue" location: {spec})
- pattern: (identifier #{param_name})
- type: {ctx.reset(); return_type}
- location: {param_name})
- (parameter
- modifier: (modifier "generated")
- external_name_node: (identifier "#newValue" location: {spec})
- pattern: (identifier #{implicit_param_name} location: {spec})
- type: {ctx.reset(); return_type}
- location: {spec})
- ]
- body: {ctx.reset(); body}
- body: [(block) if { body.is_none() }]))
+ modifier: {mods}
+ modifier: {accessor.modifiers}
+ modifier: [(modifier "chained_declaration") if { is_chained }]
+ name_node: (identifier #{kw})
+ type_parameter: {type_params}
+ parameter: {params}
+ // The new value is passed as a named parameter with the reserved
+ // external name `#newValue`, so it can't clash with the index
+ // parameters (including omitted defaults).
+ parameter: [
+ (parameter
+ external_name_node: (identifier "#newValue" location: {spec})
+ pattern: (identifier #{param_name})
+ type: {element_type}
+ location: {param_name})
+ (parameter
+ modifier: (modifier "generated")
+ external_name_node: (identifier "#newValue" location: {spec})
+ pattern: (identifier #{implicit_param_name} location: {spec})
+ type: {element_type}
+ location: {spec})
+ ]
+ body: {body}))
} else {
tree!((function_declaration
- modifier: {ctx.reset(); modifiers}
- modifier: {ctx.reset(); accessor_modifiers}
- modifier: [(modifier "chained_declaration") if { is_chained }]
- name_node: (identifier #{kw})
- type_parameter: {ctx.reset(); type_params}
- parameter: {ctx.reset(); ctx.subscript = Some(sub_ctx); params}
- return_type: {ctx.reset(); return_type}
- body: {ctx.reset(); body}
- body: [(block) if { body.is_none() }]))
+ modifier: {mods}
+ modifier: {accessor.modifiers}
+ modifier: [(modifier "chained_declaration") if { is_chained }]
+ name_node: (identifier #{kw})
+ type_parameter: {type_params}
+ parameter: {params}
+ return_type: {element_type}
+ body: {body}))
}
}There was a problem hiding this comment.
I wanted to get rid of all of the explicit ctx.translate calls, since they imply an ordering restriction that isn't actually there. I do agree, though, that separate ctx.reset() calls can be a bit overwhelming.
I think I've found a third option that satisfies everyone, however: translate modifiers first, then reset the context and do the rest. That is, we're essentially pulling all of the individual ctx.reset() calls out before the entire if-statement.
There was a problem hiding this comment.
I wanted to get rid of all of the explicit ctx.translate calls, since they imply an ordering restriction that isn't actually there.
Can you expand on this? I'm not following.
There was a problem hiding this comment.
What I meant is: when I see ctx.translate, my immediate thought is that we need to translate something now -- we can't wait for it to be auto-translated inside of the tree! macro. In this case, by translating all of the bits explicitly, we're fixing a specific but arbitrary order.
Though, on second thought this is not quite the issue it used to be, since the context changes are now isolated from each other. So, in principle if you explicitly translate foo before bar, as long as ctx hasn't been modified on the top level, it should be equally valid to do bar before foo. If we do the translations automatically inside of the tree! template, then this is less of a concern -- the translations of course still happen in a specific order, but this is an internal implementation detail that we shouldn't rely on.
I think more generally I just see every ctx.translate as a kind of code smell. There are a few cases where it's warranted, but in general the template should just do the right thing (if set up correctly).
There was a problem hiding this comment.
I've added the version I described above in b403920.
There was a problem hiding this comment.
I think the new version in b403920 is much better. I'm not sure which one I prefer though, the ctx.scoped signposted the ctx.reset() better.
I obviously agree that ctx.translate shouldn't be used when just passing on the ids will work, but when that's not the case then I don't see ctx.translate as much of a problem for the same reason that you state: The order can't matter except when the context is mutated.
But the new version is completely fine with me overall.
| } | ||
|
|
||
| #[test] | ||
| fn test_capture_is_translated_at_each_interpolation() { |
There was a problem hiding this comment.
Didn't this already work?
There was a problem hiding this comment.
You're right, it did. This test is not terribly useful -- I think I'll just get rid of it (or fold it into another test).
|
|
||
| #[test] | ||
| fn rejects_eager_capture_of_rule_root() { | ||
| fn rejects_double_at_capture_marker() { |
There was a problem hiding this comment.
Do we need to test a dead and gone syntax?
There was a problem hiding this comment.
Nope! I wasn't quite as vigilant with Copilot's added tests as I thought. (I actually removed this previously, but I think it must have snuck in at some point.)
Removes some superfluous tests, as the existing adapted tests already test capture interpolation adequately. Also cleans up the translation in `subscript_method`, to use an appropriately placed `ctx.reset()` call _before_ we build the trees. This saves adding it in every branch.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 8e0afb9d-f9c8-4556-a333-8bd0ac9c23ef
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 8e0afb9d-f9c8-4556-a333-8bd0ac9c23ef
paldepind
left a comment
There was a problem hiding this comment.
Thanks for addressing my comments. I like to see the tests go :)
I have one nitpick, but otherwise this LGTM.
| body: [ | ||
| {ctx.translate_one(body.unwrap())} if { body.is_some() } | ||
| (block) | ||
| ])) |
There was a problem hiding this comment.
Wouldn't this be the same? It's a bit shorter and avoids unwrap. The unwrap is obviously ok here, but it still smells to me.
| body: [ | |
| {ctx.translate_one(body.unwrap())} if { body.is_some() } | |
| (block) | |
| ])) | |
| body: {body.unwrap_or_else(|| tree!((block)))} |
There was a problem hiding this comment.
This particular bit of code is my nemesis. Unfortunately, your suggestion won't work, since body is an Id, but tree!((block)) is an OutputId, so the type check fails.
I think it would make sense to allow alternatives to return multiple values. That would enable us to write something like the following.
body: [
{body} if {body.is_some()}
(block)
]One might even consider treating the case where body is None as a kind of failure, leading to the next arm ((block)) being tried, but I'm not sure these are the semantics we really want.
I think this should be done as a follow-up, however, if we decide to do it.
There was a problem hiding this comment.
Oh! Unfortunate, but that makes sense.
| return_type: {return_type} | ||
| body: [ | ||
| {ctx.translate_one(body.unwrap())} if { body.is_some() } | ||
| (block) |
asgerf
left a comment
There was a problem hiding this comment.
I've only reviewed the PR description, documentation, and swift.rs changes, but I just want to add that from a user's perspective I'm very happy with this change 👍
This was the merge conflict.
With the recent additions of automatic translation of
Ids intoOutputIds inside oftree!templates, and the fact that context mutations can no longer leak between interpolations, we now have much less need for the distinction between raw and eager captures.That is, where previously you would write
relying on
bargetting eagerly translated (and rebinding it tobar) before the rule body runs (so that it would have the correct type for the interpolation), we can now just write it asand rely on the fact that if we try to interpolate an
Id, it gets automatically translated into anOutputIdat the point where that interpolation takes place. Thus, the above code is implicitly the same asOf course, in that case, we don't need the
@@syntax anymore, so we can use@to represent raw captures, always. This makes the internal implementation ofyeastsimpler, while also getting rid of a bunch ofctx.translatecalls in the Swift rules.One thing that's no longer possible to write is
tree!({bar}). Previouslybarwould have been eagerly translated as an@-capture, and so it would beOutputIdalready. With the present code, this doesn't work, sinceIds won't get automatically translated in this case. However, this is a weird use oftree!to begin with (previously it was essentially a no-op), and it's something we can fix up in a separate PR. Since it didn't affect any of our current code, I did not feel the need to fix this up here.Can be reviewed commit-by-commit. Most of the tedious
@@to@rewriting is in its own commit, to make it easier to see the bits that actually changed.