• Tom Lane's avatar
    Provide some static-assertion functionality on all compilers. · 0d0aa5d2
    Tom Lane authored
    On reflection (especially after noticing how many buildfarm critters have
    __builtin_types_compatible_p but not _Static_assert), it seems like we
    ought to try a bit harder to make these macros do something everywhere.
    The initial cut at it would have been no help to code that is compiled only
    on platforms without _Static_assert, for instance; and in any case not all
    our contributors do their initial coding on the latest gcc version.
    
    Some googling about static assertions turns up quite a bit of prior art
    for making it work in compilers that lack _Static_assert.  The method
    that seems closest to our needs involves defining a struct with a bit-field
    that has negative width if the assertion condition fails.  There seems no
    reliable way to get the error message string to be output, but throwing a
    compile error with a confusing message is better than missing the problem
    altogether.
    
    In the same spirit, if we don't have __builtin_types_compatible_p we can at
    least insist that the variable have the same width as the type.  This won't
    catch errors such as "wrong pointer type", but it's far better than
    nothing.
    
    In addition to changing the macro definitions, adjust a
    compile-time-constant Assert in contrib/hstore to use StaticAssertStmt,
    so we can get some buildfarm coverage on whether that macro behaves sanely
    or not.  There's surely more places that could be converted, but this is
    the first one I came across.
    0d0aa5d2
c.h 26.6 KB