public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: gcc-patches List <gcc-patches@gcc.gnu.org>
Subject: RFA PATCH to get_narrower for c++/48106 (ICE with scoped enum)
Date: Mon, 23 May 2011 15:04:00 -0000	[thread overview]
Message-ID: <4DDA743A.7000604@redhat.com> (raw)

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

C++0x scoped enums don't implicitly convert to an integral type, so we 
need to be careful about throwing away explicit conversions.  In this 
case, the static_cast<int>(e) creates a NOP_EXPR which get_narrower 
throws away, but 'e' itself is not a valid argument for &, so we need to 
replace the NOP_EXPR with a different one.

Tested x86_64-pc-linux-gnu.  OK for trunk?

[-- Attachment #2: 48106.patch --]
[-- Type: text/x-patch, Size: 1262 bytes --]

commit 8f9cf5d22de2418bdb2ab9cb79ae878f809299ec
Author: Jason Merrill <jason@redhat.com>
Date:   Sun May 22 15:21:19 2011 -0400

    	PR c++/48106
    	* tree.c (get_narrower): Handle scoped enum.

diff --git a/gcc/testsuite/g++.dg/cpp0x/enum14.C b/gcc/testsuite/g++.dg/cpp0x/enum14.C
new file mode 100644
index 0000000..709b201
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/enum14.C
@@ -0,0 +1,12 @@
+// PR c++/48106
+// { dg-options -std=c++0x }
+
+enum class E : char
+{
+  e
+};
+
+bool operator&(E e, char m)
+{
+  return static_cast<int>(e) & m;
+}
diff --git a/gcc/tree.c b/gcc/tree.c
index 3357d84..99402e8 100644
--- a/gcc/tree.c
+++ b/gcc/tree.c
@@ -8117,6 +8117,17 @@ get_narrower (tree op, int *unsignedp_ptr)
 	  win = fold_convert (type, op);
 	}
     }
+  else if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
+	   && ENUM_IS_SCOPED (TREE_TYPE (op)))
+    {
+      /* C++0x scoped enumerations don't implicitly convert to integral
+	 type, so if we stripped an explicit conversion to a larger type we
+	 need to replace it.  */
+      tree type = (lang_hooks.types.type_for_size
+		   (TYPE_PRECISION (TREE_TYPE (op)),
+		    TYPE_UNSIGNED (TREE_TYPE (op))));
+      win = fold_convert (type, op);
+    }
 
   *unsignedp_ptr = uns;
   return win;

             reply	other threads:[~2011-05-23 14:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-23 15:04 Jason Merrill [this message]
2011-05-23 15:46 ` Richard Guenther
2011-05-23 16:17   ` Jason Merrill
2011-05-23 16:25     ` Richard Guenther
2011-05-24  5:33     ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4DDA743A.7000604@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).