Skip to content

Commit 9f76d09

Browse files
committed
fix nim-lang#19546 Missing compiler error for array in type definition (also apply to seq, set)
1 parent be4bd8a commit 9f76d09

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

compiler/semstmts.nim

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,10 @@ proc hasEmpty(typ: PType): bool =
404404
for s in typ.sons:
405405
result = result or hasEmpty(s)
406406

407+
proc hasNone(typ: PType): bool =
408+
if typ.kind in {tySequence, tyArray, tySet}:
409+
result = typ.lastSon.kind == tyNone
410+
407411
proc hasUnresolvedParams(n: PNode; flags: TExprFlags): bool =
408412
result = tfUnresolved in n.typ.flags
409413
when false:
@@ -1442,31 +1446,42 @@ proc typeSectionRightSidePass(c: PContext, n: PNode) =
14421446
obj.typ = objTy
14431447
objTy.sym = obj
14441448

1445-
proc checkForMetaFields(c: PContext; n: PNode) =
1446-
proc checkMeta(c: PContext; n: PNode; t: PType) =
1449+
proc checkForMetaFields(c: PContext; n: PNode; hasError: var bool) =
1450+
proc checkMeta(c: PContext; n: PNode; t: PType; hasError: var bool) =
14471451
if t != nil and t.isMetaType and tfGenericTypeParam notin t.flags:
14481452
if t.kind == tyBuiltInTypeClass and t.len == 1 and t[0].kind == tyProc:
1453+
hasError = true
14491454
localError(c.config, n.info, ("'$1' is not a concrete type; " &
14501455
"for a callback without parameters use 'proc()'") % t.typeToString)
14511456
else:
1457+
hasError = true
14521458
localError(c.config, n.info, errTIsNotAConcreteType % t.typeToString)
14531459

14541460
if n.isNil: return
14551461
case n.kind
14561462
of nkRecList, nkRecCase:
1457-
for s in n: checkForMetaFields(c, s)
1463+
for s in n: checkForMetaFields(c, s, hasError)
14581464
of nkOfBranch, nkElse:
1459-
checkForMetaFields(c, n.lastSon)
1465+
checkForMetaFields(c, n.lastSon, hasError)
14601466
of nkSym:
14611467
let t = n.sym.typ
14621468
case t.kind
14631469
of tySequence, tySet, tyArray, tyOpenArray, tyVar, tyLent, tyPtr, tyRef,
14641470
tyProc, tyGenericInvocation, tyGenericInst, tyAlias, tySink, tyOwned:
14651471
let start = ord(t.kind in {tyGenericInvocation, tyGenericInst})
1472+
if t.hasNone():
1473+
hasError = true
1474+
case t.kind
1475+
of tyArray:
1476+
localError(c.config, n.info, errArrayExpectsTwoTypeParams)
1477+
of tySequence, tySet:
1478+
localError(c.config, n.info, "$1 expects one type parameter" % toHumanStr(t.kind))
1479+
else:
1480+
discard
14661481
for i in start..<t.len:
1467-
checkMeta(c, n, t[i])
1482+
checkMeta(c, n, t[i], hasError)
14681483
else:
1469-
checkMeta(c, n, t)
1484+
checkMeta(c, n, t, hasError)
14701485
else:
14711486
internalAssert c.config, false
14721487

@@ -1501,10 +1516,11 @@ proc typeSectionFinalPass(c: PContext, n: PNode) =
15011516
assert s.typ != nil
15021517
assignType(s.typ, t)
15031518
s.typ.itemId = t.itemId # same id
1504-
checkConstructedType(c.config, s.info, s.typ)
1519+
var hasError = false
15051520
if s.typ.kind in {tyObject, tyTuple} and not s.typ.n.isNil:
1506-
checkForMetaFields(c, s.typ.n)
1507-
1521+
checkForMetaFields(c, s.typ.n, hasError)
1522+
if not hasError:
1523+
checkConstructedType(c.config, s.info, s.typ)
15081524
# fix bug #5170, bug #17162, bug #15526: ensure locally scoped types get a unique name:
15091525
if s.typ.kind in {tyEnum, tyRef, tyObject} and not isTopLevel(c):
15101526
incl(s.flags, sfGenSym)

compiler/semtypinst.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ proc checkPartialConstructedType(conf: ConfigRef; info: TLineInfo, t: PType) =
2323
if t.kind in {tyVar, tyLent} and t[0].kind in {tyVar, tyLent}:
2424
localError(conf, info, "type 'var var' is not allowed")
2525

26+
proc hasNone(typ: PType): bool =
27+
if typ.kind in {tySequence, tyArray, tySet}:
28+
result = typ.lastSon.kind == tyNone
29+
2630
proc checkConstructedType*(conf: ConfigRef; info: TLineInfo, typ: PType) =
2731
var t = typ.skipTypes({tyDistinct})
2832
if t.kind in tyTypeClasses: discard

tests/errmsgs/t19546.nim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
discard """
2+
cmd: "nim check --hints:off $file"
3+
errormsg: ""
4+
nimout: '''
5+
t19546.nim(12, 5) Error: array expects two type parameters
6+
t19546.nim(16, 5) Error: sequence expects one type parameter
7+
t19546.nim(20, 5) Error: set expects one type parameter
8+
'''
9+
"""
10+
type
11+
ExampleObj1 = object
12+
arr: array
13+
14+
type
15+
ExampleObj2 = object
16+
arr: seq
17+
18+
type
19+
ExampleObj3 = object
20+
arr: set

0 commit comments

Comments
 (0)