public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/99176] New: GCC rejects const_cast of null pointer in constant expressions
@ 2021-02-19 23:11 richard-gccbugzilla at metafoo dot co.uk
  2021-02-19 23:21 ` [Bug c++/99176] [8/9/10/11 Regression] " mpolacek at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: richard-gccbugzilla at metafoo dot co.uk @ 2021-02-19 23:11 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 99176
           Summary: GCC rejects const_cast of null pointer in constant
                    expressions
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: richard-gccbugzilla at metafoo dot co.uk
  Target Milestone: ---

GCC rejects:

constexpr const int *p = nullptr;
constexpr int *q = const_cast<int*>(p);

saying:

<source>:2:20: error: conversion of 'const int*' null pointer to 'int*' is not
a constant expression
    2 | constexpr int *q = const_cast<int*>(p);
      |                    ^~~~~~~~~~~~~~~~~~~

I don't think any such rule exists, and other compilers accept. This only
appears to affect const_casts of null pointers; non-null pointer const casts
seem to work OK. Perhaps GCC thinks that this is a reinterpret_cast / cast from
void* or something like that?

It looks like this regressed between GCC 6 and GCC 7.

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

* [Bug c++/99176] [8/9/10/11 Regression] GCC rejects const_cast of null pointer in constant expressions
  2021-02-19 23:11 [Bug c++/99176] New: GCC rejects const_cast of null pointer in constant expressions richard-gccbugzilla at metafoo dot co.uk
@ 2021-02-19 23:21 ` mpolacek at gcc dot gnu.org
  2021-02-19 23:22 ` mpolacek at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-02-19 23:21 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|GCC rejects const_cast of   |[8/9/10/11 Regression] GCC
                   |null pointer in constant    |rejects const_cast of null
                   |expressions                 |pointer in constant
                   |                            |expressions
   Last reconfirmed|                            |2021-02-19
             Status|UNCONFIRMED                 |NEW
           Priority|P3                          |P2
           Keywords|                            |rejects-valid
   Target Milestone|---                         |9.4
                 CC|                            |mpolacek at gcc dot gnu.org
     Ever confirmed|0                           |1

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Thanks for the report.  Started with r238909.

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

* [Bug c++/99176] [8/9/10/11 Regression] GCC rejects const_cast of null pointer in constant expressions
  2021-02-19 23:11 [Bug c++/99176] New: GCC rejects const_cast of null pointer in constant expressions richard-gccbugzilla at metafoo dot co.uk
  2021-02-19 23:21 ` [Bug c++/99176] [8/9/10/11 Regression] " mpolacek at gcc dot gnu.org
@ 2021-02-19 23:22 ` mpolacek at gcc dot gnu.org
  2021-02-22 23:24 ` mpolacek at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-02-19 23:22 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I'll take a look.

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

* [Bug c++/99176] [8/9/10/11 Regression] GCC rejects const_cast of null pointer in constant expressions
  2021-02-19 23:11 [Bug c++/99176] New: GCC rejects const_cast of null pointer in constant expressions richard-gccbugzilla at metafoo dot co.uk
  2021-02-19 23:21 ` [Bug c++/99176] [8/9/10/11 Regression] " mpolacek at gcc dot gnu.org
  2021-02-19 23:22 ` mpolacek at gcc dot gnu.org
