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

quote variable name in UndefVarError and UndefKeywordError #47077

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions base/docs/basedocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1750,7 +1750,7 @@ A symbol in the current scope is not defined.
# Examples
```jldoctest
julia> a
ERROR: UndefVarError: a not defined
ERROR: UndefVarError: `a` not defined

julia> a = 1;

Expand All @@ -1773,7 +1773,7 @@ julia> function my_func(;my_arg)
my_func (generic function with 1 method)

julia> my_func()
ERROR: UndefKeywordError: keyword argument my_arg not assigned
ERROR: UndefKeywordError: keyword argument `my_arg` not assigned
Stacktrace:
[1] my_func() at ./REPL[1]:2
[2] top-level scope at REPL[2]:1
Expand Down
4 changes: 2 additions & 2 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ showerror(io::IO, ex::AssertionError) = print(io, "AssertionError: ", ex.msg)
showerror(io::IO, ex::OverflowError) = print(io, "OverflowError: ", ex.msg)

showerror(io::IO, ex::UndefKeywordError) =
print(io, "UndefKeywordError: keyword argument $(ex.var) not assigned")
print(io, "UndefKeywordError: keyword argument `$(ex.var)` not assigned")

function showerror(io::IO, ex::UndefVarError)
print(io, "UndefVarError: $(ex.var) not defined")
print(io, "UndefVarError: `$(ex.var)` not defined")
Experimental.show_error_hints(io, ex)
end

Expand Down
2 changes: 1 addition & 1 deletion base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ julia> Foo(b="hi")
Foo(1, "hi")

julia> Foo()
ERROR: UndefKeywordError: keyword argument b not assigned
ERROR: UndefKeywordError: keyword argument `b` not assigned
Stacktrace:
[...]
```
Expand Down
6 changes: 3 additions & 3 deletions doc/src/manual/control-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ julia> test(1,2)
x is less than y.

julia> test(2,1)
ERROR: UndefVarError: relation not defined
ERROR: UndefVarError: `relation` not defined
Stacktrace:
[1] test(::Int64, ::Int64) at ./none:7
```
Expand Down Expand Up @@ -433,7 +433,7 @@ julia> for j = 1:3
3

julia> j
ERROR: UndefVarError: j not defined
ERROR: UndefVarError: `j` not defined
```

```jldoctest
Expand Down Expand Up @@ -669,7 +669,7 @@ Additionally, some exception types take one or more arguments that are used for

```jldoctest
julia> throw(UndefVarError(:x))
ERROR: UndefVarError: x not defined
ERROR: UndefVarError: `x` not defined
```

This mechanism can be implemented easily by custom exception types following the way [`UndefVarError`](@ref)
Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/distributed-computing.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ MyType(7)

julia> fetch(@spawnat 2 MyType(7))
ERROR: On worker 2:
UndefVarError: MyType not defined
UndefVarError: `MyType` not defined

julia> fetch(@spawnat 2 DummyModule.MyType(7))
Expand Down
8 changes: 4 additions & 4 deletions doc/src/manual/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ while x < 10
end
```
and notice that it works fine in an interactive environment (like the Julia REPL),
but gives `UndefVarError: x not defined` when you try to run it in script or other
but gives ```UndefVarError: `x` not defined``` when you try to run it in script or other
file. What is going on is that Julia generally requires you to **be explicit about assigning to global variables in a local scope**.

Here, `x` is a global variable, `while` defines a [local scope](@ref scope-of-variables), and `x += 1` is
Expand Down Expand Up @@ -705,7 +705,7 @@ julia> module Foo

julia> Foo.foo()
ERROR: On worker 2:
UndefVarError: Foo not defined
UndefVarError: `Foo` not defined
Stacktrace:
[...]
```
Expand All @@ -726,7 +726,7 @@ julia> @everywhere module Foo

julia> Foo.foo()
ERROR: On worker 2:
UndefVarError: gvar not defined
UndefVarError: `gvar` not defined
Stacktrace:
[...]
```
Expand Down Expand Up @@ -762,7 +762,7 @@ bar (generic function with 1 method)

julia> remotecall_fetch(bar, 2)
ERROR: On worker 2:
UndefVarError: #bar not defined
UndefVarError: `#bar` not defined
[...]

julia> anon_bar = ()->1
Expand Down
4 changes: 2 additions & 2 deletions doc/src/manual/metaprogramming.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ julia> ex = :(a + b)
:(a + b)

julia> eval(ex)
ERROR: UndefVarError: b not defined
ERROR: UndefVarError: `b` not defined
[...]

julia> a = 1; b = 2;
Expand All @@ -382,7 +382,7 @@ julia> ex = :(x = 1)
:(x = 1)

julia> x
ERROR: UndefVarError: x not defined
ERROR: UndefVarError: `x` not defined

julia> eval(ex)
1
Expand Down
6 changes: 3 additions & 3 deletions doc/src/manual/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ julia> using .A, .B

