This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C++ PATCH for core issue 703 (float narrowing/initializer-lists)


At the San Francisco meeting the core working group decided to loosen the rules for narrowing conversion errors on floating point constant-expressions: now we only complain if the value is outside the range of the target type, we don't complain if it just loses precision.

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

2009-03-20  Jason Merrill  <jason@redhat.com>

	C++ core issue 703
	* typeck2.c (check_narrowing): Don't complain about loss of 
	precision when converting a floating-point constant.

Index: cp/typeck2.c
===================================================================
*** cp/typeck2.c	(revision 144978)
--- cp/typeck2.c	(working copy)
*************** check_narrowing (tree type, tree init)
*** 677,694 ****
      {
        if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
  	{
- 	  ok = false;
  	  if (TREE_CODE (init) == REAL_CST)
  	    {
  	      d = TREE_REAL_CST (init);
! 	      if (exact_real_truncate (TYPE_MODE (type), &d)
! 		  /* FIXME: As a temporary workaround for PR 36963, don't
! 		     complain about narrowing from a floating
! 		     literal. Hopefully this will be resolved at the
! 		     September 2008 C++ meeting. */
! 		  || !was_decl)
! 		ok = true;
  	    }
  	}
      }
    else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
--- 677,694 ----
      {
        if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
  	{
  	  if (TREE_CODE (init) == REAL_CST)
  	    {
+ 	      /* Issue 703: Loss of precision is OK as long as the value is
+ 		 within the representable range of the new type.  */
+ 	      REAL_VALUE_TYPE r;
  	      d = TREE_REAL_CST (init);
! 	      real_convert (&r, TYPE_MODE (type), &d);
! 	      if (real_isinf (&r))
! 		ok = false;
  	    }
+ 	  else
+ 	    ok = false;
  	}
      }
    else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
Index: testsuite/g++.dg/cpp0x/initlist5.C
===================================================================
*** testsuite/g++.dg/cpp0x/initlist5.C	(revision 144923)
--- testsuite/g++.dg/cpp0x/initlist5.C	(working copy)
*************** int k {}; // initialize to 0
*** 23,25 ****
--- 23,27 ----
  // PR c++/39693
  double d = 1.1;
  float fa[] = { d, 1.1 };      // { dg-error "narrowing conversion of 'd'" }
+ const double d2 = 1.1;
+ float fa2[] = { d2, 1.1 };

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]