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 +}