Skip to content

Commit

Permalink
fix(sem): crash on errors in deref expressions (#955)
Browse files Browse the repository at this point in the history
## Summary

Fix the compiler crashing when the target sub-expression of a
dereference expressions (`a[]`) is erroneous.

## Details

The error propagation in `semDerefEval` correctly detected that the
target expression is erroneous, but didn't update the `result` with the
erroneous tree prior to wrapping `result` in an error. Since
`wrapError` requires an `nkError` to be located somewhere in the
wrapped tree, this triggered an assertion failure.

In addition, errors during folding (`getConstExpr` returning an
`nkError`) weren't accounted for. This is fixed by testing
`semmedTarget` (not `derefTarget`) for being erroneous.
  • Loading branch information
zerbina authored Oct 12, 2023
1 parent e62b836 commit d608d8b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
5 changes: 3 additions & 2 deletions compiler/sem/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1948,11 +1948,12 @@ proc semDeref(c: PContext, n: PNode): PNode =
else:
nil # xxx: should probably be an error type; derefing a non-ptr/ref

case derefTarget.kind
result[0] = semmedTarget

case semmedTarget.kind
of nkError:
result = c.config.wrapError(result)
else:
result[0] = semmedTarget
result.typ = derefType

if isTargetConstNil:
Expand Down
5 changes: 4 additions & 1 deletion tests/errmsgs/tnested_errors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ action: reject
nimout: '''
tnested_errors.nim(15, 12) Error: undeclared identifier: 'b'
tnested_errors.nim(16, 11) Error: undeclared identifier: 'b'
tnested_errors.nim(21, 9) Error: undeclared identifier: 'b'
'''
"""





var a = if b:
b
else:
1

# errors in the target expression of dereference expressions were lost
discard b[]

0 comments on commit d608d8b

Please sign in to comment.