@@ -14,13 +14,83 @@ REPL.run_repl(repl)
1414"""
1515module REPL
1616
17+ Base. Experimental. @optlevel 1
18+ Base. Experimental. @max_methods 1
19+
20+ function UndefVarError_hint (io:: IO , ex:: UndefVarError )
21+ var = ex. var
22+ if var === :or
23+ print (io, " \n Suggestion: Use `||` for short-circuiting boolean OR." )
24+ elseif var === :and
25+ print (io, " \n Suggestion: Use `&&` for short-circuiting boolean AND." )
26+ elseif var === :help
27+ println (io)
28+ # Show friendly help message when user types help or help() and help is undefined
29+ show (io, MIME (" text/plain" ), Base. Docs. parsedoc (Base. Docs. keywords[:help ]))
30+ elseif var === :quit
31+ print (io, " \n Suggestion: To exit Julia, use Ctrl-D, or type exit() and press enter." )
32+ end
33+ if isdefined (ex, :scope )
34+ scope = ex. scope
35+ if scope isa Module
36+ bnd = ccall (:jl_get_module_binding , Any, (Any, Any, Cint), scope, var, true ):: Core.Binding
37+ if isdefined (bnd, :owner )
38+ owner = bnd. owner
39+ if owner === bnd
40+ print (io, " \n Suggestion: add an appropriate import or assignment. This global was declared but not assigned." )
41+ end
42+ else
43+ owner = ccall (:jl_binding_owner , Ptr{Cvoid}, (Any, Any), scope, var)
44+ if C_NULL == owner
45+ # No global of this name exists in this module.
46+ # This is the common case, so do not print that information.
47+ print (io, " \n Suggestion: check for spelling errors or missing imports." )
48+ owner = bnd
49+ else
50+ owner = unsafe_pointer_to_objref (owner):: Core.Binding
51+ end
52+ end
53+ if owner != = bnd
54+ # this could use jl_binding_dbgmodule for the exported location in the message too
55+ print (io, " \n Suggestion: this global was defined as `$(owner. globalref) ` but not assigned a value." )
56+ end
57+ elseif scope === :static_parameter
58+ print (io, " \n Suggestion: run Test.detect_unbound_args to detect method arguments that do not fully constrain a type parameter." )
59+ elseif scope === :local
60+ print (io, " \n Suggestion: check for an assignment to a local variable that shadows a global of the same name." )
61+ end
62+ else
63+ scope = undef
64+ end
65+ if scope != = Base && ! _UndefVarError_warnfor (io, Base, var)
66+ warned = false
67+ for m in Base. loaded_modules_order
68+ m === Core && continue
69+ m === Base && continue
70+ m === Main && continue
71+ m === scope && continue
72+ warned |= _UndefVarError_warnfor (io, m, var)
73+ end
74+ warned ||
75+ _UndefVarError_warnfor (io, Core, var) ||
76+ _UndefVarError_warnfor (io, Main, var)
77+ end
78+ return nothing
79+ end
80+
81+ function _UndefVarError_warnfor (io:: IO , m:: Module , var:: Symbol )
82+ Base. isbindingresolved (m, var) || return false
83+ (Base. isexported (m, var) || Base. ispublic (m, var)) || return false
84+ print (io, " \n Hint: a global variable of this name also exists in $m ." )
85+ return true
86+ end
87+
1788function __init__ ()
1889 Base. REPL_MODULE_REF[] = REPL
90+ Base. Experimental. register_error_hint (UndefVarError_hint, UndefVarError)
91+ return nothing
1992end
2093
21- Base. Experimental. @optlevel 1
22- Base. Experimental. @max_methods 1
23-
2494using Base. Meta, Sockets
2595import InteractiveUtils
2696
0 commit comments