https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70228
Bug ID: 70228 Summary: insufficient detail in diagnostics for a constexpr out of bounds array subscript Product: gcc Version: 6.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- Diagnostics issued for out of bounds array references in non-trivial constexpr don't provide sufficient detail to quickly root cause the problem. For example, the diagnostic printed for the program below makes determining the value that exceeded the bounds of the array and what those bounds are more difficult than it should be. Since the index value and the bounds are known at translation time, they both should be mentioned in the text of the diagnostic. $ cat v.c && /home/msebor/build/gcc-trunk-git/gcc/xgcc -B/home/msebor/build/gcc-trunk-git/gcc -S -Wall -Wextra -Wpedantic -o/dev/null -xc++ v.c constexpr int a[] = { 1, 2, 3 }; constexpr int f (int i) { int j = i + 1; return a[j]; } constexpr int g (int i) { int j = i + 1; return f (j); } constexpr int i = g (0); constexpr int x = g (i); v.c:16:21: in constexpr expansion of ‘g(3)’ v.c:12:14: in constexpr expansion of ‘f(j)’ v.c:16:23: error: array subscript out of bound constexpr int x = g (i); ^ For an example of what a more helpful diagnostic might look like consider the output printed by Clang for the same program: v.c:16:15: error: constexpr variable 'x' must be initialized by a constant expression constexpr int x = g (i); ^ ~~~~~ v.c:6:12: note: cannot refer to element 5 of array of 3 elements in a constant expression return a[j]; ^ v.c:12:12: note: in call to 'f(4)' return f (j); ^ v.c:16:19: note: in call to 'g(3)' constexpr int x = g (i); ^ 1 error generated.