Skip to content

Conversation

@mlechu
Copy link
Collaborator

@mlechu mlechu commented Aug 12, 2025

This change allows us to activate JuliaLowering as the core lowering implementation via @activate JuliaLowering (InteractiveUtils) or JuliaLowering.activate!().

This is a simplified version of the hook originally in #10. The previous version attempted to swap out the parser hook for one that produced SyntaxTree, but that was broken in many cases, since parser output is expected to be Expr by consumers other than lowering. This change just uses the Expr->SyntaxTree conversion before lowering, which doesn't solve our provenance problems, but is helpful for testing both the lowering implementation and the tree conversion itself.

In the future, we can update Core._parse to take an argument specifying the output type. This needs to happen after JuliaSyntax changes, and both JuliaLowering and base julia updating to use them, which will take some time.

Copy link
Owner

@c42f c42f left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

src/hooks.jl Outdated
# e.g. LineNumberNode, integer...
return Core.svec(code)
end
st0 = code isa Expr ? expr_to_syntaxtree(code) : code
Copy link
Owner

Choose a reason for hiding this comment

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

Very cool that we can do this now, with expr_to_syntaxtree(). Do we get a benefit from the following?

Suggested change
st0 = code isa Expr ? expr_to_syntaxtree(code) : code
st0 = code isa Expr ? expr_to_syntaxtree(code, LineNumberNode(line, file)) : code

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I forgot to pass that in. That will be the source attribute for the root and all nodes until we traverse a linenode.

ctx4, st4 = convert_closures(ctx3, st3)
ctx5, st5 = linearize_ir( ctx4, st4)
ex = to_lowered_expr(mod, st5)
return Core.svec(ex, st5, ctx5)
Copy link
Owner

Choose a reason for hiding this comment

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

Ok, so I see that in JuliaLang/julia#58207 you allowed for the trailing elements of this to be anything and we'll specify it later (seems reasonable). Are st5 and ctx5 used by JETLS somehow? (I see they're not used by the C code as expected ... who knows what the ctx5 API even is 😅 )

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not used by JETLS yet, and even if it was, you're free to "break" this non-API. JETLS does use ctx objects outside of Core._lower, though, I think mostly for the binding table.

I see they're not used by the C code as expected

The C code unfortunately has no idea what a SyntaxTree is yet. I ran into this when trying to make Core._parse produce a syntax tree---toplevel.c had no way to positively check the thing it parsed was a SyntaxTree, and I had to smuggle it through by wrapping it in Expr in the parse hook, which is pretty goofy.

@c42f
Copy link
Owner

c42f commented Aug 14, 2025

Oh, this also needs tests:

  • A few to cover the important branches in the hook function (doesn't need to be integration tests)
  • An integration test which calls activate!() and checks that eval(Meta.lower()) works ... or something? (we already overload Core.eval(::SyntaxTree) so can't check that entry point)

Base automatically changed from ec/expr-to-st to main August 15, 2025 23:28
Co-authored-by: Claire Foster <[email protected]>
Copy link
Owner

@c42f c42f left a comment

Choose a reason for hiding this comment

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

Looks really nice now, please merge when ready :)

@mlechu mlechu merged commit 7badaaa into main Aug 16, 2025
1 check passed
@mlechu mlechu deleted the ec/core-hook branch August 16, 2025 03:44
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