Commit 8eeb79b
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
1 file changed
+1
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| |||
0 commit comments