c++: Reject some further reinterpret casts in constexpr [PR82304, PR95307]
authorJakub Jelinek <jakub@redhat.com>
Thu, 4 Jun 2020 07:09:01 +0000 (09:09 +0200)
committerJakub Jelinek <jakub@redhat.com>
Thu, 4 Jun 2020 07:09:01 +0000 (09:09 +0200)
commiteeb54a14c48f543857f561556ab1fc49dc21af26
tree95500253f7dcd78bb2d1f598b93675917cb3e32f
parent9a5b7438c85a926a0dba9dc980039f3d67571763
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.
gcc/cp/constexpr.c
gcc/testsuite/g++.dg/cpp0x/constexpr-95307.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-82304.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-shift1.C
gcc/testsuite/g++.dg/template/pr79650.C