Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5981,9 +5981,10 @@ doswitch(void) {
constvalue *cse, *csp;
char labelname[sNAMEMAX + 1];
bool all_cases_return = true;
int switch_tag, case_tag;

endtok = matchtoken('(') ? ')' : tDO;
doexpr(TRUE, FALSE, FALSE, FALSE, NULL, NULL, TRUE); /* evaluate switch expression */
doexpr(TRUE, FALSE, FALSE, FALSE, &switch_tag, NULL, TRUE); /* evaluate switch expression */
needtoken(endtok);
/* generate the code for the switch statement, the label is the address
* of the case table (to be generated later).
Expand Down Expand Up @@ -6019,7 +6020,8 @@ doswitch(void) {
* parse all expressions until that special token.
*/

exprconst(&val, NULL, NULL);
exprconst(&val, &case_tag, NULL);
matchtag(switch_tag, case_tag, MATCHTAG_COERCE);
/* Search the insertion point (the table is kept in sorted order, so
* that advanced abstract machines can sift the case table with a
* binary search). Check for duplicate case values at the same time.
Expand Down
26 changes: 26 additions & 0 deletions tests/compile-only/warn-switch-tag-mismatch.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
enum Foo
{
Foo1,
Foo2,
Foo3
}

enum Bar
{
Bar1,
Bar2,
Bar3
}

public void OnPluginStart()
{
Foo whatever = Foo2;

switch (whatever)
{
case Bar1: whatever = Foo2;
case 3: whatever = Foo3;
case 7.4: whatever = Foo1;
case Foo3: whatever = Foo3;
}
}
3 changes: 3 additions & 0 deletions tests/compile-only/warn-switch-tag-mismatch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(21) : warning 213: tag mismatch
(22) : warning 213: tag mismatch
(23) : warning 213: tag mismatch