Skip to content

Commit 950dba4

Browse files
committed
Add STATIC_ASSERT_EXPR
This new type of a STATIC_ASSERT is used where an expression can go; hence potentially in macro expansions. Since it is compile-time, it can't be used on arguments that are expressions with side effects, so can safely be used in macros. It has limitations however, only reaching full parity when gcc brace groups are enabled, turning into an ASSUME which is a no-op in the worst case. However, most code will get compiled on a platform with gcc brace groups, and will pass or fail at compile time; so the chances of something bad actually getting into the field are minimal.
1 parent 9831cff commit 950dba4

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

perl.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4252,6 +4252,7 @@ hint to the compiler that this condition is likely to be false.
42524252

42534253
/*
42544254
=for apidoc Am||STATIC_ASSERT_DECL|const_expr
4255+
=for apidoc_item STATIC_ASSERT_EXPR
42554256
=for apidoc_item STATIC_ASSERT_STMT
42564257
42574258
These are like assert(), but for compile time invariants. That is, their
@@ -4269,9 +4270,22 @@ function.
42694270
C<STATIC_ASSERT_DECL> expands to a declaration and is suitable for use inside a
42704271
function or at file scope (outside of any function).
42714272
4272-
=cut
4273+
C<STATIC_ASSERT_EXPR> expands to an expression and is suitable anywhere an
4274+
expression is usable. It has some limitations compared to the other two when
4275+
used on a platform without L</C<PERL_USE_GCC_BRACE_GROUPS>>. On those
4276+
platforms it expands to an ASSUME(). When called with a compile-time
4277+
expression, the compiler should optimize out the expression, so that the use of
4278+
this is "free", but constness is not enforced. On non-DEBUGGING builds, this
4279+
will expand to a no-op, so again it is "free", but no checking is done.
4280+
4281+
Thus code that uses this macro can be ported to all platforms without needing
4282+
to change, and it will work as well as is possible on that platform.
4283+
Presumably it will get compiled at some point before release on a platform
4284+
where it has parity with the other two forms.
42734285
4286+
=cut
42744287
*/
4288+
42754289
#if (! defined(__IBMC__) || __IBMC__ >= 1210) \
42764290
&& (( defined(static_assert) && ( defined(_ISOC11_SOURCE) \
42774291
|| (__STDC_VERSION__ - 0) >= 201101L)) \
@@ -4335,6 +4349,12 @@ function or at file scope (outside of any function).
43354349
#define STATIC_ASSERT_STMT(COND) \
43364350
STMT_START { STATIC_ASSERT_DECL(COND); } STMT_END
43374351

4352+
#ifdef PERL_USE_GCC_BRACE_GROUPS
4353+
# define STATIC_ASSERT_EXPR(COND) ({ STATIC_ASSERT_DECL(COND); })
4354+
#else
4355+
# define STATIC_ASSERT_EXPR(COND) ASSUME(COND)
4356+
#endif
4357+
43384358
#ifndef __has_builtin
43394359
# define __has_builtin(x) 0 /* not a clang style compiler */
43404360
#endif

0 commit comments

Comments
 (0)