Skip to content

Commit

Permalink
fix(sem): crash due to ill-formed if AST (#933)
Browse files Browse the repository at this point in the history
## Summary

Fix the compiler crashing when encountering ill-formed `nkIfStmt`/
`nkIfExpr` AST and the maximum error count is larger than 1.

## Details

* in `semIf`, create an error node for the ill-formed branch instead of
  creating and reporting a `Report`
* this enables proper `nkError` propagation, which disables the second
  pass, preventing the invalid AST from being indexed into (this caused
  either an `IndexDefect` or `FieldDefect`)
* in addition, this also removes another use of immediate reporting via
  `handleReport`/`localReport`

---------

Co-authored-by: zerbina <100542850+zerbina@users.noreply.github.com>
  • Loading branch information
bung87 and zerbina authored Oct 1, 2023
1 parent bacb678 commit 0e09df4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 2 additions & 3 deletions compiler/sem/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,8 @@ proc semIf(c: PContext, n: PNode; flags: TExprFlags): PNode =
if it[0].isError:
hasError = true
else:
semReportIllformedAst(
c.config, it,
"Expected one or two subnodes for if statement, but found " & $it.len)
hasError = true
result[i] = c.config.newError(it, PAstDiag(kind: adSemIllformedAst))

if hasError:
result = c.config.wrapError(result)
Expand Down
17 changes: 17 additions & 0 deletions tests/lang_callable/macros/tif_ill_formed.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
discard """
description: '''
Ensure that ill-formed if-statement AST created by macros is detected
'''
cmd: "nim check --hints:off $options $file"
nimoutfull: "true"
action: reject
"""
import std/macros

macro t1(): untyped =
result =
nnkIfStmt.newTree(
newNimNode(nnkElseExpr)) #[tt.Error
^ illformed AST: else: <<0th child missing for nkElseExpr >>]#

t1()

0 comments on commit 0e09df4

Please sign in to comment.