* fix unused result warning on implict return * describe `{.used.}` tradeoffs * oops
This commit is contained in:
parent
94ca0c3847
commit
0a6f5854a7
|
@ -175,9 +175,25 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
|
||||||
nnkElseExpr.newTree(
|
nnkElseExpr.newTree(
|
||||||
newStmtList(
|
newStmtList(
|
||||||
quote do: {.push warning[resultshadowed]: off.},
|
quote do: {.push warning[resultshadowed]: off.},
|
||||||
# var result: `baseType`
|
# var result {.used.}: `baseType`
|
||||||
nnkVarSection.newTree(
|
# In the proc body, result may or may not end up being used
|
||||||
nnkIdentDefs.newTree(ident "result", baseType, newEmptyNode())),
|
# depending on how the body is written - with implicit returns /
|
||||||
|
# expressions in particular, it is likely but not guaranteed that
|
||||||
|
# it is not used. Ideally, we would avoid emitting it in this
|
||||||
|
# case to avoid the default initializaiton. {.used.} typically
|
||||||
|
# works better than {.push.} which has a tendency to leak out of
|
||||||
|
# scope.
|
||||||
|
# TODO figure out if there's a way to detect `result` usage in
|
||||||
|
# the proc body _after_ template exapnsion, and therefore
|
||||||
|
# avoid creating this variable - one option is to create an
|
||||||
|
# addtional when branch witha fake `result` and check
|
||||||
|
# `compiles(procBody)` - this is not without cost though
|
||||||
|
nnkVarSection.newTree(nnkIdentDefs.newTree(
|
||||||
|
nnkPragmaExpr.newTree(
|
||||||
|
ident "result",
|
||||||
|
nnkPragma.newTree(ident "used")),
|
||||||
|
baseType, newEmptyNode())
|
||||||
|
),
|
||||||
quote do: {.pop.},
|
quote do: {.pop.},
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
|
@ -177,6 +177,10 @@ suite "Macro transformations test suite":
|
||||||
of false: await implicit7(v)
|
of false: await implicit7(v)
|
||||||
of true: 42
|
of true: 42
|
||||||
|
|
||||||
|
proc implicit9(): Future[int] {.async.} =
|
||||||
|
result = 42
|
||||||
|
result
|
||||||
|
|
||||||
let fin = new int
|
let fin = new int
|
||||||
check:
|
check:
|
||||||
waitFor(implicit()) == 42
|
waitFor(implicit()) == 42
|
||||||
|
@ -193,6 +197,8 @@ suite "Macro transformations test suite":
|
||||||
waitFor(implicit8(true)) == 42
|
waitFor(implicit8(true)) == 42
|
||||||
waitFor(implicit8(false)) == 33
|
waitFor(implicit8(false)) == 33
|
||||||
|
|
||||||
|
waitFor(implicit9()) == 42
|
||||||
|
|
||||||
suite "Closure iterator's exception transformation issues":
|
suite "Closure iterator's exception transformation issues":
|
||||||
test "Nested defer/finally not called on return":
|
test "Nested defer/finally not called on return":
|
||||||
# issue #288
|
# issue #288
|
||||||
|
|
Loading…
Reference in New Issue