Return type qualifiers and function type compatibility (missing diagnostic in GCC)

LIU Hao lh_mouse@126.com
Sat Dec 6 09:02:06 GMT 2025


在 2025-12-6 16:07, Andrey Tarasevich via Gcc 写道:
> Just noticed that GCC appears to ignore top-level qualifiers on function return
> type when determining type compatibility.
> 
> C's function type compatibility rules (the last paragraph of "6.7.7.4 Function
> declarators") require exact match between function return types, including their
> top-level qualifiers. For parameter types top-level qualifiers are ignored, but
> not for the return type.

I guess 6.7.7.4 is N3220? But in paragraph 4 there goes

    Semantics
    4 If, in the declaration "T D1", D1 has the form
              D ( parameter-type-listopt ) attribute-specifier-sequenceopt
      and the type specified for ident in the declaration "T D" is
      "derived-declarator-type-list T", then the type specified for ident is
      "derived-declarator-type-list function returning >>>>>>> the
      unqualified, non-atomic version of T <<<<<<<". The optional attribute
      specifier sequence appertains to the function type.

Though not clear when it was changed, N1570 says 'derived-declarator-type-list function returning T', and 
N2176 adds 'the unqualified version of', and N3220 further adds 'non-atomic'.

Therefore, ...

> This implies that the function pointer initialization in the following code is
> supposed to be invalid
> 
>    const int foo(int a) { return 0; }

the type specified for `foo` is a function returning `int`, rather than `const int`.




-- 
Best regards,
LIU Hao
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 840 bytes
Desc: OpenPGP digital signature
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20251206/c7bd29ea/attachment-0001.sig>


More information about the Gcc mailing list