Clang error: instance variable A has conflicting type: B vs C (err_conflicting_ivar_type)

From emmtrix Wiki
Jump to navigation Jump to search
Text
error: instance variable A has conflicting type
: B vs C
 

Type Error
Category Semantic Issue
Internal Id err_conflicting_ivar_type
Internal Message instance variable %0 has conflicting type%diff{: $ vs $|}1,2
Regular Expression (?:error|fatal error)\: instance variable (.*?) has conflicting type(?:\: (.*?) vs (.*?)|)
First Commit 2009-03-04 b1c4d5507fad The basic representation of diagnostics information in tablegen format, plus (uncommented and incomp...

Description

The error is issued by the Clang compiler when there is a mismatch between the declared types of an instance variable within an Objective-C class interface and its implementation. In Objective-C, each instance variable (ivar) declared in a class interface must have a corresponding implementation that matches in type. This consistency ensures that the object's layout in memory is correctly understood by the compiler, allowing it to generate accurate machine code for object access and manipulation.

When an instance variable is declared in the class interface with a certain type and then implemented in the class implementation with a different type, the Clang compiler detects this discrepancy and generates the error message. This situation could arise due to a mistake in typing, misunderstanding of type definitions, or a change in one part of the code that was not mirrored in the other part. The error aims to alert the developer to the inconsistency that could lead to undefined behavior, crashes, or data corruption at runtime, as the compiler's assumptions about the object's layout no longer hold true.

* The error indicates a type mismatch for an instance variable between its declaration (interface) and its implementation.
* It ensures that the compiler's and the program's view of the object's memory layout are in alignment.
* Addressing this error is crucial for preventing runtime issues that stem from incorrect assumptions about data types and object layout.
 
AI Generated

Example

In the following example, an Objective-C class named C is defined with an instance variable <code>x</code> of type <code>int</code> in its interface. However, in the class's implementation, the instance variable <code>x</code> is redefined with a type of <code>float</code>. The Clang compiler identifies this discrepancy in the type of the instance variable <code>x</code> between the class's interface and its implementation. As a result, the compiler emits an error message indicating that the instance variable <code>'x'</code> has a conflicting type: <code>'float'</code> versus <code>'int'</code>. Additionally, the compiler generates a note pointing to the original declaration of the instance variable <code>x</code> in the class interface, emphasizing where the initial type was defined. Another warning is issued because the class C is defined without specifying a base class, which is a separate issue from the type conflict but also highlighted by the compiler to guide the correction process.
 
AI Generated


Flags -xobjective-c

[Try out in Compiler Explorer]

Source
@interface C { int x; } @end @implementation C { float x; } @end
Compiler Output
<source>:1:56: error: instance variable 'x' has conflicting type: 'float' vs 'int'
<source>:1:20: note: previous definition is here
<source>:1:12: warning: class 'C' defined without specifying a base class [-Wobjc-root-class]
<source>:1:13: note: add a super class to fix this problem


Clang Internals (17.0.6)

Git Commit Message

The basic representation of diagnostics information in tablegen format, plus (uncommented and incomplete) test conversions of the existing def files to this format.

llvm-svn: 66064

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/SemaDeclObjC.cpp (line 2206)

void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, ObjCIvarDecl **ivars, unsigned numIvars, SourceLocation RBrace) {
  // ...
  for (; numIvars > 0 && IVI != IVE; ++IVI) {
    // ...
    // First, make sure the types match.
    if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
      Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) << ImplIvar->getIdentifier() << ImplIvar->getType() << ClsIvar->getType();

Triggered in Clang Tests

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

clang/test/SemaObjC/class-bitfield.m

  • clang/test/SemaObjC/class-bitfield.m:32:9: error: instance variable 'isa' has conflicting type: 'char *' vs 'void *'