Clang error: type A in generic association compatible with previously specified type B (err_assoc_compatible_types)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: type A in generic association compatible with previously specified type B
Type Error
Category Semantic Issue
Internal Id err_assoc_compatible_types
Internal Message type %0 in generic association compatible with previously specified type %1
Regular Expression (?:error|fatal error)\: type (.*?) in generic association compatible with previously specified type (.*?)
First Commit 2011-04-15 91147596414d C1X: implement generic selections

Description

The error is issued by the Clang compiler when a generic association within a _Generic selection statement contains types that are compatible with types specified in previous associations. This contravenes the C standard, specifically C11, which mandates that no two generic associations in the same generic selection shall specify compatible types. Generic selection allows for compile-time type-dependent selection, enabling the choice of different expressions based on the type of a given controlling expression. The presence of compatible types in this scenario suggests a logical redundancy or a misunderstanding in the generic selection specification, as identical or compatible types should not be enumerated more than once. This check ensures the uniqueness of each association in terms of the type it addresses, thereby preserving the functionality of the _Generic keyword to selectively execute code based on distinct types.  
AI Generated

Example

In the following example, a function f is defined to return an integer value. As part of its implementation, the function attempts to use a _Generic selection statement to choose an expression based on the type of its parameter x. However, the selection statement incorrectly includes multiple associations for the type int. This is contrary to the C standard, which requires that no two associations within the same _Generic selection specify compatible types. The code snippet illustrates an erroneous attempt to use the _Generic keyword by specifying duplicate types within its associations, leading to a compilation error. The error messages generated by the compiler emphasize that the type 'int' has been specified more than once, which is not allowed within a _Generic selection statement.  
AI Generated


Flags -xc -std=c11 -pedantic-errors

[Try out in Compiler Explorer]

Source
int f(int x) {
  _Generic(x, int: x, default: x, int: x); // Duplicate type 'int'
  return 0;
}
Compiler Output
<source>:2:35: error: type 'int' in generic association compatible with previously specified type 'int'
<source>:2:15: note: compatible type 'int' specified here
<source>:4:2: error: no newline at end of file [-Werror,-Wnewline-eof]


Clang Internals (17.0.6)

Git Commit Message

C1X: implement generic selections

As an extension, generic selection support has been added for all
supported languages.  The syntax is the same as for C1X.

llvm-svn: 129554

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

ExprResult Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, SourceLocation DefaultLoc, SourceLocation RParenLoc, bool PredicateIsExpr, void *ControllingExprOrType, ArrayRef<TypeSourceInfo *> Types, ArrayRef<Expr *> Exprs) {
  // ...
  for (unsigned i = 0; i < NumAssocs; ++i) {
    // ...
    if (Types[i]) {
      // ...
      if (Types[i]->getType()->isDependentType()) {
      // ...
      } else {
        // ...
        // C11 6.5.1.1p2 "No two generic associations in the same generic
        // selection shall specify compatible types."
        for (unsigned j = i + 1; j < NumAssocs; ++j)
          if (Types[j] && !Types[j]->getType()->isDependentType() && Context.typesAreCompatible(Types[i]->getType(), Types[j]->getType())) {
            Diag(Types[j]->getTypeLoc().getBeginLoc(), diag::err_assoc_compatible_types) << Types[j]->getTypeLoc().getSourceRange() << Types[j]->getType() << Types[i]->getType();

Triggered in Clang Tests

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

clang/test/Sema/attr-btf_type_tag.c

  • clang/test/Sema/attr-btf_type_tag.c:32:19: error: type 'int *' in generic association compatible with previously specified type 'int __attribute__((btf_type_tag("tag1")))*' (aka 'int *')