public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ PATCH for core issue 903 (C++11 null pointer constant)
@ 2011-08-23 16:22 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2011-08-23 16:22 UTC (permalink / raw)
  To: gcc-patches List

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

C++11 greatly expands the set of constant expressions, which aggravates 
the existing issue with overloading and null pointer constants.  If an 
expression could potentially be a constant expression, we need to find 
its constant value in order to determine how it interacts with overload 
resolution.  In C++03 that doesn't involve much beyond the constant 
folding we already do, but in C++11 that means substituting into 
constexpr functions, so we decided to limit null pointer constants in 
C++11 to literal 0 (or 0L, etc).

This patch doesn't attempt to treat things like 0+0 as non-null pointer 
constants yet, just avoids doing anything beyond the usual constant folding.

Tested x86_64-pc-linux-gnu, applying to trunk.

[-- Attachment #2: core-903.patch --]
[-- Type: text/x-patch, Size: 1433 bytes --]

commit af3382cf8702c1dfb5a98b2b2093aac21a5c4dff
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Aug 19 10:38:12 2011 -0400

    	Core 903 (partial)
    	* call.c (null_ptr_cst_p): Only 0 qualifies in C++11.

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index d2700cb..e5f65b3 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -541,20 +541,14 @@ null_ptr_cst_p (tree t)
     return true;
   if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)))
     {
-      if (cxx_dialect >= cxx0x)
-	{
-	  t = fold_non_dependent_expr (t);
-	  t = maybe_constant_value (t);
-	  if (TREE_CONSTANT (t) && integer_zerop (t))
-	    return true;
-	}
-      else
+      /* Core issue 903 says only literal 0 is a null pointer constant.  */
+      if (cxx_dialect < cxx0x)
 	{
 	  t = integral_constant_value (t);
 	  STRIP_NOPS (t);
-	  if (integer_zerop (t) && !TREE_OVERFLOW (t))
-	    return true;
 	}
+      if (integer_zerop (t) && !TREE_OVERFLOW (t))
+	return true;
     }
   return false;
 }
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-nullptr.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-nullptr.C
index 7ac53db..6381323 100644
--- a/gcc/testsuite/g++.dg/cpp0x/constexpr-nullptr.C
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-nullptr.C
@@ -2,5 +2,5 @@
 
 constexpr int zero() { return 0; }
 
-void* ptr1 = zero(); // #1
-constexpr void* ptr2 = zero(); // #2
+void* ptr1 = zero();		// { dg-error "int" }
+constexpr void* ptr2 = zero();	// { dg-error "int" }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2011-08-23 15:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-23 16:22 C++ PATCH for core issue 903 (C++11 null pointer constant) Jason Merrill

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).