mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 08:54:53 +00:00
Unpack mSlice tupleconstr for static openarrays (#20615)
This commit is contained in:
@@ -66,6 +66,27 @@ proc annotateType*(n: PNode, t: PType; conf: ConfigRef) =
|
||||
if i >= x.len: globalError conf, n.info, "invalid field at index " & $i
|
||||
else: annotateType(n[i], x[i], conf)
|
||||
elif x.kind == tyProc and x.callConv == ccClosure:
|
||||
n.typ = t
|
||||
elif x.kind == tyOpenArray: # `opcSlice` transforms slices into tuples
|
||||
if n.kind == nkTupleConstr:
|
||||
let
|
||||
bracketExpr = newNodeI(nkBracket, n.info)
|
||||
left = int n[1].intVal
|
||||
right = int n[2].intVal
|
||||
bracketExpr.flags = n.flags
|
||||
case n[0].kind # is this a string slice or a array slice
|
||||
of nkStrKinds:
|
||||
for i in left..right:
|
||||
bracketExpr.add newIntNode(nkCharLit, BiggestInt n[0].strVal[i])
|
||||
annotateType(bracketExpr[^1], t[0], conf)
|
||||
of nkBracket:
|
||||
for i in left..right:
|
||||
bracketExpr.add n[0][i]
|
||||
annotateType(bracketExpr[^1], t[0], conf)
|
||||
else:
|
||||
globalError(conf, n.info, "Incorrectly generated tuple constr")
|
||||
n[] = bracketExpr[]
|
||||
|
||||
n.typ = t
|
||||
else:
|
||||
globalError(conf, n.info, "() must have a tuple type")
|
||||
|
||||
@@ -24,3 +24,14 @@ proc `$`(x: openArray[int]): string =
|
||||
echo c
|
||||
echo c2.data
|
||||
|
||||
|
||||
type MyObj = object
|
||||
data: openarray[char]
|
||||
|
||||
const
|
||||
val1 = Foo(data: toOpenArray([1, 2, 3], 1, 1))
|
||||
val2 = Foo(data: toOpenArray([1, 2, 3], 0, 2))
|
||||
val3 = MyObj(data: "Hello".toOpenArray(0, 2))
|
||||
assert val1.data == [2]
|
||||
assert val2.data == [1, 2, 3]
|
||||
assert val3.data == "Hel"
|
||||
|
||||
@@ -35,6 +35,10 @@ static:
|
||||
assert arr.toOpenArray(3, 4).toOpenArray(0, 0) == [1]
|
||||
|
||||
|
||||
proc doThing(s: static openArray[int]) = discard
|
||||
|
||||
doThing([10, 20, 30].toOpenArray(0, 0))
|
||||
|
||||
# bug #19969
|
||||
proc f(): array[1, byte] =
|
||||
var a: array[1, byte]
|
||||
|
||||
Reference in New Issue
Block a user