9

Why does compiling the minimal test case C code below using gcc5 (5.3.0) generate a [-Wincompatible-pointer-types] warning for the warning() function call that has a const argument and casts that argument using the ConstMyDouble typedef, but not for the nowarning() call that does not use the ConstMyDouble cast, nor for the noconst() call that uses the non-const typedef MyDouble, and how can it be fixed?

There seems to be a subtlety when [const] is used in a typedef and that typedef is used to cast arguments to a function.

The most confusing part is the warning message:

minimal.c:17:7: note: expected ‘const double (*)[2]’ but argument is
  of type ‘const ConstMyDouble (*)[2] {aka const double (*)[2]}’

which seems to be saying that const double (*)[2] is not the same as (aka) const double (*)[2]

Minimal C code test case

/* Usage:
 *
 *   ./minimal ; echo ${PIPESTATUS[0]}
 *     => echo command will output 99 (BASH)
 *
 * Compile and link, default:
 *
 *   gcc5 minimal.c -o minimal
 *     => Casts argument to minimal to [ConstMyDouble], a typedef
 *     => Generates [-Wincompatible-pointer-types] warnings
 *
 */
typedef       double      MyDouble;
typedef const double ConstMyDouble;

int   noconst(      MyDouble matrix[2][2] ) { return 32; }
int   warning( ConstMyDouble matrix[2][2] ) { return 33; }
int nowarning( ConstMyDouble matrix[2][2] ) { return 34; }

int main() {
MyDouble matrix[4];
  return   noconst((     MyDouble (*)[2])(matrix))  /* No warning */
       + nowarning(( const double (*)[2])(matrix))  /* No warning */
       +   warning((ConstMyDouble (*)[2])(matrix))  /* Warning */

Background and miscellany

I know I can turn off the warnings via the [-Wincompatible-pointer-type] option, but this is a minimal test case and does not represent what I encounter in practice.

In practice these warnings only occur because of similar typedef's and casts in a library I use (https://naif.jpl.nasa.gov/), so I want to know how to fix that to cut down the noise enough to see such warnings from my own code.

I am using GCC 5.3.0; these warnings do not occur with GCC 4.4.7.

This is only a minimal test case; it should not matter that the noconst(), nowarning() and warning() functions do nothing with the argument.

I would think this is related to Strange warning in a C function const multidimensional-array argument, or perhaps others, but here the issue seems to be using a typedef vs. not using a typedef in the cast. Still, if this is a duplicate, I apologize and will remove it.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Brian Carcich
  • 391
  • 2
  • 8
  • I do not get any warnings with GCC 7, so perhaps this was just a bug in 5.3.0. – zwol Mar 01 '18 at 19:02
  • 1
    Compiler bug. [no warning on clang](https://godbolt.org/g/mXiCsE) – llllllllll Mar 01 '18 at 19:04
  • 1
    @zwol True, so let's close this as *a problem that cannot be reproduced*. – Iharob Al Asimi Mar 01 '18 at 19:07
  • 2
    @IharobAlAsimi Well, in fact I _can_ reproduce it, see https://godbolt.org/g/6m7NfC -- but _only_ with GCC 5.3.0; not with 5.4.0 even. So I think we can conclude that it's definitely just a bug in that particular compiler. – zwol Mar 01 '18 at 19:12
  • @IharobAlAsimi et al. I was loathe to blame the compiler because, like many other developers, there are too many times it *seems* like a compiler bug and then turns out not to be, but perhaps this is (was) actually one. Thank you all very much! – Brian Carcich Mar 02 '18 at 02:15
  • @BrianCarcich Of course, you had to be sure. You have to trust the compiler and find the bug in your code, until you're sure there is no problem with your code. – Iharob Al Asimi Mar 02 '18 at 12:05
  • I answered the question, because this can actually help other people in the future. BTW, @BrianCarcich could have answered it too. – Iharob Al Asimi Mar 02 '18 at 12:10
  • Yes, I agree this should be closed. Thanks again to everyone for getting stuck in and resolving this so quickly. In retrospect, I should have installed the latest compiler, and checked issues, before asking (but it's just so easy to get others to do my work for me;-). – Brian Carcich Mar 02 '18 at 16:46

1 Answers1

2

There appears to be a compiler bug as confirmed by the commenters in your question, in fact @zowl apparently checked that the bug occurs only with GCC 5.3.0 as explained in this comment.

The user @liliscent used the clang compiler and it did not generate a warning either as is explained in this other comment.

So the conclusion that there's a bug in GCC 5.3.0 appears to be valid.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97