Clang error: '__kindof' specifier cannot be applied to non-object type A (err_objc_kindof_nonobject)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: '__kindof' specifier cannot be applied to non-object type A
Type Error
Category Semantic Issue
Internal Id err_objc_kindof_nonobject
Internal Message '__kindof' specifier cannot be applied to non-object type %0
Regular Expression (?:error|fatal error)\: '__kindof' specifier cannot be applied to non\-object type (.*?)
First Commit 2015-07-07 ab209d83be5d Implement the Objective-C __kindof type qualifier.

Description

The error is issued by the Clang compiler when the __kindof specifier is applied to a type that is not an Objective-C object. The __kindof qualifier is specific to Objective-C and is intended for use with Objective-C object types, providing them with a degree of polymorphism similar to the id type by allowing implicit downcasting to specific subclasses. This error occurs because the specifier is mistakenly used with a non-object type, such as a primitive type, struct, enum, or any other type that does not represent an Objective-C object. In Objective-C, an object type is typically a pointer to a class instance. Applying __kindof to non-object types contradicts its intended use and results in this compilation error, requiring the programmer to either remove the specifier or correct the type to an appropriate Objective-C object type.  
AI Generated

Example

In the following example, the __kindof specifier is erroneously applied to an integer type, which is a non-object type in Objective-C. The __kindof qualifier is designed exclusively for use with Objective-C object types, which are typically pointers to class instances, to facilitate polymorphic behavior similar to the id type. This specifier allows for implicit downcasting to specific subclasses, enhancing type safety and flexibility in handling various Objective-C objects. However, applying it to a primitive type like int deviates from its intended purpose, leading to a compilation error as demonstrated. The compiler detects this misuse and issues an error message, indicating that __kindof cannot be applied to the specified type, thereby prompting the programmer to correct the type to an appropriate Objective-C object type or remove the __kindof specifier altogether.  
AI Generated


Flags -xobjective-c

[Try out in Compiler Explorer]

Source
int main() {
  __kindof int x; // __kindof applied to non-object type 'int'
  return 0;
}
Compiler Output
<source>:2:3: error: '__kindof' specifier cannot be applied to non-object type 'int'


Clang Internals (17.0.6)

Git Commit Message

Implement the Objective-C __kindof type qualifier.

The __kindof type qualifier can be applied to Objective-C object
(pointer) types to indicate id-like behavior, which includes implicit
"downcasting" of __kindof types to subclasses and id-like message-send
behavior. __kindof types provide better type bounds for substitutions
into unspecified generic types, which preserves more type information.

llvm-svn: 241548

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/SemaType.cpp (line 7596)

/// Check the application of the Objective-C '__kindof' qualifier to
/// the given type.
static bool checkObjCKindOfType(TypeProcessingState &state, QualType &type, ParsedAttr &attr) {
  // ...
  // If not, we can't apply __kindof.
  if (!objType) {
    // ...
    S.Diag(attr.getLoc(), diag::err_objc_kindof_nonobject) << type;

Triggered in Clang Tests

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

clang/test/SemaObjC/kindof.m

  • clang/test/SemaObjC/kindof.m:64:9: error: '__kindof' specifier cannot be applied to non-object type 'int'
  • clang/test/SemaObjC/kindof.m:66:9: error: '__kindof' specifier cannot be applied to non-object type 'NSObject_ptr_ptr' (aka 'NSObject **')