https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110703

            Bug ID: 110703
           Summary: Incorrect "-Wfloat-conversion" diagnostic with
                    _Generic
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jepler at unpythonic dot net
  Target Milestone: ---

When compiled with `gcc -Wfloat-conversion -c boo.c` the following code results
in a `-Wfloat-conversion` diagnostic across a range of gcc versions including
gcc version 12.2.0 (Debian 12.2.0-14) and godbolt "trunk" as of today
(https://godbolt.org/z/3K7sTTv1x)

```
#include <math.h>

#define tgsin(x) \
    _Generic((x), \
            double: sin((x)), \
            float: sinf((x)) \
            )

int f(double d) {
    return tgsin(d) > 0;
}
```

The result is
```
$ gcc -Wfloat-conversion -c boo.c
boo.c: In function ‘f’:
boo.c:10:18: warning: conversion from ‘double’ to ‘float’ may change value
[-Wfloat-conversion]
   10 |     return tgsin(d) > 0;
      |                  ^
boo.c:6:25: note: in definition of macro ‘tgsin’
    6 |             float: sinf(x) \
      |                         ^
```

As far as I can tell, the diagnostic is spurious, because it applies to the
"float" case of _Generic, which is not the one that is actually chosen. That
is, inspecting the resulting object file `boo.o` there is a call to sin but not
to sinf.

This is prompted by https://github.com/v923z/micropython-ulab/pull/636 and
earlier
https://github.com/micropython/micropython/commit/f31f9a8b70db03cbcbcf39b493f959d0e284962a
-- it at first appeared to be related to -Os size optimization, but that's just
related to how glibc chooses to implement fpclassify, whether via _Generic or
not.

This may be related to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193

Reply via email to