From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 208F03847822; Fri, 9 Apr 2021 22:42:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 208F03847822 From: "cvs-commit at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/99176] [8/9/10 Regression] GCC rejects const_cast of null pointer in constant expressions Date: Fri, 09 Apr 2021 22:42:18 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: rejects-valid X-Bugzilla-Severity: normal X-Bugzilla-Who: cvs-commit at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: mpolacek at gcc dot gnu.org X-Bugzilla-Target-Milestone: 9.4 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 09 Apr 2021 22:42:19 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99176 --- Comment #6 from CVS Commits --- The releases/gcc-10 branch has been updated by Marek Polacek : https://gcc.gnu.org/g:581e87b67233556d566df3a0ea33be9c19fbcb2f commit r10-9687-g581e87b67233556d566df3a0ea33be9c19fbcb2f Author: Marek Polacek 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 =3D nullptr; constexpr int *q =3D const_cast(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 and here . Since can_convert never assumes a C-style cast, it rejects casting away constness as in the test above and in: constexpr int *q =3D (int *)(const int *) nullptr; Removing the check only breaks constexpr-nullptr-2.C by not giving any diagnostic for line 229: constexpr B *pb2 =3D static_cast(pa0); // { dg-error "not a cons= tant 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 re= move 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.=