tmacroinjectedsymwarning.nim 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. discard """
  2. matrix: "--skipParentCfg --filenames:legacyRelProj"
  3. """
  4. type Xxx = enum
  5. error
  6. value
  7. type
  8. Result[T, E] = object
  9. when T is void:
  10. when E is void:
  11. oResultPrivate*: bool
  12. else:
  13. case oResultPrivate*: bool
  14. of false:
  15. eResultPrivate*: E
  16. of true:
  17. discard
  18. else:
  19. when E is void:
  20. case oResultPrivate*: bool
  21. of false:
  22. discard
  23. of true:
  24. vResultPrivate*: T
  25. else:
  26. case oResultPrivate*: bool
  27. of false:
  28. eResultPrivate*: E
  29. of true:
  30. vResultPrivate*: T
  31. template valueOr[T: not void, E](self: Result[T, E], def: untyped): untyped =
  32. let s = (self) # TODO avoid copy
  33. case s.oResultPrivate
  34. of true:
  35. s.vResultPrivate
  36. of false:
  37. when E isnot void:
  38. template error: untyped {.used, inject.} = s.eResultPrivate
  39. def
  40. proc f(): Result[int, cstring] =
  41. Result[int, cstring](oResultPrivate: false, eResultPrivate: "f")
  42. proc g(T: type): string =
  43. let x = f().valueOr:
  44. {.push warningAsError[IgnoredSymbolInjection]: on.}
  45. # test spurious error
  46. discard true
  47. let _ = f
  48. {.pop.}
  49. return $error #[tt.Warning
  50. ^ a new symbol 'error' has been injected during template or generic instantiation, however 'error' [enumField declared in tmacroinjectedsymwarning.nim(6, 3)] captured at the proc declaration will be used instead; either enable --experimental:openSym to use the injected symbol, or `bind` this captured symbol explicitly [IgnoredSymbolInjection]]#
  51. "ok"
  52. discard g(int)