summary refs log tree commit diff stats
path: root/tests/ccgbugs/tunsafeaddr.nim
blob: f868739ded3354ddbae9e385cb27929fbb1827e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
discard """
  output: '''12
4'''
"""

{.emit: """
NI sum(NI* a, NI len) {
  NI i, result = 0;
  for (i = 0; i < len; ++i) result += a[i];
  return result;
}
""".}

proc sum(a: ptr int; len: int): int {.importc, nodecl.}

proc main =
  let foo = [8, 3, 1]
  echo sum(unsafeAddr foo[0], foo.len)


# bug #3736

proc p(x: seq[int]) = discard x[0].unsafeAddr # works
proc q(x: seq[SomeInteger]) = discard x[0].unsafeAddr # doesn't work

p(@[1])
q(@[1])

main()

# bug #9403

type
  MyObj = ref object
    len: int
    val: UncheckedArray[uint64]

proc spot(x: MyObj): int64 =
  result = cast[UncheckedArray[int64]](x.val)[0]

proc newMyObj(len: int): MyObj =
  unsafeNew(result, sizeof(result[]) + len * sizeof(uint64))
  result.len = len
  result.val[0] = 4u64
  result.val[1] = 8u64

echo spot(newMyObj(2))
="w"> slice[int]): seq[int] = for item in sl: result.add item let mySeq = @[1, 2, 3, 4, 5, 6, 7, 8, 9] doAssert testing(mySeq) == mySeq doAssert testing(mySeq[2..^2]) == mySeq[2..^2] block: type slice = openArray[int] # Proc using that alias proc testing(sl: slice): seq[int] = for item in sl: result.add item let mySeq = @[1, 2, 3, 4, 5, 6, 7, 8, 9] doAssert testing(mySeq) == mySeq doAssert testing(mySeq[2..^2]) == mySeq[2..^2] main() # static: main() # xxx bug #15952: Error: cannot generate code for: mSlice