Bug 63904 - ICE when accessing array member of constexpr struct
Summary: ICE when accessing array member of constexpr struct
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2014-11-16 21:38 UTC by Benno Evers
Modified: 2014-11-27 13:07 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-11-18 00:00:00


Attachments
Preprocessed source (333 bytes, text/x-csrc)
2014-11-16 21:38 UTC, Benno Evers
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Benno Evers 2014-11-16 21:38:31 UTC
Created attachment 33994 [details]
Preprocessed source

The following code causes an internal compiler error:


template<int N>
struct foo {
    constexpr foo() : a() {}
    int a[N];
};

int main() {
    foo< (foo<1>{}).a[0] > f;
}


constexpr-initalization.cpp:7:25:   in constexpr expansion of ‘f.foo<N>::foo<0>()’
constexpr-initalization.cpp:7:25: internal compiler error: in tree_low_cst, at tree.h:4849
 foo< (foo<1>{}).a[0] > f;
Comment 1 Kai Tietz 2014-11-18 11:26:17 UTC
It seems to be related to constexpr.c:cxx_eval_vec_init_1 function.  The line ' int max = tree_to_shwi (array_type_nelts (atype));' there should be changed into 'int max = (int) tree_to_uhwi (array_type_nelts (atype));'.

Issue is that array_type_netls returns size_type node (which is unsigned) with UHWI_MAX as value.  Means that on conversions the value won't fit into shwi.  By reading value as unsigned and then later on casting it to signed (btw shouldn't we use here instead HOST_WIDE_INT?), we solve this issue.
Comment 2 Kai Tietz 2014-11-27 12:57:30 UTC
Author: ktietz
Date: Thu Nov 27 12:56:58 2014
New Revision: 218122

URL: https://gcc.gnu.org/viewcvs?rev=218122&root=gcc&view=rev
Log:
	PR c++/63904
	* g++.dg/cpp0x/pr63904.C: New.


Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/pr63904.C
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 3 Kai Tietz 2014-11-27 13:03:16 UTC
Author: ktietz
Date: Thu Nov 27 13:02:45 2014
New Revision: 218123

URL: https://gcc.gnu.org/viewcvs?rev=218123&root=gcc&view=rev
Log:
2014-11-27  Kai Tietz  <ktietz@redhat.com>

	PR c++/63904
	* constexpr.c (cxx_eval_vec_init_1): Avoid
	type-overflow issue.


Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/constexpr.c
Comment 4 Kai Tietz 2014-11-27 13:07:03 UTC
So fixed.