Clang error: unexpected interface name A: expected expression (err_unexpected_interface)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: unexpected interface name A: expected expression
Type Error
Category Semantic Issue
Internal Id err_unexpected_interface
Internal Message unexpected interface name %0: expected expression
Regular Expression (?:error|fatal error)\: unexpected interface name (.*?)\: expected expression
First Commit 2009-03-14 5a8987ca5113 Update tablegen diagnostic files to be in sync with the def files.

Description

The error is issued by the Clang compiler when an interface name is used in a context where an expression was expected. This situation usually occurs in Objective-C programming when attempting to call a method on a class, but the class name is used directly instead of an instance of the class, or when the syntax for invoking methods is not correctly followed. The compiler expects an expression that evaluates to an object or a value, but instead finds an interface name, leading to this error. This issue may arise from a misunderstanding of how to properly call class methods or from a simple typographical error in the code. The error signals that the code, as written, cannot be executed as intended because the compiler cannot interpret the interface name as a valid operand in an expression.  
AI Generated

Example

In the following example, an interface named C is defined without specifying a base class, which is a common practice for defining classes in Objective-C. The interface declares a class method + (void)f that is marked with the objc_direct attribute. This attribute suggests that the method f is intended to be directly callable, bypassing the standard Objective-C message sending machinery. However, the objc_direct attribute's implementation details are not the focus here.

The class method f is then implemented within the @implementation block for the interface C. In the main function, an attempt is made to call the class method f directly on the class C itself. However, the syntax used casts the class C to an identifier (id) before calling f. This is where the crux of the example lies: despite the syntax suggesting an attempt to work around how class methods are typically called, this particular approach misunderstands how Objective-C expects expressions involving class names and method calls to be structured.

Instead of being recognized as a valid call to the class method f, the compiler interprets the interface name C as an unexpected element in the place where an expression was expected. This misunderstanding between the intended use of interface names and the compiler's expectations for expressions leads to the issuance of the error message documented.

The warnings about the objc_direct attribute not being implemented by the runtime and the class C being defined without a base class are also presented, but the core issue triggering the error message is the misuse of the class name C in an attempt to directly call the class method f.

 
AI Generated


Flags -xobjective-c

[Try out in Compiler Explorer]

Source
// Interface definition without a base class
@interface C

// Method flagged as directly callable but not supported
+ (void)f __attribute__((objc_direct));
@end

@implementation C
+ (void)f {}
@end

int main() {
  // Trying to call 'f' on 'C' directly, but 'C' is used as a type instead of an object
  [(id)C f];
  return 0;
}
Compiler Output
<source>:5:26: warning: 'objc_direct' attribute isn't implemented by this Objective-C runtime [-Wignored-attributes]
<source>:2:12: warning: class 'C' defined without specifying a base class [-Wobjc-root-class]
<source>:2:13: note: add a super class to fix this problem
<source>:14:8: error: unexpected interface name 'C': expected expression


Clang Internals (17.0.6)

Git Commit Message

Update tablegen diagnostic files to be in sync with the def files.

llvm-svn: 67004

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

/// Diagnoses obvious problems with the use of the given declaration
/// as an expression.  This is only actually called for lookups that
/// were not overloaded, and it doesn't promise that the declaration
/// will in fact be used.
static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D, bool AcceptInvalid) {
  // ...
  if (isa<ObjCInterfaceDecl>(D)) {
    S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName();

Triggered in Clang Tests

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

clang/test/SemaObjC/crash-on-type-args-protocols.m

  • clang/test/SemaObjC/crash-on-type-args-protocols.m:23:15: error: unexpected interface name 'X': expected expression