RE: Proposal: Add more compile-time asserts to expose inconsistencies.

Smith, Peter <peters@fast.au.fujitsu.com>

From: "Smith, Peter" <peters@fast.au.fujitsu.com>
To: Andres Freund <andres@anarazel.de>, Tom Lane <tgl@sss.pgh.pa.us>
Cc: Michael Paquier <michael@paquier.xyz>, Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>, PostgreSQL-development <pgsql-hackers@postgresql.org>
Date: 2019-10-09T22:52:41Z
Lists: pgsql-hackers

Attachments

From: Andres Freund <andres@anarazel.de> Sent: Tuesday, 1 October 2019 3:14 AM

>I wonder if defining the fallback static assert code to something like
>  extern void static_assert_func(int static_assert_failed[(condition) ? 1 : -1]); isn't a solution, however. I *think* that's standard C. Seems to work at least with gcc, clang, msvc, icc.
>
>Re standard: C99's "6.7 Declarations" + 6.7.1 defines 'declaration' to include extern specifiers and in 6.7.1 5) says "The declaration of an identifier for a function that has block scope shall have >no explicit storage-class specifier other than extern.".  And "6.8 Statements and blocks", via "6.8.2 Compound statement" allows declarations in statements.
>
>You can play with a good few compilers at: https://godbolt.org/z/fl0Mzu

I liked your idea of using an extern function declaration for implementing the file-scope compile-time asserts. AFAIK it is valid standard C.

Thank you for the useful link to that compiler explorer. I tried many scenarios of the new StaticAssertDecl and all seemed to work ok.
https://godbolt.org/z/fDrmXi

The patch has been updated accordingly. All assertions identified in the original post are now adjacent the global variables they are asserting. 

Kind Regards
--
Peter Smith
Fujitsu Australia

Commits

  1. Add declaration-level assertions for compile-time checks

  2. Add support for static assertions in C++