tstrictfuncs_misc.nim 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. discard """
  2. action: compile
  3. """
  4. {.experimental: "strictFuncs".}
  5. func sortedFake1[T](a: openArray[T]): seq[T] =
  6. for i in 0 .. a.high: result.add a[i]
  7. func sortedFake2[T](a: openArray[T]): seq[T] =
  8. result = newSeq[T](a.len)
  9. for i in 0 .. a.high: result[i] = a[i]
  10. type Foo1 = object
  11. type Foo2 = ref object
  12. block:
  13. let a1 = sortedFake1([Foo1()]) # ok
  14. let a2 = sortedFake1([Foo2()]) # ok
  15. block:
  16. let a1 = sortedFake2([Foo1()]) # ok
  17. let a2 = sortedFake2([Foo2()]) # error: Error: 'sortedFake2' can have side effects
  18. import std/sequtils
  19. type Foob = ref object
  20. x: int
  21. let a1 = zip(@[1,2], @[1,2]) # ok
  22. let a2 = zip(@[Foob(x: 1)], @[Foob(x: 2)]) # error in 1.6.0 RC2, but not 1.4.x
  23. # bug #20863
  24. type
  25. Fooc = ref object
  26. func twice(foo: Fooc) =
  27. var a = newSeq[Fooc](2)
  28. a[0] = foo # No error.
  29. a[1] = foo # Error: 'twice' can have side effects.
  30. let foo = Fooc()
  31. twice(foo)
  32. # bug #17387
  33. import json
  34. func parseColumn(columnNode: JsonNode) =
  35. let columnName = columnNode["name"].str
  36. parseColumn(%*{"a": "b"})
  37. type
  38. MyTable = object
  39. data: seq[int]
  40. JsonNode3 = ref object
  41. fields: MyTable
  42. proc `[]`(t: MyTable, key: string): int =
  43. result = t.data[0]
  44. proc `[]`(x: JsonNode3, key: string): int =
  45. result = x.fields[key]
  46. func parseColumn(columnNode: JsonNode3) =
  47. var columnName = columnNode["test"]
  48. parseColumn(JsonNode3())