mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-05 04:27:44 +00:00
fix #18964 Small string case with else statement first in AST evaluates wrongly
This commit is contained in:
@@ -1116,7 +1116,10 @@ proc semCase(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil
|
||||
popCaseContext(c)
|
||||
closeScope(c)
|
||||
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:
|
||||
setCaseContextIdx(c, i)
|
||||
var x = n[i]
|
||||
@@ -1125,6 +1128,7 @@ proc semCase(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil
|
||||
suggestEnum(c, x, caseTyp)
|
||||
case x.kind
|
||||
of nkOfBranch:
|
||||
if hasElse: invalidOrderOfBranches(x)
|
||||
checkMinSonsLen(x, 2, c.config)
|
||||
semCaseBranch(c, n, x, i, covered)
|
||||
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])
|
||||
expectedType = typ
|
||||
of nkElifBranch:
|
||||
if hasElse: invalidOrderOfBranches(x)
|
||||
chckCovered = false
|
||||
checkSonsLen(x, 2, c.config)
|
||||
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")
|
||||
Reference in New Issue
Block a user