c++: Fix ICE on complex constant with -frounding-math [PR103114]
authorJakub Jelinek <jakub@redhat.com>
Tue, 9 Nov 2021 14:29:36 +0000 (15:29 +0100)
committerJakub Jelinek <jakub@redhat.com>
Tue, 9 Nov 2021 14:29:36 +0000 (15:29 +0100)
The FE uses build_complex which assumes that fold_convert will fold
value to a constant.  With -frounding-math that isn't guaranteed though.
So, the patch instead fold_build2s COMPLEX_EXPR, which will result
in build_complex if both arguments are constants, and otherwise
will build COMPLEX_EXPR.
build_zero_cst is an optimization for fold_convert (type, integer_zero_node).

2021-11-09  Jakub Jelinek  <jakub@redhat.com>

PR c++/103114
* parser.c (cp_parser_userdef_numeric_literal): Use fold_build2
with COMPLEX_EXPR arg instead of build_complex, use build_zero_cst
instead of fold_convert from integer_zero_node.

* g++.dg/ext/complex10.C: New test.

gcc/cp/parser.c
gcc/testsuite/g++.dg/ext/complex10.C [new file with mode: 0644]

index 6061f98..32de97b 100644 (file)
@@ -4804,9 +4804,8 @@ cp_parser_userdef_numeric_literal (cp_parser *parser)
       else /* if (id_equal (suffix_id, "il")) */
        type = long_double_type_node;
 
-      value = build_complex (build_complex_type (type),
-                            fold_convert (type, integer_zero_node),
-                            fold_convert (type, value));
+      value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
+                          build_zero_cst (type), fold_convert (type, value));
     }
 
   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
diff --git a/gcc/testsuite/g++.dg/ext/complex10.C b/gcc/testsuite/g++.dg/ext/complex10.C
new file mode 100644 (file)
index 0000000..67dfb0c
--- /dev/null
@@ -0,0 +1,5 @@
+// PR c++/103114
+// { dg-do compile }
+// { dg-options "-frounding-math" }
+
+_Complex double d = 10.1i;