feat(match): 重名 case 标签 fail-fast
HandlesMatch._HandleMatchLlvm 新增前置检查(在产出任何 IR 之前):新增 _CaseLabelKey 提取 case 的 switch 标签(MatchValue 值 / MatchSingleton(None) / MatchClass 变体名 / MatchOr 组合),标签重复或出现多个 `case _` 时报 FailFastError, 并给出重复的 case 序号与标签键(C switch 不允许重复 case 标签)。
This commit is contained in:
1 parent
4353efd8ff
commit
8b6c36d24f
1 file changed
+21
@@ -36,6 +36,27 @@ class MatchHandle(BaseHandle):
|
||||
|
||||
def _HandleMatchLlvm(self, Node: ast.Match) -> None:
|
||||
Gen: "Translator.LlvmGen" = self.Trans.LlvmGen
|
||||
# 重名 case 检测(fail-fast):C 的 switch 不允许重复 case 标签——重复标签会让
|
||||
# 某个臂永远不可达,且通常意味着漏改的分支;`case _`(default)同样只允许一个。
|
||||
# 在产出任何 IR 之前检查,保证报错即终止。
|
||||
SeenLabels: dict[str, int] = {}
|
||||
DefaultCount: int = 0
|
||||
for ci, case in enumerate(Node.cases):
|
||||
Key: str | None = MatchHandle._CaseLabelKey(case.pattern)
|
||||
if Key is None:
|
||||
if isinstance(case.pattern, ast.MatchAs) and case.pattern.pattern is None:
|
||||
DefaultCount += 1
|
||||
continue
|
||||
if Key in SeenLabels:
|
||||
from lib.core.LLVMCG.FuncGen import FailFastError
|
||||
raise FailFastError(
|
||||
f"match 出现重名 case:标签 '{Key}' 已在第 {SeenLabels[Key]} 个 case 使用,"
|
||||
f"第 {ci} 个 case 重复(C switch 不允许重复 case 标签)")
|
||||
SeenLabels[Key] = ci
|
||||
if DefaultCount > 1:
|
||||
from lib.core.LLVMCG.FuncGen import FailFastError
|
||||
raise FailFastError(
|
||||
f"match 出现 {DefaultCount} 个 `case _`(default 只允许一个)")
|
||||
SubjectVal: ir.Value | None = self.HandleExprLlvm(Node.subject)
|
||||
if not SubjectVal:
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user