julia
59e9ada5 - Add builtin functions Core._import, Core._using; implement import/using logic in Julia (#57965)

Commit
245 days ago
Add builtin functions Core._import, Core._using; implement import/using logic in Julia (#57965) Currently, `import` and `using` statements are compiled into calls to `jl_toplevel_eval` with the Expr as an argument. It can be useful in an interactive environment to do import/using programmatically, for which `eval(Expr(:import, ...))` or a macro is the only option at the moment (see for example `InteractiveUtils.@activate`). This PR adds two functions, `_module_import ` and `_module_using`, with these signatures, and removes `Expr(:import/:using, ...)` from the lowered IR: ``` _module_import(explicit::Bool, to::Module, ctx::Union{Expr, Nothing}, names::Expr...) _module_using(to::Module, from::Expr{Symbol}) ``` ## Lowering example `import` statements and `using X:` statements are lowered to `_module_import` like so: ``` import A => _module_import(true, Main, nothing, Expr(:., :A)) import A.b => _module_import(true, Main, nothing, Expr(:., :A, :b)) import A.b as c => _module_import(true, Main, nothing, Expr(:as, Expr(:., :A, :b), :c)) import A.B: C.d, e => _module_import(true, Main, Expr(:., :A, :B), Expr(:., :C, :d), Expr(:., :e)) import A.B: C.d as e => _module_import(true, Main, Expr(:., :A, :B), Expr(:as, Expr(:., :C, :d), :e)) using A.B: C.d, e => _module_import(false, Main, Expr(:., :A, :B), Expr(:., :C, :d), Expr(:., :e)) ``` Plain `using` statements become `_module_using`: ``` using A.B => _module_using(Main, Expr(:., :A, :B)) ``` Multiple comma-separated `using` or `import` paths are lowered to multiple calls to the appropriate builtin: ``` julia> Meta.@lower using A.B, C :($(Expr(:thunk, CodeInfo( 1 ─ builtin Core._module_using(Main, $(QuoteNode(:($(Expr(:., :A, :B)))))) │ builtin Core._module_using(Main, $(QuoteNode(:($(Expr(:., :C)))))) │ $(Expr(:latestworld)) └── return nothing )))) ```
Author
Parents
Loading