mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-18 13:30:33 +00:00
new len(openarray) implementation
This commit is contained in:
@@ -512,7 +512,7 @@ proc typeSectionRightSidePass(c: PContext, n: PNode) =
|
||||
# symbol lookup needs to be done here.
|
||||
openScope(c.tab)
|
||||
pushOwner(s)
|
||||
s.typ.kind = tyGenericBody
|
||||
if s.magic == mNone: s.typ.kind = tyGenericBody
|
||||
if s.typ.containerID != 0:
|
||||
InternalError(a.info, "semTypeSection: containerID")
|
||||
s.typ.containerID = s.typ.id
|
||||
|
||||
@@ -760,7 +760,12 @@ proc processMagicType(c: PContext, m: PSym) =
|
||||
of mStmt: setMagicType(m, tyStmt, 0)
|
||||
of mTypeDesc: setMagicType(m, tyTypeDesc, 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)
|
||||
|
||||
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)
|
||||
else:
|
||||
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)
|
||||
#echo "came here for: ", typeToString(x)
|
||||
result.addSon(x)
|
||||
|
||||
proc semGenericParamList(c: PContext, n: PNode, father: PType = nil): PNode =
|
||||
|
||||
@@ -309,7 +309,10 @@ proc newSeq*[T](s: var seq[T], len: int) {.magic: "NewSeq", noSideEffect.}
|
||||
## This is equivalent to ``s = @[]; setlen(s, len)``, but more
|
||||
## efficient since no reallocation is needed.
|
||||
|
||||
proc len*[T](x: openArray[T]): int {.magic: "LengthOpenArray", noSideEffect.}
|
||||
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*(x: string): int {.magic: "LengthStr", noSideEffect.}
|
||||
proc len*(x: cstring): int {.magic: "LengthStr", noSideEffect.}
|
||||
proc len*[I, T](x: array[I, T]): int {.magic: "LengthArray", 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)
|
||||
|
||||
Reference in New Issue
Block a user