You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
make UndefVarError messages more precise and informative (#51979)
Record the 'scope' of the variable that was undefined (the Module, or a
descriptive word such as :local or :static_parameter). Add that scope to
the error message, and expand the hint suggestions added by the REPL to
include more specific advice on common mistakes:
- forgetting to set an initial value
- forgetting to import a global
- creating a local of the same name as a global
- not matching a static parameter in a signature subtype
Fixes#17062 (although more could probably be done to search for typos
using REPL.string_distance and getting the method from stacktrace)
Fixes#18877Fixes#25263Fixes#35126Fixes#39280Fixes#41728Fixes#48731Fixes#49917Fixes#50369
Copy file name to clipboardExpand all lines: doc/src/manual/variables-and-scoping.md
+7-6Lines changed: 7 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,7 +90,8 @@ julia> module B
90
90
julia> module D
91
91
b = a # errors as D's global scope is separate from A's
92
92
end;
93
-
ERROR: UndefVarError: `a` not defined
93
+
ERROR: UndefVarError: `a` not defined in `D`
94
+
Suggestion: check for spelling errors or missing imports. No global of this name exists in this module.
94
95
```
95
96
96
97
If a top-level expression contains a variable declaration with keyword `local`,
@@ -187,7 +188,7 @@ julia> greet()
187
188
hello
188
189
189
190
julia> x # global
190
-
ERROR: UndefVarError: `x` not defined
191
+
ERROR: UndefVarError: `x` not defined in `Main`
191
192
```
192
193
193
194
Inside of the `greet` function, the assignment `x = "hello"` causes `x` to be a new local variable
@@ -256,7 +257,7 @@ julia> sum_to(10)
256
257
55
257
258
258
259
julia> s # global
259
-
ERROR: UndefVarError: `s` not defined
260
+
ERROR: UndefVarError: `s` not defined in `Main`
260
261
```
261
262
262
263
Since `s` is local to the function `sum_to`, calling the function has no effect on the global
@@ -343,7 +344,7 @@ hello
343
344
hello
344
345
345
346
julia> x
346
-
ERROR: UndefVarError: `x` not defined
347
+
ERROR: UndefVarError: `x` not defined in `Main`
347
348
```
348
349
349
350
Since the global `x` is not defined when the `for` loop is evaluated, the first clause of the soft
@@ -408,7 +409,7 @@ julia> code = """
408
409
julia> include_string(Main, code)
409
410
┌ Warning: Assignment to `s` in soft scope is ambiguous because a global variable by the same name exists: `s` will be treated as a new local. Disambiguate by using `local s` to suppress this warning or `global s` to assign to the existing global variable.
410
411
└ @ string:4
411
-
ERROR: LoadError: UndefVarError: `s` not defined
412
+
ERROR: LoadError: UndefVarError: `s` not defined in local scope
412
413
```
413
414
414
415
Here we use [`include_string`](@ref), to evaluate `code` as though it were the contents of a file.
@@ -559,7 +560,7 @@ julia> let x = 1, z
559
560
println("z: $z") # errors as z has not been assigned yet but is local
560
561
end
561
562
x: 1, y: -1
562
-
ERROR: UndefVarError: `z` not defined
563
+
ERROR: UndefVarError: `z` not defined in local scope
563
564
```
564
565
565
566
The assignments are evaluated in order, with each right-hand side evaluated in the scope before
0 commit comments