forked from nim-lang/Nim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow replacing captured syms in macro calls in generics
fixes nim-lang#22605, separated from nim-lang#22744
- Loading branch information
Showing
4 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
block: # issue #22605, normal call syntax | ||
const error = "bad" | ||
|
||
template valueOr(self: int, def: untyped): untyped = | ||
case false | ||
of true: "" | ||
of false: | ||
template error: untyped {.used, inject.} = "good" | ||
def | ||
|
||
proc g(T: type): string = | ||
let x = valueOr 123: | ||
return $error | ||
|
||
"ok" | ||
|
||
doAssert g(int) == "good" | ||
|
||
block: # issue #22605, method call syntax | ||
const error = "bad" | ||
|
||
template valueOr(self: int, def: untyped): untyped = | ||
case false | ||
of true: "" | ||
of false: | ||
template error: untyped {.used, inject.} = "good" | ||
def | ||
|
||
proc g(T: type): string = | ||
let x = 123.valueOr: | ||
return $error | ||
|
||
"ok" | ||
|
||
doAssert g(int) == "good" | ||
|
||
block: # issue #22605, original complex example | ||
type Xxx = enum | ||
error | ||
value | ||
|
||
type | ||
Result[T, E] = object | ||
when T is void: | ||
when E is void: | ||
oResultPrivate*: bool | ||
else: | ||
case oResultPrivate*: bool | ||
of false: | ||
eResultPrivate*: E | ||
of true: | ||
discard | ||
else: | ||
when E is void: | ||
case oResultPrivate*: bool | ||
of false: | ||
discard | ||
of true: | ||
vResultPrivate*: T | ||
else: | ||
case oResultPrivate*: bool | ||
of false: | ||
eResultPrivate*: E | ||
of true: | ||
vResultPrivate*: T | ||
|
||
template valueOr[T: not void, E](self: Result[T, E], def: untyped): untyped = | ||
let s = (self) # TODO avoid copy | ||
case s.oResultPrivate | ||
of true: | ||
s.vResultPrivate | ||
of false: | ||
when E isnot void: | ||
template error: untyped {.used, inject.} = s.eResultPrivate | ||
def | ||
|
||
proc f(): Result[int, cstring] = | ||
Result[int, cstring](oResultPrivate: false, eResultPrivate: "f") | ||
|
||
proc g(T: type): string = | ||
let x = f().valueOr: | ||
return $error | ||
|
||
"ok" | ||
|
||
doAssert g(int) == "f" |