tinitchecks_v2.nim 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. discard """
  2. cmd: "nim check $file"
  3. action: "compile"
  4. """
  5. {.experimental: "strictDefs".}
  6. proc myopen(f: out File; s: string): bool =
  7. f = default(File)
  8. result = false
  9. proc main =
  10. var f: File
  11. if myopen(f, "aarg"):
  12. f.close
  13. proc invalid =
  14. var s: seq[string]
  15. s.add "abc" #[tt.Warning
  16. ^ use explicit initialization of 's' for clarity [Uninit] ]#
  17. proc valid =
  18. var s: seq[string] = @[]
  19. s.add "abc" # valid!
  20. main()
  21. invalid()
  22. valid()
  23. proc branchy(cond: bool) =
  24. var s: seq[string]
  25. if cond:
  26. s = @["y"]
  27. else:
  28. s = @[]
  29. s.add "abc" # valid!
  30. branchy true
  31. proc p(x: out int; y: out string; cond: bool) = #[tt.Warning
  32. ^ Cannot prove that 'y' is initialized. This will become a compile time error in the future. [ProveInit] ]#
  33. x = 4
  34. if cond:
  35. y = "abc"
  36. # error: not every path initializes 'y'
  37. var gl: int
  38. var gs: string
  39. p gl, gs, false
  40. proc canRaise(x: int): int =
  41. result = x
  42. raise newException(ValueError, "wrong")
  43. proc currentlyValid(x: out int; y: out string; cond: bool) =
  44. x = canRaise(45)
  45. y = "abc" # <-- error: not every path initializes 'y'
  46. currentlyValid gl, gs, false
  47. block: # previously effects/toutparam
  48. proc gah[T](x: out T) =
  49. x = 3
  50. proc arr1 =
  51. var a: array[2, int]
  52. var x: int
  53. gah(x)
  54. a[0] = 3
  55. a[x] = 3
  56. echo x
  57. arr1()
  58. proc arr2 =
  59. var a: array[2, int]
  60. var x: int
  61. a[0] = 3
  62. a[x] = 3 #[tt.Warning
  63. ^ use explicit initialization of 'x' for clarity [Uninit] ]#
  64. echo x
  65. arr2()