fix #18964 Small string case with else statement first in AST evaluates wrongly
This commit is contained in:
parent
0448f30fd9
commit
8cfce70738
2 changed files with 18 additions and 1 deletions
|
|
@ -1116,7 +1116,10 @@ proc semCase(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil
|
||||||
popCaseContext(c)
|
popCaseContext(c)
|
||||||
closeScope(c)
|
closeScope(c)
|
||||||
return handleCaseStmtMacro(c, n, flags)
|
return handleCaseStmtMacro(c, n, flags)
|
||||||
|
template invalidOrderOfBranches(n: PNode) =
|
||||||
|
localError(c.config, n.info, "invalid order of case branches")
|
||||||
|
break
|
||||||
|
|
||||||
for i in 1..<n.len:
|
for i in 1..<n.len:
|
||||||
setCaseContextIdx(c, i)
|
setCaseContextIdx(c, i)
|
||||||
var x = n[i]
|
var x = n[i]
|
||||||
|
|
@ -1125,6 +1128,7 @@ proc semCase(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil
|
||||||
suggestEnum(c, x, caseTyp)
|
suggestEnum(c, x, caseTyp)
|
||||||
case x.kind
|
case x.kind
|
||||||
of nkOfBranch:
|
of nkOfBranch:
|
||||||
|
if hasElse: invalidOrderOfBranches(x)
|
||||||
checkMinSonsLen(x, 2, c.config)
|
checkMinSonsLen(x, 2, c.config)
|
||||||
semCaseBranch(c, n, x, i, covered)
|
semCaseBranch(c, n, x, i, covered)
|
||||||
var last = x.len-1
|
var last = x.len-1
|
||||||
|
|
@ -1132,6 +1136,7 @@ proc semCase(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil
|
||||||
typ = commonType(c, typ, x[last])
|
typ = commonType(c, typ, x[last])
|
||||||
expectedType = typ
|
expectedType = typ
|
||||||
of nkElifBranch:
|
of nkElifBranch:
|
||||||
|
if hasElse: invalidOrderOfBranches(x)
|
||||||
chckCovered = false
|
chckCovered = false
|
||||||
checkSonsLen(x, 2, c.config)
|
checkSonsLen(x, 2, c.config)
|
||||||
openScope(c)
|
openScope(c)
|
||||||
|
|
|
||||||
12
tests/casestmt/t18964.nim
Normal file
12
tests/casestmt/t18964.nim
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
discard """
|
||||||
|
errormsg: "invalid order of case branches"
|
||||||
|
"""
|
||||||
|
|
||||||
|
import macros
|
||||||
|
|
||||||
|
macro genCase(val: string): untyped =
|
||||||
|
result = nnkCaseStmt.newTree(val,
|
||||||
|
nnkElse.newTree(quote do: echo "else"),
|
||||||
|
nnkOfBranch.newTree(newLit("miauz"), quote do: echo "first branch"))
|
||||||
|
|
||||||
|
genCase("miauz")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue