tinitchecks_v2.nim 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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