> -----Original Message----- > From: David Brown [mailto:da...@westcontrol.com] > Sent: 19 March 2014 14:44 > To: Paulo Matos; gcc@gcc.gnu.org > Subject: Re: returning short-enum and truncate doesn't trigger > conversion warning > > On 19/03/14 15:33, Paulo Matos wrote: > > Hi all, > > > > This is either a C standard subtlety or a bug in GCC. > > This is perfectly normal behaviour for C, and not a bug. It is also a > topic for the gcc help list, rather than the development list. >
Apologies, but I submitted this to gcc devel because I do think it is a bug. > > > > enum xpto > > { > > A = 0, > > B = 1, > > X = 512 > > }; > > > > extern void print (unsigned int); > > > > unsigned char bar (enum xpto a) > > { > > print (sizeof (unsigned char)); > > print (sizeof (enum xpto)); > > return a; > > } > > The sizeof operator returns an integer of type size_t (typically the > same as unsigned int or unsigned long, depending on the platform > details). But the compiler can see that the particular values in > question - 1 and 2 - can be converted to unsigned int without loss of > precision of changing the value. Therefore no warning is giving. > 512 cannot be converted to an unsigned char without loss of precision. The prints are actually not relevant for the question. They were just used for me to confirm the sizes of the types. enum xpto { A = 0, B = 1, X = 512 }; extern void print (unsigned int); unsigned char bar (enum xpto a) { return a; } Variable a could be 0, 1 or 512. In case of the latter there is loss of precision but still no warning. > > > > $ ~/work/tmp/GCC/builds/gcc-trunk/gcc/cc1 -O2 test.c -Wall > > -Wconversion --short-enums -quiet > > test.c: In function 'foo': > > test.c:3:3: warning: conversion to 'short unsigned int' from > 'unsigned int' may alter its value [-Wconversion] > > return a; > > ^ > > > > I was expecting a warning for bar as well since sizeof unsigned char > is 1 and sizeof enum xpto is 2, therefore the value is truncated but > no warning is issued. > > > > Shall I open a PR? > > > > Paulo Matos > > > >