mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 08:54:53 +00:00
make binary not not parse complex expressions on right side (#22078)
* binary `not` only parses simple expressions fixes #16324 * switch to primary
This commit is contained in:
@@ -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
|
||||
@@ -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:
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
18
tests/notnil/tparse.nim
Normal file
18
tests/notnil/tparse.nim
Normal file
@@ -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.}
|
||||
Reference in New Issue
Block a user