Bug 82307 - unscoped enum-base incorrect cast
Summary: unscoped enum-base incorrect cast
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.4.0
: P3 normal
Target Milestone: 8.0
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2017-09-23 12:17 UTC by Maxim
Modified: 2021-10-18 21:08 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 7.2.0
Last reconfirmed: 2017-09-25 00:00:00


Attachments
The code have a bug (141 bytes, text/plain)
2017-09-23 12:17 UTC, Maxim
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Maxim 2017-09-23 12:17:33 UTC
Created attachment 42228 [details]
The code have a bug

Hello to GCC ( g++ ) developers!
My English is bad, but i hope you will understand me.

I found a bug ( if it is ) in g++ 6.4.0 version ( and earlier ).
I will just provide a code which have the bug:

/* Begin */
#include <iostream>

enum : unsigned long long { VAL };


void foo( unsigned long long )
{
    std::cout << "unsigned long long" << std::endl;
}

void foo( int )
{
    std::cout << "int" << std::endl;
}


int main( void )
{
    foo( VAL );
}
/* End */

I compiled this code in a Unix-like environment and command-line interface for Microsoft Windows -- Cygwin ( 64-bit ). I used following compiler options:
-Wall -Wextra -std=c++17 <path_to_file.cpp>

The following error messages were displayed:
C:\Users\Maxim\Desktop\test.cpp: In function ‘int main()’:
C:\Users\Maxim\Desktop\test.cpp:19:14: error: call of overloaded ‘foo(<anonymous enum>)’ is ambiguous
     foo( VAL );
              ^
C:\Users\Maxim\Desktop\test.cpp:6:6: note: candidate: void foo(long long unsigned int)
 void foo( unsigned long long )
      ^~~
C:\Users\Maxim\Desktop\test.cpp:11:6: note: candidate: void foo(int)
 void foo( int )
      ^~~

----
Thank you.
Comment 1 Richard Biener 2017-09-25 08:03:53 UTC
clang accepts it.
Comment 2 Maxim 2017-09-25 21:53:07 UTC
(In reply to Richard Biener from comment #1)
> clang accepts it.

Yes, I know. I would like to know if the g++ is deviating from the standard, that nothing is written about it, or will it be fixed?
Comment 3 Jonathan Wakely 2017-09-26 09:58:03 UTC
The bug status was changed to NEW, that means it's been confirmed as a GCC bug that should be fixed.

Richard was noting for other people's benefit that Clang accepts the code.
Comment 4 Mukesh Kapoor 2017-10-05 17:01:41 UTC
This seems to be a problem in integral promotion in function
type_promotes_to(). The following change fixes the bug:

--- gcc/cp/cvt.c        (revision 253270)
+++ gcc/cp/cvt.c        (working copy)
@@ -1854,11 +1854,15 @@
       tree prom = type;
       if (TREE_CODE (prom) == ENUMERAL_TYPE)
        prom = ENUM_UNDERLYING_TYPE (prom);
-      if (TYPE_UNSIGNED (prom)
-         && ! int_fits_type_p (TYPE_MAX_VALUE (prom), totype))
-       prom = c_common_type_for_size (precision, 1);
-      else
-       prom = totype;
+      // If enum has fixed underlying type, use that type (bug 82307)
+      if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
+       {
+         if (TYPE_UNSIGNED (prom)
+             && ! int_fits_type_p (TYPE_MAX_VALUE (prom), totype))
+           prom = c_common_type_for_size (precision, 1);
+         else
+           prom = totype;
+       }
       if (SCOPED_ENUM_P (type))
        {
          if (abi_version_crosses (6)
Comment 5 Paolo Carlini 2017-10-11 09:20:15 UTC
Mukesh is on it.
Comment 6 Mukesh Kapoor 2017-10-18 20:11:38 UTC
Submitted fix: https://gcc.gnu.org/ml/gcc-patches/2017-10/msg00524.html
Comment 7 paolo@gcc.gnu.org 2017-10-24 13:49:44 UTC
Author: paolo
Date: Tue Oct 24 13:49:13 2017
New Revision: 254046

URL: https://gcc.gnu.org/viewcvs?rev=254046&root=gcc&view=rev
Log:
/cp
2017-10-24  Mukesh Kapoor  <mukesh.kapoor@oracle.com>
	    Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/82307
	* cvt.c (type_promotes_to): Implement C++17, 7.6/4, about unscoped
	enumeration type whose underlying type is fixed.

/testsuite
2017-10-24  Mukesh Kapoor  <mukesh.kapoor@oracle.com>
	    Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/82307
	* g++.dg/cpp0x/enum35.C: New.
	* g++.dg/cpp0x/enum36.C: Likewise.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/enum35.C
    trunk/gcc/testsuite/g++.dg/cpp0x/enum36.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cvt.c
    trunk/gcc/testsuite/ChangeLog
Comment 8 Paolo Carlini 2017-10-24 13:52:26 UTC
Fixed.