From patchwork Wed Jul 8 09:16:07 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: gerris.rs@gmail.com X-Patchwork-Id: 138716 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 655B54BA2E10 for ; Wed, 8 Jul 2026 09:17:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 655B54BA2E10 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=aCQ4Ctqk X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by sourceware.org (Postfix) with ESMTPS id 750B94BA2E07 for ; Wed, 8 Jul 2026 09:16:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 750B94BA2E07 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 750B94BA2E07 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1034 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783502172; cv=none; b=muCAuV37k4I7y4FtuASVZB4ebCxlys4Jtp9lfjXLXwk68FJaPhOVdF/UlEzAr7+1zLzbllWB1t622gvQGArdEnI8syfMT/WJNo5Ea/9SbEaxo1t9/t3QhqF/NmDSwfLpTbuChsZYcDBXpOUT4KmVdZF4dkZkXZQxyveEUku7aSU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783502172; c=relaxed/simple; bh=huXeDDI4a0Ga+IJ0sUKanDaWQzes7CPFxkRc1MDJVME=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=oSJGifbsAxe9xzu3yum6TvTFCxP4qfihjtF50Mtpqtm2rq3SsmE4XZ/bgChu+FdsbXp/zfpVIS76hNFDcj+g62JbiWCmBHoeCNjRp8iSFLIvML/JHXR4oDJpDUk9kmwwjgs0+9nHRiizpR+E2AiZUmSjbpIiREelSvHgitEBEeU= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=aCQ4Ctqk DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 750B94BA2E07 Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-3817f800c8bso406967a91.1 for ; Wed, 08 Jul 2026 02:16:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783502171; x=1784106971; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=dxqoOpuXvSK2eBOyOlznztw24DX8MUxtRuwxj3ZJsOk=; b=aCQ4CtqkqXyuwSpOTqUFFLXZHH/OgQwLyDqOnoh5kvWiNulWNFxKJYuQh4XPgG31Ya 3Zc047pcOK50u8hBu9pOYyOU8zqD44mdDgKA6sx8QrxcO6XElRnC6GIvru2frHUJF0JK LIf4tMysT+Nu+AOKpB3wEzrnzaRpr9tx32AmOpdpvQm+R3ab7kUAXK+6JeuBvX6wKp7A HR3G88sB0A1z70XR1tKxfSxkT5rOuIvcI5RMapj4Ude1lkUHSXM9tUg+IIqxmMburRXu 48ub9tFsdRlcifrh5qD0vkP9UBFhUc7P28FrjYXbkBnQwJI6HnU/0LQMijStvJr6eYdc l8kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783502171; x=1784106971; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=dxqoOpuXvSK2eBOyOlznztw24DX8MUxtRuwxj3ZJsOk=; b=AnRD7RERqbLI2Fz0jQaSpRIf5DFh0VUNk+S6LNJkVdHQarwaC7C3oOgburP8FYzapI eWuFeJm+6nAynlKUN5SSHKpFc1btZSpz+L8znnq7wUaYnXgS9+xoph3X7nvWG6Lp/W+9 EPIvd7h0ZuEcCoJq0RP/0wildr1v+Ef9FgVmkvowLMiODBtRgbjmHITN3VGyhmj6ItWy Iimxxj+8CzLZWDVxSKtj3JpqPWR7zSl8CdthM8r+02dvsqwiPkhiAMUkMiio8ulePH72 ZXWkW9rpqVnmaPiCCbvy+BJjN7zo7bpy24Zl2VSQGde++cszACWP7ZxF2afV/FBrzF1o aD8w== X-Gm-Message-State: AOJu0Yz1iA/Sn/ERy3WOj8xQZ9S4leiCWHbZjIOX1vSYf1yC4CkK747d VMlilN+iyfo7G02e+31uW7RDlFVRnhepI2ho3ljRgltaNTo2Vi01nezqetoscg== X-Gm-Gg: AfdE7cn5FvgDo2rl3NTYlDiQ6l3NQaCr1AZIp10JoNmHJP4nq4dyyTpOnMxA1gPlwyZ Ty8giMiV5/mBpqcyoM+Mpk0/CZ2qEOUp9gL6YbGkV26AvcKEmx8Snf2IeHbzlwlA0eSVzBihFUj qcMRK8Ll3tERN1KN49JsbfrKb9B7oyvySGlb8qHtWwjJMOoc0EPKzx9uwtmQjqdNVWPqum15Wyv mqsshwt4o0v2FGXREOoqG5ZFGdRNALYiGCRv91Xq5LsZSQEjlsFolBX+R8Z7k2jK9Wzvqpo+Wqs DImvCws8daQIxohzS+lLfXVu9svOYTBmRj0fVbHmmd1RuxR5zUmeLcTUP54lgamxDRR2PwOt20t PatJW8tMVouKm6kiuTDyJpsgO7r+5rwRNOC0+AnWpMF3BhuzhBLikMa+cOYFz0NE1Suiyd91yQ5 ESNIuTG2aJkRF6XmI8nMiPy/Q5LuwCR19biCtZ9kbAtihevtWVDFEPoJgBZE5vinzRWDFRsbsyz +UkzNQa3Zo= X-Received: by 2002:a17:90b:54c6:b0:37d:f72c:9636 with SMTP id 98e67ed59e1d1-3894043be54mr1880047a91.11.1783502171286; Wed, 08 Jul 2026 02:16:11 -0700 (PDT) Received: from runnervm5mmn9.nkr5xsuno4uefblpyhv20ya3ue.dx.internal.cloudapp.net ([52.250.243.39]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-31174a583bcsm20466552eec.19.2026.07.08.02.16.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jul 2026 02:16:10 -0700 (PDT) From: gerris.rs@gmail.com To: gcc-patches@gcc.gnu.org, gcc-rust@gcc.gnu.org Cc: pierre-emmanuel.patry@embecosm.com, arthur.cohen@embecosm.com, dkm+gccrs@kataplop.net, Yap Zhi Heng Subject: [gccrs COMMIT 1/3] gccrs: Add support for #[repr(transparent)] attribute Date: Wed, 8 Jul 2026 09:16:07 +0000 Message-ID: <20260708091609.3108-1-gerris.rs@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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 From: Yap Zhi Heng gcc/rust/ChangeLog: * typecheck/rust-tyty.h (TyTy::ADTType::ReprKind): Add TRANSPARENT variant. * typecheck/rust-hir-type-check-base.cc (TypeCheckBase::parse_repr_options): Add parsing of transparent repr, throw error for invalid repr. * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit (StructStruct)): Throw error if #[repr(transparent)] struct/enum has more than 1 field. Signed-Off-By: Yap Zhi Heng --- This change was merged into the gccrs repository and is posted here for upstream visibility and potential drive-by review, as requested by GCC release managers. Each commit email contains a link to its details on github from where you can find the Pull-Request and associated discussions. Commit on github: https://github.com/Rust-GCC/gccrs/commit/ca4f17810c0dcb5974bdb0a50e1c4620ddf961de The commit has NOT been mentioned in any issue. The commit has been mentioned in the following pull-request(s): - https://github.com/Rust-GCC/gccrs/pull/4650 .../typecheck/rust-hir-type-check-base.cc | 22 ++++++++++++++++--- .../typecheck/rust-hir-type-check-item.cc | 18 ++++++++++----- gcc/rust/typecheck/rust-tyty.h | 2 +- .../rust/compile/invalid_repr_hint.rs | 12 ++++++++++ .../rust/compile/repr_transparent_fields.rs | 16 ++++++++++++++ 5 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/rust/compile/invalid_repr_hint.rs create mode 100644 gcc/testsuite/rust/compile/repr_transparent_fields.rs base-commit: eef61cb13a49d1a2dbc6cd874b2ecc8906ee1da3 diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc index 8b6b4d6f5..7ff37c924 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-base.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc @@ -517,12 +517,12 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus) bool is_align = false; bool is_c = false; bool is_integer = false; + bool is_transparent = false; unsigned char value = 1; if (oparen == std::string::npos) { is_pack = inline_option.compare ("packed") == 0; - is_align = inline_option.compare ("align") == 0; is_c = inline_option.compare ("C") == 0; is_integer = (inline_option.compare ("isize") == 0 || inline_option.compare ("i8") == 0 @@ -536,6 +536,7 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus) || inline_option.compare ("u32") == 0 || inline_option.compare ("u64") == 0 || inline_option.compare ("u128") == 0); + is_transparent = inline_option.compare ("transparent") == 0; } else @@ -554,7 +555,16 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus) value = strtoul (value_str.c_str () + 1, NULL, 10); } - if (is_pack) + if (is_transparent) + { + if (is_pack || is_align || is_c || is_integer) + rust_error_at ( + locus, ErrorCode::E0692, + "transparent struct cannot have other repr hints"); + + repr.repr_kind = TyTy::ADTType::ReprKind::TRANSPARENT; + } + else if (is_pack) { repr.repr_kind = TyTy::ADTType::ReprKind::PACKED; repr.pack = value; @@ -574,9 +584,15 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus) bool ok = context->lookup_builtin (inline_option, &repr.repr); if (!ok) { - rust_error_at (attr.get_locus (), "Invalid repr type"); + rust_error_at (attr.get_locus (), ErrorCode::E0552, + "unrecognized representation hint"); } } + else + { + rust_error_at (attr.get_locus (), ErrorCode::E0552, + "unrecognized representation hint"); + } delete meta_items; diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc index 065615a23..ac0eeaf27 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-item.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc @@ -339,6 +339,19 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl) ResolveWhereClauseItem::Resolve (*where_clause_item, region_constraints); } + // Process #[repr(X)] attribute, if any + const AST::AttrVec &attrs = struct_decl.get_outer_attrs (); + TyTy::ADTType::ReprOptions repr + = parse_repr_options (attrs, struct_decl.get_locus ()); + if (repr.repr_kind == TyTy::ADTType::ReprKind::TRANSPARENT + && struct_decl.get_fields ().size () > 1) + { + rust_error_at (struct_decl.get_locus (), ErrorCode::E0690, + "transparent struct needs at most one field with " + "non-trivial size or alignment, but has %lu", + (unsigned long) struct_decl.get_fields ().size ()); + } + std::vector fields; for (auto &field : struct_decl.get_fields ()) { @@ -379,11 +392,6 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl) struct_decl.get_identifier ().as_string (), ident, variant_type, tl::nullopt, std::move (fields))); - // Process #[repr(X)] attribute, if any - const AST::AttrVec &attrs = struct_decl.get_outer_attrs (); - TyTy::ADTType::ReprOptions repr - = parse_repr_options (attrs, struct_decl.get_locus ()); - auto *type = new TyTy::ADTType ( struct_decl.get_mappings ().get_defid (), struct_decl.get_mappings ().get_hirid (), diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index b5e5f02c6..f645f673f 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -910,7 +910,7 @@ public: INT, ALIGN, PACKED, - // TRANSPARENT, + TRANSPARENT, // SIMD, // ... }; diff --git a/gcc/testsuite/rust/compile/invalid_repr_hint.rs b/gcc/testsuite/rust/compile/invalid_repr_hint.rs new file mode 100644 index 000000000..7ac48a921 --- /dev/null +++ b/gcc/testsuite/rust/compile/invalid_repr_hint.rs @@ -0,0 +1,12 @@ +#![feature(no_core)] +#![no_core] + +#[repr(InvalidRepr)] // { dg-error "unrecognized representation hint" } +struct Foo { + x: i32, +} + +#[repr(align)] // { dg-error "unrecognized representation hint" } +struct Bar { + x: i32, +} diff --git a/gcc/testsuite/rust/compile/repr_transparent_fields.rs b/gcc/testsuite/rust/compile/repr_transparent_fields.rs new file mode 100644 index 000000000..190021386 --- /dev/null +++ b/gcc/testsuite/rust/compile/repr_transparent_fields.rs @@ -0,0 +1,16 @@ +#![feature(no_core)] +#![no_core] + +#[repr(transparent)] +struct Foo { // { dg-error "transparent struct needs at most one field with non-trivial size or alignment, but has 2" } + foo: i32, + bar: i32 +} + +#[repr(transparent)] +struct Bar {} + +#[repr(transparent)] +struct Baz { + foo: i32 +} From patchwork Wed Jul 8 09:16:08 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: gerris.rs@gmail.com X-Patchwork-Id: 138717 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 108374BA2E10 for ; Wed, 8 Jul 2026 09:19:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 108374BA2E10 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=Htjm+h/8 X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by sourceware.org (Postfix) with ESMTPS id A0F694BA2E09 for ; Wed, 8 Jul 2026 09:16:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A0F694BA2E09 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A0F694BA2E09 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783502173; cv=none; b=XU1uEPFJrmrDGmTxIGBIsjGx2WckvSCJBanbRcSCKCA7MJDJdmL566ZvlJcusGVxSKZgdRjIy/7JGBD0wELnNreAoJ0OWgqhPh+8zCGnV8EOm1EZkT/Mxe4BiUcrwffSM36ia1P9zDsPj3hm2ByoO8iD7gApPS/WsgSPwWyi2W8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783502173; c=relaxed/simple; bh=4Igu1bww8b6IlBpv4gGSNmzRIhVGrXteHPwRSlzYpj0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=JZtjEJLKEBW0Q0OlE63DhncBfnbdZmJkBGuPK/9qkyPX5jyPlRCrhtxnBZVQbwRfTra81gk63nJAA8BZaR+3Ay/uXPkfJ4c2rQdIZif9nSyhyHQYBvXNPbdL40RMeEUx9G9UDcDy6KvdDzH/JvDr0oNVEjv+8yaCsEKRlTuRZ9E= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=Htjm+h/8 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A0F694BA2E09 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-2caea3f742bso8159885ad.0 for ; Wed, 08 Jul 2026 02:16:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783502172; x=1784106972; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=N5iTOT+0MNmoTutYNtfJ75Ux4EZayahvbHJ8F6WLDm8=; b=Htjm+h/8eQi4tL62TP4w5+rGO0V52UUyocy3IifMkzDR7ExaBIA1v3yb9dHTtgaoVH AsQCJEjUPE8Irg3k2CYzRzx67XP9Da/qALpkcjSU8OgMenppMtyRHcweeis2rb9PF7T9 pAPtFXicCvyClKzb8q33LKq4+ulG+J2So1jolDqwv/kFLQR6Xib59Us4O4vlj7E+NRhI XXMaGpQ/HaH9jn4b+e0TXGdioI45GDWUi8sdvfrzqfLOOu3u7+r9Hz2GrTHLPtC5at5G iLxQCirEjqC7B7qJ76d2QKVcxC/3q+e6Vffk1xvi/oq9lpXEkpnRaatGin0z/bmeWejZ r6EA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783502172; x=1784106972; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=N5iTOT+0MNmoTutYNtfJ75Ux4EZayahvbHJ8F6WLDm8=; b=FER5CLrZo3U58j/mlSbUP0y8+q+ayfFSNwsXMz694qcdHRrctIEPr7IFA1hOR5T1Ak +NJWQ/lXBw0wfx9cqS3egYTZt5DK7A8VQvthYPBMGWoEo5nOOtSGr9H85geoB2yNfdJa eyw7wnxmIaYAvOgNCPbkSXGaxYTl7jYOXCY2pBPyBNPONkJwGWLzqYT+amTmbqiFQj6L ex2FGAO2CwGmZk95vkz6cYRlBw+Pu3qor1lQQSj/A7jiR/gWZ0IeUUTKzCwojPek1IDX UPae2neNeu/aLofM0JoQswu8RMiprXJ/jGfiF0sZIKICvugbJU7U1AekA6DYluBoViC7 sXnw== X-Gm-Message-State: AOJu0Yz2M39tyIOu/VunbXDHW8kCFNNmk6IYm5akY65DP7XChBU7vdBd igtzlgu1ZaE+zhytQIUq6upzHsxo3q2KsuEb4Nq1uW8LxmKqxlTTqDQQN45z/Q== X-Gm-Gg: AfdE7cksV1G27nyNgsIF/DkoojJWLFBwoLtc2eVsMaRSWWG0Jw2bvSq0tXTL4F6NHJ4 2qFCkk7InuhPrF0Co8m/HF3dLF+DMIeq1Oc3efPpg6bwBEwNzAzLH6hwKI6v3nQmeVXegbFolcW ckOToD6ABDbxtMbzvqS52XJH79C6bdOBNMbORVoTKbqM8a3iSXw7hTpTfsvmULy9R6OQ007wmTn bWYQCinSGOFvknOHYGN66g7yMi1fsPVPLLhBpls97pfCDNoI4xyD71c9ulYE1UG2nNQd7L/IXes 6cg/sz4swrXvQ5Du/IRpjxST2yF6JXYSf3olalQw4UDn+bSxSYEbTNSAxvRG/JuRCzjno6heIh/ Wv90jd7JkIAliF7SbEzl0FB8PdChdGEueMV9yqpJxrqxy/Oz0lXKpRfHHXfCtZii4Am2D0X54+x 5NeRCMRJD45xYEvwJr+9EfDlkLPV+kFXaGqkE6G6P2qWEFMd6xZUGVKvg1bGO/+xQAEB6lAX9qR bl73teY/uNR5i/vAK8qTA== X-Received: by 2002:a05:6a20:a124:b0:3b4:6a23:6ba with SMTP id adf61e73a8af0-3c0bccafb92mr2131657637.26.1783502172349; Wed, 08 Jul 2026 02:16:12 -0700 (PDT) Received: from runnervm5mmn9.nkr5xsuno4uefblpyhv20ya3ue.dx.internal.cloudapp.net ([52.250.243.39]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-31174a583bcsm20466552eec.19.2026.07.08.02.16.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jul 2026 02:16:11 -0700 (PDT) From: gerris.rs@gmail.com To: gcc-patches@gcc.gnu.org, gcc-rust@gcc.gnu.org Cc: pierre-emmanuel.patry@embecosm.com, arthur.cohen@embecosm.com, dkm+gccrs@kataplop.net, Yap Zhi Heng Subject: [gccrs COMMIT 2/3] gccrs: Implement compilation of #[repr(transparent)] structs Date: Wed, 8 Jul 2026 09:16:08 +0000 Message-ID: <20260708091609.3108-2-gerris.rs@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260708091609.3108-1-gerris.rs@gmail.com> References: <20260708091609.3108-1-gerris.rs@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, PROLO_LEO1, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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 From: Yap Zhi Heng gcc/rust/ChangeLog: * backend/rust-compile-expr.h (CompileExpr::compile_transparent_field_access): New helper function. * backend/rust-compile-expr.cc (CompileExpr::compile_transparent_field_access): Implement helper function for accessing the field of #[repr(transparent)] ADTs. (CompileExpr::visit (HIR::StructExprStructFields)): Support struct field construction properly for #[repr(transparent)] structs. (CompileExpr::visit (HIR::FieldAccessExpr)): Implement proper compilation of field access for #[repr(transparent)] ADTs. * backend/rust-compile-type.cc (TyTyResolveCompile::visit (TyTy::ADTType)): Implement proper compilation of typing for #[repr(transparent)] ADTs. gcc/testsuite/ChangeLog: * rust/execute/torture/c_string.rs: Fix missing #[repr(transparent)] * rust/execute/torture/c_string_ensure_null_term.rs: Ditto. * rust/compile/c_string_null_byte_check.rs: Ditto. Signed-Off-By: Yap Zhi Heng --- This change was merged into the gccrs repository and is posted here for upstream visibility and potential drive-by review, as requested by GCC release managers. Each commit email contains a link to its details on github from where you can find the Pull-Request and associated discussions. Commit on github: https://github.com/Rust-GCC/gccrs/commit/e85dfa8e6cc46043f027cbf4cec1734b730e966d The commit has NOT been mentioned in any issue. The commit has been mentioned in the following pull-request(s): - https://github.com/Rust-GCC/gccrs/pull/4650 gcc/rust/backend/rust-compile-expr.cc | 53 +++++++++++++----- gcc/rust/backend/rust-compile-expr.h | 3 ++ gcc/rust/backend/rust-compile-type.cc | 54 ++++++++++++++----- .../rust/compile/c_string_null_byte_check.rs | 1 + .../rust/compile/repr_transparent_fields2.rs | 21 ++++++++ .../rust/compile/transparent_struct_deref.rs | 43 +++++++++++++++ .../rust/execute/torture/c_string.rs | 1 + .../torture/c_string_ensure_null_term.rs | 1 + 8 files changed, 152 insertions(+), 25 deletions(-) create mode 100644 gcc/testsuite/rust/compile/repr_transparent_fields2.rs create mode 100644 gcc/testsuite/rust/compile/transparent_struct_deref.rs diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 55ff3263a..4bcd11da4 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -769,10 +769,22 @@ CompileExpr::visit (HIR::StructExprStructFields &struct_expr) if (!adt->is_enum ()) { - translated - = Backend::constructor_expression (compiled_adt_type, adt->is_enum (), - arguments, union_disriminator, - struct_expr.get_locus ()); + auto repr_kind = adt->get_repr_options ().repr_kind; + if (repr_kind == TyTy::ADTType::ReprKind::TRANSPARENT) + { + translated + = fold_build1_loc (struct_expr.get_locus (), VIEW_CONVERT_EXPR, + compiled_adt_type, arguments.front ()); + } + else + { + translated + = Backend::constructor_expression (compiled_adt_type, + adt->is_enum (), arguments, + union_disriminator, + struct_expr.get_locus ()); + } + return; } @@ -843,6 +855,15 @@ CompileExpr::visit (HIR::FieldAccessExpr &expr) bool ok = variant->lookup_field (expr.get_field_name ().as_string (), nullptr, &field_index); rust_assert (ok); + + auto repr_kind = adt->get_repr_options ().repr_kind; + if (repr_kind == TyTy::ADTType::ReprKind::TRANSPARENT) + { + translated + = compile_transparent_field_access (variant, expr.get_locus (), + receiver_ref); + return; + } } else if (receiver->get_kind () == TyTy::TypeKind::REF) { @@ -859,16 +880,12 @@ CompileExpr::visit (HIR::FieldAccessExpr &expr) nullptr, &field_index); rust_assert (ok); - // TODO this check is only used for CStr, test again when we support - // compilation of #[repr(transparent)] structs - if (RS_DST_FLAG_P (TREE_TYPE (receiver_ref))) + auto repr_kind = adt->get_repr_options ().repr_kind; + if (repr_kind == TyTy::ADTType::ReprKind::TRANSPARENT) { - const TyTy::StructFieldType *field - = variant->get_field_at_index (field_index); - tree field_type - = TyTyResolveCompile::compile (ctx, field->get_field_type ()); - translated = fold_build1_loc (expr.get_locus (), VIEW_CONVERT_EXPR, - field_type, receiver_ref); + translated + = compile_transparent_field_access (variant, expr.get_locus (), + receiver_ref); return; } else @@ -2114,6 +2131,16 @@ CompileExpr::compile_c_string_literal (const HIR::LiteralExpr &expr, expr.get_locus ()); } +tree +CompileExpr::compile_transparent_field_access (TyTy::VariantDef *variant, + location_t locus, + tree source_expr) +{ + const TyTy::StructFieldType *field = variant->get_field_at_index (0); + tree field_type = TyTyResolveCompile::compile (ctx, field->get_field_type ()); + return fold_build1_loc (locus, VIEW_CONVERT_EXPR, field_type, source_expr); +} + tree CompileExpr::type_cast_expression (tree type_to_cast_to, tree expr_tree, location_t location) diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 90e1985c9..ad5c48322 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -145,6 +145,9 @@ protected: const TyTy::ArrayType &array_tyty, tree array_type, HIR::ArrayElemsCopied &elems); + tree compile_transparent_field_access (TyTy::VariantDef *variant, + location_t locus, tree source_expr); + protected: tree generate_closure_function (HIR::ClosureExpr &expr, TyTy::ClosureType &closure_tyty, diff --git a/gcc/rust/backend/rust-compile-type.cc b/gcc/rust/backend/rust-compile-type.cc index e71d404f1..01f80da52 100644 --- a/gcc/rust/backend/rust-compile-type.cc +++ b/gcc/rust/backend/rust-compile-type.cc @@ -295,7 +295,35 @@ void TyTyResolveCompile::visit (const TyTy::ADTType &type) { tree type_record = error_mark_node; - if (!type.is_enum ()) + + TyTy::ADTType::ReprOptions repr = type.get_repr_options (); + if (repr.repr_kind == TyTy::ADTType::ReprKind::TRANSPARENT) + { + rust_assert (type.number_of_variants () == 1); + TyTy::VariantDef &variant = *type.get_variants ().at (0); + + rust_assert (variant.num_fields () <= 1); + if (variant.num_fields () == 0) + { + // 0-field transparent repr + // Rustonomicon states that transparent structs should have a single + // non-zero-sized field, but rustc compiles one with 0 fields happily + // without errors, so not sure what's the correct treatment. + // + // For now, treat it as a unit struct + type_record = Backend::struct_type ({}); + } + else + { + // single field transparent repr + const TyTy::StructFieldType *field = variant.get_field_at_index (0); + type_record + = TyTyResolveCompile::compile (ctx, field->get_field_type ()); + } + } + + // compilation of non-transparent ADTs below + else if (!type.is_enum ()) { rust_assert (type.number_of_variants () == 1); @@ -442,22 +470,24 @@ TyTyResolveCompile::visit (const TyTy::ADTType &type) // TODO: "packed" should only narrow type alignment and "align" should only // widen it. Do we need to check and enforce this here, or is it taken care of // later on in the gcc middle-end? - TyTy::ADTType::ReprOptions repr = type.get_repr_options (); - if (repr.pack) + if (repr.repr_kind != TyTy::ADTType::ReprKind::TRANSPARENT) { - TYPE_PACKED (type_record) = 1; - if (repr.pack > 1) + if (repr.pack) + { + TYPE_PACKED (type_record) = 1; + if (repr.pack > 1) + { + SET_TYPE_ALIGN (type_record, repr.pack * 8); + TYPE_USER_ALIGN (type_record) = 1; + } + } + else if (repr.align) { - SET_TYPE_ALIGN (type_record, repr.pack * 8); + SET_TYPE_ALIGN (type_record, repr.align * 8); TYPE_USER_ALIGN (type_record) = 1; } + layout_type (type_record); } - else if (repr.align) - { - SET_TYPE_ALIGN (type_record, repr.align * 8); - TYPE_USER_ALIGN (type_record) = 1; - } - layout_type (type_record); std::string named_struct_str = type.get_ident ().path.get () + type.subst_as_string (); diff --git a/gcc/testsuite/rust/compile/c_string_null_byte_check.rs b/gcc/testsuite/rust/compile/c_string_null_byte_check.rs index 040ba9ad4..89a4bcdca 100644 --- a/gcc/testsuite/rust/compile/c_string_null_byte_check.rs +++ b/gcc/testsuite/rust/compile/c_string_null_byte_check.rs @@ -5,6 +5,7 @@ type c_char = u8; #[lang = "CStr"] +#[repr(transparent)] pub struct CStr { inner: [c_char] } diff --git a/gcc/testsuite/rust/compile/repr_transparent_fields2.rs b/gcc/testsuite/rust/compile/repr_transparent_fields2.rs new file mode 100644 index 000000000..611cd851c --- /dev/null +++ b/gcc/testsuite/rust/compile/repr_transparent_fields2.rs @@ -0,0 +1,21 @@ +// { dg-additional-options "-fdump-tree-gimple" } +#![feature(no_core)] +#![no_core] + +struct NonTransparent { + foo: i32 +} + +#[repr(transparent)] +struct Transparent { + foo: i32 +} + +fn main () -> i32 { + // { dg-final { scan-tree-dump-times {(?n)my_obj . 42;$} 1 gimple } } + let mut my_obj = Transparent { foo: 42 }; + // { dg-final { scan-tree-dump-times {(?n)my_obj2.foo . 40;$} 1 gimple } } + let my_obj2 = NonTransparent { foo: 40 }; + my_obj.foo -= 2; + my_obj.foo - my_obj2.foo +} \ No newline at end of file diff --git a/gcc/testsuite/rust/compile/transparent_struct_deref.rs b/gcc/testsuite/rust/compile/transparent_struct_deref.rs new file mode 100644 index 000000000..97ca93017 --- /dev/null +++ b/gcc/testsuite/rust/compile/transparent_struct_deref.rs @@ -0,0 +1,43 @@ +#![feature(no_core, intrinsics, staged_api, lang_items)] +#![no_core] + +#[lang = "sized"] +pub trait Sized {} + +// below's helper code copied from issue-1232.rs +extern "rust-intrinsic" { + #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")] + fn offset(dst: *const T, offset: isize) -> *const T; +} + +#[lang = "const_ptr"] +impl *const T { + pub const unsafe fn offset(self, count: isize) -> *const T { + unsafe { offset(self, count) } + } + + pub const unsafe fn add(self, count: usize) -> Self { + unsafe { self.offset(count as isize) } + } + + pub const fn as_ptr(self) -> *const T { + self as *const T + } +} + +#[repr(transparent)] +pub struct Foo { + inner: i32 +} + +impl Foo { + pub const fn to_ptr(&self) -> *const i32 { + &self.inner as *const i32 + } +} + +pub fn main() -> i32 { + let a = Foo { inner: 67 }; + let val = unsafe { a.to_ptr() }; + unsafe { *val - 67 } +} diff --git a/gcc/testsuite/rust/execute/torture/c_string.rs b/gcc/testsuite/rust/execute/torture/c_string.rs index 5aef4db0c..9f4cd5c03 100644 --- a/gcc/testsuite/rust/execute/torture/c_string.rs +++ b/gcc/testsuite/rust/execute/torture/c_string.rs @@ -10,6 +10,7 @@ extern "C" { type c_char = u8; #[lang = "CStr"] +#[repr(transparent)] pub struct CStr { inner: [c_char] } diff --git a/gcc/testsuite/rust/execute/torture/c_string_ensure_null_term.rs b/gcc/testsuite/rust/execute/torture/c_string_ensure_null_term.rs index 26e0fed21..60da8a1dd 100644 --- a/gcc/testsuite/rust/execute/torture/c_string_ensure_null_term.rs +++ b/gcc/testsuite/rust/execute/torture/c_string_ensure_null_term.rs @@ -33,6 +33,7 @@ extern "C" { type c_char = u8; #[lang = "CStr"] +#[repr(transparent)] pub struct CStr { inner: [c_char] } From patchwork Wed Jul 8 09:16:09 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: gerris.rs@gmail.com X-Patchwork-Id: 138715 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4699C4BA2E20 for ; Wed, 8 Jul 2026 09:16:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4699C4BA2E20 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=O+wUqA5I X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by sourceware.org (Postfix) with ESMTPS id 836784BA2E0B for ; Wed, 8 Jul 2026 09:16:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 836784BA2E0B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 836784BA2E0B Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783502174; cv=none; b=GG9/JW0Wh8WxczAe6qhyqT1R2Ev4GzcGxLJyxlmOv8oGHFAdXHfpLBYLcY6Mz0NPAAZQXC61107YSr4EXAgf1zB4iyvnjJmuIsQpKbfo0OS2Rg+sa35qy+Z56EThkoKJ26FS2twlsck97WCMTwF1Y/tvGs6ExCfV3AiATFXJpTo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783502174; c=relaxed/simple; bh=YQa6XTbriJPWyOgQieLUjZMB6df5SbY9EcRGjqZPX7s=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ueRbsuFEgiS4mbablpzluuknoLnI1R8fq1ZCt0Xdvf1+u7hf7nnqusxKAnY41efcBexm3GyKhEDOC7ahAeg2JrZ3KWyZP31KD0RRPSA6onrAi0JVvieJ/hKbpKJCCC2YC5l54Uin8e1Ge7nTbleCmCcwGxgeZ/TvesDEw9z4WrI= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=O+wUqA5I DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 836784BA2E0B Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-381891a9525so584479a91.3 for ; Wed, 08 Jul 2026 02:16:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783502173; x=1784106973; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=LYLQdjvBngAfAHuHjqojQxMROe3ISmFBmZ8X98cGR2M=; b=O+wUqA5Ih7KI6Bfz2tPmNjIRY29BkGFgcUEUFs3Y3vzRt/3tYzIg4d4z1h518z/4fb /NNJ5USwgh2jlGWIXlR0URY357V9JS66UqfKJtsH4mPoCPehUAbbsnWDRoTQ626mGcwp okKQUWEWdM5kWOL1De8x7hxJdKNsaNSbmgFdTBclqRIAA71Eb9azNaQV6kfOrVP1Fzxt e+9evwv30IALkUBiJEZttZwxQzx6vpZzUXF6XjZbrMkGSf/UWfKNMXId7zm61SfHUKR0 bgyY3ZXqlPPckv+whCRRKydOUzRzbf88LdTE8db5rT1Mm9R1YaC4WP/SO9xvqbgbMIOi Z9PQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783502173; x=1784106973; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=LYLQdjvBngAfAHuHjqojQxMROe3ISmFBmZ8X98cGR2M=; b=Q/zRTs/6ozjohf+CypqW4tNq7m8RasA9L2RW5PSKfQ6wiAlBK2yG6OVatA7mHqbk8z gXINHSe65UvEK5xfiFW/04FK/OJr6EIxIvpVWzeuNtL+lUU9SaJX2sUNi202SLK1uxtL CmRljbuTQiSjrq2OmVONY8DrU5HQlS+FciCrVmlQBdorIxhGPnNI+Q8PjNHY3ZQ8mDba CsTNTVrrwNJCxoMLy6kgcUOKZ0DL3XpTIfNRxA3QPClyoqq01C+8hRMrIOdw6P5ANFVm 4+1r5FSondbZdle+KeGwfUnwHJYZQqkwlD31qjx6HoeiDp2VkCyEKdpGl9o6swiE1R6/ /S4g== X-Gm-Message-State: AOJu0YwMhlXCc2s0a8Waj/Z/3lA0V42f2JdkzTvi2R2rSD1G0k1bLU8x 3VfJqfTlDyp29nzFWZP/7OweokgdhLZNM42ivtbG4lDMdBdail1bOezFNrvJrQ== X-Gm-Gg: AfdE7cnVZXufnI/NaGsvU7PWHCKjHDKSpZ3X1loQTCWeSqOpqH0903yq8ykA1LZBGjJ wx9tYjnwd8suYeWKwdqEwy6/V9/YDNwhT+1WUM13sGJGq8AixXtax7k2x+U044dLVXGxO0uYiOJ Dq5pcjc8FTLJMUDWG9z0NyuPpgtQAz5slryu0nXp+FH00kuUANWzRl8HlJVgeiMqFrmgfXAOW06 M7moEJU21xmP7uWsPYrSVpGPcjFkhtAlLaSjpKfIhBgVPSXGM2n3R+paB7dDEIghSi7ZO1HmWMl KxuvhTYV2iVne0+9NBbxSSxa0E5G7UV2RQXGo8S6zSbq0zmIdtkltmebGzAxeWy6A0+7f5RnEMY +0y5VJaWI/XuBD0kmZRQ8iy58dDcduTxAKTgbjBlR597pBXcTGqyIYBzrhOpqc29yWbhjvGWBmF Aq9uBLNT9sRWusFBLCNkRCS4K3fq8Pbc82CqgvchW2I1fFcMcemLFZU3Yl8mTnitiMxisZzXOt3 8rkWxYBUog= X-Received: by 2002:a17:90b:2682:b0:37f:9ce1:7364 with SMTP id 98e67ed59e1d1-38941201e0cmr1820274a91.26.1783502173406; Wed, 08 Jul 2026 02:16:13 -0700 (PDT) Received: from runnervm5mmn9.nkr5xsuno4uefblpyhv20ya3ue.dx.internal.cloudapp.net ([52.250.243.39]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-31174a583bcsm20466552eec.19.2026.07.08.02.16.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jul 2026 02:16:12 -0700 (PDT) From: gerris.rs@gmail.com To: gcc-patches@gcc.gnu.org, gcc-rust@gcc.gnu.org Cc: pierre-emmanuel.patry@embecosm.com, arthur.cohen@embecosm.com, dkm+gccrs@kataplop.net, Yap Zhi Heng Subject: [gccrs COMMIT 3/3] gccrs: Add new checks for `#[repr(align)]` attribute Date: Wed, 8 Jul 2026 09:16:09 +0000 Message-ID: <20260708091609.3108-3-gerris.rs@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260708091609.3108-1-gerris.rs@gmail.com> References: <20260708091609.3108-1-gerris.rs@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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 From: Yap Zhi Heng gcc/rust/ChangeLog: * typecheck/rust-hir-type-check-base.cc (TypeCheckBase::parse_repr_options): New check for `#[repr(align)]` to enforce having a parameter that is a power of 2. gcc/testsuite/ChangeLog: * rust/compile/invalid_repr_hint.rs: Update existing and add new `align` test cases. Signed-Off-By: Yap Zhi Heng --- This change was merged into the gccrs repository and is posted here for upstream visibility and potential drive-by review, as requested by GCC release managers. Each commit email contains a link to its details on github from where you can find the Pull-Request and associated discussions. Commit on github: https://github.com/Rust-GCC/gccrs/commit/864defecb6c0faf055005b48a24914651ef5c727 The commit has NOT been mentioned in any issue. The commit has been mentioned in the following pull-request(s): - https://github.com/Rust-GCC/gccrs/pull/4650 gcc/rust/typecheck/rust-hir-type-check-base.cc | 13 +++++++++++++ gcc/testsuite/rust/compile/invalid_repr_hint.rs | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc index 7ff37c924..f966f002a 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-base.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc @@ -522,6 +522,15 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus) if (oparen == std::string::npos) { + if (inline_option.compare ("align") == 0) + { + rust_error_at (attr.get_locus (), ErrorCode::E0589, + "invalid % attribute: % " + "needs an argument"); + delete meta_items; + break; + } + is_pack = inline_option.compare ("packed") == 0; is_c = inline_option.compare ("C") == 0; is_integer = (inline_option.compare ("isize") == 0 @@ -571,6 +580,10 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus) } else if (is_align) { + if (value == 0 || (value & (value - 1)) != 0) + rust_error_at ( + attr.get_locus (), ErrorCode::E0589, + "invalid % attribute: not a power of two"); repr.repr_kind = TyTy::ADTType::ReprKind::ALIGN; repr.align = value; } diff --git a/gcc/testsuite/rust/compile/invalid_repr_hint.rs b/gcc/testsuite/rust/compile/invalid_repr_hint.rs index 7ac48a921..5e1af4d1c 100644 --- a/gcc/testsuite/rust/compile/invalid_repr_hint.rs +++ b/gcc/testsuite/rust/compile/invalid_repr_hint.rs @@ -6,7 +6,10 @@ struct Foo { x: i32, } -#[repr(align)] // { dg-error "unrecognized representation hint" } +#[repr(align)] // { dg-error "invalid .repr.align.. attribute: .align. needs an argument" } struct Bar { x: i32, } + +#[repr(align(3))] // { dg-error "invalid .repr.align.. attribute: not a power of two" } +struct Baz {}