C macro _Generic gives unexpected compiler error

bd2357

Using gcc.exe (Rev3, Built by MSYS2 project) 8.2.0.

I was trying to build a macro to automatically do type conversions between two types, where the two parameters should never be the same type. My problem is the compiler throws an error if I don't also include the same type case. What I wanted:

#include <stdio.h>
#include <stdint.h>
// Macro to return string based on two different types
#define bob( to, from ) \
    _Generic( to , \
    int32_t: _Generic(from,  \
      int16_t: "s-l", \
      int8_t:  "c-l" ) , \
    int16_t: _Generic(from, \
      int32_t: "l-s", \
      int8_t:  "c-s") , \
    int8_t:_Generic(from, \
      int32_t: "l-c",  \
      int16_t: "s-c")  \
    )

    void main(void)
    {
        int32_t i1;
        int16_t s1;
        int8_t  c1;

        printf("%s\n", bob(i1,s1));
        printf("%s\n", bob(i1,c1));
        printf("%s\n", bob(s1,c1));
        printf("%s\n", bob(s1,i1));
        printf("%s\n", bob(c1,s1));
        printf("%s\n", bob(c1,s1));

    }

$ gcc gbug.c -o gbug.exe
gbug.c: In function 'main':
gbug.c:23:27: error: '_Generic' selector of type 'short int' is not compatible with any association
     printf("%s\n", bob(i1,s1));
                           ^~
