Bug 30357 - Enum typecast warning
Summary: Enum typecast warning
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2007-01-03 13:05 UTC by Marek Chovanec
Modified: 2021-09-16 17:50 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-11-21 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marek Chovanec 2007-01-03 13:05:07 UTC
I would appreciate adding the enumeration typecast warnings. I mean:
When I cast one enum to another and the first enum is not a subset of the second warning will be shown.

Example:

enum EParent
{
  EP_VAL0 = 0,
  EP_VAL1 = 1,
  EP_VAL2 = 2
};

enum EChild
{
  EC_VAL0 = 0,
  EC_VAL1 = 1
};

int main()
{
  EChild childValue;
  EParent parentValue;

  ...

  childValue = (EChild)parentValue; // Warning, because value 2 is not present in EChild.

  parentValue = (EParent)childValue; // Ok, because all EChild values are present in EParent.

  return 0;
};
Comment 1 Andrew Pinski 2007-01-03 16:46:08 UTC
Related to PR 12242.
Comment 2 Manuel López-Ibáñez 2007-01-21 18:08:38 UTC
I am not sure this is such a good idea. A casting typically means "I want to really do this". GCC normally suppress warnings when casting is added. A warning when you assign when enum type to another and the first enum is not a subset of the second could still be useful, but the casting should prevent the warning.

I am not sure what happens for such assignment, is it undefined behaviour? If so, perhaps it should be warned by Wundefined. If not, then perhaps we could add it to Wconversion.
Comment 3 Jonathan Wakely 2009-12-08 21:30:53 UTC
(In reply to comment #2)
> I am not sure this is such a good idea. A casting typically means "I want to
> really do this". GCC normally suppress warnings when casting is added. A
> warning when you assign when enum type to another and the first enum is not a
> subset of the second could still be useful, but the casting should prevent the
> warning.

That won't even compile without a cast, in C++ there is no implicit conversion to enumeration types.
 
> I am not sure what happens for such assignment, is it undefined behaviour?

If the source value is within the range of the target enum it's well-defined.  It might be reasonable to warn as part of -Wconversion except that usually applies to implicit conversions, and for enums the implicit conversion isn't allowed.

Comment 4 Eric Gallager 2018-02-20 09:29:33 UTC
(In reply to Andrew Pinski from comment #1)
> Related to PR 12242.

And also bug 67314 and bug 7654 (assignments without the cast), bug 78736 and bug 60591 (conversions), and bug 52763 (comparisons). Should probably be closed as a dup of one of them. Or maybe the other way around.
Comment 5 Eric Gallager 2019-02-21 17:38:23 UTC
(In reply to Eric Gallager from comment #4)
> (In reply to Andrew Pinski from comment #1)
> > Related to PR 12242.
> 
> And also bug 67314 and bug 7654 (assignments without the cast), bug 78736
> and bug 60591 (conversions), and bug 52763 (comparisons). Should probably be
> closed as a dup of one of them. Or maybe the other way around.

Any preferences as to which way to go?
Comment 6 Eric Gallager 2019-11-21 09:16:43 UTC
(In reply to Marek Chovanec from comment #0)
> I would appreciate adding the enumeration typecast warnings. I mean:
> When I cast one enum to another and the first enum is not a subset of the
> second warning will be shown.
> 
> Example:
> 
> enum EParent
> {
>   EP_VAL0 = 0,
>   EP_VAL1 = 1,
>   EP_VAL2 = 2
> };
> 
> enum EChild
> {
>   EC_VAL0 = 0,
>   EC_VAL1 = 1
> };
> 
> int main()
> {
>   EChild childValue;
>   EParent parentValue;
> 
>   ...
> 
>   childValue = (EChild)parentValue; // Warning, because value 2 is not
> present in EChild.
> 
>   parentValue = (EParent)childValue; // Ok, because all EChild values are
> present in EParent.
> 
>   return 0;
> };

At least as far as this particular testcase goes, there's a warning from -Wuninitialized at least... if you initialize parentValue to EP_VAL2 like I think you intended, I can confirm that there are no warnings then, even after -Wenum-conversion was added for bug 78736. Since that was for just C, though, and this is C++, I guess that would make this bug 92159? But that was closed as WORKSFORME though... I dunno, I'll just confirm this as separate for now...
Comment 7 Jonathan Wakely 2019-11-21 10:48:22 UTC
(In reply to Eric Gallager from comment #6)
> At least as far as this particular testcase goes, there's a warning from
> -Wuninitialized at least... if you initialize parentValue to EP_VAL2 like I
> think you intended, I can confirm that there are no warnings then, even
> after -Wenum-conversion was added for bug 78736. Since that was for just C,
> though, and this is C++, I guess that would make this bug 92159? But that
> was closed as WORKSFORME though...

Because as I said in comment 3 above, implicit conversions between distinct enum types is ill-formed in C++. You don't need a warning, because it's already an error.

I think this is just another case of a user not understanding how enums actually work in C++. Values of the enum type which do not correspond to an enumerator are still valid values of the type. Adding a warning for explicit conversions between distinct types doesn't seem useful (as Manu said, you should know what you're doing if you use an explicit cast to do conversions that aren't allowed implicitly).

My inclination is INVALID or WONTFIX.