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

Fix #35231 improve type error message #35238

Merged
merged 8 commits into from
Mar 24, 2020
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
6 changes: 3 additions & 3 deletions base/docs/basedocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2203,7 +2203,7 @@ julia> "Hello!" :: IntOrString
"Hello!"

julia> 1.0 :: IntOrString
ERROR: TypeError: in typeassert, expected Union{Int64, AbstractString}, got Float64
ERROR: TypeError: in typeassert, expected Union{Int64, AbstractString}, got a value of type Float64
```
"""
Union
Expand Down Expand Up @@ -2237,7 +2237,7 @@ Outside of declarations `::` is used to assert that expressions and variables in
# Examples
```jldoctest
julia> (1+2)::AbstractFloat
ERROR: TypeError: typeassert: expected AbstractFloat, got Int64
ERROR: TypeError: typeassert: expected AbstractFloat, got a value of type Int64

julia> (1+2)::Int
3
Expand Down Expand Up @@ -2320,7 +2320,7 @@ The syntax `x::type` calls this function.
# Examples
```jldoctest
julia> typeassert(2.5, Int)
ERROR: TypeError: in typeassert, expected Int64, got Float64
ERROR: TypeError: in typeassert, expected Int64, got a value of type Float64
Stacktrace:
[...]
```
Expand Down
2 changes: 1 addition & 1 deletion base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function showerror(io::IO, ex::TypeError)
elseif isa(ex.got, Type)
targs = ("Type{", ex.got, "}")
else
targs = (typeof(ex.got),)
targs = ("a value of type $(typeof(ex.got))",)
end
if ex.context == ""
ctx = "in $(ex.func)"
Expand Down
6 changes: 3 additions & 3 deletions doc/src/manual/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ an exception is thrown, otherwise, the left-hand value is returned:

```jldoctest
julia> (1+2)::AbstractFloat
ERROR: TypeError: in typeassert, expected AbstractFloat, got Int64
ERROR: TypeError: in typeassert, expected AbstractFloat, got a value of type Int64

julia> (1+2)::Int
3
Expand Down Expand Up @@ -493,7 +493,7 @@ julia> "Hello!" :: IntOrString
"Hello!"

julia> 1.0 :: IntOrString
ERROR: TypeError: in typeassert, expected Union{Int64, AbstractString}, got Float64
ERROR: TypeError: in typeassert, expected Union{Int64, AbstractString}, got a value of type Float64
```

The compilers for many languages have an internal union construct for reasoning about types; Julia
Expand Down Expand Up @@ -812,7 +812,7 @@ julia> Pointy{AbstractString}
ERROR: TypeError: in Pointy, in T, expected T<:Real, got Type{AbstractString}

julia> Pointy{1}
ERROR: TypeError: in Pointy, in T, expected T<:Real, got Int64
ERROR: TypeError: in Pointy, in T, expected T<:Real, got a value of type Int64
```

Type parameters for parametric composite types can be restricted in the same manner:
Expand Down
10 changes: 5 additions & 5 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,15 @@ let undefvar
err_str = @except_str 0::Bool TypeError
@test err_str == "TypeError: non-boolean ($Int) used in boolean context"
err_str = @except_str 0::AbstractFloat TypeError
@test err_str == "TypeError: in typeassert, expected AbstractFloat, got $Int"
@test err_str == "TypeError: in typeassert, expected AbstractFloat, got a value of type $Int"
err_str = @except_str 0::7 TypeError
@test err_str == "TypeError: in typeassert, expected Type, got $Int"
@test err_str == "TypeError: in typeassert, expected Type, got a value of type $Int"
err_str = @except_str "" <: AbstractString TypeError
@test err_str == "TypeError: in <:, expected Type, got String"
@test err_str == "TypeError: in <:, expected Type, got a value of type String"
err_str = @except_str AbstractString <: "" TypeError
@test err_str == "TypeError: in <:, expected Type, got String"
@test err_str == "TypeError: in <:, expected Type, got a value of type String"
err_str = @except_str Type{""} TypeError
@test err_str == "TypeError: in Type, in parameter, expected Type, got String"
@test err_str == "TypeError: in Type, in parameter, expected Type, got a value of type String"
err_str = @except_str TypeWithIntParam{Any} TypeError
@test err_str == "TypeError: in TypeWithIntParam, in T, expected T<:Integer, got Type{Any}"
err_str = @except_str Type{Vararg} TypeError
Expand Down