diff mbox series

[C++] PR 70808 ("Spurious -Wzero-as-null-pointer-constant for nullptr_t")

Message ID b1061ea1-853b-a891-53aa-865cb9ba9af8@oracle.com
State New
Headers show
Series [C++] PR 70808 ("Spurious -Wzero-as-null-pointer-constant for nullptr_t") | expand

Commit Message

Paolo Carlini April 10, 2018, 6:57 p.m. UTC
Hi,

this isn't a regression, but deciding what we want to do should be easy 
and quick enough. The issue is that "mysteriously" we warn 
-Wzero-as-null-pointer-constant for:

decltype( nullptr ) warn = {};

and we don't for:

int* no_warn = {};

That's easily explained given the code we have in build_zero_init_1 
which handles types satisfying TYPE_PTR_OR_PTRMEM_P separately from all 
the other scalar types, std::nullptr_t included. I think we should 
resolve the inconsistency - the below does that without regressions - 
but frankly, at first, given the letter of the standard under 11.6/6 
about zero-initialization I expected that we weren't handling types 
satisfying TYPE_PTR_OR_PTRMEM_P in a different way, thus something like 
a sheer integer_zero_node consistently for all the scalars (which would 
mean resolving the inconsistency precisely the other way round). 
Probably it's a tricky detail of our internal representations...

Thanks, Paolo.

///////////////////
/cp
2018-04-10  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/70808
	* init.c (build_zero_init_1): Handle NULLPTR_TYPE_P being true of
	the type like TYPE_PTR_OR_PTRMEM_P.

/testsuite
2018-04-10  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/70808
	* g++.dg/warn/Wzero-as-null-pointer-constant-7.C: New.

Comments

Jason Merrill April 10, 2018, 7:34 p.m. UTC | #1
OK.

On Tue, Apr 10, 2018 at 2:57 PM, Paolo Carlini <paolo.carlini@oracle.com> wrote:
> Hi,
>
> this isn't a regression, but deciding what we want to do should be easy and
> quick enough. The issue is that "mysteriously" we warn
> -Wzero-as-null-pointer-constant for:
>
> decltype( nullptr ) warn = {};
>
> and we don't for:
>
> int* no_warn = {};
>
> That's easily explained given the code we have in build_zero_init_1 which
> handles types satisfying TYPE_PTR_OR_PTRMEM_P separately from all the other
> scalar types, std::nullptr_t included. I think we should resolve the
> inconsistency - the below does that without regressions - but frankly, at
> first, given the letter of the standard under 11.6/6 about
> zero-initialization I expected that we weren't handling types satisfying
> TYPE_PTR_OR_PTRMEM_P in a different way, thus something like a sheer
> integer_zero_node consistently for all the scalars (which would mean
> resolving the inconsistency precisely the other way round). Probably it's a
> tricky detail of our internal representations...
>
> Thanks, Paolo.
>
> ///////////////////
>
Jakub Jelinek April 11, 2018, 3:57 p.m. UTC | #2
On Tue, Apr 10, 2018 at 08:57:22PM +0200, Paolo Carlini wrote:
> 2018-04-10  Paolo Carlini  <paolo.carlini@oracle.com>
> 
> 	PR c++/70808
> 	* g++.dg/warn/Wzero-as-null-pointer-constant-7.C: New.

The testcase FAILs in -std=c++98 mode for obvious reasons.

I've committed following after testing it with
make check-c++-all RUNTESTFLAGS=dg.exp=Wzero-as-null-pointer-constant-7.C

2018-04-11  Jakub Jelinek  <jakub@redhat.com>

	PR c++/70808
	* g++.dg/warn/Wzero-as-null-pointer-constant-7.C: Require c++11
	effective target.

--- gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-7.C	(revision 259324)
+++ gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-7.C	(revision 259325)
@@ -1,4 +1,5 @@
 // PR c++/70808
+// { dg-do compile { target c++11 } }
 // { dg-options "-Wzero-as-null-pointer-constant" }
 
 int* no_warn = {};


	Jakub
Paolo Carlini April 11, 2018, 6:06 p.m. UTC | #3
Hi,

On 11/04/2018 17:57, Jakub Jelinek wrote:
> On Tue, Apr 10, 2018 at 08:57:22PM +0200, Paolo Carlini wrote:
>> 2018-04-10  Paolo Carlini  <paolo.carlini@oracle.com>
>>
>> 	PR c++/70808
>> 	* g++.dg/warn/Wzero-as-null-pointer-constant-7.C: New.
> The testcase FAILs in -std=c++98 mode for obvious reasons.
Indeed, thanks Jakub and sorry about the stupid mistake. I'm moving the 
new testcase to g++.dg/cpp0x/Wzero-as-null-pointer-constant-3.C, more 
consistent with how we placed these testcases in the past.

Paolo.
diff mbox series

Patch

Index: cp/init.c
===================================================================
--- cp/init.c	(revision 259287)
+++ cp/init.c	(working copy)
@@ -180,7 +180,7 @@  build_zero_init_1 (tree type, tree nelts, bool sta
        items with static storage duration that are not otherwise
        initialized are initialized to zero.  */
     ;
-  else if (TYPE_PTR_OR_PTRMEM_P (type))
+  else if (TYPE_PTR_OR_PTRMEM_P (type) || NULLPTR_TYPE_P (type))
     init = fold (convert (type, nullptr_node));
   else if (SCALAR_TYPE_P (type))
     init = fold (convert (type, integer_zero_node));
Index: testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-7.C
===================================================================
--- testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-7.C	(nonexistent)
+++ testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-7.C	(working copy)
@@ -0,0 +1,5 @@ 
+// PR c++/70808
+// { dg-options "-Wzero-as-null-pointer-constant" }
+
+int* no_warn = {};
+decltype( nullptr ) warn = {};