From 35616c6eabc661a8d5be257a040a8effeb9e01f1 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Sun, 29 Sep 2024 11:50:38 -0700 Subject: [PATCH 1/3] make UndefVarError aware of imported modules --- base/experimental.jl | 2 +- stdlib/REPL/src/REPL.jl | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/base/experimental.jl b/base/experimental.jl index 6e757e9fa0e5f..555d5e03d667a 100644 --- a/base/experimental.jl +++ b/base/experimental.jl @@ -321,7 +321,7 @@ function show_error_hints(io, ex, args...) @invokelatest handler(io, ex, args...) catch err tn = typeof(handler).name - @error "Hint-handler $handler for $(typeof(ex)) in $(tn.module) caused an error" + @error "Hint-handler $handler for $(typeof(ex)) in $(tn.module) caused an error" err end end end diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index 44fe0446240c6..8482e39538b64 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -74,7 +74,18 @@ end function _UndefVarError_warnfor(io::IO, m::Module, var::Symbol) Base.isbindingresolved(m, var) || return false (Base.isexported(m, var) || Base.ispublic(m, var)) || return false - print(io, "\nHint: a global variable of this name also exists in $m.") + active_mod = Base.active_module() + mod_imported = in(Symbol(m), names(active_mod, imported=true)) + if !mod_imported && Symbol(m) == var + print(io, "\nHint: $m is loaded but not imported in the active module `$(active_mod)`.") + else + print(io, "\nHint: a global variable of this name also exists in $m") + if mod_imported + print(io, ".") + else + print(io, ", which is loaded but not imported in the active module `$(active_mod)`.") + end + end return true end From d25c7f1b394e83b0efa9ec9090b153ed6856f0d5 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Sun, 29 Sep 2024 15:33:58 -0700 Subject: [PATCH 2/3] use isdefined --- stdlib/REPL/src/REPL.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index 8482e39538b64..a2496bbb0d03d 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -75,7 +75,7 @@ function _UndefVarError_warnfor(io::IO, m::Module, var::Symbol) Base.isbindingresolved(m, var) || return false (Base.isexported(m, var) || Base.ispublic(m, var)) || return false active_mod = Base.active_module() - mod_imported = in(Symbol(m), names(active_mod, imported=true)) + mod_imported = isdefined(active_mod, Symbol(m)) if !mod_imported && Symbol(m) == var print(io, "\nHint: $m is loaded but not imported in the active module `$(active_mod)`.") else From 0984e42b7daad9a63809e8b7e7879583a93ef11e Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Tue, 1 Oct 2024 11:49:15 -0400 Subject: [PATCH 3/3] suggestions Co-Authored-By: Jameson Nash --- base/experimental.jl | 4 ++-- stdlib/REPL/src/REPL.jl | 13 ++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/base/experimental.jl b/base/experimental.jl index 555d5e03d667a..648b5da0ed9a1 100644 --- a/base/experimental.jl +++ b/base/experimental.jl @@ -319,9 +319,9 @@ function show_error_hints(io, ex, args...) for handler in hinters try @invokelatest handler(io, ex, args...) - catch err + catch tn = typeof(handler).name - @error "Hint-handler $handler for $(typeof(ex)) in $(tn.module) caused an error" err + @error "Hint-handler $handler for $(typeof(ex)) in $(tn.module) caused an error" exception=current_exceptions() end end end diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index a2496bbb0d03d..272b907165341 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -75,15 +75,14 @@ function _UndefVarError_warnfor(io::IO, m::Module, var::Symbol) Base.isbindingresolved(m, var) || return false (Base.isexported(m, var) || Base.ispublic(m, var)) || return false active_mod = Base.active_module() - mod_imported = isdefined(active_mod, Symbol(m)) - if !mod_imported && Symbol(m) == var - print(io, "\nHint: $m is loaded but not imported in the active module `$(active_mod)`.") + print(io, "\nHint: ") + if isdefined(active_mod, Symbol(m)) + print(io, "a global variable of this name also exists in $m.") else - print(io, "\nHint: a global variable of this name also exists in $m") - if mod_imported - print(io, ".") + if Symbol(m) == var + print(io, "$m is loaded but not imported in the active module $active_mod.") else - print(io, ", which is loaded but not imported in the active module `$(active_mod)`.") + print(io, "a global variable of this name may be made accessible by importing $m in the current active module $active_mod") end end return true