Clang error: illegal type A used in a boxed expression (err_objc_illegal_boxed_expression_type)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: illegal type A used in a boxed expression
Type Error
Category Semantic Issue
Internal Id err_objc_illegal_boxed_expression_type
Internal Message illegal type %0 used in a boxed expression
Regular Expression (?:error|fatal error)\: illegal type (.*?) used in a boxed expression
First Commit 2012-04-19 0caa39474bfc Implements boxed expressions for Objective-C. <rdar://problem/10194391>

Description

The error is issued by the Clang compiler when an attempt is made to use a type in a boxed expression that is not allowed according to Objective-C conventions. Boxed expressions in Objective-C allow basic C types and enumerations to be wrapped in NSNumber or NSValue objects automatically, simplifying syntax and improving code readability. However, not all types can be boxed. Specifically, this error message is generated when the type being boxed does not meet the necessary criteria set for such expressions. This could be due to the type being a pointer to an incomplete type, a structure, or any other type that Objective-C does not support for boxing. The compiler detects these cases and issues an error to indicate that the type cannot be used in this context.  
AI Generated

Example

In the following example, an attempt is made to use a boxed expression with an illegal type. The code defines an interface for class C but leaves it as an incomplete type, meaning it is declared but not defined. The main function attempts to box a pointer to this incomplete type, C, which is not permitted by Objective-C's conventions for boxed expressions. Boxed expressions are typically used for wrapping basic C types and enumerations in NSNumber or NSValue objects. However, a pointer to an incomplete type does not meet the criteria for these expressions, leading to the compiler error that indicates an illegal type was used in a boxed expression. Additionally, the code example also demonstrates a deprecated practice of defining a function within an Objective-C container and a syntax error for missing the @end directive, further contributing to its illustrative purpose of common mistakes related to boxed expressions in Objective-C.  
AI Generated


Flags -xobjective-c

[Try out in Compiler Explorer]

Source
@interface C; // Incomplete type definition

int main() {
  @((C*)0); // Illegal type used in boxed expression
  return 0;
}
Compiler Output
<source>:3:5: warning: function definition inside an Objective-C container is deprecated [-Wfunction-def-in-objc-container]
<source>:4:3: error: illegal type 'C *' used in a boxed expression
<source>:6:2: error: missing '@end'
<source>:1:1: note: class started here


Clang Internals (17.0.6)

Git Commit Message

Implements boxed expressions for Objective-C. <rdar://problem/10194391>

llvm-svn: 155082

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/SemaExprObjC.cpp (line 731)

ExprResult Sema::BuildObjCBoxedExpr(SourceRange SR, Expr *ValueExpr) {
  // ...
  if (!BoxingMethod) {
    Diag(Loc, diag::err_objc_illegal_boxed_expression_type) << ValueType << ValueExpr->getSourceRange();

Triggered in Clang Tests

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

clang/test/SemaObjC/objc-literal-nsnumber.m

  • clang/test/SemaObjC/objc-literal-nsnumber.m:16:13: error: illegal type 'int' used in a boxed expression
  • clang/test/SemaObjC/objc-literal-nsnumber.m:26:13: error: illegal type 'int' used in a boxed expression