public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++ Patch] PR 82307
@ 2017-10-09 19:28 Mukesh Kapoor
  2017-10-18 16:52 ` [PING] " Mukesh Kapoor
  0 siblings, 1 reply; 6+ messages in thread
From: Mukesh Kapoor @ 2017-10-09 19:28 UTC (permalink / raw)
  To: gcc-patches; +Cc: nathan

[-- Attachment #1: Type: text/plain, Size: 429 bytes --]

Hi,

This patch fixes https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82307.
For an unscoped enum with a fixed underlying type, the function 
type_promotes_to() does not always return the same type as the 
underlying type. The fix is to use the underlying type of the enum 
instead of creating a new one by calling c_common_type_for_size().

Bootstrapped and tested with 'make check' on x86_64-linux. New test case 
added.

Mukesh


[-- Attachment #2: patch_82307 --]
[-- Type: text/plain, Size: 1526 bytes --]

Index: gcc/cp/cvt.c
===================================================================
--- gcc/cp/cvt.c	(revision 253551)
+++ gcc/cp/cvt.c	(working copy)
@@ -1854,11 +1854,16 @@
       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 an unscoped enum has fixed underlying type,
+      // use that type (bug 82307)
+      if (!ENUM_FIXED_UNDERLYING_TYPE_P (type) || SCOPED_ENUM_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)
Index: gcc/testsuite/g++.dg/cpp0x/pr_82307.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/pr_82307.C	(revision 0)
+++ gcc/testsuite/g++.dg/cpp0x/pr_82307.C	(working copy)
@@ -0,0 +1,24 @@
+// PR c++/82307
+// { dg-do compile { target c++11 } }
+
+#include <cstdlib>
+#include <cstring>
+
+enum : unsigned long long { VAL };
+
+const char* foo( unsigned long long )
+{
+  return "unsigned long long";
+}
+
+const char* foo( int )
+{
+  return "int";
+}
+
+int main( void )
+{
+  if (strcmp(foo(VAL), "unsigned long long") != 0)
+    abort();
+}
+

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PING] [C++ Patch] PR 82307
  2017-10-09 19:28 [C++ Patch] PR 82307 Mukesh Kapoor
@ 2017-10-18 16:52 ` Mukesh Kapoor
  2017-10-18 20:25   ` Nathan Sidwell
  0 siblings, 1 reply; 6+ messages in thread
From: Mukesh Kapoor @ 2017-10-18 16:52 UTC (permalink / raw)
  To: gcc-patches; +Cc: nathan

On 10/9/2017 12:20 PM, Mukesh Kapoor wrote:
> Hi,
>
> This patch fixes https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82307.
> For an unscoped enum with a fixed underlying type, the function 
> type_promotes_to() does not always return the same type as the 
> underlying type. The fix is to use the underlying type of the enum 
> instead of creating a new one by calling c_common_type_for_size().
>
> Bootstrapped and tested with 'make check' on x86_64-linux. New test 
> case added.
>
> Mukesh
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PING] [C++ Patch] PR 82307
  2017-10-18 16:52 ` [PING] " Mukesh Kapoor
@ 2017-10-18 20:25   ` Nathan Sidwell
  2017-10-18 20:40     ` Mukesh Kapoor
  0 siblings, 1 reply; 6+ messages in thread
From: Nathan Sidwell @ 2017-10-18 20:25 UTC (permalink / raw)
  To: Mukesh Kapoor, gcc-patches

On 10/18/2017 12:17 PM, Mukesh Kapoor wrote:
> On 10/9/2017 12:20 PM, Mukesh Kapoor wrote:
>> Hi,
>>
>> This patch fixes https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82307.
>> For an unscoped enum with a fixed underlying type, the function 
>> type_promotes_to() does not always return the same type as the 
>> underlying type. The fix is to use the underlying type of the enum 
>> instead of creating a new one by calling c_common_type_for_size().


The diff looks wrong.  Just before the changed piece it (attempts to) 
deal with enum types.  Why is that failing?

nathan

-- 
Nathan Sidwell

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PING] [C++ Patch] PR 82307
  2017-10-18 20:25   ` Nathan Sidwell
@ 2017-10-18 20:40     ` Mukesh Kapoor
  2017-10-23 13:47       ` Paolo Carlini
  0 siblings, 1 reply; 6+ messages in thread
From: Mukesh Kapoor @ 2017-10-18 20:40 UTC (permalink / raw)
  To: Nathan Sidwell, gcc-patches

The bug happens only for enum types with a fixed underlying type. The 
existing code tries to create another type based on it's precision by 
calling c_common_type_for_size(). For the precision value of an unsigned 
long long type, the call to c_common_type_for_size() returns an unsigned 
long type and this causes compilation errors later on. The fix is to 
simply use the fixed underlying type of the enum instead of creating a 
new one.

Mukesh

On 10/18/2017 1:10 PM, Nathan Sidwell wrote:
> On 10/18/2017 12:17 PM, Mukesh Kapoor wrote:
>> On 10/9/2017 12:20 PM, Mukesh Kapoor wrote:
>>> Hi,
>>>
>>> This patch fixes https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82307.
>>> For an unscoped enum with a fixed underlying type, the function 
>>> type_promotes_to() does not always return the same type as the 
>>> underlying type. The fix is to use the underlying type of the enum 
>>> instead of creating a new one by calling c_common_type_for_size().
>
>
> The diff looks wrong.  Just before the changed piece it (attempts to) 
> deal with enum types.  Why is that failing?
>
> nathan
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PING] [C++ Patch] PR 82307
  2017-10-18 20:40     ` Mukesh Kapoor
