Bug 90659 - [13 Regression] ICE in tree_to_uhwi, at tree.h:4352/7291
Summary: [13 Regression] ICE in tree_to_uhwi, at tree.h:4352/7291
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.0
: P2 normal
Target Milestone: 13.5
Assignee: Not yet assigned to anyone
URL:
Keywords: c++-lambda, ice-on-invalid-code, needs-bisection
Depends on:
Blocks: C++VLA
  Show dependency treegraph
 
Reported: 2019-05-28 17:45 UTC by G. Steinmetz
Modified: 2025-07-11 08:59 UTC (History)
3 users (show)

See Also:
Host:
Target: x86_64-pc-linux-gnu
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-05-28 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description G. Steinmetz 2019-05-28 17:45:47 UTC
Changed between 20190224 and 20190303 :


$ cat z1.cc
template <typename T>
void foo (int n)
{
  T a[n];
  [=]{a;};
}
void bar ()
{
  foo <double>;
}


$ g++-10-20190526 -c z1.cc
z1.cc: In instantiation of 'void foo(int) [with T = double]':
z1.cc:9:15:   required from here
z1.cc:5:3: internal compiler error: in tree_to_uhwi, at tree.h:4352
    5 |   [=]{a;};
      |   ^~~~~~~
0x617f9c tree_to_uhwi(tree_node const*)
        ../../gcc/tree.h:4352
0x617f9c cxx_eval_vec_init_1
        ../../gcc/cp/constexpr.c:3126
0x610857 cxx_eval_vec_init
        ../../gcc/cp/constexpr.c:3221
0x610857 cxx_eval_constant_expression
        ../../gcc/cp/constexpr.c:4946
0x61392e cxx_eval_outermost_constant_expr
        ../../gcc/cp/constexpr.c:5328
0x61617c maybe_constant_init_1
        ../../gcc/cp/constexpr.c:5705
0x73f396 massage_init_elt
        ../../gcc/cp/typeck2.c:1348
0x73e666 process_init_constructor_record
        ../../gcc/cp/typeck2.c:1567
0x73e666 process_init_constructor
        ../../gcc/cp/typeck2.c:1841
0x73e666 digest_init_r
        ../../gcc/cp/typeck2.c:1239
0x708501 finish_compound_literal(tree_node*, tree_node*, int, fcl_t)
        ../../gcc/cp/semantics.c:2874
0x6695c4 build_lambda_object(tree_node*)
        ../../gcc/cp/lambda.c:117
0x6de512 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
        ../../gcc/cp/pt.c:19547
0x6e64c9 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
        ../../gcc/cp/pt.c:18247
0x6e64c9 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
        ../../gcc/cp/pt.c:17923
0x6e5467 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
        ../../gcc/cp/pt.c:17050
0x6e55f1 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
        ../../gcc/cp/pt.c:17036
0x6e6151 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
        ../../gcc/cp/pt.c:17330
0x6e4624 instantiate_decl(tree_node*, bool, bool)
        ../../gcc/cp/pt.c:24760
0x6f987b instantiate_pending_templates(int)
        ../../gcc/cp/pt.c:24876
Comment 1 Marek Polacek 2019-05-28 19:13:13 UTC
Confirmed.
Comment 2 Marek Polacek 2019-05-28 19:13:58 UTC
The crash is here:

3126	  unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);

(gdb) p nelts
$1 = <plus_expr 0x7fffeab40140>
(gdb) pge
(sizetype) (SAVE_EXPR <(ssizetype) n + -1>) + 1
Comment 3 Marek Polacek 2019-05-28 21:19:30 UTC
Looks like missing verify_constant after the get_array_or_vector_nelts call.
Comment 4 G. Steinmetz 2019-06-18 16:28:40 UTC
A modified variant :


$ cat z2.cc
template <int ...>
void foo (int n)
{
  int a[n];
  [a]{};
}
void bar ()
{
  foo (2);
}
Comment 5 Jakub Jelinek 2019-08-12 08:54:35 UTC
GCC 9.2 has been released.
Comment 6 Jakub Jelinek 2019-11-18 18:31:46 UTC
Started with r269292 aka PR86969 fix.
Comment 7 Jakub Jelinek 2019-11-18 18:46:59 UTC
The following fixes the ICE:

--- gcc/cp/constexpr.c.jj	2019-11-16 18:13:28.524822284 +0100
+++ gcc/cp/constexpr.c	2019-11-18 19:38:40.032618830 +0100
@@ -39,7 +39,7 @@ along with GCC; see the file COPYING3.
 static bool verify_constant (tree, bool, bool *, bool *);
 #define VERIFY_CONSTANT(X)						\
 do {									\
-  if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
+  if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p))	\
     return t;								\
  } while (0)
 
@@ -3405,7 +3405,11 @@ cxx_eval_vec_init_1 (const constexpr_ctx
 
   tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
 					  overflow_p);
-  unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
+  unsigned HOST_WIDE_INT max;
+  if (verify_constant (nelts, ctx->quiet, non_constant_p, overflow_p))
+    max = 0;
+  else
+    max = tree_to_uhwi (nelts);
   for (i = 0; i < max; ++i)
     {
       tree idx = build_int_cst (size_type_node, i);
--- gcc/testsuite/g++.dg/cpp0x/pr90659.C.jj	2019-11-18 19:41:14.805308816 +0100
+++ gcc/testsuite/g++.dg/cpp0x/pr90659.C	2019-11-18 19:43:50.630983085 +0100
@@ -0,0 +1,17 @@
+// PR c++/90659
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+template <typename T>
+void
+foo (int n)
+{
+  T a[n];
+  [=]{ a; };
+}
+
+void
+bar ()
+{
+  foo <double>;
+}

but the testcase still FAILs with -std=gnu++17/2a with weird errors:
/usr/src/gcc/gcc/testsuite/g++.dg/cpp0x/pr90659.C:10:3: error: name '__a' used in a GNU-style designated initializer for an array
/usr/src/gcc/gcc/testsuite/g++.dg/cpp0x/pr90659.C:10:3: error: cannot convert 'double*' to 'double' in initialization

and e.g. with auto f = [=]{ a; }; instead it still ICEs elsewhere - in check_initializer.  Having a VLA return from a lambda isn't the best idea...
Comment 8 Jakub Jelinek 2020-03-12 11:58:59 UTC
GCC 9.3.0 has been released, adjusting target milestone.
Comment 9 Richard Biener 2021-06-01 08:14:19 UTC
GCC 9.4 is being released, retargeting bugs to GCC 9.5.
Comment 10 Richard Biener 2022-05-27 09:40:52 UTC
GCC 9 branch is being closed
Comment 11 Jakub Jelinek 2022-06-28 10:37:25 UTC
GCC 10.4 is being released, retargeting bugs to GCC 10.5.
Comment 12 Richard Biener 2023-07-07 10:35:27 UTC
GCC 10 branch is being closed.
Comment 13 Drea Pinski 2024-02-20 07:31:45 UTC
I can't seem to reproduce this any more.
Comment 14 Jeffrey A. Law 2024-03-10 06:12:30 UTC
Same as c#13.  Removing gcc-14 regression marker.
Comment 15 Richard Biener 2024-07-19 13:05:02 UTC
GCC 11 branch is being closed.
Comment 16 Richard Biener 2025-07-11 08:59:06 UTC
GCC 12 branch is being closed.