Add builtin functions _module_import, _module_using #57965
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
Currently,
import
andusing
statements are compiled into calls tojl_toplevel_eval
with the Expr as an argument. It can be useful in aninteractive environment to do import/using programmatically, for which
eval(Expr(:import, ...))
or a macro is the only option at the moment (see forexample
InteractiveUtils.@activate
).This PR adds two builtins,
_module_import
and_module_using
, with thesesignatures, and removes
Expr(:import/:using, ...)
from the lowered IR:Lowering example
import
statements andusing X:
statements are lowered to_module_import
like so:
Plain
using
statements become_module_using
:Multiple comma-separated
using
orimport
paths are lowered to multiple calls to the appropriate builtin:Alternative designs
Some alternatives to these builtins are worth considering.
The lowering pass could do even less work by inserting a call to
_module_import
or_module_using
that takes a single Expr argument for theentire statement.
Another option is to do even more in lowering, introducing very low-level
builtins that wrap
jl_module_import
andjl_module_using
directly. Thelowering pass would expand the import path into direct calls to
Base.require
,Base.getproperty
, etc. For example,import A.B: C.d, e
could becomesomething like:
I like the idea of supporting calls to
_module_import
with a module forctx
instead of an Expr, but this could be added to the proposed design too.