Skip to content

Commit

Permalink
improve apply_type error message (#42422)
Browse files Browse the repository at this point in the history
Fixes #42401

(cherry picked from commit 00a602b)
  • Loading branch information
vtjnash authored and KristofferC committed Oct 19, 2021
1 parent f7103b8 commit ada1cd2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,8 @@ jl_value_t *jl_apply_type(jl_value_t *tc, jl_value_t **params, size_t n)
}
// if this is a wrapper, let check_datatype_parameters give the error
if (!iswrapper)
jl_type_error_rt("Type", jl_symbol_name(ua->var->name), (jl_value_t*)ua->var, pi);
jl_type_error_rt(jl_is_datatype(inner) ? jl_symbol_name(inner->name->name) : "Type",
jl_symbol_name(ua->var->name), (jl_value_t*)ua->var, pi);
}

tc = jl_instantiate_unionall(ua, pi);
Expand Down Expand Up @@ -1063,16 +1064,19 @@ static void check_datatype_parameters(jl_typename_t *tn, jl_value_t **params, si
}
assert(i == np*2);
wrapper = tn->wrapper;
for(i=0; i < np; i++) {
for (i = 0; i < np; i++) {
assert(jl_is_unionall(wrapper));
jl_tvar_t *tv = ((jl_unionall_t*)wrapper)->var;
if (!within_typevar(params[i], bounds[2*i], bounds[2*i+1])) {
// TODO: pass a new version of `tv` containing the instantiated bounds
if (tv->lb != bounds[2*i] || tv->ub != bounds[2*i+1])
// pass a new version of `tv` containing the instantiated bounds
tv = jl_new_typevar(tv->name, bounds[2*i], bounds[2*i+1]);
JL_GC_PUSH1(&tv);
jl_type_error_rt(jl_symbol_name(tn->name), jl_symbol_name(tv->name), (jl_value_t*)tv, params[i]);
}
int j;
for(j=2*i+2; j < 2*np; j++) {
jl_value_t*bj = bounds[j];
for (j = 2*i + 2; j < 2*np; j++) {
jl_value_t *bj = bounds[j];
if (bj != (jl_value_t*)jl_any_type && bj != jl_bottom_type)
bounds[j] = jl_substitute_var(bj, tv, params[i]);
}
Expand Down
8 changes: 7 additions & 1 deletion test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ let
@test occursin("column vector", err_str)
end

struct TypeWithIntParam{T <: Integer} end
struct TypeWithIntParam{T<:Integer, Vector{T}<:A<:AbstractArray{T}} end
struct Bounded # not an AbstractArray
bound::Int
end
Expand Down Expand Up @@ -315,8 +315,14 @@ let undefvar
@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 TypeWithIntParam{Int64,Vector{Float64}} TypeError
@test err_str == "TypeError: in TypeWithIntParam, in A, expected Vector{Int64}<:A<:(AbstractArray{Int64}), got Type{Vector{Float64}}"
err_str = @except_str TypeWithIntParam{Int64}{Vector{Float64}} TypeError
@test err_str == "TypeError: in TypeWithIntParam, in A, expected Vector{Int64}<:A<:(AbstractArray{Int64}), got Type{Vector{Float64}}"
err_str = @except_str Type{Vararg} TypeError
@test err_str == "TypeError: in Type, in parameter, expected Type, got Vararg"
err_str = @except_str Ref{Vararg} TypeError
@test err_str == "TypeError: in Type, in parameter, expected Type, got Vararg"

err_str = @except_str mod(1,0) DivideError
@test err_str == "DivideError: integer division error"
Expand Down

0 comments on commit ada1cd2

Please sign in to comment.