public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95307] New: Compiler accepts reinterpret_cast in constexpr
@ 2020-05-24 16:06 vincent.hamp at higaski dot at
  2020-05-24 18:38 ` [Bug c++/95307] " daniel.kruegler at googlemail dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: vincent.hamp at higaski dot at @ 2020-05-24 16:06 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95307
           Summary: Compiler accepts reinterpret_cast in constexpr
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent.hamp at higaski dot at
  Target Milestone: ---

The following snippet allows using reinterpret_casts inside a constexpr.

#include <cstdint>
uint64_t v;
constexpr auto p{reinterpret_cast<uint64_t>(&v) - 1u};

Compiled with GCC 10.1 and 9.3 with -std=c++2a


Interestingly subtracting 0u results in an error.

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

* [Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
  2020-05-24 16:06 [Bug c++/95307] New: Compiler accepts reinterpret_cast in constexpr vincent.hamp at higaski dot at
@ 2020-05-24 18:38 ` daniel.kruegler at googlemail dot com
  2020-05-25  9:44 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2020-05-24 18:38 UTC (permalink / raw)
  To: gcc-bugs

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

Daniel Krügler <daniel.kruegler at googlemail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |daniel.kruegler@googlemail.
                   |                            |com

--- Comment #1 from Daniel Krügler <daniel.kruegler at googlemail dot com> ---
(In reply to Vincent Hamp from comment #0)
> The following snippet allows using reinterpret_casts inside a constexpr.
> 
> #include <cstdint>
> uint64_t v;
> constexpr auto p{reinterpret_cast<uint64_t>(&v) - 1u};
> 
> Compiled with GCC 10.1 and 9.3 with -std=c++2a
> 
> 
> Interestingly subtracting 0u results in an error.

Here a library-free variant of the code including the compiler flags used:

-Wall -Wextra -std=gnu++2a -pedantic 

tested using gcc 11.0.0 20200522 (experimental):

//<<<<<<<<<<<<<<<<<<<<<
using uint64_t = unsigned long;
static_assert(sizeof(uint64_t) * 8 == 64);
uint64_t v;
constexpr auto p{reinterpret_cast<uint64_t>(&v) - 1u};

int main() 
{
}
//>>>>>>>>>>>>>>>>>>>>>>>

The essential part of the reproducer is the fact that we have a variable of
static storage duration involved. Using a local variable in main() does make
the compiler reject the code.

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

* [Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
  2020-05-24 16:06 [Bug c++/95307] New: Compiler accepts reinterpret_cast in constexpr vincent.hamp at higaski dot at
  2020-05-24 18:38 ` [Bug c++/95307] " daniel.kruegler at googlemail dot com
@ 2020-05-25  9:44 ` redi at gcc dot gnu.org
  2020-05-25 10:11 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-05-25  9:44 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2020-05-25
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

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

* [Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
  2020-05-24 16:06 [Bug c++/95307] New: Compiler accepts reinterpret_cast in constexpr vincent.hamp at higaski dot at
  2020-05-24 18:38 ` [Bug c++/95307] " daniel.kruegler at googlemail dot com
  2020-05-25  9:44 ` redi at gcc dot gnu.org
@ 2020-05-25 10:11 ` jakub at gcc dot gnu.org
  2020-05-26 15:57 ` msebor at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-05-25 10:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
For - 0 it is diagnosed by:
  /* Technically we should check this for all subexpressions, but that
     runs into problems with our internal representation of pointer
     subtraction and the 5.19 rules are still in flux.  */
  if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
      && ARITHMETIC_TYPE_P (TREE_TYPE (r))
      && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
    {
      if (!allow_non_constant)
        error ("conversion from pointer type %qT "
               "to arithmetic type %qT in a constant expression",
               TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
      non_constant_p = true;
    }
and what matters is what the comment says, we really should be checking it for
subexpressions (therefore move into cxx_eval_constant_expression in
NOP_EXPR/CONVERT_EXPR case).
We have POINTER_DIFF_EXPR now so one would hope we don't run into the issues
mentioned there.

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

* [Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
  2020-05-24 16:06 [Bug c++/95307] New: Compiler accepts reinterpret_cast in constexpr vincent.hamp at higaski dot at
                   ` (2 preceding siblings ...)
  2020-05-25 10:11 ` jakub at gcc dot gnu.org
@ 2020-05-26 15:57 ` msebor at gcc dot gnu.org
  2020-05-26 21:03 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-05-26 15:57 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org
             Blocks|                            |55004

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
This looks like a duplicate of pr82304.


Referenced Bugs:

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

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

* [Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
  2020-05-24 16:06 [Bug c++/95307] New: Compiler accepts reinterpret_cast in constexpr vincent.hamp at higaski dot at
                   ` (3 preceding siblings ...)
  2020-05-26 15:57 ` msebor at gcc dot gnu.org
@ 2020-05-26 21:03 ` mpolacek at gcc dot gnu.org
  2020-05-26 21:15 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-05-26 21:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
And related to bug 93955.

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

* [Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
  2020-05-24 16:06 [Bug c++/95307] New: Compiler accepts reinterpret_cast in constexpr vincent.hamp at higaski dot at
                   ` (4 preceding siblings ...)
  2020-05-26 21:03 ` mpolacek at gcc dot gnu.org
@ 2020-05-26 21:15 ` jakub at gcc dot gnu.org
  2020-06-04  7:09 ` cvs-commit at gcc dot gnu.org
  2021-12-03 16:33 ` mpolacek at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-05-26 21:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I've tried:
--- gcc/cp/constexpr.c.jj       2020-05-25 10:06:59.886175941 +0200
+++ gcc/cp/constexpr.c  2020-05-26 22:02:23.661355854 +0200
@@ -6196,6 +6196,18 @@ cxx_eval_constant_expression (const cons
        if (VOID_TYPE_P (type))
          return void_node;

+       if (CONVERT_EXPR_CODE_P (TREE_CODE (t))
+           && ARITHMETIC_TYPE_P (type)
+           && INDIRECT_TYPE_P (TREE_TYPE (op)))
+         {
+           if (!ctx->quiet)
+             error ("conversion from pointer type %qT "
+                    "to arithmetic type %qT in a constant expression",
+                    TREE_TYPE (op), type);
+           *non_constant_p = true;
+           return t;
+         }
+
        if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
          op = cplus_expand_constant (op);

@@ -6797,19 +6809,6 @@ cxx_eval_outermost_constant_expr (tree t
       non_constant_p = true;
     }

-  /* Technically we should check this for all subexpressions, but that
-     runs into problems with our internal representation of pointer
-     subtraction and the 5.19 rules are still in flux.  */
-  if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
-      && ARITHMETIC_TYPE_P (TREE_TYPE (r))
-      && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
-    {
-      if (!allow_non_constant)
-       error ("conversion from pointer type %qT "
-              "to arithmetic type %qT in a constant expression",
-              TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
-      non_constant_p = true;
-    }

   if (!non_constant_p && overflow_p)
     non_constant_p = true;
but will need to look through testsuite regressions and find out which tests
just need adjustments and if there isn't something really broken by that.

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

* [Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
  2020-05-24 16:06 [Bug c++/95307] New: Compiler accepts reinterpret_cast in constexpr vincent.hamp at higaski dot at
                   ` (5 preceding siblings ...)
  2020-05-26 21:15 ` jakub at gcc dot gnu.org
@ 2020-06-04  7:09 ` cvs-commit at gcc dot gnu.org
  2021-12-03 16:33 ` mpolacek at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-06-04  7:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:eeb54a14c48f543857f561556ab1fc49dc21af26

commit r11-893-geeb54a14c48f543857f561556ab1fc49dc21af26
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Jun 4 09:09:01 2020 +0200

    c++: Reject some further reinterpret casts in constexpr [PR82304, PR95307]

    cxx_eval_outermost_constant_expr had a check for reinterpret_casts from
    pointers (well, it checked from ADDR_EXPRs) to integral type, but that
    only caught such cases at the toplevel of expressions.
    As the comment said, it should be done even inside of the expressions,
    but at the point of the writing e.g. pointer differences used to be a
    problem.  We now have POINTER_DIFF_EXPR, so this is no longer an issue.

    Had to do it just for CONVERT_EXPR, because the FE emits NOP_EXPR casts
    from pointers to integrals in various spots, e.g. for the PMR & 1 tests,
    though on NOP_EXPR we have the REINTERPRET_CAST_P bit that we do check,
    while on CONVERT_EXPR we don't.

    2020-06-04  Jakub Jelinek  <jakub@redhat.com>

            PR c++/82304
            PR c++/95307
            * constexpr.c (cxx_eval_constant_expression): Diagnose CONVERT_EXPR
            conversions from pointer types to arithmetic types here...
            (cxx_eval_outermost_constant_expr): ... instead of here.

            * g++.dg/template/pr79650.C: Expect different diagnostics and
expect
            it on all lines that do pointer to integer casts.
            * g++.dg/cpp1y/constexpr-shift1.C: Expect different diagnostics.
            * g++.dg/cpp1y/constexpr-82304.C: New test.
            * g++.dg/cpp0x/constexpr-95307.C: New test.

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

* [Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
  2020-05-24 16:06 [Bug c++/95307] New: Compiler accepts reinterpret_cast in constexpr vincent.hamp at higaski dot at
                   ` (6 preceding siblings ...)
  2020-06-04  7:09 ` cvs-commit at gcc dot gnu.org
@ 2021-12-03 16:33 ` mpolacek at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-12-03 16:33 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #7 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Comment 1 testcase is rejected with GCC 11+.  If there are more cases left to
be resolved, let's open a new PR.

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

end of thread, other threads:[~2021-12-03 16:33 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-24 16:06 [Bug c++/95307] New: Compiler accepts reinterpret_cast in constexpr vincent.hamp at higaski dot at
2020-05-24 18:38 ` [Bug c++/95307] " daniel.kruegler at googlemail dot com
2020-05-25  9:44 ` redi at gcc dot gnu.org
2020-05-25 10:11 ` jakub at gcc dot gnu.org
2020-05-26 15:57 ` msebor at gcc dot gnu.org
2020-05-26 21:03 ` mpolacek at gcc dot gnu.org
2020-05-26 21:15 ` jakub at gcc dot gnu.org
2020-06-04  7:09 ` cvs-commit at gcc dot gnu.org
2021-12-03 16:33 ` 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).