Skip to content

Commit

Permalink
[mycpp] Fix check for missing default block
Browse files Browse the repository at this point in the history
It only happens in str_switch().

In contrast, tagswitch() are often exhaustive.
  • Loading branch information
Andy C committed Mar 11, 2024
1 parent 6efd0db commit e56b698
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
15 changes: 13 additions & 2 deletions mycpp/cppgen_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -1974,8 +1974,9 @@ def _write_cases(self, switch_expr, cases, default_block):
# an error occurred
return
if default_block is False:
self.report_error(switch_expr,
'switch got no else: for default block')
# This is too restrictive
#self.report_error(switch_expr,
# 'switch got no else: for default block')
return

self.def_write_ind('default: ')
Expand Down Expand Up @@ -2056,6 +2057,8 @@ def _write_str_switch(self, expr, o):
"""Write a switch statement over strings."""
assert len(expr.args) == 1, expr.args

switch_expr = expr # for later error

switch_var = expr.args[0]
if not isinstance(switch_var, NameExpr):
self.report_error(
Expand Down Expand Up @@ -2104,6 +2107,14 @@ def _write_str_switch(self, expr, o):
self.def_write_ind('}\n')
self.def_write_ind(' break;\n')

if default_block is None:
# an error occurred
return
if default_block is False:
self.report_error(switch_expr,
'str_switch got no else: for default block')
return

self.def_write('\n')
self.def_write_ind('str_switch_default:\n')
self.def_write_ind('default: ')
Expand Down
7 changes: 4 additions & 3 deletions mycpp/examples/invalid_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
def NoDefault():
# type: () -> None

with switch(42) as case:
if case(42):
print('42')
s = "foo"
with str_switch(s) as case:
if case("bar"):
print('bar')


def TagSwitch():
Expand Down

0 comments on commit e56b698

Please sign in to comment.