gbug.c:9:19: note: in definition of macro 'bob'
 int16_t: _Generic(from, \
                   ^~~~
gbug.c:24:27: error: '_Generic' selector of type 'signed char' is not compatible with any association
     printf("%s\n", bob(i1,c1));
                           ^~
gbug.c:12:17: note: in definition of macro 'bob'
 int8_t:_Generic(from, \
                 ^~~~
gbug.c:25:27: error: '_Generic' selector of type 'signed char' is not compatible with any association
     printf("%s\n", bob(s1,c1));
                           ^~
gbug.c:12:17: note: in definition of macro 'bob'
 int8_t:_Generic(from, \
                 ^~~~
gbug.c:26:27: error: '_Generic' selector of type 'int' is not compatible with any association
     printf("%s\n", bob(s1,i1));
                           ^~
gbug.c:6:19: note: in definition of macro 'bob'
 int32_t: _Generic(from,  \
                   ^~~~
gbug.c:27:27: error: '_Generic' selector of type 'short int' is not compatible with any association
     printf("%s\n", bob(c1,s1));
                           ^~
gbug.c:9:19: note: in definition of macro 'bob'
 int16_t: _Generic(from, \
                   ^~~~
gbug.c:28:27: error: '_Generic' selector of type 'short int' is not compatible with any association
     printf("%s\n", bob(c1,s1));
                           ^~
gbug.c:9:19: note: in definition of macro 'bob'
 int16_t: _Generic(from, \

This example is the simplest that I have found that will fail.

If I add in the "same type" conversion lines like this:

#define bob( to, from ) \
_Generic( to , \
int32_t: _Generic(from,  \
  int16_t: "s-l", \
  int32_t: "bug", \
  int8_t: "c-l" ) , \
int16_t: _Generic(from, \
  int32_t: "l-s", \
  int16_t: "bug", \
  int8_t: "c-s") , \
int8_t:_Generic(from, \
  int32_t: "l-c",  \
  int8_t: "bug", \
  int16_t: "s-c")  \
)

It build and runs with the expected result:

$ ./gbug.exe
s-l
c-l
c-s
l-s
s-c
s-c

verifying that I am not using the macro to expand any same type conditions. I understand _Generic is not a string substitution macro but I also thought that if you could use it without a default case it would correctly throw a compile error if you used an unknown type (or an unsupported combination of types, which is the behaviour I wanted) It is like the pre-processor is getting the two macro parameters mixed up.

Edit: So I have a better understanding, (See my answer below) but still looking to get the macro to throw a compile error if the two parameters are the same type. So far I have a trick to force a link error which is still better than a runtime error.

Artyer

The problem is that every branch of a generic selection must be valid, even if they are not evaluated.

For example, your first macro:

bob(i1, s1)

Expands to (types added for clarity):

_Generic( ((int32_t) i1),
  int32_t: _Generic( ((int16_t) s1),
    int16_t: "s-l",
    int8_t:  "c-l" ),
  int16_t: _Generic( ((int16_t) s1),  // The error is here
    int32_t: "l-s",
    int8_t:  "c-s"),
  int8_t:_Generic( ((int16_t) s1),
    int32_t: "l-c",
    int16_t: "s-c")
)

Obviously the uint32_t branch is valid: It just selects "s-l". But the int16_t branch is not valid, as from (An int16_t itself) does not have a corresponding branch.

In this particular scenario, it wouldn't hurt to add a self-conversion operator that does nothing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Generic permutation function element comparison gives compiler error

From Dev

Unexpected compiler error when attempting to use generic type in closure

From Dev

compiler error c1001 in generic class

From Dev

Macro to raise compiler error for string literal C++

From Dev

MongoDb "AnyIn" filter method in C# gives compiler error

From Dev

Unable to install Ruby 2.5.1 using rbenv, gives C Compiler error

From Dev

Intel C++ compiler gives "offload constructs are not supported on this platform" error

From Dev

not " using namespace std; " gives me an unexpected error (C++)

From Dev

c# compiler error when passing `this` to a delegate that accepts generic parameter

From Dev

Unexpected "method not found" compiler error

From Dev

Generic error: Runtime compiler is not loaded,

From Dev

GLSL Compiler Error error C0000: syntax error, unexpected identifier, expecting "::" at token <var>

From Dev

Why does call to an undefined function does not give a compiler error and gives linker error in C?

From Dev

C++ macro gives C4430 error: Missing type specifier

From Dev

Why using a parenthesis in define macro gives error?

From Dev

Macro causes compiler error with name of NSString variable

From Dev

Compiler gives lambda function cast error

From Dev

Comparing value of enum gives compiler error

From Dev

Why Kotlin compiler gives an error in a valid IF statement?

From Dev

Firebase and Create React Gives 'Unexpected token <' Error

From Dev

ZipArchive gives Unexpected end of data corrupted error

From Dev

Firebase console gives error "Unexpected .read"

From Dev

iPython gives error for unexpected keyword argument 'inputhook'

From Dev

Simple, but correct equation gives "Error, "-" unexpected" in Maple

From Dev

Keras output layer gives unexpected error

From Dev

SQLCMD - Username gives error of unexpected argument

From Dev

Unexpected behavior and compiler error when using pointers

From Dev

Why c++ compiler gives error when using `std::max()` function for two different type of numeric variable

From Dev

why is it that my compiler gives me error when working with dynamically allocated memory in c++

Related Related

  1. 1

    Generic permutation function element comparison gives compiler error

  2. 2

    Unexpected compiler error when attempting to use generic type in closure

  3. 3

    compiler error c1001 in generic class

  4. 4

    Macro to raise compiler error for string literal C++

  5. 5

    MongoDb "AnyIn" filter method in C# gives compiler error

  6. 6

    Unable to install Ruby 2.5.1 using rbenv, gives C Compiler error

  7. 7

    Intel C++ compiler gives "offload constructs are not supported on this platform" error

  8. 8

    not " using namespace std; " gives me an unexpected error (C++)

  9. 9

    c# compiler error when passing `this` to a delegate that accepts generic parameter

  10. 10

    Unexpected "method not found" compiler error

  11. 11

    Generic error: Runtime compiler is not loaded,

  12. 12

    GLSL Compiler Error error C0000: syntax error, unexpected identifier, expecting "::" at token <var>

  13. 13

    Why does call to an undefined function does not give a compiler error and gives linker error in C?

  14. 14

    C++ macro gives C4430 error: Missing type specifier

  15. 15

    Why using a parenthesis in define macro gives error?

  16. 16

    Macro causes compiler error with name of NSString variable

  17. 17

    Compiler gives lambda function cast error

  18. 18

    Comparing value of enum gives compiler error

  19. 19

    Why Kotlin compiler gives an error in a valid IF statement?

  20. 20

    Firebase and Create React Gives 'Unexpected token <' Error

  21. 21

    ZipArchive gives Unexpected end of data corrupted error

  22. 22

    Firebase console gives error "Unexpected .read"

  23. 23

    iPython gives error for unexpected keyword argument 'inputhook'

  24. 24

    Simple, but correct equation gives "Error, "-" unexpected" in Maple

  25. 25

    Keras output layer gives unexpected error

  26. 26

    SQLCMD - Username gives error of unexpected argument

  27. 27

    Unexpected behavior and compiler error when using pointers

  28. 28

    Why c++ compiler gives error when using `std::max()` function for two different type of numeric variable

  29. 29

    why is it that my compiler gives me error when working with dynamically allocated memory in c++

HotTag

Archive