fix results compiletime regression (#203)

* fix results compiletime regression

* disable results.value compiletime test for pre 1.6 nim
This commit is contained in:
andri lim 2023-07-05 15:07:17 +07:00 committed by GitHub
parent 8bb07fac39
commit 5c519d8582
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -801,7 +801,10 @@ func value*[T, E](self: Result[T, E]): T {.inline.} =
## See also: Option.get
withAssertOk(self):
when T isnot void:
self.vResultPrivate
# TODO: remove result usage.
# A workaround for nim VM bug:
# https://github.com/nim-lang/Nim/issues/22216
result = self.vResultPrivate
func value*[T: not void, E](self: var Result[T, E]): var T {.inline.} =
## Fetch value of result if set, or raise Defect

View File

@ -504,3 +504,29 @@ block: # Constants
doAssert c == [1] and d == [2]
let (e, f) = v.unsafeGet()
doAssert e == [1] and f == [2]
block:
# withAssertOk evaluated as statement instead of expr
# https://github.com/nim-lang/Nim/issues/22216
func bug(): Result[uint16, string] =
ok(1234)
const
x = bug()
y = x.value()
doAssert y == 1234
when (NimMajor, NimMinor) >= (1,6):
# pre 1.6 nim vm have worse bug
static:
var z = bug()
z.value() = 15
let w = z.get()
doAssert w == 15
let
xx = bug()
yy = x.value()
doAssert yy == 1234