bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* assert-h: Make static_assert work on Solaris 11.4
@ 2022-10-23 14:47 Bruno Haible
  2022-10-25 18:12 ` Paul Eggert
  0 siblings, 1 reply; 8+ messages in thread
From: Bruno Haible @ 2022-10-23 14:47 UTC (permalink / raw)
  To: bug-gnulib

The GNU sed prerelease testing [1] and the GNU texinfo prerelease testing [2]
have revealed compilation errors on Solaris 11.4, due to the use of
'static_assert'. The <assert.h> of Solaris 11.4 defines it in such a way
that it can only be invoked with 2 arguments. However, since 2022-09-13,
many Gnulib source files use 'static_assert' with 1 argument only.

[1] https://lists.gnu.org/archive/html/sed-devel/2022-10/msg00004.html
[2] https://lists.gnu.org/archive/html/bug-texinfo/2022-10/msg00188.html

This patch fixes it.


2022-10-23  Bruno Haible  <bruno@clisp.org>

	assert-h: Make static_assert work on Solaris 11.4.
	* m4/assert_h.m4 (gl_ASSERT_H): After including <assert.h>, on Solaris,
	redefine static_assert.

diff --git a/m4/assert_h.m4 b/m4/assert_h.m4
index c1306daef4..e892ea2f01 100644
--- a/m4/assert_h.m4
+++ b/m4/assert_h.m4
@@ -57,5 +57,11 @@ AC_DEFUN([gl_ASSERT_H],
              && __GNUG__ < 6 && __clang_major__ < 6)))
  #include <assert.h>
  #undef/**/assert
+ /* Solaris 11.4 <assert.h> defines static_assert as a macro with 2 arguments.
+    We need it also to be invocable with a single argument.  */
+ #if defined __sun && (__STDC_VERSION__ - 0 >= 201112L) && !defined __cplusplus
+  #undef static_assert
+  #define static_assert _Static_assert
+ #endif
 #endif])
 ])





^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: assert-h: Make static_assert work on Solaris 11.4
  2022-10-23 14:47 assert-h: Make static_assert work on Solaris 11.4 Bruno Haible
@ 2022-10-25 18:12 ` Paul Eggert
  2022-10-25 18:18   ` Jeffrey Walton
  2022-10-25 20:05   ` Bruno Haible
  0 siblings, 2 replies; 8+ messages in thread
From: Paul Eggert @ 2022-10-25 18:12 UTC (permalink / raw)
  To: Bruno Haible, bug-gnulib

On 2022-10-23 07:47, Bruno Haible wrote:
>    #include <assert.h>
>    #undef/**/assert
> + /* Solaris 11.4 <assert.h> defines static_assert as a macro with 2 arguments.
> +    We need it also to be invocable with a single argument.  */
> + #if defined __sun && (__STDC_VERSION__ - 0 >= 201112L) && !defined __cplusplus
> +  #undef static_assert
> +  #define static_assert _Static_assert
> + #endif
>   #endif])

Will this approach work if code does something like the following? I 
worry that the later <assert.h> includes would collide with config.h's 
definition of static_assert.

   #include <config.h>

   #define NDEBUG 1
   #include <assert.h>

   #define NDEBUG 0
   #include <assert.h>

   static_assert (true);

Come to think of it, the latest C23 draft is a little squirrelly here, 
as its section 7.2 says that <assert.h> defines a static_assert macro. 
This must be a typo because it never goes no to say anything about what 
the macro does, and static_assert is a keyword in C23.

Also, while we're on the topic, why does the latest C23 draft require 
that when NDEBUG is defined, the assert macro is defined via "#define 
assert(...) ((void)0)" rather than as "#define assert(ignore) 
((void)0)"? What's the point of requiring the ellipsis?


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: assert-h: Make static_assert work on Solaris 11.4
  2022-10-25 18:12 ` Paul Eggert
@ 2022-10-25 18:18   ` Jeffrey Walton
  2022-10-25 18:22     ` Paul Eggert
  2022-10-25 20:05   ` Bruno Haible
  1 sibling, 1 reply; 8+ messages in thread
From: Jeffrey Walton @ 2022-10-25 18:18 UTC (permalink / raw)
  To: Paul Eggert; +Cc: Bruno Haible, bug-gnulib

