diff --git a/base/client.jl b/base/client.jl index 3045c229a5d35..3aebfaf6de696 100644 --- a/base/client.jl +++ b/base/client.jl @@ -532,10 +532,6 @@ The thrown errors are collected in a stack of exceptions. """ global err = nothing -# For numbered REPL prompt -global Out::Any -export Out - # weakly exposes ans and err variables to Main export ans, err end diff --git a/src/method.c b/src/method.c index 6b92680c676fe..d4457b1549353 100644 --- a/src/method.c +++ b/src/method.c @@ -215,28 +215,6 @@ static jl_value_t *resolve_globals(jl_value_t *expr, jl_module_t *module, jl_sve if (e->head == jl_method_sym || e->head == jl_module_sym || e->head == jl_throw_undef_if_not_sym) { i++; } - if (e->head == jl_assign_sym && binding_effects) { - jl_value_t *lhs = jl_exprarg(e, 0); - if (jl_is_globalref(lhs) || jl_is_symbol(lhs)) { - jl_module_t *mod = jl_is_globalref(lhs) ? jl_globalref_mod(lhs) : module; - jl_sym_t *name = jl_is_globalref(lhs) ? jl_globalref_name(lhs) : (jl_sym_t*)lhs; - if (mod == module) { - // Assignment does not create bindings in foreign modules (#54678) - jl_binding_t *b = jl_get_module_binding(mod, name, 1); - jl_binding_partition_t *bpart = jl_get_binding_partition(b, jl_current_task->world_age); - jl_ptr_kind_union_t pku = jl_atomic_load_relaxed(&bpart->restriction); - while (1) { - if (!jl_bkind_is_some_guard(decode_restriction_kind(pku))) - break; - jl_ptr_kind_union_t new_pku = encode_restriction((jl_value_t*)jl_any_type, BINDING_KIND_GLOBAL); - if (jl_atomic_cmpswap(&bpart->restriction, &pku, new_pku)) { - jl_gc_wb(bpart, jl_any_type); - break; - } - } - } - } - } for (; i < nargs; i++) { // TODO: this should be making a copy, not mutating the source jl_exprargset(e, i, resolve_globals(jl_exprarg(e, i), module, sparam_vals, binding_effects, eager_resolve)); diff --git a/src/module.c b/src/module.c index 77d51cc882977..9b20b23a17e3f 100644 --- a/src/module.c +++ b/src/module.c @@ -222,7 +222,7 @@ static jl_binding_t *new_binding(jl_module_t *mod, jl_sym_t *name) extern jl_mutex_t jl_modules_mutex; -static void check_safe_newbinding(jl_module_t *m, jl_sym_t *var) +extern void check_safe_newbinding(jl_module_t *m, jl_sym_t *var) { if (jl_current_task->ptls->in_pure_callback) jl_errorf("new globals cannot be created in a generated function"); @@ -475,7 +475,7 @@ static jl_binding_t *jl_resolve_owner(jl_binding_t *b/*optional*/, jl_module_t * if (decode_restriction_kind(pku) == BINDING_KIND_FAILED) return NULL; if (decode_restriction_kind(pku) == BINDING_KIND_DECLARED) { - return NULL; + return b; } if (decode_restriction_kind(pku) == BINDING_KIND_GUARD) { jl_binding_t *b2 = NULL; @@ -1126,7 +1126,7 @@ void append_module_names(jl_array_t* a, jl_module_t *m, int all, int imported, i if (((b->publicp) || (imported && (kind == BINDING_KIND_CONST_IMPORT || kind == BINDING_KIND_IMPORTED)) || (usings && kind == BINDING_KIND_EXPLICIT) || - ((kind == BINDING_KIND_GLOBAL || kind == BINDING_KIND_CONST) && (all || main_public))) && + ((kind == BINDING_KIND_GLOBAL || kind == BINDING_KIND_CONST || kind == BINDING_KIND_DECLARED) && (all || main_public))) && (all || (!b->deprecated && !hidden))) _append_symbol_to_bindings_array(a, asname); } diff --git a/src/toplevel.c b/src/toplevel.c index f02277b2bec21..9f434aaed917d 100644 --- a/src/toplevel.c +++ b/src/toplevel.c @@ -324,6 +324,7 @@ void jl_binding_set_type(jl_binding_t *b, jl_value_t *ty) jl_gc_wb(bpart, ty); } +extern void check_safe_newbinding(jl_module_t *m, jl_sym_t *var); void jl_declare_global(jl_module_t *m, jl_value_t *arg, jl_value_t *set_type) { // create uninitialized mutable binding for "global x" decl sometimes or probably jl_module_t *gm; @@ -342,6 +343,7 @@ void jl_declare_global(jl_module_t *m, jl_value_t *arg, jl_value_t *set_type) { jl_binding_partition_t *bpart = jl_get_binding_partition(b, jl_current_task->world_age); jl_ptr_kind_union_t pku = jl_atomic_load_relaxed(&bpart->restriction); while (decode_restriction_kind(pku) == BINDING_KIND_GUARD || decode_restriction_kind(pku) == BINDING_KIND_FAILED) { + check_safe_newbinding(gm, gs); if (jl_atomic_cmpswap(&bpart->restriction, &pku, encode_restriction(NULL, BINDING_KIND_DECLARED))) break; } diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index f0aacea80879a..a2eb7be828f4f 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -1820,6 +1820,8 @@ function capture_result(n::Ref{Int}, @nospecialize(x)) n = n[] mod = Base.MainInclude if !isdefined(mod, :Out) + @eval mod global Out + @eval mod export Out setglobal!(mod, :Out, Dict{Int, Any}()) end if x !== getglobal(mod, :Out) && x !== nothing # remove this? diff --git a/stdlib/REPL/test/repl.jl b/stdlib/REPL/test/repl.jl index e9e7964c63bbb..f4d594b2a02e1 100644 --- a/stdlib/REPL/test/repl.jl +++ b/stdlib/REPL/test/repl.jl @@ -1210,7 +1210,7 @@ end # Issue 39427 @test occursin("does not exist.", sprint(show, help_result(":="))) -global some_undef_global::Any +global some_undef_global @test occursin("exists,", sprint(show, help_result("some_undef_global", @__MODULE__))) # Issue #40563 diff --git a/test/compiler/effects.jl b/test/compiler/effects.jl index 24cca937ff21e..a27d52d68b9a9 100644 --- a/test/compiler/effects.jl +++ b/test/compiler/effects.jl @@ -376,18 +376,23 @@ end # as the cached effects can be easily wrong otherwise # since the inference currently doesn't track "world-age" of global variables @eval global_assignment_undefinedyet() = $(GlobalRef(@__MODULE__, :UNDEFINEDYET)) = 42 -setglobal!_nothrow_undefinedyet() = setglobal!(@__MODULE__, :UNDEFINEDYET2, 42) +setglobal!_nothrow_undefinedyet() = setglobal!(@__MODULE__, :UNDEFINEDYET, 42) let effects = Base.infer_effects() do global_assignment_undefinedyet() end - @test Core.Compiler.is_nothrow(effects) + @test !Core.Compiler.is_nothrow(effects) end let effects = Base.infer_effects() do setglobal!_nothrow_undefinedyet() end @test !Core.Compiler.is_nothrow(effects) end -global UNDEFINEDYET2::String = "0" +global UNDEFINEDYET::String = "0" +let effects = Base.infer_effects() do + global_assignment_undefinedyet() + end + @test !Core.Compiler.is_nothrow(effects) +end let effects = Base.infer_effects() do setglobal!_nothrow_undefinedyet() end diff --git a/test/misc.jl b/test/misc.jl index 11eb03f384105..87605d685fb3e 100644 --- a/test/misc.jl +++ b/test/misc.jl @@ -286,7 +286,7 @@ end # timing macros # test that they don't introduce global vars -global v11801::Any, t11801::Any, names_before_timing::Any +global v11801, t11801, names_before_timing names_before_timing = names(@__MODULE__, all = true) let t = @elapsed 1+1 @@ -318,9 +318,7 @@ v11801, t11801 = @timed sin(1) @test v11801 == sin(1) @test isa(t11801,Real) && t11801 >= 0 -let diff = setdiff(names(@__MODULE__, all = true), names_before_timing) -@test isempty(diff) -end +@test names(@__MODULE__, all = true) == names_before_timing redirect_stdout(devnull) do # suppress time prints diff --git a/test/staged.jl b/test/staged.jl index 8977a807f0fad..aec4a3bf135d3 100644 --- a/test/staged.jl +++ b/test/staged.jl @@ -311,8 +311,8 @@ end @generated function f33243() :(global x33243 = 2) end -@test_skip(@test_throws ErrorException f33243()) -global x33243::Any +@test_throws ErrorException f33243() +global x33243 @test f33243() === 2 @test x33243 === 2 diff --git a/test/syntax.jl b/test/syntax.jl index 89a95c2d77895..595f56903799f 100644 --- a/test/syntax.jl +++ b/test/syntax.jl @@ -2595,7 +2595,7 @@ using Test module Mod const x = 1 -global maybe_undef::Any, always_undef::Any +global maybe_undef, always_undef export always_undef def() = (global maybe_undef = 0) func(x) = 2x + 1 @@ -3719,7 +3719,7 @@ module Foreign54607 end @test_throws ErrorException (Foreign54607.foo = 1) @test_throws ErrorException Foreign54607.try_to_create_binding1() -Foreign54607.try_to_create_binding2() +@test_throws ErrorException Foreign54607.try_to_create_binding2() function assign_in_foreign_module() (Foreign54607.foo = 1) nothing