Bug 48911 - [C++0x] Error for valid array subscript
Summary: [C++0x] Error for valid array subscript
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: 4.6.1
Assignee: Jason Merrill
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-06 05:50 UTC by sscrisk
Modified: 2011-05-06 22:01 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-05-06 09:27:51


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description sscrisk 2011-05-06 05:50:38 UTC
Source:

int main()
{
 {
  constexpr int array[1] = {0};
  constexpr int i = array[0]; // OK.
 }
 {
  constexpr int array[1] = {};
  constexpr int i = array[0]; // error: array subscript out of bound. Why?
 }
}


Compile errors:

a.cpp: In function 'int main()':
a.cpp:5:17: warning: unused variable 'i' [-Wunused-variable]
a.cpp:9:28: error: array subscript out of bound
a.cpp:9:17: warning: unused variable 'i' [-Wunused-variable]


Access to array[0] is valid. But, compiler says "that's an error."
Comment 1 Paolo Carlini 2011-05-06 09:27:51 UTC
Confirmed mainline and 4_6-branch.
Comment 2 Jakub Jelinek 2011-05-06 11:00:19 UTC
Better testcase:

// PR c++/48911
// { dg-do compile }
// { dg-options "-std=c++0x" }

struct A
{
  constexpr A () : a (6) {}
  int a;
};

int
main ()
{
  constexpr int a[1] = { 0 };
  constexpr int i = a[0];
  constexpr int b[1] = { };
  constexpr int j = b[0];
  constexpr char c[2] = "a";
  constexpr char k = c[1];
  constexpr char d[2] = "";
  constexpr char l = d[1];
  constexpr wchar_t e[2] = L"a";
  constexpr wchar_t m = e[1];
  constexpr wchar_t f[2] = L"";
  constexpr wchar_t n = f[1];
  constexpr A g[2] = { A () };
  constexpr A o = g[0];
  constexpr A p = g[1];
}

which covers also the case where ary is not CONSTRUCTOR, but STRING_CST.
I think we want to to the len check as done right now, but if index is above len, we shouldn't error or set non-constant unconditionally, instead we should check domain of TREE_TYPE (oldary).  If it is within the range, we want to return zero of the appropriate type for PODs, not sure what exactly for constexpr non-PODs.
Comment 3 Jason Merrill 2011-05-06 21:57:45 UTC
Author: jason
Date: Fri May  6 21:57:41 2011
New Revision: 173510

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173510
Log:
	PR c++/48911
	* semantics.c (cxx_eval_array_reference): Handle implicit
	initializers.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-missing.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/semantics.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Jason Merrill 2011-05-06 21:58:40 UTC
Author: jason
Date: Fri May  6 21:58:37 2011
New Revision: 173515

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173515
Log:
	PR c++/48911
	* semantics.c (cxx_eval_array_reference): Handle implicit
	initializers.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/constexpr-missing.C
Modified:
    branches/gcc-4_6-branch/gcc/cp/ChangeLog
    branches/gcc-4_6-branch/gcc/cp/semantics.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
Comment 5 Jason Merrill 2011-05-06 22:01:27 UTC
Fixed for 4.6.1.