On Tue, Oct 25, 2022 at 2:13 PM Paul Eggert <eggert@cs.ucla.edu> wrote:
>
> On 2022-10-23 07:47, Bruno Haible wrote:
> >    #include <assert.h>
> >    #undef/**/assert
> > + /* Solaris 11.4 <assert.h> defines static_assert as a macro with 2 arguments.
> > +    We need it also to be invocable with a single argument.  */
> > + #if defined __sun && (__STDC_VERSION__ - 0 >= 201112L) && !defined __cplusplus
> > +  #undef static_assert
> > +  #define static_assert _Static_assert
> > + #endif
> >   #endif])
>
> Will this approach work if code does something like the following? I
> worry that the later <assert.h> includes would collide with config.h's
> definition of static_assert.
>
>    #include <config.h>
>
>    #define NDEBUG 1
>    #include <assert.h>
>
>    #define NDEBUG 0
>    #include <assert.h>
>
>    static_assert (true);
>
> Come to think of it, the latest C23 draft is a little squirrelly here,
> as its section 7.2 says that <assert.h> defines a static_assert macro.
> This must be a typo because it never goes no to say anything about what
> the macro does, and static_assert is a keyword in C23.
>
> Also, while we're on the topic, why does the latest C23 draft require
> that when NDEBUG is defined, the assert macro is defined via "#define
> assert(...) ((void)0)" rather than as "#define assert(ignore)
> ((void)0)"? What's the point of requiring the ellipsis?

Re: the ellipses. From
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2264r3.html,
Section 8:

    These changes are relative to N2573. If those changes are
    applied the minimal change for C++ standard proposed above
    could be used.

    In section 7.2 (Diagnostics <assert.h>) change the
    definition of the assert() macro to use elipsis instead of
    a single macro parameter:
    ...

Jeff


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: assert-h: Make static_assert work on Solaris 11.4
  2022-10-25 18:18   ` Jeffrey Walton
@ 2022-10-25 18:22     ` Paul Eggert
  2022-10-25 20:11       ` Bruno Haible
  0 siblings, 1 reply; 8+ messages in thread
From: Paul Eggert @ 2022-10-25 18:22 UTC (permalink / raw)
  To: noloader; +Cc: Bruno Haible, bug-gnulib

On 2022-10-25 11:18, Jeffrey Walton wrote:
> Re: the ellipses. From
> https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2264r3.html,
> Section 8:

Thanks. Although that sort of explains things, it doesn't explain why 
the latest draft uses ellipses only when NDEBUG is defined. Shouldn't 
ellipses also be used when NDEBUG is not defined?

Sounds like another typo (or at least lack of clarity) in the current draft.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: assert-h: Make static_assert work on Solaris 11.4
  2022-10-25 18:12 ` Paul Eggert
  2022-10-25 18:18   ` Jeffrey Walton
@ 2022-10-25 20:05   ` Bruno Haible
  1 sibling, 0 replies; 8+ messages in thread
From: Bruno Haible @ 2022-10-25 20:05 UTC (permalink / raw)
  To: bug-gnulib, Paul Eggert

Paul Eggert asked:
> >    #include <assert.h>
> >    #undef/**/assert
> > + /* Solaris 11.4 <assert.h> defines static_assert as a macro with 2 arguments.
> > +    We need it also to be invocable with a single argument.  */
> > + #if defined __sun && (__STDC_VERSION__ - 0 >= 201112L) && !defined __cplusplus
> > +  #undef static_assert
> > +  #define static_assert _Static_assert
> > + #endif
> >   #endif])
> 
> Will this approach work if code does something like the following? I 
> worry that the later <assert.h> includes would collide with config.h's 
> definition of static_assert.
> 
>    #include <config.h>
> 
>    #define NDEBUG 1
>    #include <assert.h>
> 
>    #define NDEBUG 0
>    #include <assert.h>
> 
>    static_assert (true);

It will work. The reason is that <assert.h> looks like this:

   #ifndef _ASSERT_H
   #define _ASSERT_H
   /* Part 1: definitions that don't depend on NDEBUG */
   #endif
   /* Part 2: definitions that depend on NDEBUG */

and the definition of 'static_assert', on Solaris 11.4, happens to be in
part 1.

