Skip to content

Commit

Permalink
Fix accidental early evaluation of imported using binding (#54956)
Browse files Browse the repository at this point in the history
In `using A.B`, we need to evaluate `A.B` to add the module to the using
list. However, in `using A: B`, we do not care about the value of `A.B`,
we only operate at the binding level. These two operations share a code
path and the evaluation of `A.B` happens early and is unused on the
`using A: B` path. I believe this was an unintentional oversight when
the latter syntax was added. Fixes #54954.

(cherry picked from commit 89e391b)
  • Loading branch information
Keno authored and KristofferC committed Jul 23, 2024
1 parent 73353dc commit 54bd4b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/toplevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -757,14 +757,14 @@ jl_value_t *jl_toplevel_eval_flex(jl_module_t *JL_NONNULL m, jl_value_t *e, int
if (jl_is_expr(a) && ((jl_expr_t*)a)->head == jl_dot_sym) {
name = NULL;
jl_module_t *import = eval_import_path(m, from, ((jl_expr_t*)a)->args, &name, "using");
jl_module_t *u = import;
if (name != NULL)
u = (jl_module_t*)jl_eval_global_var(import, name);
if (from) {
// `using A: B` and `using A: B.c` syntax
jl_module_use(m, import, name);
}
else {
jl_module_t *u = import;
if (name != NULL)
u = (jl_module_t*)jl_eval_global_var(import, name);
if (!jl_is_module(u))
jl_eval_errorf(m, "invalid using path: \"%s\" does not name a module",
jl_symbol_name(name));
Expand Down
7 changes: 7 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3690,3 +3690,10 @@ begin
Foreign54607.bar = 9
end
@test Foreign54607.bar == 9

# Test that globals can be `using`'d even if they are not yet defined
module UndefGlobal54954
global theglobal54954::Int
end
using .UndefGlobal54954: theglobal54954
@test Core.get_binding_type(@__MODULE__, :theglobal54954) === Int

0 comments on commit 54bd4b9

Please sign in to comment.