Skip to content

Commit ac148ce

Browse files
authored
Merge pull request #23604 from JuliaLang/jn/17951
fieldtype validation: disallow `Vararg` as a field type
2 parents f569169 + e21f35a commit ac148ce

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/interpreter.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ static jl_value_t *eval(jl_value_t *e, interpreter_state *s)
380380
}
381381
else if (ex->head == primtype_sym) {
382382
if (inside_typedef)
383-
jl_error("cannot eval a new bits type definition while defining another type");
383+
jl_error("cannot eval a new primitive type definition while defining another type");
384384
jl_value_t *name = args[0];
385385
jl_value_t *super = NULL, *para = NULL, *vnb = NULL, *temp = NULL;
386386
jl_datatype_t *dt = NULL;
@@ -395,11 +395,11 @@ static jl_value_t *eval(jl_value_t *e, interpreter_state *s)
395395
assert(jl_is_svec(para));
396396
vnb = eval(args[2], s);
397397
if (!jl_is_long(vnb))
398-
jl_errorf("invalid declaration of bits type %s",
398+
jl_errorf("invalid declaration of primitive type %s",
399399
jl_symbol_name((jl_sym_t*)name));
400400
ssize_t nb = jl_unbox_long(vnb);
401-
if (nb < 1 || nb>=(1<<23) || (nb&7) != 0)
402-
jl_errorf("invalid number of bits in type %s",
401+
if (nb < 1 || nb >= (1 << 23) || (nb & 7) != 0)
402+
jl_errorf("invalid number of bits in primitive type %s",
403403
jl_symbol_name((jl_sym_t*)name));
404404
dt = jl_new_primitivetype(name, modu, NULL, (jl_svec_t*)para, nb);
405405
w = dt->name->wrapper;
@@ -428,7 +428,7 @@ static jl_value_t *eval(jl_value_t *e, interpreter_state *s)
428428
}
429429
else if (ex->head == structtype_sym) {
430430
if (inside_typedef)
431-
jl_error("cannot eval a new data type definition while defining another type");
431+
jl_error("cannot eval a new struct type definition while defining another type");
432432
jl_value_t *name = args[0];
433433
jl_value_t *para = eval(args[1], s);
434434
jl_value_t *temp = NULL;
@@ -462,12 +462,13 @@ static jl_value_t *eval(jl_value_t *e, interpreter_state *s)
462462
jl_set_datatype_super(dt, super);
463463
dt->types = (jl_svec_t*)eval(args[4], s);
464464
jl_gc_wb(dt, dt->types);
465-
for(size_t i=0; i < jl_svec_len(dt->types); i++) {
465+
for (size_t i = 0; i < jl_svec_len(dt->types); i++) {
466466
jl_value_t *elt = jl_svecref(dt->types, i);
467-
if (!jl_is_type(elt) && !jl_is_typevar(elt))
467+
if ((!jl_is_type(elt) && !jl_is_typevar(elt)) || jl_is_vararg_type(elt)) {
468468
jl_type_error_rt(jl_symbol_name(dt->name->name),
469469
"type definition",
470470
(jl_value_t*)jl_type_type, elt);
471+
}
471472
}
472473
jl_reinstantiate_inner_types(dt);
473474
}

test/core.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,14 +1276,13 @@ let
12761276
@test foo(x) == [1.0, 2.0, 3.0]
12771277
end
12781278

1279-
# TODO!!
12801279
# issue #4115
1281-
#mutable struct Foo4115
1282-
#end
1283-
#const Foo4115s = NTuple{3,Union{Foo4115,Type{Foo4115}}}
1284-
#baz4115(x::Foo4115s) = x
1285-
#@test baz4115(convert(Tuple{Type{Foo4115},Type{Foo4115},Foo4115},
1286-
# (Foo4115,Foo4115,Foo4115()))) == (Foo4115,Foo4115,Foo4115())
1280+
mutable struct Foo4115 end
1281+
const Foo4115s = NTuple{3, Union{Foo4115, Type{Foo4115}}}
1282+
baz4115(x::Foo4115s) = x
1283+
let t = (Foo4115, Foo4115, Foo4115())
1284+
@test_throws MethodError baz4115(t)
1285+
end
12871286

12881287
# issue #4129
12891288
mutable struct Foo4129; end
@@ -4866,6 +4865,12 @@ let a = Array{Core.TypeofBottom, 1}(2)
48664865
@test a == [Union{}, Union{}]
48674866
end
48684867

4868+
@test_throws TypeError(:T17951, "type definition", Type, Vararg) @eval begin
4869+
struct T17951
4870+
x::Vararg
4871+
end
4872+
end
4873+
48694874
# issue #21178
48704875
struct F21178{A,B} end
48714876
b21178(::F1,::F2) where {B1,B2,F1<:F21178{B1,<:Any},F2<:F21178{B2}} = F1,F2,B1,B2

0 commit comments

Comments
 (0)