new len(openarray) implementation
This commit is contained in:
parent
8c993733b9
commit
0d8e6dda69
4 changed files with 22 additions and 5 deletions
|
|
@ -512,7 +512,7 @@ proc typeSectionRightSidePass(c: PContext, n: PNode) =
|
||||||
# symbol lookup needs to be done here.
|
# symbol lookup needs to be done here.
|
||||||
openScope(c.tab)
|
openScope(c.tab)
|
||||||
pushOwner(s)
|
pushOwner(s)
|
||||||
s.typ.kind = tyGenericBody
|
if s.magic == mNone: s.typ.kind = tyGenericBody
|
||||||
if s.typ.containerID != 0:
|
if s.typ.containerID != 0:
|
||||||
InternalError(a.info, "semTypeSection: containerID")
|
InternalError(a.info, "semTypeSection: containerID")
|
||||||
s.typ.containerID = s.typ.id
|
s.typ.containerID = s.typ.id
|
||||||
|
|
|
||||||
|
|
@ -760,7 +760,12 @@ proc processMagicType(c: PContext, m: PSym) =
|
||||||
of mStmt: setMagicType(m, tyStmt, 0)
|
of mStmt: setMagicType(m, tyStmt, 0)
|
||||||
of mTypeDesc: setMagicType(m, tyTypeDesc, 0)
|
of mTypeDesc: setMagicType(m, tyTypeDesc, 0)
|
||||||
of mVoidType: setMagicType(m, tyEmpty, 0)
|
of mVoidType: setMagicType(m, tyEmpty, 0)
|
||||||
of mArray, mOpenArray, mRange, mSet, mSeq, mOrdinal: nil
|
of mArray: setMagicType(m, tyArray, 0)
|
||||||
|
of mOpenArray: setMagicType(m, tyOpenArray, 0)
|
||||||
|
of mRange: setMagicType(m, tyRange, 0)
|
||||||
|
of mSet: setMagicType(m, tySet, 0)
|
||||||
|
of mSeq: setMagicType(m, tySequence, 0)
|
||||||
|
of mOrdinal: nil
|
||||||
else: GlobalError(m.info, errTypeExpected)
|
else: GlobalError(m.info, errTypeExpected)
|
||||||
|
|
||||||
proc newConstraint(c: PContext, k: TTypeKind): PType =
|
proc newConstraint(c: PContext, k: TTypeKind): PType =
|
||||||
|
|
@ -782,9 +787,9 @@ proc semGenericConstraints(c: PContext, n: PNode, result: PType) =
|
||||||
semGenericConstraints(c, n.sons[2], result)
|
semGenericConstraints(c, n.sons[2], result)
|
||||||
else:
|
else:
|
||||||
var x = semTypeNode(c, n, nil)
|
var x = semTypeNode(c, n, nil)
|
||||||
if x.kind in StructuralEquivTypes and sonsLen(x) == 0:
|
if x.kind in StructuralEquivTypes and (
|
||||||
|
sonsLen(x) == 0 or x.sons[0].kind == tyEmpty):
|
||||||
x = newConstraint(c, x.kind)
|
x = newConstraint(c, x.kind)
|
||||||
#echo "came here for: ", typeToString(x)
|
|
||||||
result.addSon(x)
|
result.addSon(x)
|
||||||
|
|
||||||
proc semGenericParamList(c: PContext, n: PNode, father: PType = nil): PNode =
|
proc semGenericParamList(c: PContext, n: PNode, father: PType = nil): PNode =
|
||||||
|
|
|
||||||
|
|
@ -309,6 +309,9 @@ proc newSeq*[T](s: var seq[T], len: int) {.magic: "NewSeq", noSideEffect.}
|
||||||
## This is equivalent to ``s = @[]; setlen(s, len)``, but more
|
## This is equivalent to ``s = @[]; setlen(s, len)``, but more
|
||||||
## efficient since no reallocation is needed.
|
## efficient since no reallocation is needed.
|
||||||
|
|
||||||
|
when defined(newOpenArrayLen):
|
||||||
|
proc len*[T: openArray](x: T): int {.magic: "LengthOpenArray", noSideEffect.}
|
||||||
|
else:
|
||||||
proc len*[T](x: openArray[T]): int {.magic: "LengthOpenArray", noSideEffect.}
|
proc len*[T](x: openArray[T]): int {.magic: "LengthOpenArray", noSideEffect.}
|
||||||
proc len*(x: string): int {.magic: "LengthStr", noSideEffect.}
|
proc len*(x: string): int {.magic: "LengthStr", noSideEffect.}
|
||||||
proc len*(x: cstring): int {.magic: "LengthStr", noSideEffect.}
|
proc len*(x: cstring): int {.magic: "LengthStr", noSideEffect.}
|
||||||
|
|
|
||||||
9
tests/reject/tnolen.nim
Normal file
9
tests/reject/tnolen.nim
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
discard """
|
||||||
|
line: 8
|
||||||
|
msg: "Error: type mismatch: got (int)"
|
||||||
|
"""
|
||||||
|
|
||||||
|
# please finally disallow Len(3)
|
||||||
|
|
||||||
|
echo len(3)
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue