tmacroinjectedsymwarning.nim 1.4 KB

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