<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/141140>141140</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [HLSL] Investigate how to handle erroring/warning of out-of-bounds array access
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Icohedron
      </td>
    </tr>
</table>

<pre>
    Currently, out-of-bounds array accesses are inconsistently warned about.

In trivial cases such as https://godbolt.org/z/qnoe3advn an appropriate warning is produced
```hlsl
export float test(float a4[4]) {
    return a4[5];
}
/*
<source>:2:12: warning: array index 5 is past the end of the array (that has type 'float[4]') [-Warray-bounds]
    2 |     return a4[5];
      |            ^  ~
*/
```

But in a slightly less trivial case such as https://godbolt.org/z/vs3n1bjso no warning is produced unless `-Weverything` is provided on the command-line, and the warning is for a more generic case of "unsafe buffer access".
```hlsl
export float2 test(float a4[4]) {
    return { a4[4], a4[5] };
}
/* (with -Weverything)
<source>:2:14: warning: unsafe buffer access [-Wunsafe-buffer-usage]
    2 |     return { a4[4], a4[5] };
      | ^~
<source>:2:21: warning: unsafe buffer access [-Wunsafe-buffer-usage]
    2 |     return { a4[4], a4[5] };
      | 
*/
```

The `-Warray-bounds` warning should be more consistently applied to all cases where array bounds and the index are statically determinable.

We may also want to consider treating out-of-bounds array accesses as errors rather than warnings. If we want to do so, we should also consider the implications of such a change: Do we want this to be an HLSL-specific error, or an error for all languages?  
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMVU2P4zYM_TXKhXBgy3Y8OfiQzGzQAfbWAnOmLdrSQpFcSU42PexvLyQn87GdmW5vDQLHCiXykXx8Qu_VaIhaVu9Z_bDCOUjr2sfeShLOmlVnxaW9n50jE_SF8Xuwc8jskHV2NsIDOocXwL4n7ykuCZTprfHKh3QEzugMCcDOzmHN8h3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