Skip to content

Commit 5d5dcc6

Browse files
committed
c++: Detect long double -> double narrowing [PR94590]
This PR points out that we don't detect long double -> double narrowing when long double happens to have the same precision as double; on x86_64 this can be achieved by -mlong-double-64. [dcl.init.list]gcc-mirror#7.2 specifically says "from long double to double or float, or from double to float", but check_narrowing only checks TYPE_PRECISION (type) < TYPE_PRECISION (ftype) so we need to handle the other cases too, e.g. by same_type_p as in the following patch. PR c++/94590 - Detect long double -> double narrowing. * typeck2.c (check_narrowing): Detect long double -> double narrowing even when double and long double have the same precision. Make it handle conversions to float too. * g++.dg/cpp0x/Wnarrowing18.C: New test.
1 parent 21968d4 commit 5d5dcc6

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

gcc/cp/ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2020-05-07 Marek Polacek <[email protected]>
2+
3+
PR c++/94590 - Detect long double -> double narrowing.
4+
* typeck2.c (check_narrowing): Detect long double -> double
5+
narrowing even when double and long double have the same
6+
precision. Make it handle conversions to float too.
7+
18
2020-05-07 Marek Polacek <[email protected]>
29

310
PR c++/94255

gcc/cp/typeck2.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,10 +1008,17 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain,
10081008
|| !int_fits_type_p (init, type)))
10091009
ok = false;
10101010
}
1011+
/* [dcl.init.list]#7.2: "from long double to double or float, or from
1012+
double to float". */
10111013
else if (TREE_CODE (ftype) == REAL_TYPE
10121014
&& TREE_CODE (type) == REAL_TYPE)
10131015
{
1014-
if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
1016+
if ((same_type_p (ftype, long_double_type_node)
1017+
&& (same_type_p (type, double_type_node)
1018+
|| same_type_p (type, float_type_node)))
1019+
|| (same_type_p (ftype, double_type_node)
1020+
&& same_type_p (type, float_type_node))
1021+
|| (TYPE_PRECISION (type) < TYPE_PRECISION (ftype)))
10151022
{
10161023
if (TREE_CODE (init) == REAL_CST)
10171024
{

gcc/testsuite/ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2020-05-07 Marek Polacek <[email protected]>
2+
3+
PR c++/94590 - Detect long double -> double narrowing.
4+
* g++.dg/cpp0x/Wnarrowing18.C: New test.
5+
16
2020-05-07 Marek Polacek <[email protected]>
27

38
PR c++/94255
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// PR c++/94590 - Detect long double -> double narrowing.
2+
// { dg-do compile { target c++11 } }
3+
// { dg-additional-options "-mlong-double-64" { target x86_64-*-* i?86-*-* } }
4+
5+
int
6+
main ()
7+
{
8+
using T = long double;
9+
extern long double ld;
10+
extern T ld2;
11+
extern const T ld3;
12+
double d{ld}; // { dg-error "narrowing conversion" }
13+
double d2{ld2}; // { dg-error "narrowing conversion" }
14+
double d3{ld3}; // { dg-error "narrowing conversion" }
15+
}

0 commit comments

Comments
 (0)