diff --git a/stew/results.nim b/stew/results.nim index f8996cd..44a7f1e 100644 --- a/stew/results.nim +++ b/stew/results.nim @@ -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 diff --git a/tests/test_results.nim b/tests/test_results.nim index 2b12fd2..aa520b5 100644 --- a/tests/test_results.nim +++ b/tests/test_results.nim @@ -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