fix #18964 Small string case with else statement first in AST evaluat… (#20862)

fix #18964 Small string case with else statement first in AST evaluates wrongly
This commit is contained in:
Bung
2022-11-23 03:08:17 +08:00
committed by GitHub
parent 0448f30fd9
commit 8cfce70738
2 changed files with 18 additions and 1 deletions

View File

@@ -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
View 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")