Skip to content

Commit

Permalink
make binary not not parse complex expressions on right side (#22078)
Browse files Browse the repository at this point in the history
* binary `not` only parses simple expressions

fixes #16324

* switch to primary
  • Loading branch information
metagn authored Jun 12, 2023
1 parent 7b1c448 commit e0ad71a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
8 changes: 4 additions & 4 deletions compiler/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ proc binaryNot(p: var Parser; a: PNode): PNode =
let notOpr = newIdentNodeP(p.tok.ident, p)
getTok(p)
optInd(p, notOpr)
let b = parseExpr(p)
let b = primary(p, pmTypeDesc)
result = newNodeP(nkInfix, p)
result.add notOpr
result.add a
Expand All @@ -1407,8 +1407,8 @@ proc binaryNot(p: var Parser; a: PNode): PNode =
proc parseTypeDesc(p: var Parser, fullExpr = false): PNode =
#| rawTypeDesc = (tupleType | routineType | 'enum' | 'object' |
#| ('var' | 'out' | 'ref' | 'ptr' | 'distinct') typeDesc?)
#| ('not' expr)?
#| typeDescExpr = (routineType / simpleExpr) ('not' expr)?
#| ('not' primary)?
#| typeDescExpr = (routineType / simpleExpr) ('not' primary)?
#| typeDesc = rawTypeDesc / typeDescExpr
newlineWasSplitting(p)
if fullExpr:
Expand Down Expand Up @@ -1445,7 +1445,7 @@ proc parseTypeDefValue(p: var Parser): PNode =
#| typeDefValue = ((tupleDecl | enumDecl | objectDecl | conceptDecl |
#| ('ref' | 'ptr' | 'distinct') (tupleDecl | objectDecl))
#| / (simpleExpr (exprEqExpr ^+ comma postExprBlocks?)?))
#| ('not' expr)?
#| ('not' primary)?
case p.tok.tokType
of tkTuple: result = parseTuple(p, true)
of tkRef: result = parseTypeDescKAux(p, nkRefTy, pmTypeDef)
Expand Down
6 changes: 3 additions & 3 deletions doc/grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ primary = simplePrimary (commandStart expr (doBlock extraPostExprBlock*)?)?
/ prefixOperator primary
rawTypeDesc = (tupleType | routineType | 'enum' | 'object' |
('var' | 'out' | 'ref' | 'ptr' | 'distinct') typeDesc?)
('not' expr)?
typeDescExpr = (routineType / simpleExpr) ('not' expr)?
('not' primary)?
typeDescExpr = (routineType / simpleExpr) ('not' primary)?
typeDesc = rawTypeDesc / typeDescExpr
typeDefValue = ((tupleDecl | enumDecl | objectDecl | conceptDecl |
('ref' | 'ptr' | 'distinct') (tupleDecl | objectDecl))
/ (simpleExpr (exprEqExpr ^+ comma postExprBlocks?)?))
('not' expr)?
('not' primary)?
extraPostExprBlock = ( IND{=} doBlock
| IND{=} 'of' exprList ':' stmt
| IND{=} 'elif' expr ':' stmt
Expand Down
18 changes: 18 additions & 0 deletions tests/notnil/tparse.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# issue #16324

{.push experimental: "notnil".}

block:
type Foo = ref object
value: int

proc newFoo1(): Foo not nil = # This compiles
return Foo(value: 1)

proc newFoo2(): Foo not nil {.inline.} = # This does not
return Foo(value: 1)

doAssert newFoo1().value == 1
doAssert newFoo2().value == 1

{.pop.}

0 comments on commit e0ad71a

Please sign in to comment.