From patchwork Fri Oct 20 16:31:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 156226 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp1180318vqb; Fri, 20 Oct 2023 09:31:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEVo3lgbZbc9c76g3f67UaW6i1yVVXy4sgSY6yg6rc3OzPW7RYgfbaURw/ioaQFqabSK/Fq X-Received: by 2002:a05:6214:caa:b0:66d:1a20:f278 with SMTP id s10-20020a0562140caa00b0066d1a20f278mr2811697qvs.29.1697819518012; Fri, 20 Oct 2023 09:31:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697819517; cv=pass; d=google.com; s=arc-20160816; b=MI3Rm0c7mKUNBghTQsMM2siWZJlKi2a6r7TcnEiplPop/J0FzNG34DILWCqGVuSHan I2oEOA9aC1rhGy+eytAxGyv9kMOxduPJsRbwP5ZMR++AgmD39Y8u7T1WfcIDhzveukLR 27N5IKXSvbtz8+WfJ0TK4BEOZMtKeBIftHNCAUUI+RQoE/xCOvTk1fxdHAVgYAi4uTSP HZw/ux2WoSv4EVFjg57wEhfxGhAIxcU1Awi2MJ2fM5aMRQMaxmbmFBD9BJP1SiJh4A0O JGIpGCIVTOYqsC/SYOPLn4Fd+5LY1Y2sL7wvXLS7Mp4cnLVW6tERDsfPV1G8vdvS/5JS GXWQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=zctliR2W+68uAsJpr6aUmuRxR3cwKMOrIf8iosEr8tY=; fh=P2wy31v18ipFyd1Cp5dTLkQx8U69T4lxl3ttjRTnoYA=; b=F+z6eH+vD7gK2Bf4SVTLAjvsvllJRECzvrBGcTrDB1NNgS0/X5XZslVpVPm1BdElKW tjROVgTaQENuu2obru7BDSS2HwMcdR0i41vdzCsmgiOLRhmd5UC3c3V6CXMUbmSDJPAV ezT6KkewdFDvFvN6MCAYYYfF9sz2LZx0PeDsbPvdMfFKWdlRPabv5VFxzvy3UNnFVH23 WDDRvQpHCfTZRbqCszdyAhNVucCRNimMereo2mg+x1PPPKtMxCekI+2qGptKvd6MehPN VBjicGt0zv24vv30BvmbABR7/IiLVzFZpn+7v4LHOhVZ9Kk4qet1tkOABHzqeK239UFB ULXg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FVQ4j8Hg; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id k16-20020ad44510000000b0065b284d5678si1626473qvu.109.2023.10.20.09.31.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 09:31:57 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FVQ4j8Hg; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C404D38582BC for ; Fri, 20 Oct 2023 16:31:57 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id F00143858D20 for ; Fri, 20 Oct 2023 16:31:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F00143858D20 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 F00143858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697819495; cv=none; b=qRC3dmKpHHmhk1ztBKLUwoCWMuFYPepqh2L3EoTSdX2WMWxqpusv7gnDwh53EMEbXB3v0RDWRWo2rcICZBPXTG532/3qUQ9HnizE+E0abMFFtb/xyCafWwr78iO2xRJrtvP8C0ovf8DTwl2aDhtwoFa9hxjBNWT30f17fG5v99M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697819495; c=relaxed/simple; bh=gkxuVyftIIMkN/RHEMT3vmcl9wvlpg1etSb2JXkV39c=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=B8fWW1pQ1r9ZumDErxkYw9waLtgysTln2TOheNOx7HZHZjz5Aa9nwwlb2aNB8RCk8kzALXUtx0KfyiSp+Lc0vU4niln8wkaxxbisBVntWGq8V4g7F5VxdmY8wiooq9eArxxpYHF5IuHDcFE9g7McZbXQxUjRxolL5rIi/ha10NQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697819493; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zctliR2W+68uAsJpr6aUmuRxR3cwKMOrIf8iosEr8tY=; b=FVQ4j8HgMmAamNnCpoz7ttRKZqbRXojXweYSurZVIUuLdCPZGpWMR08Jnsf6opeQxNuSv2 WMfisPyhReeAUV+4DNhvHmUL83U+HgeVm5soYrrqNX2vTnvG8CL+wrBt4/hpyIdB9gYJeW YDW1p9aBVL6LwljJIPEuU4prq/88vqY= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-543-9Aplr61HO2ecAMd_Kgubtw-1; Fri, 20 Oct 2023 12:31:29 -0400 X-MC-Unique: 9Aplr61HO2ecAMd_Kgubtw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 155A229ABA05; Fri, 20 Oct 2023 16:31:29 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.17.177]) by smtp.corp.redhat.com (Postfix) with ESMTP id DAC9B503C; Fri, 20 Oct 2023 16:31:28 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill , Joseph Myers Subject: [PATCH] c-family: char8_t and aliasing in C vs C++ [PR111884] Date: Fri, 20 Oct 2023 12:31:21 -0400 Message-ID: <20231020163121.25120-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780292799311643303 X-GMAIL-MSGID: 1780292799311643303 Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- In the PR, Joseph says that in C char8_t is not a distinct type. So we should behave as if it can alias anything, like ordinary char. In C, unsigned_char_type_node == char8_type_node, so with this patch we return 0 instead of -1. And the following comment says: /* The C standard guarantees that any object may be accessed via an lvalue that has narrow character type (except char8_t). */ if (t == char_type_node || t == signed_char_type_node || t == unsigned_char_type_node) return 0; Which appears to be wrong, so I'm adjusting that as well. PR c/111884 gcc/c-family/ChangeLog: * c-common.cc (c_common_get_alias_set): Return -1 for char8_t only in C++. gcc/testsuite/ChangeLog: * c-c++-common/alias-1.c: New test. --- gcc/c-family/c-common.cc | 7 ++++--- gcc/testsuite/c-c++-common/alias-1.c | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/alias-1.c base-commit: eb15fad3190a8b33e3e451b964ff1ecf08bbb113 diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index f044db5b797..0efdc677217 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -3828,12 +3828,13 @@ c_common_get_alias_set (tree t) if (!TYPE_P (t)) return -1; - /* Unlike char, char8_t doesn't alias. */ - if (flag_char8_t && t == char8_type_node) + /* Unlike char, char8_t doesn't alias in C++. (In C, char8_t is not + a distinct type.) */ + if (flag_char8_t && t == char8_type_node && c_dialect_cxx ()) return -1; /* The C standard guarantees that any object may be accessed via an - lvalue that has narrow character type (except char8_t). */ + lvalue that has narrow character type. */ if (t == char_type_node || t == signed_char_type_node || t == unsigned_char_type_node) diff --git a/gcc/testsuite/c-c++-common/alias-1.c b/gcc/testsuite/c-c++-common/alias-1.c new file mode 100644 index 00000000000..d72fec47f76 --- /dev/null +++ b/gcc/testsuite/c-c++-common/alias-1.c @@ -0,0 +1,23 @@ +/* PR c/111884 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wall" } */ +/* { dg-additional-options "-std=c++20" { target c++ } } */ +/* { dg-additional-options "-std=c2x" { target c } } */ + +int f(int i) +{ + int f = 1; + return i[(unsigned char *)&f]; +} + +int g(int i) +{ + int f = 1; + return i[(signed char *)&f]; +} + +int h(int i) +{ + int f = 1; + return i[(char *)&f]; +}