diff options
author | Bung <crc32@qq.com> | 2022-11-23 03:08:17 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-22 20:08:17 +0100 |
commit | 8cfce70738de2d488b621e6936e6398bfbe0a3ce (patch) | |
tree | 5e99bfa8de4c107c44e6c3d7e0cb8a67786a2ff4 | |
parent | 0448f30fd9c2e53084ee8b284f6fa24684661bc3 (diff) | |
download | Nim-8cfce70738de2d488b621e6936e6398bfbe0a3ce.tar.gz |
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
-rw-r--r-- | compiler/semstmts.nim | 7 | ||||
-rw-r--r-- | tests/casestmt/t18964.nim | 12 |
2 files changed, 18 insertions, 1 deletions
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index ab8c23ba4..f2434db87 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -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) diff --git a/tests/casestmt/t18964.nim b/tests/casestmt/t18964.nim new file mode 100644 index 000000000..1d2de2bbc --- /dev/null +++ b/tests/casestmt/t18964.nim @@ -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") |