julia> f
WARNING: both B and A export "f"; uses of it in module Main must be qualified
ERROR: UndefVarError: f not defined
ERROR: UndefVarError: `f` not defined
```

Here, Julia cannot decide which `f` you are referring to, so you have to make a choice. The following solutions are commonly used:
Expand Down Expand Up @@ -397,7 +397,7 @@ x = 0

module Sub
using ..TestPackage
z = y # ERROR: UndefVarError: y not defined
z = y # ERROR: UndefVarError: `y` not defined
end

y = 1
Expand All @@ -413,7 +413,7 @@ For similar reasons, you cannot use a cyclic ordering:
module A

module B
using ..C # ERROR: UndefVarError: C not defined
using ..C # ERROR: UndefVarError: `C` not defined
end

module C
Expand Down
12 changes: 6 additions & 6 deletions doc/src/manual/variables-and-scoping.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ julia> module B
julia> module D
b = a # errors as D's global scope is separate from A's
end;
ERROR: UndefVarError: a not defined
ERROR: UndefVarError: `a` not defined
```

If a top-level expression contains a variable declaration with keyword `local`,
Expand Down Expand Up @@ -187,7 +187,7 @@ julia> greet()
hello

julia> x # global
ERROR: UndefVarError: x not defined
ERROR: UndefVarError: `x` not defined
```

Inside of the `greet` function, the assignment `x = "hello"` causes `x` to be a new local variable
Expand Down Expand Up @@ -256,7 +256,7 @@ julia> sum_to(10)
55

julia> s # global
ERROR: UndefVarError: s not defined
ERROR: UndefVarError: `s` not defined
```

Since `s` is local to the function `sum_to`, calling the function has no effect on the global
Expand Down Expand Up @@ -343,7 +343,7 @@ hello
hello

julia> x
ERROR: UndefVarError: x not defined
ERROR: UndefVarError: `x` not defined
```

Since the global `x` is not defined when the `for` loop is evaluated, the first clause of the soft
Expand Down Expand Up @@ -408,7 +408,7 @@ julia> code = """
julia> include_string(Main, code)
┌ 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.
└ @ string:4
ERROR: LoadError: UndefVarError: s not defined
ERROR: LoadError: UndefVarError: `s` not defined
```

Here we use [`include_string`](@ref), to evaluate `code` as though it were the contents of a file.
Expand Down Expand Up @@ -559,7 +559,7 @@ julia> let x = 1, z
println("z: $z") # errors as z has not been assigned yet but is local
end
x: 1, y: -1
ERROR: UndefVarError: z not defined
ERROR: UndefVarError: `z` not defined
```

The assignments are evaluated in order, with each right-hand side evaluated in the scope before
Expand Down
6 changes: 3 additions & 3 deletions stdlib/REPL/test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ fake_repl() do stdin_write, stdout_read, repl
# generate top-level error
write(stdin_write, "foobar\n")
readline(stdout_read)
@test readline(stdout_read) == "\e[0mERROR: UndefVarError: foobar not defined"
@test readline(stdout_read) == "\e[0mERROR: UndefVarError: `foobar` not defined"
@test readline(stdout_read) == ""
readuntil(stdout_read, "julia> ", keep=true)
# check that top-level error did not change `err`
Expand All @@ -1458,13 +1458,13 @@ fake_repl() do stdin_write, stdout_read, repl
readuntil(stdout_read, "julia> ", keep=true)
write(stdin_write, "foo()\n")
readline(stdout_read)
@test readline(stdout_read) == "\e[0mERROR: UndefVarError: foobar not defined"
@test readline(stdout_read) == "\e[0mERROR: UndefVarError: `foobar` not defined"
readuntil(stdout_read, "julia> ", keep=true)
# check that deeper error did set `err`
write(stdin_write, "err\n")
readline(stdout_read)
@test readline(stdout_read) == "\e[0m1-element ExceptionStack:"
@test readline(stdout_read) == "UndefVarError: foobar not defined"
@test readline(stdout_read) == "UndefVarError: `foobar` not defined"
@test readline(stdout_read) == "Stacktrace:"
write(stdin_write, '\x04')
Base.wait(repltask)
Expand Down
4 changes: 2 additions & 2 deletions test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ nested_error_pattern = r"""
ERROR: DivideError: integer division error
Stacktrace:.*

caused by: UndefVarError: __not_a_binding__ not defined
caused by: UndefVarError: `__not_a_binding__` not defined
Stacktrace:.*
"""s

Expand All @@ -31,7 +31,7 @@ nested_error_pattern = r"""
DivideError: integer division error
Stacktrace:.*

caused by: UndefVarError: __not_a_binding__ not defined
caused by: UndefVarError: `__not_a_binding__` not defined
Stacktrace:.*
"""s, sprint(show, excs))
end
Expand Down
2 changes: 1 addition & 1 deletion test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ let undefvar
err_str = @except_str Vector{Any}(undef, 1)[1] UndefRefError
@test err_str == "UndefRefError: access to undefined reference"
err_str = @except_str undefvar UndefVarError
@test err_str == "UndefVarError: undefvar not defined"
@test err_str == "UndefVarError: `undefvar` not defined"
err_str = @except_str read(IOBuffer(), UInt8) EOFError
@test err_str == "EOFError: read end of file"
err_str = @except_str Dict()[:doesnotexist] KeyError
Expand Down