Skip to content

Commit ada1cd2

Browse files
vtjnashKristofferC
authored andcommitted
improve apply_type error message (#42422)
Fixes #42401 (cherry picked from commit 00a602b)
1 parent f7103b8 commit ada1cd2

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/jltypes.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,8 @@ jl_value_t *jl_apply_type(jl_value_t *tc, jl_value_t **params, size_t n)
846846
}
847847
// if this is a wrapper, let check_datatype_parameters give the error
848848
if (!iswrapper)
849-
jl_type_error_rt("Type", jl_symbol_name(ua->var->name), (jl_value_t*)ua->var, pi);
849+
jl_type_error_rt(jl_is_datatype(inner) ? jl_symbol_name(inner->name->name) : "Type",
850+
jl_symbol_name(ua->var->name), (jl_value_t*)ua->var, pi);
850851
}
851852

852853
tc = jl_instantiate_unionall(ua, pi);
@@ -1063,16 +1064,19 @@ static void check_datatype_parameters(jl_typename_t *tn, jl_value_t **params, si
10631064
}
10641065
assert(i == np*2);
10651066
wrapper = tn->wrapper;
1066-
for(i=0; i < np; i++) {
1067+
for (i = 0; i < np; i++) {
10671068
assert(jl_is_unionall(wrapper));
10681069
jl_tvar_t *tv = ((jl_unionall_t*)wrapper)->var;
10691070
if (!within_typevar(params[i], bounds[2*i], bounds[2*i+1])) {
1070-
// TODO: pass a new version of `tv` containing the instantiated bounds
1071+
if (tv->lb != bounds[2*i] || tv->ub != bounds[2*i+1])
1072+
// pass a new version of `tv` containing the instantiated bounds
1073+
tv = jl_new_typevar(tv->name, bounds[2*i], bounds[2*i+1]);
1074+
JL_GC_PUSH1(&tv);
10711075
jl_type_error_rt(jl_symbol_name(tn->name), jl_symbol_name(tv->name), (jl_value_t*)tv, params[i]);
10721076
}
10731077
int j;
1074-
for(j=2*i+2; j < 2*np; j++) {
1075-
jl_value_t*bj = bounds[j];
1078+
for (j = 2*i + 2; j < 2*np; j++) {
1079+
jl_value_t *bj = bounds[j];
10761080
if (bj != (jl_value_t*)jl_any_type && bj != jl_bottom_type)
10771081
bounds[j] = jl_substitute_var(bj, tv, params[i]);
10781082
}

test/errorshow.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ let
271271
@test occursin("column vector", err_str)
272272
end
273273

274-
struct TypeWithIntParam{T <: Integer} end
274+
struct TypeWithIntParam{T<:Integer, Vector{T}<:A<:AbstractArray{T}} end
275275
struct Bounded # not an AbstractArray
276276
bound::Int
277277
end
@@ -315,8 +315,14 @@ let undefvar
315315
@test err_str == "TypeError: in Type, in parameter, expected Type, got a value of type String"
316316
err_str = @except_str TypeWithIntParam{Any} TypeError
317317
@test err_str == "TypeError: in TypeWithIntParam, in T, expected T<:Integer, got Type{Any}"
318+
err_str = @except_str TypeWithIntParam{Int64,Vector{Float64}} TypeError
319+
@test err_str == "TypeError: in TypeWithIntParam, in A, expected Vector{Int64}<:A<:(AbstractArray{Int64}), got Type{Vector{Float64}}"
320+
err_str = @except_str TypeWithIntParam{Int64}{Vector{Float64}} TypeError
321+
@test err_str == "TypeError: in TypeWithIntParam, in A, expected Vector{Int64}<:A<:(AbstractArray{Int64}), got Type{Vector{Float64}}"
318322
err_str = @except_str Type{Vararg} TypeError
319323
@test err_str == "TypeError: in Type, in parameter, expected Type, got Vararg"
324+
err_str = @except_str Ref{Vararg} TypeError
325+
@test err_str == "TypeError: in Type, in parameter, expected Type, got Vararg"
320326

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

0 commit comments

Comments
 (0)