@ 2017-10-23 13:47       ` Paolo Carlini
  2017-10-23 16:59         ` Nathan Sidwell
  0 siblings, 1 reply; 6+ messages in thread
From: Paolo Carlini @ 2017-10-23 13:47 UTC (permalink / raw)
  To: Mukesh Kapoor, Nathan Sidwell, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 238 bytes --]

Hi,

following up to a short off-line exchange with Nathan, I'm sending a 
reworked patch which - among other things - avoids regressing on the 
second testcase (cpp0x/enum36.C). Tested x86_64-linux.

Thanks,
Paolo.

////////////////////

[-- Attachment #2: CL_82307 --]
[-- Type: text/plain, Size: 433 bytes --]

/cp
2017-10-23  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-23  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.

[-- Attachment #3: patch_82307 --]
[-- Type: text/plain, Size: 2244 bytes --]

Index: cp/cvt.c
===================================================================
--- cp/cvt.c	(revision 254005)
+++ cp/cvt.c	(working copy)
@@ -1834,12 +1834,27 @@ type_promotes_to (tree type)
 	   || type == char32_type_node
 	   || type == wchar_type_node)
     {
+      tree prom = type;
+
+      if (TREE_CODE (type) == ENUMERAL_TYPE)
+	{
+	  prom = ENUM_UNDERLYING_TYPE (prom);
+	  if (!ENUM_IS_SCOPED (type)
+	      && ENUM_FIXED_UNDERLYING_TYPE_P (type))
+	    {
+	      /* ISO C++17, 7.6/4.  A prvalue of an unscoped enumeration type
+		 whose underlying type is fixed (10.2) can be converted to a
+		 prvalue of its underlying type. Moreover, if integral promotion
+		 can be applied to its underlying type, a prvalue of an unscoped
+		 enumeration type whose underlying type is fixed can also be 
+		 converted to a prvalue of the promoted underlying type.  */
+	      return type_promotes_to (prom);
+	    }
+	}
+
       int precision = MAX (TYPE_PRECISION (type),
 			   TYPE_PRECISION (integer_type_node));
       tree totype = c_common_type_for_size (precision, 0);
-      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);
Index: testsuite/g++.dg/cpp0x/enum35.C
===================================================================
--- testsuite/g++.dg/cpp0x/enum35.C	(nonexistent)
+++ testsuite/g++.dg/cpp0x/enum35.C	(working copy)
@@ -0,0 +1,14 @@
+// PR c++/82307
+// { dg-do run { target c++11 } }
+
+#include <cassert>
+
+enum : unsigned long long { VAL };
+
+bool foo (unsigned long long) { return true; }
+bool foo (int) { return false; }
+
+int main()
+{
+  assert (foo(VAL));
+}
Index: testsuite/g++.dg/cpp0x/enum36.C
===================================================================
--- testsuite/g++.dg/cpp0x/enum36.C	(nonexistent)
+++ testsuite/g++.dg/cpp0x/enum36.C	(working copy)
@@ -0,0 +1,14 @@
+// PR c++/82307
+// { dg-do run { target c++11 } }
+
+#include <cassert>
+
+enum : short { VAL };
+
+bool foo (int) { return true; }
+bool foo (unsigned long long) { return false; }
+
+int main()
+{
+  assert (foo (VAL));
+}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PING] [C++ Patch] PR 82307
  2017-10-23 13:47       ` Paolo Carlini
@ 2017-10-23 16:59         ` Nathan Sidwell
  0 siblings, 0 replies; 6+ messages in thread
From: Nathan Sidwell @ 2017-10-23 16:59 UTC (permalink / raw)
  To: Paolo Carlini, Mukesh Kapoor, gcc-patches

On 10/23/2017 09:15 AM, Paolo Carlini wrote:
> Hi,
> 
> following up to a short off-line exchange with Nathan, I'm sending a 
> reworked patch which - among other things - avoids regressing on the 
> second testcase (cpp0x/enum36.C). Tested x86_64-linux.

ok, thanks!

nathan


-- 
Nathan Sidwell

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-10-23 16:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-09 19:28 [C++ Patch] PR 82307 Mukesh Kapoor
2017-10-18 16:52 ` [PING] " Mukesh Kapoor
2017-10-18 20:25   ` Nathan Sidwell
2017-10-18 20:40     ` Mukesh Kapoor
2017-10-23 13:47       ` Paolo Carlini
2017-10-23 16:59         ` Nathan Sidwell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).