Re: Proposal: Add more compile-time asserts to expose inconsistencies.
Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
From: ilmari@ilmari.org (Dagfinn Ilmari Mannsåker )
To: Michael Paquier <michael@paquier.xyz>
Cc: "Smith\, Peter" <peters@fast.au.fujitsu.com>,
PostgreSQL-development <pgsql-hackers@postgresql.org>
Date: 2019-09-18T15:46:30Z
Lists: pgsql-hackers
Michael Paquier <michael@paquier.xyz> writes: > On Wed, Sep 18, 2019 at 06:46:24AM +0000, Smith, Peter wrote: >> I have identified some OSS code where more compile-time asserts could be added. >> >> Mostly these are asserting that arrays have the necessary length to >> accommodate the enums that are used to index into them. >> >> In general the code is already commented with warnings such as: >> * "If you add a new entry, remember to ..." >> * "When modifying this enum, update the table in ..." >> * "Display names for enums in ..." >> * etc. >> >> But comments can be accidentally overlooked, so adding the >> compile-time asserts can help eliminate human error. > > For some of them it could help, and we could think about a better > location for that stuff than an unused routine. Postgres doesn't seem to have it, but it would be possible to define a StaticAssertDecl macro that can be used at the file level, outside any function. See for example Perl's STATIC_ASSERT_DECL: https://github.com/Perl/perl5/blob/v5.30.0/perl.h#L3455-L3488 - ilmari -- "The surreality of the universe tends towards a maximum" -- Skud's Law "Never formulate a law or axiom that you're not prepared to live with the consequences of." -- Skud's Meta-Law
Commits
-
Add declaration-level assertions for compile-time checks
- f1f10a1ba9e1 13.0 landed
-
Add support for static assertions in C++
- a2c8e5cfdb9d 11.0 cited