mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 22:10:33 +00:00
Don't process a user pragma if its invalid (#23041)
When running `check`/`suggest` in a file with an invalid user pragma
like
```nim
{.pragma foo: test.}
```
It will continue to try and process it which leads to the compiler
running into a `FieldDefect`
```
fatal.nim(53) sysFatal
Error: unhandled exception: field 'sons' is not accessible for type 'TNode' using 'kind = nkIdent' [FieldDefect]
```
This makes it instead bail out trying to process the user pragma if its
invalid
This commit is contained in:
@@ -685,9 +685,12 @@ proc pragmaLine(c: PContext, n: PNode) =
|
||||
proc processPragma(c: PContext, n: PNode, i: int) =
|
||||
## Create and add a new custom pragma `{.pragma: name.}` node to the module's context.
|
||||
let it = n[i]
|
||||
if it.kind notin nkPragmaCallKinds and it.safeLen == 2: invalidPragma(c, n)
|
||||
if it.kind notin nkPragmaCallKinds and it.safeLen == 2:
|
||||
invalidPragma(c, n)
|
||||
return
|
||||
elif it.safeLen != 2 or it[0].kind != nkIdent or it[1].kind != nkIdent:
|
||||
invalidPragma(c, n)
|
||||
return
|
||||
|
||||
var userPragma = newSym(skTemplate, it[1].ident, c.idgen, c.module, it.info, c.config.options)
|
||||
styleCheckDef(c, userPragma)
|
||||
|
||||
9
tests/pragmas/tinvalid_user_pragma.nim
Normal file
9
tests/pragmas/tinvalid_user_pragma.nim
Normal file
@@ -0,0 +1,9 @@
|
||||
discard """
|
||||
cmd: "nim check $file"
|
||||
"""
|
||||
|
||||
{.pragma test: foo.} #[tt.Error
|
||||
^ invalid pragma: {.pragma, test: foo.} ]#
|
||||
|
||||
{.pragma: 1.} #[tt.Error
|
||||
^ invalid pragma: {.pragma: 1.} ]#
|
||||
Reference in New Issue
Block a user