@ 2021-02-22 23:24 ` mpolacek at gcc dot gnu.org
  2021-02-25 21:29 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-02-22 23:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Same problem for:

constexpr int* ptr = (int *) (const int *) nullptr;

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

* [Bug c++/99176] [8/9/10/11 Regression] GCC rejects const_cast of null pointer in constant expressions
  2021-02-19 23:11 [Bug c++/99176] New: GCC rejects const_cast of null pointer in constant expressions richard-gccbugzilla at metafoo dot co.uk
                   ` (2 preceding siblings ...)
  2021-02-22 23:24 ` mpolacek at gcc dot gnu.org
@ 2021-02-25 21:29 ` cvs-commit at gcc dot gnu.org
  2021-02-25 21:32 ` [Bug c++/99176] [8/9/10 " mpolacek at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-02-25 21:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:2ffc26458dd7ba7b3fa00897f2d8c6cd24ba06f3

commit r11-7404-g2ffc26458dd7ba7b3fa00897f2d8c6cd24ba06f3
Author: Marek Polacek <polacek@redhat.com>
Date:   Wed Feb 24 10:08:44 2021 -0500

    c++: const_cast of null pointer in constant expr [PR99176]

    Here we reject

      constexpr const int *p = nullptr;
      constexpr int *q = const_cast<int*>(p);

    with "conversion of 'const int*' null pointer to 'int*' is not a
    constant expression", which seems bogus.  This code has been rejected
    since r238909 which added the can_convert check when converting a null
    pointer.  I'm not finding any standard rule that this check was supposed
    to enforce.  The original discussion was here
    <https://gcc.gnu.org/legacy-ml/gcc-patches/2016-06/msg01447.html>
    and here
    <https://gcc.gnu.org/legacy-ml/gcc-patches/2016-07/msg00280.html>.

    Since can_convert never assumes a C-style cast, it rejects casting
    away constness as in the test above and in:

      constexpr int *q = (int *)(const int *) nullptr;

    Removing the check only breaks constexpr-nullptr-2.C by not giving any
    diagnostic for line 229:

      constexpr B *pb2 = static_cast<B*>(pa0);  // { dg-error "not a constant
expression" }

    but the cast seems to be valid: we do [expr.static.cast]/7, and
    [expr.const] only says that a reinterpreter_cast and converting from
    void* is invalid in constexpr.  The can_convert check rejected convering
    from void *, but only when converting from a null pointer, so it's not
    good enough.  So I've added a check to catch conversions from cv void*.
    I realize it's not a great time to be adding additional checking, but
    removing the can_convert check would then technically be a regression.

    Let's limit the new check to only trigger for integer_zerop and then remove
    it in GCC 12.

    gcc/cp/ChangeLog:

            DR 1312
            PR c++/99176
            * constexpr.c (is_std_construct_at): New overload.
            (is_std_allocator_allocate): New overload.
            (cxx_eval_call_expression): Use the new overloads.
            (cxx_eval_constant_expression): Reject casting
            from void * as per DR 1312.  Don't check can_convert.

    gcc/testsuite/ChangeLog:

            DR 1312
            PR c++/99176
            * g++.dg/cpp0x/constexpr-nullptr-2.C: Adjust dg-error.
            * g++.dg/cpp0x/constexpr-cast2.C: New test.
            * g++.dg/cpp0x/constexpr-cast3.C: New test.

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

* [Bug c++/99176] [8/9/10 Regression] GCC rejects const_cast of null pointer in constant expressions
  2021-02-19 23:11 [Bug c++/99176] New: GCC rejects const_cast of null pointer in constant expressions richard-gccbugzilla at metafoo dot co.uk
                   ` (3 preceding siblings ...)
  2021-02-25 21:29 ` cvs-commit at gcc dot gnu.org
@ 2021-02-25 21:32 ` mpolacek at gcc dot gnu.org
  2021-04-09 22:42 ` cvs-commit at gcc dot gnu.org
  2021-04-09 22:43 ` mpolacek at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-02-25 21:32 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[8/9/10/11 Regression] GCC  |[8/9/10 Regression] GCC
                   |rejects const_cast of null  |rejects const_cast of null
                   |pointer in constant         |pointer in constant
                   |expressions                 |expressions

--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed on trunk so far.

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

