Skip to content

Conversation

@c42f
Copy link
Owner

@c42f c42f commented Aug 28, 2025

Ensure that interpolating values into quoted expressions works in Expr compat mode.

  • To produce Expr in CodeInfo conversion, add an as_Expr meta attribute to K"inert" generated by expand_quote. I left this as a meta attribute because it doesn't need to be strongly typed.
  • In analogy with eval(), add expr_compat_mode keyword to include_string. We may want to rethink this when we rethink how options to eval() work.
  • Adapt the runtime function interpolate_ast to work with both Expr and SyntaxTree as the source expression.
  • Allow Symbol to be interpolated into SyntaxTree as syntax rather than a K"Value": Even in "new style macro mode", :xis interpreted as aSymbol` rather than an AST as it's used by many packages as a kind of lightweight untyped enum (ie, a usage which is almost entirely unrelated to AST manipulation)
  • Fix SyntaxTree->Expr conversion so that embedded Expr K"Value" means a leaf, not an embedded tree.

Follows up on #44, in particular, resolves the comment at #44 (comment)_

In terms of the implementation, I tried the alternative of keeping SyntaxTree in the CodeInfo rather than Expr and using that as the reference expression in interpolate_ast - I had hoped this might be cleaner and less effort. However, this approach ends up needing a "partial" non-recursive implementation of Expr->SyntaxTree conversion (in particular, one where child nodes have already been converted) and that would have required more changes on the JuliaSyntax side than I wanted. However, we might need that kind of non-recursive machinery in the future. For example, for customizing SyntaxTree->Expr conversion - if we do, we can reconsider the implementation of interpolate_ast at that point.

Ensure that interpolating values into quoted expressions works in Expr
compat mode.

* To produce `Expr` in `CodeInfo` conversion, add an `as_Expr` attribute
  to `K"inert"`. expand_quote
* In analogy with `eval()`, add `expr_compat_mode` keyword to
  `include_string`. We may want to rethink this when we rethink how
  options to `eval()` work.
* Adapt the runtime function `interpolate_ast` to work with both `Expr`
  and `SyntaxTree` as the source expression.
* Allow `Symbol` to be interpolated into `SyntaxTree` as syntax rather
  than a `K"Value": Even in "new style macro mode", `:x` is interpreted
  as a `Symbol` rather than an AST as it's used by many packages as a
  kind of lightweight untyped enum (ie, a usage which is almost entirely
  unrelated to AST manipulation)
* Fix `SyntaxTree->Expr` conversion so that embedded Expr `K"Value"`
  means a leaf, not an embedded tree.
@c42f c42f requested a review from mlechu August 28, 2025 05:58
Copy link
Collaborator

@mlechu mlechu left a comment

Choose a reason for hiding this comment

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

Thanks for implementing this; this approach makes sense. I also ran it through JETLS tests and stdlib compilation to be safe, where I didn't see any compatibility regressions.

@c42f
Copy link
Owner Author

c42f commented Aug 30, 2025

Awesome, thanks for the extra testing :)

@c42f c42f merged commit f6227bd into main Aug 30, 2025
2 checks passed
@c42f c42f deleted the caf/expr-interpolation branch August 30, 2025 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants