diff mbox

[C++] PR 53159 (Take 2)

Message ID 53BFB95D.8070805@oracle.com
State New
Headers show

Commit Message

Paolo Carlini July 11, 2014, 10:15 a.m. UTC
Hi,

only today it occurred to me that we can as well delay all the 
diagnostic at issue to the check_narrowing at the end of 
convert_like_real and avoid at once possible issues with duplicate 
warnings. Tested x86_64-linux.

Thanks,
Paolo.

//////////////
/cp
2014-07-11  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/53159
	* call.c (build_user_type_conversion_1): Copy LOOKUP_NO_NARROWING
	into convflags.
	* decl.c (check_initializer): Dont' call check_narrowing here,
	set LOOKUP_NO_NARROWING.
	* typeck2.c (digest_init_r): Likewise.

/testsuite
2014-07-11  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/53159
	* g++.dg/cpp0x/Wnarrowing1.C: New.

Comments

Jason Merrill July 11, 2014, 5:58 p.m. UTC | #1
Oops, this patch didn't thread with the earlier one...

On 07/11/2014 06:15 AM, Paolo Carlini wrote:
>   	      if (SCALAR_TYPE_P (type))

Is the condition still necessary?

> -		check_narrowing (type, init);
> +		flags |= LOOKUP_NO_NARROWING;

Jason
Paolo Carlini July 11, 2014, 6:02 p.m. UTC | #2
Hi,

On 07/11/2014 07:58 PM, Jason Merrill wrote:
> Oops, this patch didn't thread with the earlier one...
Yeah, sorry, I considered my drafts of yesterday evening a distraction 
and wanted to start a fresh thread with a complete (more meaningful) new 
proposal. Probably not a good idea, after all, confusing...
> On 07/11/2014 06:15 AM, Paolo Carlini wrote:
>>             if (SCALAR_TYPE_P (type))
>
> Is the condition still necessary?
I'm removing it and retesting. In any case (the eventual) 
check_narrowing will return immediately if !ARITHMETIC_TYPE_P (type) is 
true... Would the amended patch be Ok?

Thanks!
Paolo.
Jason Merrill July 11, 2014, 8:49 p.m. UTC | #3
On 07/11/2014 02:02 PM, Paolo Carlini wrote:
>> Is the condition still necessary?
> I'm removing it and retesting. In any case (the eventual)
> check_narrowing will return immediately if !ARITHMETIC_TYPE_P (type) is
> true... Would the amended patch be Ok?

Yes, thanks.

Jason
diff mbox

Patch

Index: cp/call.c
===================================================================
--- cp/call.c	(revision 212449)
+++ cp/call.c	(working copy)
@@ -3586,7 +3586,8 @@  build_user_type_conversion_1 (tree totype, tree ex
 
   /* It's OK to bind a temporary for converting constructor arguments, but
      not in converting the return value of a conversion operator.  */
-  convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION);
+  convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
+	       | (flags & LOOKUP_NO_NARROWING));
   flags &= ~LOOKUP_NO_TEMP_BIND;
 
   if (ctors)
Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 212449)
+++ cp/decl.c	(working copy)
@@ -5757,7 +5757,7 @@  check_initializer (tree decl, tree init, int flags
 	    {
 	      init = reshape_init (type, init, tf_warning_or_error);
 	      if (SCALAR_TYPE_P (type))
-		check_narrowing (type, init);
+		flags |= LOOKUP_NO_NARROWING;
 	    }
 	}
       else if (TREE_CODE (init) == TREE_LIST
Index: cp/typeck2.c
===================================================================
--- cp/typeck2.c	(revision 212449)
+++ cp/typeck2.c	(working copy)
@@ -1027,7 +1027,7 @@  digest_init_r (tree type, tree init, bool nested,
       tree *exp;
 
       if (nested)
-	check_narrowing (type, init);
+	flags |= LOOKUP_NO_NARROWING;
       init = convert_for_initialization (0, type, init, flags,
 					 ICR_INIT, NULL_TREE, 0,
 					 complain);
Index: testsuite/g++.dg/cpp0x/Wnarrowing1.C
===================================================================
--- testsuite/g++.dg/cpp0x/Wnarrowing1.C	(revision 0)
+++ testsuite/g++.dg/cpp0x/Wnarrowing1.C	(working copy)
@@ -0,0 +1,18 @@ 
+// PR c++/53159
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wnarrowing -Wno-overflow" }
+
+struct X
+{
+  constexpr operator int() { return __INT_MAX__; }
+};
+
+int f() { return __INT_MAX__; }
+
+signed char a { __INT_MAX__ };     // { dg-warning "narrowing conversion" }
+signed char b { f() };             // { dg-warning "narrowing conversion" }
+signed char c { X{} };             // { dg-warning "narrowing conversion" }
+
+signed char ar[] { __INT_MAX__ };  // { dg-warning "narrowing conversion" }
+signed char br[] { f() };          // { dg-warning "narrowing conversion" }
+signed char cr[] { X{} };          // { dg-warning "narrowing conversion" }