Skip to content

Commit 8eeb79b

Browse files
authored
Add builtin functions Core._import, Core._using; implement import/using logic in Julia (JuliaLang#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 )))) ```
1 parent 9ef02e4 commit 8eeb79b

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/Compiler.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ using Base: @_foldable_meta, @_gc_preserve_begin, @_gc_preserve_end, @nospeciali
6565
structdiff, tls_world_age, unconstrain_vararg_length, unionlen, uniontype_layout,
6666
uniontypes, unsafe_convert, unwrap_unionall, unwrapva, vect, widen_diagonal,
6767
_uncompressed_ir, maybe_add_binding_backedge!, datatype_min_ninitialized,
68-
partialstruct_init_undefs, fieldcount_noerror
68+
partialstruct_init_undefs, fieldcount_noerror, _eval_import, _eval_using
6969
using Base.Order
7070

7171
import Base: ==, _topmod, append!, convert, copy, copy!, findall, first, get, get!,

0 commit comments

Comments
 (0)