From patchwork Wed Jul 3 14:39:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1956294 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=GY8gPl6k; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WDj986Ywkz1xqm for ; Thu, 4 Jul 2024 00:39:40 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 28C733860C37 for ; Wed, 3 Jul 2024 14:39:39 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 7768F3860C34 for ; Wed, 3 Jul 2024 14:39:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7768F3860C34 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7768F3860C34 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720017560; cv=none; b=pTEsyEPQ4Wg7zN8x1VV20VOdF6020BXoaIX1AFIOVm3iClgZZlh8XPi2u9z6jb6OaRorEaCHFtjgjZn5r16DOlHD1kxTAC8KIoewenhNRy3P+u/psNeMcyiRA14+8PH9aD5/GzdmobWKMDfXNwHkd+B8mubov2IqOyoL5J4cbYs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720017560; c=relaxed/simple; bh=3ChdyniawIdg6o3rDyAIsDUzGK1yOlOdXv66w0V7REM=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Wndf74vzyg97cZ3nAteQTb32284Gva0fayKJHeqnnP1b58+1fBuac7OY97NxJR5C+Mioj+i+FddutoYDgWWov/VikNvU7/9aDE0ziNhoQuXcgeHN1ltI322eaBLwh6nabRFLX/Z7Yn8yUvSGJArC4U2wdT988k2AQPwaeuTXVC4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720017558; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=XTdnJvz4mubiUAFpCIEvaGzoeueHPzjKy28PSOs31ks=; b=GY8gPl6k8P3QIjhwRlDDON5NIgpjGNNhbqKSpzPZbsLUp8XCIRK1tZQPaSRyv8ytmX9Lau DtH36cO5Uu6VrQifst95ln6W5ovVC537fQ0qpR+3j/Isp+QDkWx2HvktQFPJexCM9A6Ox3 EJSQmWKgRaksMFybunu8QKapAqRtiXM= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-182-yTV9kaNYOOanKJ7vp6BGNg-1; Wed, 03 Jul 2024 10:39:15 -0400 X-MC-Unique: yTV9kaNYOOanKJ7vp6BGNg-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A63701955BCB for ; Wed, 3 Jul 2024 14:39:13 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.5]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E543D1955F21; Wed, 3 Jul 2024 14:39:12 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 463Ed9fa3666124 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 3 Jul 2024 16:39:10 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 463Ed9U63666123; Wed, 3 Jul 2024 16:39:09 +0200 Date: Wed, 3 Jul 2024 16:39:09 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Implement C++26 CWG2819 - Allow cv void * null pointer value conversion to object types in constant expressions Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! The following patch implements CWG2819 (which wasn't a DR because it changes behavior of C++26 only). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-07-03 Jakub Jelinek * constexpr.cc (cxx_eval_constant_expression): CWG2819 - Allow cv void * null pointer value conversion to object types in constant expressions. * g++.dg/cpp26/constexpr-voidptr3.C: New test. * g++.dg/cpp0x/constexpr-cast2.C: Adjust expected diagnostics for C++26. * g++.dg/cpp0x/constexpr-cast4.C: Likewise. Jakub --- gcc/cp/constexpr.cc.jj 2024-07-02 22:09:52.493176541 +0200 +++ gcc/cp/constexpr.cc 2024-07-03 12:46:57.255025849 +0200 @@ -8157,10 +8157,13 @@ cxx_eval_constant_expression (const cons || DECL_NAME (decl) == heap_vec_uninit_identifier)) /* OK */; /* P2738 (C++26): a conversion from a prvalue P of type "pointer to - cv void" to a pointer-to-object type T unless P points to an - object whose type is similar to T. */ + cv void" to a pointer-to-object type T unless P is a null + pointer value or points to an object whose type is similar to + T. */ else if (cxx_dialect > cxx23) { + if (integer_zerop (sop)) + return build_int_cst (type, 0); r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop); if (r) { --- gcc/testsuite/g++.dg/cpp26/constexpr-voidptr3.C.jj 2024-07-03 12:35:56.301762995 +0200 +++ gcc/testsuite/g++.dg/cpp26/constexpr-voidptr3.C 2024-07-03 12:35:51.577825446 +0200 @@ -0,0 +1,13 @@ +// CWG 2819 - Cast from null pointer value in a constant expression +// { dg-do compile { target c++26 } } + +struct S { int s; }; + +constexpr S * +foo () +{ + void *p = nullptr; + return static_cast (p); +} + +static_assert (foo () == nullptr); --- gcc/testsuite/g++.dg/cpp0x/constexpr-cast2.C.jj 2023-07-17 09:07:42.104283529 +0200 +++ gcc/testsuite/g++.dg/cpp0x/constexpr-cast2.C 2024-07-03 16:22:33.294916937 +0200 @@ -5,9 +5,9 @@ static int i; constexpr void *vp0 = nullptr; constexpr void *vpi = &i; -constexpr int *p1 = (int *) vp0; // { dg-error "cast from .void\\*. is not allowed" } +constexpr int *p1 = (int *) vp0; // { dg-error "cast from .void\\*. is not allowed" "" { target c++23_down } } constexpr int *p2 = (int *) vpi; // { dg-error "cast from .void\\*. is not allowed" "" { target c++23_down } } -constexpr int *p3 = static_cast(vp0); // { dg-error "cast from .void\\*. is not allowed" } +constexpr int *p3 = static_cast(vp0); // { dg-error "cast from .void\\*. is not allowed" "" { target c++23_down } } constexpr int *p4 = static_cast(vpi); // { dg-error "cast from .void\\*. is not allowed" "" { target c++23_down } } constexpr void *p5 = vp0; constexpr void *p6 = vpi; --- gcc/testsuite/g++.dg/cpp0x/constexpr-cast4.C.jj 2023-11-02 07:39:18.679201173 +0100 +++ gcc/testsuite/g++.dg/cpp0x/constexpr-cast4.C 2024-07-03 16:23:29.424197809 +0200 @@ -8,4 +8,3 @@ constexpr float* pf = static_cast(vnp); // { dg-error "cast from .void\\*. is not allowed" "" { target c++23_down } } -// { dg-error "cast from .void\\*. is not allowed in a constant expression because .vnp. does not point to an object" "" { target c++26 } .-1 }