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