Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make binary not not parse complex expressions on right side #22078

Merged
merged 2 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.}