[PATCH] D135920: [clang][Sema] Use correct array size for diagnostic

Shafik Yaghmour via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 17 17:02:00 PDT 2022


shafik requested changes to this revision.
shafik added a comment.
This revision now requires changes to proceed.

The current approach of mixing bytes and indices in the same diagnostic is too confusing. I think we see some acceptable messages for out of bounds access by looking at three different implementations diagnostic for OOB access in a constant expression context:

  int main() {
      constexpr int arr[1]{};
      constexpr int x = arr[3];
  }

We have clang which produces:

  <source>:3:23: note: cannot refer to element 3 of array of 1 element in a constant expression
      constexpr int x = arr[3];
                        ^

gcc produces:

  <source>:3:28: error: array subscript value '3' is outside the bounds of array 'arr' of type 'const int [1]'
      3 |     constexpr int x = arr[3];
        |                       ~~~~~^

and MSVC produces:

  <source>(3): note: failure was caused by out of range index 3; allowed range is 0 <= index < 1

I think any approach similar to this would be acceptable.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135920/new/

https://reviews.llvm.org/D135920



More information about the cfe-commits mailing list