public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101681] New: PMF comparison to nullptr is not considered a constexpr
@ 2021-07-29 21:05 pinskia at gcc dot gnu.org
  2021-08-06 19:08 ` [Bug c++/101681] " StevenSun2021 at hotmail dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-29 21:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101681

            Bug ID: 101681
           Summary: PMF comparison to nullptr is not considered a
                    constexpr
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: rejects-valid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
            Blocks: 55004, 101603
  Target Milestone: ---

Take:
template <bool> struct Z { };

struct C
{
    void f();
    Z<&C::f == nullptr> z;
};
---- CUT ---
This should be accepted in C++11 or newer.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55004
[Bug 55004] [meta-bug] constexpr issues
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101603
[Bug 101603] [meta-bug] pointer to member functions issues

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

* [Bug c++/101681] PMF comparison to nullptr is not considered a constexpr
  2021-07-29 21:05 [Bug c++/101681] New: PMF comparison to nullptr is not considered a constexpr pinskia at gcc dot gnu.org
@ 2021-08-06 19:08 ` StevenSun2021 at hotmail dot com
  2021-08-07  0:33 ` StevenSun2021 at hotmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: StevenSun2021 at hotmail dot com @ 2021-08-06 19:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101681

Steven Sun <StevenSun2021 at hotmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |StevenSun2021 at hotmail dot com

--- Comment #1 from Steven Sun <StevenSun2021 at hotmail dot com> ---
The following program compiles. https://godbolt.org/z/aTvchYxYW

```
struct C {
  void f() {}
  static_assert(__builtin_constant_p(&C::f));
  static_assert(!__builtin_constant_p(&C::f == nullptr)); // not nonzero yet
};

static_assert(__builtin_constant_p(&C::f == nullptr)); // nonzero now

struct D {
  void f() {}
  static_assert(__builtin_constant_p(&C::f == nullptr));
  static_assert(!__builtin_constant_p(&D::f == nullptr));
};

static_assert(__builtin_constant_p(&C::f == nullptr));
static_assert(__builtin_constant_p(&D::f == nullptr));

```


Looks that the `&C::f` is known to be constexpr right after the function was
parsed.

But only when the class completely parsed, its value was assigned. We can then
compare it to nullptr.


To make code in comment0 accepted, we need some kind of `not null' mark on the
expression tree. 0ne possible way is to assign the `&C::f` in advance, right
after it was parsed.

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

* [Bug c++/101681] PMF comparison to nullptr is not considered a constexpr
  2021-07-29 21:05 [Bug c++/101681] New: PMF comparison to nullptr is not considered a constexpr pinskia at gcc dot gnu.org
  2021-08-06 19:08 ` [Bug c++/101681] " StevenSun2021 at hotmail dot com
@ 2021-08-07  0:33 ` StevenSun2021 at hotmail dot com
  2021-08-07  0:43 ` StevenSun2021 at hotmail dot com
  2022-01-11 18:40 ` [Bug c++/101681] PMF comparison to nullptr is not considered a constexpr inside a template argument pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: StevenSun2021 at hotmail dot com @ 2021-08-07  0:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101681

--- Comment #2 from Steven Sun <StevenSun2021 at hotmail dot com> ---
The root cause for this is that the compiler forbids constant folding when
involving PMF of an incomplete class.

https://gcc.gnu.org/git?p=gcc.git;a=blob;f=gcc/cp/expr.c;h=d16d1896f2ddd08264b389b02b9640cca332ec13;hb=refs/heads/master#l42

(gcc/cp/expr.c)
> 42   /* We can't lower this until the class is complete.  */
> 43         if (!COMPLETE_TYPE_P (DECL_CONTEXT (member)))
> 44           return cst;


If we comment this `if`, the constant folding will succeed at


(gcc/cp/expr.c)
> 67             expand_ptrmemfunc_cst (cst, &delta, &pfn);
> 68             cst = build_ptrmemfunc1 (type, delta, pfn);


solving everything.

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

* [Bug c++/101681] PMF comparison to nullptr is not considered a constexpr
  2021-07-29 21:05 [Bug c++/101681] New: PMF comparison to nullptr is not considered a constexpr pinskia at gcc dot gnu.org
  2021-08-06 19:08 ` [Bug c++/101681] " StevenSun2021 at hotmail dot com
  2021-08-07  0:33 ` StevenSun2021 at hotmail dot com
@ 2021-08-07  0:43 ` StevenSun2021 at hotmail dot com
  2022-01-11 18:40 ` [Bug c++/101681] PMF comparison to nullptr is not considered a constexpr inside a template argument pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: StevenSun2021 at hotmail dot com @ 2021-08-07  0:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101681

--- Comment #3 from Steven Sun <StevenSun2021 at hotmail dot com> ---
By the way, in the current design, the class definition is passed twice in
order we can see every member data/function declaration before parsing NSDMI
and member functions.

The class is complete after parsing all declaration, which means `&C::f ==
nullptr` can reduce to false since that.

So, under current design, the following code compiles on GCC.
https://godbolt.org/z/fMTsf4KoM

```
struct C {
  C() {
    static_assert(&C::f != 0);  // complete type
  }
  void f() noexcept(&C::f != 0) {
    static_assert(&C::f != 0);  // complete type
  }
  static_assert(__builtin_constant_p(&C::f));        // incomplete type
  static_assert(!__builtin_constant_p(&C::f == 0));  // incomplete type
};

static_assert(&C::f != 0);  // complete type

```

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

* [Bug c++/101681] PMF comparison to nullptr is not considered a constexpr inside a template argument
  2021-07-29 21:05 [Bug c++/101681] New: PMF comparison to nullptr is not considered a constexpr pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2021-08-07  0:43 ` StevenSun2021 at hotmail dot com
@ 2022-01-11 18:40 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-11 18:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101681

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|PMF comparison to nullptr   |PMF comparison to nullptr
                   |is not considered a         |is not considered a
                   |constexpr                   |constexpr inside a template
                   |                            |argument

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note completeness of the class does not matter either:
template <bool> struct Z { };
struct C
{
    void f();
};
static_assert ((&C::f == 0) == false,"");
Z<&C::f == 0> z;

--- CUT ---
The static_assert passes just fine but the template is still rejected.

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

end of thread, other threads:[~2022-01-11 18:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-29 21:05 [Bug c++/101681] New: PMF comparison to nullptr is not considered a constexpr pinskia at gcc dot gnu.org
2021-08-06 19:08 ` [Bug c++/101681] " StevenSun2021 at hotmail dot com
2021-08-07  0:33 ` StevenSun2021 at hotmail dot com
2021-08-07  0:43 ` StevenSun2021 at hotmail dot com
2022-01-11 18:40 ` [Bug c++/101681] PMF comparison to nullptr is not considered a constexpr inside a template argument pinskia at gcc dot gnu.org

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).