Skip to content

Commit e1c26a9

Browse files
committed
Strict Future value / error access
Strict future access is now always enabled, making it a panic to access value/error when the Future is still pending - `read` and `readError` raise catchable exceptions instead which may be preferable to use. * remove `chronosStrictFutureAccess` enabling its functionality by default * disallow setting a cancellation callback on a non-pending future
1 parent 1306170 commit e1c26a9

File tree

3 files changed

+14
-25
lines changed

3 files changed

+14
-25
lines changed

chronos/config.nim

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
## in transition periods leading up to a breaking release that starts enforcing
55
## them and removes the option.
66
##
7-
## `chronosPreviewV4` is a preview flag to enable v4 semantics - in particular,
8-
## it enables strict exception checking and disables parts of the deprecated
9-
## API and other changes being prepared for the upcoming release
10-
##
117
## `chronosDebug` can be defined to enable several debugging helpers that come
128
## with a runtime cost - it is recommeneded to not enable these in production
139
## code.
@@ -26,8 +22,6 @@ const
2622
##
2723
## `Exception` handling may be removed in future chronos versions.
2824

29-
chronosStrictFutureAccess* {.booldefine.}: bool = defined(chronosPreviewV4)
30-
3125
chronosStackTrace* {.booldefine.}: bool = defined(chronosDebug)
3226
## Include stack traces in futures for creation and completion points
3327

chronos/futures.nim

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,31 +209,27 @@ func value*[T: not void](future: Future[T]): lent T =
209209
##
210210
## See `read` for a version that raises a catchable error when future
211211
## has not completed.
212-
when chronosStrictFutureAccess:
213-
if not future.completed():
214-
raiseFutureDefect("Future not completed while accessing value", future)
212+
if not future.completed():
213+
raiseFutureDefect("Future not completed while accessing value", future)
215214

216215
future.internalValue
217216

218217
func value*(future: Future[void]) =
219-
## Return the value in a completed future - raises Defect when
220-
## `fut.completed()` is `false`.
218+
## Raises Defect when `fut.completed()` is `false`.
221219
##
222220
## See `read` for a version that raises a catchable error when future
223221
## has not completed.
224-
when chronosStrictFutureAccess:
225-
if not future.completed():
226-
raiseFutureDefect("Future not completed while accessing value", future)
222+
if not future.completed():
223+
raiseFutureDefect("Future not completed while accessing value", future)
227224

228225
func error*(future: FutureBase): ref CatchableError =
229226
## Return the error of `future`, or `nil` if future did not fail.
230227
##
231228
## See `readError` for a version that raises a catchable error when the
232229
## future has not failed.
233-
when chronosStrictFutureAccess:
234-
if not future.failed() and not future.cancelled():
235-
raiseFutureDefect(
236-
"Future not failed/cancelled while accessing error", future)
230+
if not future.failed() and not future.cancelled():
231+
raiseFutureDefect(
232+
"Future not failed/cancelled while accessing error", future)
237233

238234
future.internalError
239235

chronos/internal/asyncfutures.nim

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,9 @@ proc tryCancel(future: FutureBase, loc: ptr SrcLoc): bool =
283283
if not(isNil(future.internalChild)):
284284
# If you hit this assertion, you should have used the `CancelledError`
285285
# mechanism and/or use a regular `addCallback`
286-
when chronosStrictFutureAccess:
287-
doAssert future.internalCancelcb.isNil,
288-
"futures returned from `{.async.}` functions must not use " &
289-
"`cancelCallback`"
286+
doAssert future.internalCancelcb.isNil,
287+
"futures returned from `{.async.}` functions must not use " &
288+
"`cancelCallback`"
290289
tryCancel(future.internalChild, loc)
291290
else:
292291
if not(isNil(future.internalCancelcb)):
@@ -353,9 +352,9 @@ proc `cancelCallback=`*(future: FutureBase, cb: CallbackFunc) =
353352
## This callback will be called immediately as ``future.cancel()`` invoked and
354353
## must be set before future is finished.
355354

356-
when chronosStrictFutureAccess:
357-
doAssert not future.finished(),
358-
"cancellation callback must be set before finishing the future"
355+
doAssert not future.finished(),
356+
"cancellation callback must be set before finishing the future"
357+
359358
future.internalCancelcb = cb
360359

361360
{.push stackTrace: off.}

0 commit comments

Comments
 (0)