Skip to content

Commit

Permalink
try
Browse files Browse the repository at this point in the history
  • Loading branch information
Graveflo committed Nov 18, 2024
1 parent f053767 commit e72af7f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 21 deletions.
38 changes: 17 additions & 21 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,9 @@ proc typeSectionRightSidePass(c: PContext, n: PNode) =
# we fill it out later. For magic generics like 'seq', it won't be filled
# so we use tyNone instead of nil to not crash for strange conversions
# like: mydata.seq
if s.typ.kind in {tyOpenArray, tyVarargs} and s.typ.len == 1:
if s.typ.kind == tyArray and s.typ.len == 2:
discard
elif s.typ.kind in {tyOpenArray, tyVarargs} and s.typ.len == 1:
# XXX investigate why `tySequence` cannot be added here for now.
discard
else:
Expand Down Expand Up @@ -1717,18 +1719,16 @@ proc typeSectionRightSidePass(c: PContext, n: PNode) =
c.skipTypes = @[]

proc checkForMetaFields(c: PContext; n: PNode; hasError: var bool) =
proc checkMeta(c: PContext; n: PNode; t: PType; hasError: var bool; parent: PType) =
if t != nil and (t.isMetaType or t.kind == tyNone) and tfGenericTypeParam notin t.flags:
if t.kind == tyBuiltInTypeClass and t.len == 1 and t.elementType.kind == tyProc:
localError(c.config, n.info, ("'$1' is not a concrete type; " &
"for a callback without parameters use 'proc()'") % t.typeToString)
elif t.kind == tyNone and parent != nil:
# TODO: openarray has the `tfGenericTypeParam` flag & generics
# TODO: handle special cases (sink etc.) and views
localError(c.config, n.info, errTIsNotAConcreteType % parent.typeToString)
else:
localError(c.config, n.info, errTIsNotAConcreteType % t.typeToString)
hasError = true
proc errMsg(c: PContext; n: PNode; t: PType; parent: PType)=
if t.kind == tyBuiltInTypeClass and t.len == 1 and t.elementType.kind == tyProc:
localError(c.config, n.info, ("'$1' is not a concrete type; " &
"for a callback without parameters use 'proc()'") % t.typeToString)
elif t.kind == tyNone and parent != nil:
# TODO: openarray has the `tfGenericTypeParam` flag & generics
# TODO: handle special cases (sink etc.) and views
localError(c.config, n.info, errTIsNotAConcreteType % parent.typeToString)
else:
localError(c.config, n.info, errTIsNotAConcreteType % t.typeToString)

if n.isNil: return
case n.kind
Expand All @@ -1738,14 +1738,10 @@ proc checkForMetaFields(c: PContext; n: PNode; hasError: var bool) =
checkForMetaFields(c, n.lastSon, hasError)
of nkSym:
let t = n.sym.typ
case t.kind
of tySequence, tySet, tyArray, tyOpenArray, tyVar, tyLent, tyPtr, tyRef,
tyProc, tyGenericInvocation, tyGenericInst, tyAlias, tySink, tyOwned:
let start = ord(t.kind in {tyGenericInvocation, tyGenericInst})
for i in start..<t.len:
checkMeta(c, n, t[i], hasError, t)
else:
checkMeta(c, n, t, hasError, nil)
let comp = findUnspecifiedGenericsOrNil(t)
if comp != nil:
errMsg(c, n, comp, t)
hasError = true
else:
internalAssert c.config, false

Expand Down
27 changes: 27 additions & 0 deletions compiler/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2030,3 +2030,30 @@ proc genericRoot*(t: PType): PType =
result = t.sym.typ
else:
result = nil

proc findUnspecifiedGenericsOrNil*(t: PType): PType=
result = nil
if t == nil:
return nil
if tfHasMeta in t.flags:
result = t
if t.size == szIllegalRecursion:
return nil
let tmp = t.size
t.size = szIllegalRecursion
case t.kind
of tyMetaTypes, tyNone:
result = t
of tyStatic:
if t.n == nil:
result = t
of tySequence, tySet, tyArray, tyOpenArray, tyVar, tyLent, tyPtr, tyRef,
tyProc, tyGenericInvocation, tyGenericInst, tyAlias, tySink, tyOwned:
let start = ord(t.kind in {tyGenericInvocation, tyGenericInst})
for i in start..<t.len:
result = findUnspecifiedGenericsOrNil(t[i])
if result != nil:
break
else:
discard
t.size = tmp
22 changes: 22 additions & 0 deletions tests/concepts/tconceptsv2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
discard """
action: "run"
output: '''
A[array[0..0, int]]
A[seq[int]]
'''
"""
type
SomethingLike[T] = concept
proc len(s: Self): int
proc `[]`(s: Self; index: int): T

A[T] = object
x: T

proc initA*(x: SomethingLike): auto =
A[type x](x: x)

var a: array[1, int]
var s: seq[int]
echo typeof(initA(a))
echo typeof(initA(s))

0 comments on commit e72af7f

Please sign in to comment.