From patchwork Wed Dec 27 19:23:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 82891 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 92C433858C2C for ; Wed, 27 Dec 2023 19:24:32 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202]) by sourceware.org (Postfix) with ESMTPS id C68B93858D32 for ; Wed, 27 Dec 2023 19:24:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C68B93858D32 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=tugraz.at Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tugraz.at ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C68B93858D32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=129.27.2.202 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703705044; cv=none; b=EnOh3IhMeBiCntU1R3hYSCdOsjyfQ5y/C7bMDiHtHF6k+6nkBEPrwUjp0c8USZk1ds18D7WvZ9/Kf4XM4Ip9Dx16VD7wjpUKmKEKLBgh0ETjz/MOyaHS3v9o8CxXOWurroicPn6hiLaFyxm5ak5V+Iir9ep1HnqE2iaS78N2ztI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703705044; c=relaxed/simple; bh=d9XGmGCJXojLdwlXpvnpTy4WeKha7XxIyOB4IzrrJVA=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=MI7xRQCB2cFWh5ye3sRms3r3ta8wzmJ0pbr2QHG7mplnc84zdktrwKPzQqaVbqDwBGkKhkKfAuxgHY/bBpMqtMCAtiWU6H9eXD5KpRynJqgXS4ytBb1jIvf/iVdl6daqjhALa6Mc3Uadgekp47uHwDt8eVw/WAbwjK32EZ/t+ZE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from vra-171-231.tugraz.at (vra-171-231.tugraz.at [129.27.171.231]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4T0hQG61VHz3wgT; Wed, 27 Dec 2023 20:23:50 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1703705031; bh=xuDZ+vfRzByRriyb0owj2nmrU116Wi4F6xl0pbUPyO8=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=MNWJQerZt6bciNhZF6k7GQ3BD5YXt4v1lDv0a11XILFHJoIfGgJBbG1GKv23lIjKS c/JzvxqVuEW9HA0w5zDAN0ICiAoUpZCxfsin6356VQyB/tB3y6Qj4ZOusBBHPCyp3l 4itdBM2K2cvx1hREjxbf0Bn+5lZZarSwP1aONTbI= Message-ID: <8ec71de2b1c42f2ed75a97cabced6561861791ab.camel@tugraz.at> Subject: [C PATCH] C: Fix type compatibility for structs with variable sized fields. From: Martin Uecker To: gcc-patches@gcc.gnu.org Cc: Joseph Myers , "Jiang, Haochen" Date: Wed, 27 Dec 2023 20:23:50 +0100 In-Reply-To: <02a9b94e4d653b6f1b9f89a1b62187f46e871738.camel@tugraz.at> References: <02a9b94e4d653b6f1b9f89a1b62187f46e871738.camel@tugraz.at> User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 X-TUG-Backscatter-control: G/VXY7/6zeyuAY/PU2/0qw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -1.9 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.117 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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+patchwork=sourceware.org@gcc.gnu.org This patch hopefully fixes the test failure we see with gnu23-tag-4.c. It does for me locally with -march=native (which otherwise reproduces the problem). Bootstrapped and regession tested on x86_64 C: Fix type compatibility for structs with variable sized fields. This fixes the test gcc.dg/gnu23-tag-4.c introduced by commit 23fee88f which fails for -march=... because the DECL_FIELD_BIT_OFFSET are set inconsistently for types with and without variable-sized field. This is fixed by testing for DECL_ALIGN instead. The code is further simplified by removing some unnecessary conditions, i.e. anon_field is set unconditionaly and all fields are assumed to be DECL_FIELDs. gcc/c: * c-typeck.c (tagged_types_tu_compatible_p): Revise. gcc/testsuite: * gcc.dg./c23-tag-9.c: New test. --- gcc/c/c-typeck.cc | 19 ++++++++----------- gcc/testsuite/gcc.dg/c23-tag-9.c | 8 ++++++++ 2 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/c23-tag-9.c diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 2d9139d09d2..84ddda1ebab 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -1511,8 +1511,6 @@ tagged_types_tu_compatible_p (const_tree t1, const_tree t2, if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2)) data->different_types_p = true; - data->anon_field = false; - /* Incomplete types are incompatible inside a TU. */ if (TYPE_SIZE (t1) == NULL || TYPE_SIZE (t2) == NULL) return false; @@ -1592,22 +1590,21 @@ tagged_types_tu_compatible_p (const_tree t1, const_tree t2, s1 && s2; s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2)) { - if (TREE_CODE (s1) != TREE_CODE (s2) - || DECL_NAME (s1) != DECL_NAME (s2)) + gcc_assert (TREE_CODE (s1) == FIELD_DECL); + gcc_assert (TREE_CODE (s2) == FIELD_DECL); + + if (DECL_NAME (s1) != DECL_NAME (s2)) + return false; + + if (DECL_ALIGN (s1) != DECL_ALIGN (s2)) return false; - if (!DECL_NAME (s1) && RECORD_OR_UNION_TYPE_P (TREE_TYPE (s1))) - data->anon_field = true; + data->anon_field = !DECL_NAME (s1); data->cache = &entry; if (!comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), data)) return false; - if (TREE_CODE (s1) == FIELD_DECL - && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1), - DECL_FIELD_BIT_OFFSET (s2)) != 1) - return false; - tree st1 = TYPE_SIZE (TREE_TYPE (s1)); tree st2 = TYPE_SIZE (TREE_TYPE (s2)); diff --git a/gcc/testsuite/gcc.dg/c23-tag-9.c b/gcc/testsuite/gcc.dg/c23-tag-9.c new file mode 100644 index 00000000000..1d32560ec23 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c23-tag-9.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c23" } */ + +struct foo { int x; } x; +struct foo { alignas(128) int x; } y; /* { dg-error "redefinition" } */ +static_assert(alignof(y) == 128); + +