Clang error: subscript of pointer to ... type B (err_subscript_incomplete_or_sizeless_type)

From emmtrix Wiki
Jump to navigation Jump to search
Text
error: subscript of pointer to
incomplete
sizeless
type B

(since 11.0)

Type Error
Category Semantic Issue (since 11.0)
Internal Id err_subscript_incomplete_or_sizeless_type (since 11.0)
Internal Message subscript of pointer to %select{incomplete|sizeless}0 type %1 (since 11.0)
Regular Expression (?:error|fatal error)\: subscript of pointer to (?:incomplete|sizeless) type (.*?)
First Commit 2019-12-21 931fcd3ba011 [WebAssembly] Improve clang diagnostics for wasm attributes

Description

The error is issued by the Clang compiler when an attempt is made to access an element of an array through a pointer, but the pointed-to type is either incomplete or sizeless. An incomplete type is one that has been declared but not defined; that is, its complete structure is unknown at the point of access. A sizeless type lacks a size usually because it is a new, specialized, or unbounded type that cannot have its size determined. This can occur in various contexts, including when working with dynamically allocated arrays, accessing elements of an array whose type is not fully known, or when templating code that operates on types that may be incomplete or sizeless under certain conditions.

The primary issue here is that the compiler needs to know the complete size and structure of a type in order to correctly compute the location of elements when using array subscript notation. Without this information, operations such as indexing into an array or pointer arithmetic become impossible for the compiler to resolve safely and accurately, leading to this error.

The conditions for this error include:

  • A type used in an array or pointed to by a pointer is declared but not defined in the scope where it is accessed.
  • A type does not have a known size at the point where an array subscript operation is attempted.

Resolving this error typically involves ensuring that any types used in an array or as the target of a pointer are fully defined before being accessed with array subscript syntax. This may require including the appropriate headers, moving type definitions to an appropriate location, or otherwise structuring the code to ensure complete type information is available where needed.

 
AI Generated

Example

In the following example, a structure named S is forward-declared, which means its complete definition is not provided at this point. Then, a std::unique_ptr to an array of S objects is created using new S[10]. However, because S is an incomplete type at the time of allocation, the compiler cannot determine the size of S, leading to an error when trying to allocate memory for an array of S. Additionally, the attempt to access an element of the array through p[0] results in further errors. The compiler's error messages highlight the issue with attempting to perform operations on pointers to incomplete types, particularly allocation and element access, operations which require complete type information. These errors demonstrate the necessity of having a complete type definition for classes or structures when performing such operations.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
#include <memory>
struct S; // Forward declaration
int main() {
  std::unique_ptr<S[]> p(new S[10]); // Allocation
  auto o = p[0]; // Error: S is incomplete
}
Compiler Output
<source>:4:30: error: allocation of incomplete type 'S'
<source>:2:8: note: forward declaration of 'S'
<source>:5:8: error: variable has incomplete type 'S'
<source>:2:8: note: forward declaration of 'S'
In file included from <source>:1:
In file included from /opt/compiler-explorer/gcc-13.2.0/lib/gcc/x86_64-linux-gnu/13.2.0/../../../../include/c++/13.2.0/memory:78:
/opt/compiler-explorer/gcc-13.2.0/lib/gcc/x86_64-linux-gnu/13.2.0/../../../../include/c++/13.2.0/bits/unique_ptr.h:725:9: error: subscript of pointer to incomplete type 'S'
<source>:5:13: note: in instantiation of member function 'std::unique_ptr<S[]>::operator[]' requested here
<source>:2:8: note: forward declaration of 'S'


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 6025)

ExprResult Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, Expr *Idx, SourceLocation RLoc) {
  // ...
  } else if (!ResultType->isDependentType() && !ResultType.isWebAssemblyReferenceType() && RequireCompleteSizedType(LLoc, ResultType, diag::err_subscript_incomplete_or_sizeless_type, BaseExpr))

Triggered in Clang Tests

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

clang/test/Sema/crash-invalid-array.c

  • clang/test/Sema/crash-invalid-array.c:13:9: error: subscript of pointer to incomplete type 'int[]'