Skip to content

Commit

Permalink
fix: in the case of overlapping values in a Switch (ie Python match-c…
Browse files Browse the repository at this point in the history
…ase), mark subsequent cases as unreachable to prevent a critical error from occurring ("Attempted to add a (non-entry) block with no predecessors")
  • Loading branch information
achidlow committed Jul 9, 2024
1 parent 19ccebd commit f21efc1
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/puya/ir/builder/flow_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,17 @@ def handle_switch(context: IRFunctionBuildContext, statement: awst_nodes.Switch)
ir_blocks = dict[awst_nodes.Block, BasicBlock]()
for value, block in statement.cases.items():
ir_value = context.visitor.visit_and_materialise_single(value)
case_blocks[ir_value] = lazy_setdefault(
ir_blocks,
block,
lambda b: BasicBlock(
source_location=b.source_location,
comment=b.description or f"switch_case_{len(ir_blocks)}",
),
)
if ir_value in case_blocks:
logger.error("code block is unreachable", location=block.source_location)
else:
case_blocks[ir_value] = lazy_setdefault(
ir_blocks,
block,
lambda b: BasicBlock(
source_location=b.source_location,
comment=b.description or f"switch_case_{len(ir_blocks)}",
),
)
default_block, next_block = mkblocks(
statement.source_location,
(statement.default_case and statement.default_case.description) or "switch_case_default",
Expand Down

0 comments on commit f21efc1

Please sign in to comment.