> Come to think of it, the latest C23 draft is a little squirrelly here, 
> as its section 7.2 says that <assert.h> defines a static_assert macro. 
> This must be a typo because it never goes no to say anything about what 
> the macro does, and static_assert is a keyword in C23.

I agree. Still, 'static_assert' is _allowed_ to be a macro, by § 6.10.9.(2).

> Also, while we're on the topic, why does the latest C23 draft require 
> that when NDEBUG is defined, the assert macro is defined via "#define 
> assert(...) ((void)0)" rather than as "#define assert(ignore) 
> ((void)0)"? What's the point of requiring the ellipsis?

I would guess that it's for compatibility for C++. ISO C++ 17 does not
require anything about the argument when NDEBUG is defined. Thus, a
program can contain

  #undef NDEBUG
  assert (new HashMap<A,B>(3).count == 0);

and the compiler should effectively ignore this. But when the types HashMap,
A, and B are not defined, the only way to ignore this line is to allow
multiple arguments to 'assert' at the preprocessor level. (Because of the
comma.)

Most <assert.h> implementations get this wrong. Effectively requiring the
programmer to insert extra parentheses:

  #undef NDEBUG
  assert ((new HashMap<A,B>(3).count == 0));

instead.

Bruno





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: assert-h: Make static_assert work on Solaris 11.4
  2022-10-25 18:22     ` Paul Eggert
@ 2022-10-25 20:11       ` Bruno Haible
  2022-10-25 21:47         ` Paul Eggert
  0 siblings, 1 reply; 8+ messages in thread
From: Bruno Haible @ 2022-10-25 20:11 UTC (permalink / raw)
  To: noloader, Paul Eggert; +Cc: bug-gnulib

Paul Eggert wrote:
> > Re: the ellipses. From
> > https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2264r3.html,
> > Section 8:
> 
> Thanks. Although that sort of explains things, it doesn't explain why 
> the latest draft uses ellipses only when NDEBUG is defined. Shouldn't 
> ellipses also be used when NDEBUG is not defined?

That's exactly what the standard says in § 7.2.(2):

  The assert macro shall be implemented as a macro with an ellipsis parameter,
  not as an actual function.

Bruno





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: assert-h: Make static_assert work on Solaris 11.4
  2022-10-25 20:11       ` Bruno Haible
@ 2022-10-25 21:47         ` Paul Eggert
  2022-10-26  0:17           ` Bruno Haible
  0 siblings, 1 reply; 8+ messages in thread
From: Paul Eggert @ 2022-10-25 21:47 UTC (permalink / raw)
  To: Bruno Haible; +Cc: bug-gnulib, noloader

On 10/25/22 13:11, Bruno Haible wrote:
>    The assert macro shall be implemented as a macro with an ellipsis parameter,
>    not as an actual function.

Thanks for clarifying it for me.

I suppose the Gnulib assert-h module could be enhanced to support this 
C23 extension. Low priority for me as I don't deal with Gnulib + C++ and 
the problem comes up more rarely in C than in C++.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: assert-h: Make static_assert work on Solaris 11.4
  2022-10-25 21:47         ` Paul Eggert
@ 2022-10-26  0:17           ` Bruno Haible
  0 siblings, 0 replies; 8+ messages in thread
From: Bruno Haible @ 2022-10-26  0:17 UTC (permalink / raw)
  To: Paul Eggert; +Cc: bug-gnulib, noloader

Paul Eggert wrote:
> I suppose the Gnulib assert-h module could be enhanced to support this 
> C23 extension. Low priority for me as I don't deal with Gnulib + C++ and 
> the problem comes up more rarely in C than in C++.

I agree. Especially since I know of no system (so far) whose <assert.h>
does it right.

Bruno





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-10-26  0:19 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-23 14:47 assert-h: Make static_assert work on Solaris 11.4 Bruno Haible
2022-10-25 18:12 ` Paul Eggert
2022-10-25 18:18   ` Jeffrey Walton
2022-10-25 18:22     ` Paul Eggert
2022-10-25 20:11       ` Bruno Haible
2022-10-25 21:47         ` Paul Eggert
2022-10-26  0:17           ` Bruno Haible
2022-10-25 20:05   ` Bruno Haible

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).