From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Matt Armstrong Newsgroups: gmane.emacs.devel Subject: Re: master 31544bc: =?utf-8?Q?Don=E2=80=99t?= convert pointer to bool Date: Sat, 20 Mar 2021 11:21:03 -0700 Message-ID: <87ft0phek0.fsf@rfc20.org> References: <20210320004804.9441.45535@vcs0.savannah.gnu.org> <20210320004805.B7BBF2101A@vcs0.savannah.gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="30270"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Stefan Monnier , Paul Eggert Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Mar 20 19:22:21 2021 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNgEv-0007mD-9b for ged-emacs-devel@m.gmane-mx.org; Sat, 20 Mar 2021 19:22:21 +0100 Original-Received: from localhost ([::1]:37490 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lNgEu-0005du-Bc for ged-emacs-devel@m.gmane-mx.org; Sat, 20 Mar 2021 14:22:20 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42450) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lNgDs-0005DK-BG for emacs-devel@gnu.org; Sat, 20 Mar 2021 14:21:16 -0400 Original-Received: from relay6-d.mail.gandi.net ([217.70.183.198]:60395) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lNgDq-0004tm-0z for emacs-devel@gnu.org; Sat, 20 Mar 2021 14:21:16 -0400 X-Originating-IP: 24.113.169.116 Original-Received: from mdeb (24-113-169-116.wavecable.com [24.113.169.116]) (Authenticated sender: matt@rfc20.org) by relay6-d.mail.gandi.net (Postfix) with ESMTPSA id EF14EC0003; Sat, 20 Mar 2021 18:21:08 +0000 (UTC) Original-Received: from matt by mdeb with local (Exim 4.94) (envelope-from ) id 1lNgDg-00017r-19; Sat, 20 Mar 2021 11:21:04 -0700 In-Reply-To: Received-SPF: pass client-ip=217.70.183.198; envelope-from=matt@rfc20.org; helo=relay6-d.mail.gandi.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:266660 Archived-At: Stefan Monnier writes: >> Without this patch, Oracle Studio 12.6 complains about converting >> pointer to bool. > > Is it right to complain or is that a bug in that tool? > To my naive intuition, it looks wrong since conversion to bool is > standard practice when doing `if (ptr) ...`. In code portable to pre-C99 the diff looks right to me. Commit 31544bc was about assignment to "bool", not "if (ptr)...", and the differece is relevant. "if (ptr)..." and the like has always been valid C. Instead, the statememnts test the scalar expression against the zero value, which has always well defined for pointers. Essentially, in conditional statements there is an implicit "!= 0" there. There is no implicit "!= 0" when converting pointers to integral types. In older compilers without _Bool, "bool" usually devolves to char. void* p = ...; char c = p; The above code has undefined behavior, since a pointer doesn't fit in a char. In practice, most compilers will generate code that assigns the low order byte of p to b. This can do things like set b zero for non-zero pointers and set b to values outside [0, 1]. So, either of these are better: char c = !!p; char c = p ? true : false;