[Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95307 Marek Polacek changed: What|Removed |Added Status|NEW |RESOLVED Resolution|--- |FIXED --- Comment #7 from Marek Polacek --- Comment 1 testcase is rejected with GCC 11+. If there are more cases left to be resolved, let's open a new PR.
[Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95307 --- Comment #6 from CVS Commits --- The master branch has been updated by Jakub Jelinek : https://gcc.gnu.org/g:eeb54a14c48f543857f561556ab1fc49dc21af26 commit r11-893-geeb54a14c48f543857f561556ab1fc49dc21af26 Author: Jakub Jelinek 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 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.
[Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95307 --- Comment #5 from Jakub Jelinek --- 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.
[Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95307 --- Comment #4 from Marek Polacek --- And related to bug 93955.
[Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95307 Martin Sebor changed: What|Removed |Added CC||msebor at gcc dot gnu.org Blocks||55004 --- Comment #3 from Martin Sebor --- 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
[Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95307 --- Comment #2 from Jakub Jelinek --- 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.
[Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95307 Jonathan Wakely changed: What|Removed |Added Last reconfirmed||2020-05-25 Ever confirmed|0 |1 Status|UNCONFIRMED |NEW
[Bug c++/95307] Compiler accepts reinterpret_cast in constexpr
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95307 Daniel Krügler changed: What|Removed |Added CC||daniel.kruegler@googlemail. ||com --- Comment #1 from Daniel Krügler --- (In reply to Vincent Hamp from comment #0) > The following snippet allows using reinterpret_casts inside a constexpr. > > #include > uint64_t v; > constexpr auto p{reinterpret_cast() - 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() - 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.