topensymwarning.nim 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. template g(T: type): string =
  43. var res = "ok"
  44. let x = f().valueOr:
  45. {.push warningAsError[IgnoredSymbolInjection]: on.}
  46. # test spurious error
  47. discard true
  48. let _ = f
  49. {.pop.}
  50. res = $error #[tt.Warning
  51. ^ a new symbol 'error' has been injected during template or generic instantiation, however 'error' [enumField declared in topensymwarning.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]]#
  52. 123
  53. res
  54. discard g(int)