* [Bug c++/99176] [8/9/10 Regression] GCC rejects const_cast of null pointer in constant expressions
  2021-02-19 23:11 [Bug c++/99176] New: GCC rejects const_cast of null pointer in constant expressions richard-gccbugzilla at metafoo dot co.uk
                   ` (4 preceding siblings ...)
  2021-02-25 21:32 ` [Bug c++/99176] [8/9/10 " mpolacek at gcc dot gnu.org
@ 2021-04-09 22:42 ` cvs-commit at gcc dot gnu.org
  2021-04-09 22:43 ` mpolacek at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-04-09 22:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Marek Polacek
<mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:581e87b67233556d566df3a0ea33be9c19fbcb2f

commit r10-9687-g581e87b67233556d566df3a0ea33be9c19fbcb2f
Author: Marek Polacek <polacek@redhat.com>
Date:   Fri Apr 9 18:41:21 2021 -0400

    c++: const_cast of null pointer in constant expr [PR99176]

    Here we reject

      constexpr const int *p = nullptr;
      constexpr int *q = const_cast<int*>(p);

    with "conversion of 'const int*' null pointer to 'int*' is not a
    constant expression", which seems bogus.  This code has been rejected
    since r238909 which added the can_convert check when converting a null
    pointer.  I'm not finding any standard rule that this check was supposed
    to enforce.  The original discussion was here
    <https://gcc.gnu.org/legacy-ml/gcc-patches/2016-06/msg01447.html>
    and here
    <https://gcc.gnu.org/legacy-ml/gcc-patches/2016-07/msg00280.html>.

    Since can_convert never assumes a C-style cast, it rejects casting
    away constness as in the test above and in:

      constexpr int *q = (int *)(const int *) nullptr;

    Removing the check only breaks constexpr-nullptr-2.C by not giving any
    diagnostic for line 229:

      constexpr B *pb2 = static_cast<B*>(pa0);  // { dg-error "not a constant
expression" }

    but the cast seems to be valid: we do [expr.static.cast]/7, and
    [expr.const] only says that a reinterpreter_cast and converting from
    void* is invalid in constexpr.  The can_convert check rejected convering
    from void *, but only when converting from a null pointer, so it's not
    good enough.  So I've added a check to catch conversions from cv void*.
    I realize it's not a great time to be adding additional checking, but
    removing the can_convert check would then technically be a regression.

    Let's limit the new check to only trigger for integer_zerop and then remove
    it in GCC 12.

    gcc/cp/ChangeLog:

            DR 1312
            PR c++/99176
            * constexpr.c (is_std_construct_at): New overload.
            (is_std_allocator_allocate): New overload.
            (cxx_eval_call_expression): Use the new overloads.
            (cxx_eval_constant_expression): Reject casting
            from void * as per DR 1312.  Don't check can_convert.

    gcc/testsuite/ChangeLog:

            DR 1312
            PR c++/99176
            * g++.dg/cpp0x/constexpr-nullptr-2.C: Adjust dg-error.
            * g++.dg/cpp0x/constexpr-cast2.C: New test.
            * g++.dg/cpp0x/constexpr-cast3.C: New test.

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

* [Bug c++/99176] [8/9/10 Regression] GCC rejects const_cast of null pointer in constant expressions
  2021-02-19 23:11 [Bug c++/99176] New: GCC rejects const_cast of null pointer in constant expressions richard-gccbugzilla at metafoo dot co.uk
                   ` (5 preceding siblings ...)
  2021-04-09 22:42 ` cvs-commit at gcc dot gnu.org
@ 2021-04-09 22:43 ` mpolacek at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-04-09 22:43 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #7 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed in GCC 10.4 too.

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

end of thread, other threads:[~2021-04-09 22:43 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-19 23:11 [Bug c++/99176] New: GCC rejects const_cast of null pointer in constant expressions richard-gccbugzilla at metafoo dot co.uk
2021-02-19 23:21 ` [Bug c++/99176] [8/9/10/11 Regression] " mpolacek at gcc dot gnu.org
2021-02-19 23:22 ` mpolacek at gcc dot gnu.org
2021-02-22 23:24 ` mpolacek at gcc dot gnu.org
2021-02-25 21:29 ` cvs-commit at gcc dot gnu.org
2021-02-25 21:32 ` [Bug c++/99176] [8/9/10 " mpolacek at gcc dot gnu.org
2021-04-09 22:42 ` cvs-commit at gcc dot gnu.org
2021-04-09 22:43 ` mpolacek 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).