tdefined_overload.nim 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. discard """
  2. output: "Valid and not defined"
  3. """
  4. # test for issue #7997
  5. # checking for `when not defined` in a template for some compile time symbol
  6. # results in a compilation error of:
  7. # Error: obsolete usage of 'defined', use 'declared' instead
  8. # if the symbol is 'overloaded' by some variable or procedure, because in
  9. # that case the argument of `defined` is of kind `nkSym` instead of `nkIdent`
  10. # (for which was checked in `semexprs.semDefined`).
  11. block:
  12. # check whether a proc with the same name as the argument to `defined`
  13. # compiles
  14. proc overloaded() =
  15. discard
  16. template definedCheck(): untyped =
  17. when not defined(overloaded): true
  18. else: false
  19. doAssert definedCheck == true
  20. block:
  21. # check whether a variable with the same name as the argument to `defined`
  22. # compiles
  23. var overloaded: int
  24. template definedCheck(): untyped =
  25. when not defined(overloaded): true
  26. else: false
  27. doAssert definedCheck == true
  28. block:
  29. # check whether a non overloaded when check still works properly
  30. when not defined(validIdentifier):
  31. echo "Valid and not defined"
  32. block:
  33. # now check that invalid identifiers cause a compilation error
  34. # by using reject template.
  35. template reject(b) =
  36. static: doAssert(not compiles(b))
  37. reject:
  38. when defined(123):
  39. echo "Invalid identifier! Will not be echoed"