Clang error: matrix ... index is outside the allowed range [0, B) (err_matrix_index_outside_range)

From emmtrix Wiki
Jump to navigation Jump to search
Text
error: matrix
row
column
index is outside the allowed range [0, B)

(since 11.0)

Type Error
Category None (since 11.0)
Internal Id err_matrix_index_outside_range (since 11.0)
Internal Message matrix %select{row|column}0 index is outside the allowed range [0, %1) (since 11.0)
Regular Expression (?:error|fatal error)\: matrix (?:row|column) index is outside the allowed range \[0, (.*?)\)
First Commit 2019-12-21 931fcd3ba011 [WebAssembly] Improve clang diagnostics for wasm attributes

Description

The error is issued by the Clang compiler when there is an attempt to access a matrix element using an index that is outside the valid range for either the rows or the columns of the matrix. Matrix types, in certain programming contexts such as with Clang extensions or attributes that denote specific matrix dimensions, enforce strict bounds on the indices that can be used to access elements within them. The valid range of indices is determined by the dimensions of the matrix, starting from 0 up to but not including the maximum size specified for the row or column. Attempts to access an element outside of these bounds result in this error, indicating a boundary violation that could lead to undefined behavior or memory access errors. The diagnostic message specifies whether the row or column index is outside the allowed range, presenting the permissible bounds in the format [0, N), where N is the limit for the index that is exclusive.  
AI Generated

Example

In the following example, a matrix is defined using a specialized attribute that denotes its dimensions as \(2 \times 2\). An attempt is made to assign a value to an element of the matrix using an index that is outside the valid range for row indices. Specifically, an index of \(2\) is used for the row, despite the fact that valid row indices for this matrix are only \(0\) and \(1\), corresponding to the allowed range \([0, 2)\) where \(2\) is the upper limit but is not included in the range. This results in a compilation error indicating that the row index used is outside the allowed range. The error output demonstrates how the Clang compiler enforces strict bounds checking on matrix indices to prevent access to out-of-bounds elements, thereby avoiding undefined behavior or memory access errors.  
AI Generated


Flags -xc -fclang-abi-compat=17.0 -fenable-matrix

[Try out in Compiler Explorer]

Source
int main() {
  float m __attribute__((matrix_type(2, 2)));
  m[2][0] = 1.0; // Invalid row index
}
Compiler Output
<source>:2:26: error: 'matrix_type' attribute only applies to typedefs
<source>:3:5: error: matrix row index is outside the allowed range [0, 2)


Clang Internals (17.0.6)

Git Commit Message

[WebAssembly] Improve clang diagnostics for wasm attributes

This patch addresses the review comments on r352930:

 - Removes redundant diagnostic checking code
 - Removes errnoneous use of diag::err_alias_is_definition, which
   turned out to be ineffective anyway since functions can be defined later
   in the translation unit and avoid detection.
 - Adds a test for various invalid cases for import_name and import_module.

Differential Revision: https://reviews.llvm.org/D59520

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaExpr.cpp (line 5302)

ExprResult Sema::CreateBuiltinMatrixSubscriptExpr(Expr *Base, Expr *RowIdx, Expr *ColumnIdx, SourceLocation RBLoc) {
  // ...
  // Check that IndexExpr is an integer expression. If it is a constant
  // expression, check that it is less than Dim (= the number of elements in the
  // corresponding dimension).
  auto IsIndexValid = [&](Expr *IndexExpr, unsigned Dim, bool IsColumnIdx) -> Expr * {
    // ...
    if (std::optional<llvm::APSInt> Idx = IndexExpr->getIntegerConstantExpr(Context)) {
      if ((*Idx < 0 || *Idx >= Dim)) {
        Diag(IndexExpr->getBeginLoc(), diag::err_matrix_index_outside_range) << IsColumnIdx << Dim;

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/Sema/matrix-type-operators.c

  • clang/test/Sema/matrix-type-operators.c:154:5: error: matrix row index is outside the allowed range [0, 5)
  • clang/test/Sema/matrix-type-operators.c:156:8: error: matrix column index is outside the allowed range [0, 10)
  • clang/test/Sema/matrix-type-operators.c:158:8: error: matrix column index is outside the allowed range [0, 10)
  • clang/test/Sema/matrix-type-operators.c:160:5: error: matrix row index is outside the allowed range [0, 5)
  • clang/test/Sema/matrix-type-operators.c:162:5: error: matrix row index is outside the allowed range [0, 5)
  • clang/test/Sema/matrix-type-operators.c:164:8: error: matrix column index is outside the allowed range [0, 10)
  • clang/test/Sema/matrix-type-operators.c:166:5: error: matrix row index is outside the allowed range [0, 5)
  • clang/test/Sema/matrix-type-operators.c:179:16: error: matrix row index is outside the allowed range [0, 5)
  • clang/test/Sema/matrix-type-operators.c:210:16: error: matrix row index is outside the allowed range [0, 5)
  • clang/test/Sema/matrix-type-operators.c:212:19: error: matrix column index is outside the allowed range [0, 10)
  • clang/test/Sema/matrix-type-operators.c:214:16: error: matrix row index is outside the allowed range [0, 5)
  • clang/test/Sema/matrix-type-operators.c:216:16: error: matrix row index is outside the allowed range [0, 5)
  • clang/test/Sema/matrix-type-operators.c:218:20: error: matrix column index is outside the allowed range [0, 10)
  • clang/test/Sema/matrix-type-operators.c:220:17: error: matrix row index is outside the allowed range [0, 5)