Skip to content

Commit

Permalink
Fix JuliaLang#35231 improve type error message (JuliaLang#35238)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssikdar1 committed Mar 24, 2020
1 parent e3e9def commit f449765
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
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

0 comments on commit f449765

Please sign in to comment.