Bug 80485 - rejects-valid: constexpr static_cast of pointer-to-member-function to bool
Summary: rejects-valid: constexpr static_cast of pointer-to-member-function to bool
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: 8.2
Assignee: Jason Merrill
URL:
Keywords: rejects-valid
: 67245 85823 100498 (view as bug list)
Depends on:
Blocks: constexpr pmf, ptmf
  Show dependency treegraph
 
Reported: 2017-04-21 20:11 UTC by Tony E Lewis
Modified: 2021-07-23 19:28 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work: 8.2.0, 9.0
Known to fail: 7.4.0, 8.1.0
Last reconfirmed: 2018-05-17 00:00:00


Attachments
proposed fix (757 bytes, patch)
2018-05-22 20:08 UTC, Jason Merrill
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tony E Lewis 2017-04-21 20:11:57 UTC
GCC rejects the following code (partly adapted from Boost's tribool), which I think is valid:


struct dummy {
  void nonnull() {};
};

typedef void (dummy::*safe_bool)();

constexpr safe_bool a = &dummy::nonnull;

static_assert( static_cast<bool>( a ), "" );

int main () { return 0; }


I'm compiling with -std=c++14 and I'm seeing these errors:


<source>:9:1: error: non-constant condition for static assertion
 static_assert( static_cast<bool>( a ), "" );
 ^~~~~~~~~~~~~
<source>:9:16: error: '(dummy::nonnull != 0)' is not a constant expression
 static_assert( static_cast<bool>( a ), "" );
                ^~~~~~~~~~~~~~~~~~~~~~
Compiler exited with result code 1


This code is accepted by Clang.

This looks related to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71896 but that has status "RESOLVED FIXED" but I'm seeing this on "GCC 7 (snapshot)" on godbolt.org (see https://godbolt.org/g/JnrZss ) as well as on my GCC 6.2.0.
Comment 1 Jonathan Wakely 2018-05-17 22:02:30 UTC
*** Bug 85823 has been marked as a duplicate of this bug. ***
Comment 2 Jason Merrill 2018-05-22 20:08:42 UTC
Created attachment 44167 [details]
proposed fix

https://gcc.gnu.org/ml/gcc-patches/2018-05/msg00968.html
Comment 3 Jason Merrill 2018-05-25 16:45:27 UTC
Author: jason
Date: Fri May 25 16:44:55 2018
New Revision: 260762

URL: https://gcc.gnu.org/viewcvs?rev=260762&root=gcc&view=rev
Log:
	PR c++/80485 - inline function non-zero address.

	* symtab.c (nonzero_address): Check DECL_COMDAT.

Added:
    trunk/gcc/testsuite/g++.dg/expr/pmf-3.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/symtab.c
Comment 4 Jason Merrill 2018-06-11 18:57:28 UTC
Author: jason
Date: Mon Jun 11 18:56:56 2018
New Revision: 261449

URL: https://gcc.gnu.org/viewcvs?rev=261449&root=gcc&view=rev
Log:
	PR c++/80485 - inline function non-zero address.

	* symtab.c (nonzero_address): Check DECL_COMDAT.

Added:
    branches/gcc-8-branch/gcc/testsuite/g++.dg/expr/pmf-3.C
Modified:
    branches/gcc-8-branch/gcc/ChangeLog
    branches/gcc-8-branch/gcc/symtab.c
Comment 5 Tony E Lewis 2018-06-15 08:50:52 UTC
Thanks to all for all work on this.

(Apologies if this isn't helpful but just in case it is...) I notice that the original Godbolt snippet ( https://godbolt.org/g/JnrZss ) has regressed from a rejects-valid in 8.1 to an ICE on trunk ("9.0.0 20180610") :


g++: internal compiler error: Segmentation fault signal terminated program as

Please submit a full bug report,

with preprocessed source if appropriate.

See <https://gcc.gnu.org/bugs/> for instructions.

Compiler returned: 4
Comment 6 Marek Polacek 2018-06-15 16:50:37 UTC
(In reply to Tony E Lewis from comment #5)
> Thanks to all for all work on this.
> 
> (Apologies if this isn't helpful but just in case it is...) I notice that
> the original Godbolt snippet ( https://godbolt.org/g/JnrZss ) has regressed
> from a rejects-valid in 8.1 to an ICE on trunk ("9.0.0 20180610") :

I don't see that.  This compiles fine with trunk:

struct dummy {
  void nonnull() {};
};

typedef void (dummy::*safe_bool)();

constexpr safe_bool a = &dummy::nonnull;

static_assert( static_cast<bool>( a ), "" );

int main () { return 0; }
Comment 7 Tony E Lewis 2018-06-16 18:55:55 UTC
Ah yes - I'm seeing it compiling cleanly now on Godbolt's trunk (9.0.0 20180615).

Must have been a temporary glitch in the build (and couldn't possibly have been due to my error :P ).
Comment 8 Tony E Lewis 2019-04-18 13:34:43 UTC
As far as I can see on Godbolt, this is now fixed in trunk. I'm happy for this issue to be closed.
Comment 9 Jonathan Wakely 2019-04-18 13:38:02 UTC
Jason, I assume you don't plan to backport this to gcc-7-branch?
Comment 10 Jonathan Wakely 2019-10-23 11:58:32 UTC
*** Bug 67245 has been marked as a duplicate of this bug. ***
Comment 11 Jonathan Wakely 2021-05-10 09:48:13 UTC
*** Bug 100498 has been marked as a duplicate of this bug. ***
Comment 12 Drea Pinski 2021-07-23 19:28:07 UTC
Fixed.