Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide better error hint when UndefVarError results from name clashes #53469

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ function UndefVarError_hint(io::IO, ex::UndefVarError)
if C_NULL == owner
# No global of this name exists in this module.
# This is the common case, so do not print that information.
print(io, "\nSuggestion: check for spelling errors or missing imports.")
# It could be the binding was exported by two modules, which we can detect
# by the `usingfailed` flag in the binding:
if isdefined(bnd, :flags) && Bool(bnd.flags >> 4 & 1) # magic location of the `usingfailed` flag
print(io, "\nHint: It looks like this name was exported by two different modules, resulting in ambiguity. Try explicitly importing it, or qualifying the name with the module it should come from.")
else
print(io, "\nSuggestion: check for spelling errors or missing imports.")
end
owner = bnd
else
owner = unsafe_pointer_to_objref(owner)::Core.Binding
Expand Down
32 changes: 32 additions & 0 deletions stdlib/REPL/test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,38 @@ finally
empty!(Base.Experimental._hint_handlers)
end

try # test the functionality of `UndefVarError_hint` against import clashes
@assert isempty(Base.Experimental._hint_handlers)
Base.Experimental.register_error_hint(REPL.UndefVarError_hint, UndefVarError)

@eval module X

module A
export x
x = 1
end # A

module B
export x
x = 2
end # B

using .A, .B

end # X

str = try
X.x
@test false # should not reach this
catch e
sprint(Base.showerror, e)
end

@test contains(str, "Hint: It looks like this name was exported by two different modules, resulting in ambiguity. Try explicitly importing it, or qualifying the name with the module it should come from.")
finally
empty!(Base.Experimental._hint_handlers)
end

# Hints for tab completes

fake_repl() do stdin_write, stdout_read, repl
Expand Down