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.
macros: disallow creating special nodes manually (nim-lang#1235)
## Summary Attempting to manually create a node (via `newNimNode`) of kind `nnkError`, `nnkIdent`, `nnkSym`, or `nnkType` now results in a compiler error. Creating them was previously allowed, but the compiler could crash when such incomplete nodes appear in macro output. ## Details * all node kinds where the initial state is not valid (`nkError`, `nkIdent`, `nkSym`, and `nkType`) are forbidden * the `opcNNewNimNode` implementation guards against the node kinds * a VM event, diagnostic, and report is added for the error * to not introduce a new variant, the `msg` variant is re-used for the new VM event * the untyped `nnkType` test case is removed from `tempty_type_nodes`; macros cannot create untyped `nnkType` nodes anymore
- Loading branch information
Showing
9 changed files
with
55 additions
and
17 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
26 changes: 26 additions & 0 deletions
26
tests/lang_callable/macros/tdisallow_create_special_nodes.nim
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,26 @@ | ||
discard """ | ||
description: ''' | ||
Ensure that an error is reported when attempting to create special | ||
atom nodes with `newNimNode` | ||
''' | ||
matrix: "--errorMax:4" | ||
action: reject | ||
""" | ||
|
||
import std/macros | ||
|
||
static: | ||
discard newNimNode(nnkError) #[tt.Error | ||
^ cannot manually create a node of kind: nnkError]# | ||
|
||
static: | ||
discard newNimNode(nnkIdent) #[tt.Error | ||
^ cannot manually create a node of kind: nnkIdent]# | ||
|
||
static: | ||
discard newNimNode(nnkSym) #[tt.Error | ||
^ cannot manually create a node of kind: nnkSym]# | ||
|
||
static: | ||
discard newNimNode(nnkType) #[tt.Error | ||
^ cannot manually create a node of kind: nnkType]# |
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