[net.lang.c] Problems with typechecking enumerated types

tallman@dspo.UUCP (07/11/84)

In the process of writing a new C compiler for the NS32032,
I have been trying some unusual expressions using enums on the
Berkley 4.2 C compiler on a Vax 11/780.

For example:

enum color {red,orange,yellow,green,blue,indigo,violet};
enum color crayon,rainbow;

main() {
	int x[5];

	x[yellow] = 1;			/* causes "illegal type for +" error */
	crayon = red*blue;		/* no error or warning */
	crayon = red + blue;		/* no error or warning */
	rainbow = red + 1;		/* "enumeration type clash" warning */
	rainbow = orange << 1;		/* no error or warning */
	rainbow = orange & 1;		/* no error or warning */
	if (rainbow > orange) x[1]=2;	/* "illegal comparison of enums" */
	if (rainbow == orange) x[1]=2;	/* legal */
}

These results show the inconsistency in type checking enums - some 
bizzare operations are allowed without any warning (like enum * enum)
and some reasonable operations are not allowed (like array indexing).

Has a standard been developed for what operations should and should not
be allowed on enums?

henry@utzoo.UUCP (Henry Spencer) (07/13/84)

> These results show the inconsistency in type checking enums - some 
> bizzare operations are allowed without any warning (like enum * enum)
> and some reasonable operations are not allowed (like array indexing).
> 
> Has a standard been developed for what operations should and should not
> be allowed on enums?

The addendum page in the V7 C Reference Manual says, in part:

	Objects of a given enumeration type are regarded as having a
	type distinct from objects of all other types...

In other words, proper type-checking should probably bounce *all* your
examples.  If enums are distinct from all the integer types, for example,
then none of the arithmetic operations apply to them.  The problem is
that enums were sort of patched into some of the existing compilers, and
for that matter some of the existing compilers aren't too fussy about
type checking to begin with.  Don't confuse the eccentricities of the
existing compilers with the definition of the language.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry