This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [C++0x] nullptr
- From: Magnus Fromreide <magfr at lysator dot liu dot se>
- To: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- Cc: Paolo Carlini <paolo dot carlini at oracle dot com>, libstdc++ <libstdc++ at gcc dot gnu dot org>, gcc-patches at gcc dot gnu dot org
- Date: Sat, 05 Jun 2010 00:13:45 +0200
- Subject: Re: [C++0x] nullptr
- References: <1268546192.2028.32.camel@sara.home> <4BA46E77.6080507@redhat.com> <1269813166.2249.8.camel@sara.home> <4BB12B74.80703@redhat.com> <4BDD0915.2020301@redhat.com> <1272842221.2000.26.camel@sara.home> <s2x4348dea51005021734p72dff67cxb3b0fc26b6484400@mail.gmail.com> <4BEBC0AB.1000307@oracle.com> <AANLkTikykY1CJmw3rUkXdjpc5Gm2NaCM0GHR_9Eg8AgN@mail.gmail.com> <4BED2842.8050702@oracle.com> <4C023C7A.6010002@oracle.com> <4C023EC0.9020000@gmail.com> <AANLkTimzhcVlFJC6ZsB8iZ_6FPRXhQOpnujMO4Gqk2l1@mail.gmail.com>
On Thu, 2010-06-03 at 02:06 +0100, Jonathan Wakely wrote:
> On 30 May 2010 11:32, Paolo Carlini wrote:
> > ... oops, as a matter of fact you can as well directly define in
> > c++config std::nullptr_t, instead of std::__nullptr_t, in complete
> > analogy with the other stddef typedefs and be done with it. Sorry, I was
> > following a previous version of my ideas ;)
>
> I've done that but am working on applying the other library changes -
> the shared_ptr parts needed some work and I'm getting an ICE and
> errors about not being able to convert nullptr_t to int*, which seems
> wrong. I'm still trying to reduce the problem or figure out what I'm
> doing wrong.
>
> Will update soon ...
It looks as if the nullptr code is buggy.
With the attached patch it seems to work.
/MF
Index: gcc/testsuite/g++.dg/cpp0x/nullptr22.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr22.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr22.C (working copy)
@@ -13,4 +13,8 @@
f2(nullptr); // { dg-warning "null argument where non-null required " }
f3("x", "y", __null); // { dg-warning "missing sentinel in function call" }
f3("x", "y", nullptr);
+ decltype(nullptr) mynull = 0;
+ f1("%p", mynull);
+ f2(mynull); // { dg-warning "null argument where non-null required " }
+ f3("x", "y", mynull);
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr05.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr05.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr05.C (working copy)
@@ -10,3 +10,5 @@
const pmf pmf1 = nullptr;
const pmf pmf2 = __null;
const pmf pmf3 = 0;
+decltype(nullptr) mynull = 0;
+const pmf pmf4 = mynull;
Index: gcc/testsuite/g++.dg/cpp0x/nullptr14.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr14.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr14.C (working copy)
@@ -20,4 +20,6 @@
//
type_equal<char*>(f(nullptr));
type_equal<int>(f(0));
+ decltype(nullptr) mynull = 0;
+ type_equal<char*>(f(mynull));
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr06.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr06.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr06.C (working copy)
@@ -10,4 +10,6 @@
void fun()
{
assert_true(cp1 == nullptr);
+ decltype(nullptr) mynull = 0;
+ assert_true(cp1 == mynull);
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr15.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr15.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr15.C (working copy)
@@ -18,4 +18,7 @@
//
g(nullptr); // { dg-error "no matching function for call to " }
type_equal<float*>(g((float*)nullptr));
+ decltype(nullptr) mynull = 0;
+ g(mynull); // { dg-error "no matching function for call to " }
+ type_equal<float*>(g((float*)mynull));
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr07.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr07.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr07.C (working copy)
@@ -9,4 +9,7 @@
if( n == nullptr ); // { dg-error "invalid operands of types " }
const int m = 1;
if( m == nullptr ); // { dg-error "invalid operands of types " }
+ decltype(nullptr) mynull = 0;
+ if( n == mynull ); // { dg-error "invalid operands of types " }
+ if( m == mynull ); // { dg-error "invalid operands of types " }
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr16.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr16.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr16.C (working copy)
@@ -19,4 +19,7 @@
type_equal<int>(h(0));
type_equal<nullptr_t>(h(nullptr));
type_equal<float*>(h((float*)nullptr));
+ nullptr_t mynull = 0;
+ type_equal<nullptr_t>(h(mynull));
+ type_equal<float*>(h((float*)mynull));
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr08.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr08.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr08.C (working copy)
@@ -8,4 +8,6 @@
void fun()
{
assert_true(nullptr ? false : true);
+ decltype(nullptr) mynull = 0;
+ assert_true(mynull ? false : true);
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr17.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr17.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr17.C (working copy)
@@ -18,4 +18,6 @@
{
// Overload to bool, not int
type_equal<bool>(i(nullptr));
+ decltype(nullptr) mynull = 0;
+ type_equal<bool>(i(mynull));
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr01.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr01.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr01.C (working copy)
@@ -6,3 +6,5 @@
char* const cp1 = nullptr;
char* const cp2 = __null;
char* const cp3 = 0;
+decltype(nullptr) mynull = 0;
+char* const cp4 = mynull;
Index: gcc/testsuite/g++.dg/cpp0x/nullptr09.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr09.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr09.C (working copy)
@@ -6,4 +6,6 @@
void fun()
{
if( nullptr == 0 );
+ decltype(nullptr) mynull = 0;
+ if( mynull == 0 );
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr10.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr10.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr10.C (working copy)
@@ -7,4 +7,8 @@
{
nullptr = 0; // { dg-error "lvalue required as left operand" }
nullptr + 2; // { dg-error "invalid operands of types " }
+ decltype(nullptr) mynull = 0;
+ mynull = 1; // { dg-error "cannot convert" }
+ mynull = 0;
+ mynull + 2; // { dg-error "invalid operands of types " }
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr18.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr18.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr18.C (working copy)
@@ -16,4 +16,6 @@
void test_j()
{
type_equal<char*>(j(nullptr));
+ decltype(nullptr) mynull = 0;
+ type_equal<char*>(j(mynull));
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr02.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr02.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr02.C (working copy)
@@ -8,3 +8,9 @@
const nullptr_t np1 = nullptr;
const nullptr_t np2 = __null;
const nullptr_t np3 = 0;
+const nullptr_t np4 = np1;
+const nullptr_t np5 = np2;
+const nullptr_t np6 = np3;
+const nullptr_t np7 = np4;
+const nullptr_t np8 = np5;
+const nullptr_t np9 = np6;
Index: gcc/testsuite/g++.dg/cpp0x/nullptr11.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr11.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr11.C (working copy)
@@ -14,4 +14,27 @@
assert_false(nullptr > nullptr);
assert_true(nullptr <= nullptr);
assert_true(nullptr >= nullptr);
+
+ decltype(nullptr) mynull = 0;
+
+ assert_true(mynull == nullptr);
+ assert_false(mynull != nullptr);
+ assert_false(mynull < nullptr);
+ assert_false(mynull > nullptr);
+ assert_true(mynull <= nullptr);
+ assert_true(mynull >= nullptr);
+
+ assert_true(nullptr == mynull);
+ assert_false(nullptr != mynull);
+ assert_false(nullptr < mynull);
+ assert_false(nullptr > mynull);
+ assert_true(nullptr <= mynull);
+ assert_true(nullptr >= mynull);
+
+ assert_true(mynull == mynull);
+ assert_false(mynull != mynull);
+ assert_false(mynull < mynull);
+ assert_false(mynull > mynull);
+ assert_true(mynull <= mynull);
+ assert_true(mynull >= mynull);
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr20.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr20.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr20.C (working copy)
@@ -10,8 +10,11 @@
{
char buf1[64];
char buf2[64];
+ char buf3[64];
std::sprintf(buf1, "%p", (void*)0);
std::sprintf(buf2, "%p", nullptr);
- return std::strcmp(buf1, buf2) != 0;
+ decltype(nullptr) mynull = 0;
+ std::sprintf(buf3, "%p", nullptr);
+ return std::strcmp(buf1, buf2) != 0 || std::strcmp(buf1, buf3) != 0;
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr03.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr03.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr03.C (working copy)
@@ -3,4 +3,7 @@
// Test assignment to int
-const int n = nullptr; // { dg-error "cannot convert " }
+const int n1 = nullptr; // { dg-error "cannot convert " }
+decltype(nullptr) mynull = 0;
+const int n2 = mynull; // { dg-error "cannot convert " }
+
Index: gcc/testsuite/g++.dg/cpp0x/nullptr12.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr12.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr12.C (working copy)
@@ -4,3 +4,5 @@
// Test sizeof
static_assert(sizeof(nullptr) == sizeof(void*), "sizeof(nullptr) is wrong");
+const decltype(nullptr) mynull = 0;
+static_assert(sizeof(mynull) == sizeof(void*), "sizeof(nullptr) is wrong");
Index: gcc/testsuite/g++.dg/cpp0x/nullptr21.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr21.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr21.C (working copy)
@@ -24,4 +24,21 @@
} catch (...) {
printf("Test 1 Fail");
} // { dg-output "Test 1 OK" }
+
+ nullptr_t mynull = 0;
+ try {
+ throw mynull;
+ } catch (void*) {
+ printf("Test 2 Fail");
+ } catch (bool) {
+ printf("Test 2 Fail");
+ } catch (int) {
+ printf("Test 2 Fail");
+ } catch (long int) {
+ printf("Test 2 Fail");
+ } catch (nullptr_t) {
+ printf("Test 2 OK");
+ } catch (...) {
+ printf("Test 2 Fail");
+ } // { dg-output "Test 2 OK" }
}
Index: gcc/testsuite/g++.dg/cpp0x/nullptr04.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr04.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr04.C (working copy)
@@ -11,3 +11,9 @@
const short int n5 = reinterpret_cast<short int>(nullptr); // { dg-error "loses precision" }
const ssize_t n6 = reinterpret_cast<ssize_t>(nullptr);
const ssize_t n7 = (ssize_t)nullptr;
+
+decltype(nullptr) mynull = 0;
+const int n8 = static_cast<const int>(mynull); // { dg-error "invalid static_cast " }
+const short int n9 = reinterpret_cast<short int>(mynull); // { dg-error "loses precision" }
+const ssize_t n10 = reinterpret_cast<ssize_t>(mynull);
+const ssize_t n11 = (ssize_t)mynull;
Index: gcc/testsuite/g++.dg/cpp0x/nullptr13.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/nullptr13.C (revision 160252)
+++ gcc/testsuite/g++.dg/cpp0x/nullptr13.C (working copy)
@@ -5,7 +5,12 @@
#include <typeinfo>
+#define assert_true(b) do { char c[2 * bool(b) - 1]; } while(0)
+
void fun()
{
typeid(nullptr);
+ const decltype(nullptr) mynull = 0;
+ typeid(mynull);
+ assert_true(typeid(nullptr) == typeid(mynull));
}
Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c (revision 160252)
+++ gcc/cp/typeck.c (working copy)
@@ -7019,7 +7019,7 @@
}
/* Handle null pointer to member function conversions. */
- if (integer_zerop (pfn))
+ if (null_ptr_cst_p (pfn))
{
pfn = build_c_cast (input_location, type, integer_zero_node);
return build_ptrmemfunc1 (to_type,
Index: gcc/cp/cvt.c
===================================================================
--- gcc/cp/cvt.c (revision 160252)
+++ gcc/cp/cvt.c (working copy)
@@ -196,7 +196,7 @@
return error_mark_node;
}
- if (integer_zerop (expr))
+ if (null_ptr_cst_p (expr))
{
if (TYPE_PTRMEMFUNC_P (type))
return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,