Skip to content

yeast: Remove eager translation - #23302

Merged
tausbn merged 9 commits into
mainfrom
tausbn/remove-eager-translation
Dec 2, 2026
Merged

tausbn merged 9 commits into
mainfrom
tausbn/remove-eager-translation

Conversation

@tausbn

@tausbn tausbn commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

With the recent additions of automatic translation of Ids into OutputIds inside of tree! 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

(input in_field: @bar)
=>
(output out_field: {bar})

relying on bar getting eagerly translated (and rebinding it to bar) before the rule body runs (so that it would have the correct type for the interpolation), we can now just write it as

(input in_field: @@bar)
=>
(output out_field: {bar})

and rely on the fact that if we try to interpolate an Id, it gets automatically translated into an OutputId at the point where that interpolation takes place. Thus, the above code is implicitly the same as

(input in_field: @@bar)
=>
(output out_field: {let bar = ctx.translate(bar); bar})

Of 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 of yeast simpler, while also getting rid of a bunch of ctx.translate calls in the Swift rules.

One thing that's no longer possible to write is tree!({bar}). Previously bar would have been eagerly translated as an @-capture, and so it would be OutputId already. With the present code, this doesn't work, since Ids won't get automatically translated in this case. However, this is a weird use of tree! 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.

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.
@tausbn
tausbn marked this pull request as ready for review November 30, 2026 16:08
@tausbn
tausbn requested a review from a team as a code owner November 30, 2026 16:08
Copilot AI adaptive review requested due to automatic review settings November 30, 2026 16:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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 paldepind left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread shared/yeast/src/build.rs Outdated
/// 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() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can use single_id from #23292 (once rebased or merged with main).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() }]))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}))
     }
 }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the version I described above in b403920.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread shared/yeast/tests/test.rs Outdated
}

#[test]
fn test_capture_is_translated_at_each_interpolation() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't this already work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in b403920.

Comment thread shared/yeast-macros/src/rule_parse.rs Outdated

#[test]
fn rejects_eager_capture_of_rule_root() {
fn rejects_double_at_capture_marker() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to test a dead and gone syntax?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in b403920.

tausbn and others added 3 commits December 1, 2026 16:00
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
@tausbn
tausbn requested a review from paldepind December 1, 2026 16:16
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 8e0afb9d-f9c8-4556-a333-8bd0ac9c23ef

@paldepind paldepind left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing my comments. I like to see the tests go :)

I have one nitpick, but otherwise this LGTM.

Comment on lines +282 to +285
body: [
{ctx.translate_one(body.unwrap())} if { body.is_some() }
(block)
]))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
body: [
{ctx.translate_one(body.unwrap())} if { body.is_some() }
(block)
]))
body: {body.unwrap_or_else(|| tree!((block)))}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! Unfortunate, but that makes sense.

return_type: {return_type}
body: [
{ctx.translate_one(body.unwrap())} if { body.is_some() }
(block)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

@asgerf asgerf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 👍

paldepind
paldepind previously approved these changes Dec 2, 2026
@tausbn
tausbn merged commit 92a6466 into main Dec 2, 2026
23 checks passed
@tausbn
tausbn deleted the tausbn/remove-eager-translation branch December 2, 2026 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants