From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-3.7 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS, SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id DCDA31F4B4 for ; Tue, 6 Apr 2021 19:34:41 +0000 (UTC) Received: from localhost ([::1]:56402 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lTrTE-0008L6-Lf for normalperson@yhbt.net; Tue, 06 Apr 2021 15:34:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46166) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lTrTB-0008Ku-5O for bug-gnulib@gnu.org; Tue, 06 Apr 2021 15:34:37 -0400 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]:34394) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lTrT4-0002gw-JH for bug-gnulib@gnu.org; Tue, 06 Apr 2021 15:34:36 -0400 Received: by mail-pl1-x635.google.com with SMTP id a6so4741266pls.1 for ; Tue, 06 Apr 2021 12:34:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Wf0uLdRxzqHgDCUtDYUJ8o/esMTdvfoZUyFTISl+9mA=; b=l93mhs6j3FIt8DXETv15euGwk3dO/MD764xlwWBLFBZDu5PBCACRgbGfwDrOGVUewj cVdQCqHvMaF0MOVpKd8qx8cXkw4rv4gbvpgLTtA0i7ZTdWdAWqV97Lo7flF/gHVF694E GsJygG2bTMdhDcIBUo7B4Am8kPS+b9hmop0afNUZfx1AfwcTgP92istVVTIhb7stkQNc qPvgCauePca1z5uRI1DSB3yYGUyx6yi42wZimT8X1II2hD2zM4EzrVXIFQWLo9iH1zd+ qKibckHUw+jaoV6eE+z1VJOxfaL3dSWjXIiyxzX7g9N4aDec2OMKIAJGnNuCWL7Py2s0 fvgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Wf0uLdRxzqHgDCUtDYUJ8o/esMTdvfoZUyFTISl+9mA=; b=iPFGPStqtU/nH3l2UWf+o8xY6BVd58xh2GZW3OpQUUIljgDGgBjprnc1D7BLScXk0o C6S+sk8iliy6oW2OaR9f7fA80Fah4oGXeZ+xkTmEbzxM4OjthiuW6FONTdb6gteGb/Tk nc9+MSk76TTei/FDCkQCEdQNPtBq8ifMgrjdGi91/VzoJaCmhH51+OVRddKB9ce5T+z9 QK2Brq9cl4fV3gyftcQLgTdDErr8/izz5+mk6JLAyNGtmaCE2tu3L1wPYIqFPgVO36oT L3q4uQT+3woPruq7cziMEJDMes5iEJ0W8odkYkBZT8tNayjzCBPrCpkdc7fhoZYLfpc+ aXlg== X-Gm-Message-State: AOAM532aogwU5sCkHtiuAqGsVjRkNMq+ELrxFgc4cJsu4Rfbhy+gK2NO gXv8mTg2BQa93XIZEBOve8U8xRwuJWJEket7GTA= X-Google-Smtp-Source: ABdhPJxHybBOIQnsbMUpEIZJTr9rJH24ZnE1Ix1WGz7ogr5klmmWNxxyIqOgU7exEiHADwM12XXr8JgtKop/K5f6l3w= X-Received: by 2002:a17:90a:5884:: with SMTP id j4mr5964278pji.33.1617737668634; Tue, 06 Apr 2021 12:34:28 -0700 (PDT) MIME-Version: 1.0 References: <84aa607b-197b-6e15-ff1d-3bf4a078075c@cs.ucla.edu> In-Reply-To: From: =?UTF-8?Q?Marc_Nieper=2DWi=C3=9Fkirchen?= Date: Tue, 6 Apr 2021 21:34:17 +0200 Message-ID: Subject: Re: Type-safe typecasts To: Paul Eggert Content-Type: multipart/alternative; boundary="000000000000142ff805bf52e79a" Received-SPF: pass client-ip=2607:f8b0:4864:20::635; envelope-from=marc.nieper@gmail.com; helo=mail-pl1-x635.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?Q?Marc_Nieper=2DWi=C3=9Fkirchen?= , Gnulib bugs , Bruno Haible Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" --000000000000142ff805bf52e79a Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable CCing Bruno because of his involvement with the Gnulib list modules. Disallowing NULL list elements could break existing code that actually uses them but returning elements with type void * instead of const void * would be much less incompatible. Code can trivially be ported to such an updated interface (for example, in my snippet above, I would have to replace 'const void *e' with 'void *e', All in all, it would make working with the code a lot easier. For lists where there are no non-null list elements, one could add void *gl_list_iterator_next_element_or_null (gl_list_iterator_t iter, gl_list_node_t *node_ptr) { void *e; return gl_list_iterator_next (iter, &e, node_ptr) ? e : NULL; } to the global API (modulo a better name for the procedure). Am Di., 6. Apr. 2021 um 21:20 Uhr schrieb Paul Eggert : > On 4/6/21 12:13 PM, Marc Nieper-Wi=C3=9Fkirchen wrote: > > gl_list_iterator_next has to return two things: An element (represented > by > > a const void *) and a boolean value. As elements may be NULL > > Ah, OK, then that's the problem. The API shouldn't allow null elements. > They're not that useful anyway. If they really are needed for some > lists, I suppose one could have a more-complicated API to return them > (by setting a bool elsewhere); but usually they aren't. > > > So to make my original code portable C, I would have to code > > > > ... > > const void *e; > > while (gl_list_iterator_next (&i, &e, NULL)) > > { > > struct foo *foo =3D (void *) e; > > ++foo->bar; > > } > > ... > > > > The const typecast is, unfortunately, still needed to silence compiler > > warnings > > Yes, that would be portable. But that cast indicates another problem > with the API. It should return void *, not void const * (think of strchr > as the model here). The API should discourage type-casts, since they're > more dangerous than the alternatives. > --000000000000142ff805bf52e79a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
CCing Bruno because of his involvement with the Gnulib list modules.

Disallowing NULL= list elements could break existing code that actually uses them but return= ing elements with type void * instead of const void * would be much less in= compatible. Code can trivially be ported to such an updated interface (for = example, in my snippet above, I would have to replace 'const void *e= 9; with 'void *e', All in all, it would make working with the code = a lot easier.

For lists where there are no non-null list elements, one could add

void *gl_list_iterat= or_next_element_or_null (gl_list_iterator_t iter, gl_list_node_t *node_ptr)=
{
<= div class=3D"gmail_default" style=3D"font-family:monospace">=C2=A0 void *e;=
=C2=A0 r= eturn gl_list_iterator_next (iter, &e, node_ptr) ? e : NULL;
<= div class=3D"gmail_default" style=3D"font-family:monospace">}

to the global API (modulo a better name for the procedure).


Am Di., 6. Apr. 2021 um 21:20=C2=A0Uhr schrieb Paul Eggert <eggert@cs.ucla.edu>:
On 4/6/21 12:13 PM, Marc Nieper-Wi= =C3=9Fkirchen wrote:
> gl_list_iterator_next has to return two things: An element (represente= d by
> a const void *) and a boolean value. As elements may be NULL

Ah, OK, then that's the problem. The API shouldn't allow null eleme= nts.
They're not that useful anyway. If they really are needed for some
lists, I suppose one could have a more-complicated API to return them
(by setting a bool elsewhere); but usually they aren't.

> So to make my original code portable C, I would have to code
>
> ...
> const void *e;
> while (gl_list_iterator_next (&i, &e, NULL))
>=C2=A0 =C2=A0{
>=C2=A0 =C2=A0 =C2=A0struct foo *foo =3D (void *) e;
>=C2=A0 =C2=A0 =C2=A0++foo->bar;
>=C2=A0 =C2=A0}
> ...
>
> The const typecast is, unfortunately, still needed to silence compiler=
> warnings

Yes, that would be portable. But that cast indicates another problem
with the API. It should return void *, not void const * (think of strchr as the model here). The API should discourage type-casts, since they're=
more dangerous than the alternatives.
--000000000000142ff805bf52e79a--