diff --git a/NEWS.md b/NEWS.md index d38da1cfe1300..6ffdcd4f8b0d1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -15,6 +15,7 @@ Language changes is considered a bug fix ([#47102]) - The `hash` algorithm and its values have changed. Most `hash` specializations will remain correct and require no action. Types that reimplement the core hashing logic independently, such as some third-party string packages do, may require a migration to the new algorithm. ([#57509]) + - `using Foo: _` for a module or package `Foo` no longer issues a warning. This syntax can be used to load `Foo` without bringing any of its names (even the name `Foo` itself) into your namespace. Compiler/Runtime improvements ----------------------------- diff --git a/src/module.c b/src/module.c index 7c83d9329ab50..284041f4c3fec 100644 --- a/src/module.c +++ b/src/module.c @@ -1232,10 +1232,15 @@ JL_DLLEXPORT void jl_module_import(jl_task_t *ct, jl_module_t *to, jl_module_t * jl_walk_binding_inplace(&ownerb, &ownerbpart, ct->world_age); if (jl_bkind_is_some_guard(jl_binding_kind(ownerbpart))) { - jl_printf(JL_STDERR, - "WARNING: Imported binding %s.%s was undeclared at import time during import to %s.\n", - jl_symbol_name(from->name), jl_symbol_name(s), - jl_symbol_name(to->name)); + if (strcmp(jl_symbol_name(s), "_") == 0) { + // importing `_` loads the package but does not import a binding + return; + } else { + jl_printf(JL_STDERR, + "WARNING: Imported binding %s.%s was undeclared at import time during import to %s.\n", + jl_symbol_name(from->name), jl_symbol_name(s), + jl_symbol_name(to->name)); + } } jl_binding_t *bto = jl_get_module_binding(to, asname, 1); diff --git a/test/syntax.jl b/test/syntax.jl index 92d8391345312..682c05b2f2bda 100644 --- a/test/syntax.jl +++ b/test/syntax.jl @@ -2752,6 +2752,19 @@ import .TestImportAs.Mod2 as M2 @test !@isdefined(Mod2) @test M2 === TestImportAs.Mod2 +# `using Foo: _` is special cased to not warn so it can be used to load +# a module only for its side effects. (#58667) +side_effect_triggered = Ref(false) +module Mod58667 +__init__() = (parentmodule(@__MODULE__).side_effect_triggered[] = true; nothing) +end +@test_nowarn using .Mod58667: _ +@test side_effect_triggered[] # module was loaded! +# does not warn if you import again: +@test_nowarn using .Mod58667: _ +# but importing a non-existent non-`_` binding does warn: +@test_warn "WARNING: Imported binding Mod58667.y was undeclared at import time during import to Main." using .Mod58667: y + # 57702: nearby bindings shouldn't cause us to closure-convert in import/using module OddImports using Test