-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add switch, warning, and
bind
support for new generic injection beh…
…avior (#23102) refs #23091, especially post merge comments Unsure if `experimental` and `bind` are the perfect constructs to use but they seem to get the job done here. Symbol nodes do not get marked `nfOpenSym` if the `bind` statement is used for their symbol, and `nfOpenSym` nodes do not get replaced by new local symbols if the experimental switch is not enabled in the local context (meaning it also works with `push experimental`). However this incurs a warning as the fact that the node is marked `nfOpenSym` means we did not `bind` it, so we might want to do that or turn on the experimental switch if we didn't intend to bind it. The experimental switch name is arbitrary and could be changed. --------- Co-authored-by: Andreas Rumpf <[email protected]>
- Loading branch information
Showing
8 changed files
with
176 additions
and
6 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
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,50 @@ | ||
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 #[tt.Warning | ||
^ a new symbol 'error' has been injected during instantiation of g, however 'error' [enumField declared in tmacroinjectedsymwarning.nim(2, 3)] captured at the proc declaration will be used instead; either enable --experimental:genericsOpenSym to use the injected symbol or `bind` this captured symbol explicitly [GenericsIgnoredInjection]]# | ||
|
||
"ok" | ||
|
||
discard g(int) |