Skip to contentSkip to conversation

case label types

DISCUSSION Morten Welinder 2 participants 2 messages Mar 16, 2006 Sparse checker

lore
Share & tools
lore
#0 lore raw

Shouldn't sparse complain over this kind of code, or does it have legitimate uses?

Specifically, enum constants from two different enums occur in the same switch.

Morten

-----------------------------------------------------------------------------

typedef enum { A1, A2 } enumA;
typedef enum { B1 = 10, B2 } enumB;

int
main (int argc, char **argv)
{
switch (A1) {
case A1: break;
case A2: break;
case B1: break;
case B2: break;
default: break;
}
return 0;
}

#1 lore raw
Quoted 3 lines · Morten Welinder

Shouldn't sparse complain over this kind of code, or does it have legitimate uses?

Hm.. Making sparse complain is probably a good idea.

Quoted 2 lines

Specifically, enum constants from two different enums occur in the same switch.

However, this is actually no different from

	typedef enum { A1, A2 } enumA;
	typedef enum { B1 = 10, B2 } enumB;

	..
	if (A1 == B1)
		..

and the lack of warning comes from the exact same cause. Indeed, these days (since commit 464710af, to be exact), the case type-checking shares the exact same logic - it does

	...
	if (compatible_restricted_binop(SPECIAL_EQUAL, &switch_expr, &case_expr))
		return;
	..

so it literally considers the case/switch expression equality testing to be 100% the same as the binop "==" test.

Oh, except there is a bug there: it _meant_ to check that they are both integer types, but it does that wrong:

	if (is_int_type(switch_type) && is_int_type(case_type))
		return;

which looks way too permissive. That should probably be testing for "compatible_integer_binop(switch_type, case_type)" instead.

I assume that in general we _do_ want the case/switch statements to have the same test as if you tested for equality explicitly.

Hmm?

Linus