https://gcc.gnu.org/g:a80152a63921d86353f4f92ea964a1d5927954c0
commit r17-6253-ga80152a63921d86353f4f92ea964a1d5927954c0 Author: Wesley Lewis <[email protected]> Date: Fri Oct 2 15:58:40 2026 +0000 gccrs: Validate repr(transparent) for derive CoercePointee Fixes Rust-GCC/gccrs#4914 gcc/rust/ChangeLog: * rust-error-codes.def (ERROR): Add E0802. * expand/rust-derive-coerce-pointee.cc (DeriveCoercePointee::validate_repr_transparent): New (DeriveCoercePointee::go): Call validate_repr_transparent. * expand/rust-derive-coerce-pointee.h (DeriveCoercePointee::validate_repr_transparent): Declare. gcc/testsuite/ChangeLog: * rust/compile/derive-coerce-pointee4.rs: New test. * rust/compile/derive-coerce-pointee1.rs: Added #[repr(transparent)]. * rust/compile/derive-coerce-pointee2.rs: Likewise. * rust/compile/derive-coerce-pointee3.rs: Likewise. Signed-off-by: Wesley Lewis <[email protected]> Diff: --- gcc/rust/expand/rust-derive-coerce-pointee.cc | 31 ++++++++++++++++++++++ gcc/rust/expand/rust-derive-coerce-pointee.h | 1 + gcc/rust/rust-error-codes.def | 3 ++- .../rust/compile/derive-coerce-pointee1.rs | 1 + .../rust/compile/derive-coerce-pointee2.rs | 1 + .../rust/compile/derive-coerce-pointee3.rs | 1 + .../rust/compile/derive-coerce-pointee4.rs | 17 ++++++++++++ 7 files changed, 54 insertions(+), 1 deletion(-) diff --git a/gcc/rust/expand/rust-derive-coerce-pointee.cc b/gcc/rust/expand/rust-derive-coerce-pointee.cc index 51bb7d3e4eae..50386b48f560 100644 --- a/gcc/rust/expand/rust-derive-coerce-pointee.cc +++ b/gcc/rust/expand/rust-derive-coerce-pointee.cc @@ -16,6 +16,7 @@ // along with GCC; see the file COPYING3. If not see // <http://www.gnu.org/licenses/>. +#include "rust-attribute-values.h" #include "rust-derive-coerce-pointee.h" #include "rust-session-manager.h" #include "rust-feature.h" @@ -32,6 +33,7 @@ DeriveCoercePointee::DeriveCoercePointee (location_t loc, std::unique_ptr<AST::Item> DeriveCoercePointee::go (Item &item) { + validate_repr_transparent (item); Features::EarlyFeatureGateStore::get ().add ( Feature::Name::DERIVE_COERCE_POINTEE, Error (loc, "use of unstable library feature %<derive_coerce_pointee%>")); @@ -72,5 +74,34 @@ DeriveCoercePointee::go (Item &item) return {}; } +// CoercePointee requires the Struct or Tuple to have transparent representation +bool +DeriveCoercePointee::validate_repr_transparent (const Item &item) +{ + const auto &attrs = item.get_outer_attrs (); + for (const auto &attr : attrs) + { + if (attr.get_path ().as_string () != Values::Attributes::REPR) + continue; + if (attr.empty_input ()) + continue; + + const auto &attr_input = attr.get_attr_input (); + if (attr_input.get_attr_input_type () + != AST::AttrInput::AttrInputType::TOKEN_TREE) + continue; + + auto meta_item = static_cast<const AST::DelimTokenTree &> (attr_input) + .parse_to_meta_item (); + + for (const auto &item : meta_item->get_items ()) + if (item->as_string () == "transparent") + return true; + } + rust_error_at (item.get_locus (), ErrorCode::E0802, + "%<CoercePointee%> is only applicable to struct/tuple with " + "repr(transparent) layout"); + return false; +} } // namespace AST } // namespace Rust diff --git a/gcc/rust/expand/rust-derive-coerce-pointee.h b/gcc/rust/expand/rust-derive-coerce-pointee.h index b6f5c2b464f2..b18704782f98 100644 --- a/gcc/rust/expand/rust-derive-coerce-pointee.h +++ b/gcc/rust/expand/rust-derive-coerce-pointee.h @@ -31,6 +31,7 @@ public: std::unique_ptr<Item> go (Item &item); + bool validate_repr_transparent (const Item &item); private: virtual void visit_struct (StructStruct &item) override {} virtual void visit_tuple (TupleStruct &item) override {} diff --git a/gcc/rust/rust-error-codes.def b/gcc/rust/rust-error-codes.def index 7de1e3d8d7e3..a5086f1f4815 100644 --- a/gcc/rust/rust-error-codes.def +++ b/gcc/rust/rust-error-codes.def @@ -521,4 +521,5 @@ ERROR(0790), ERROR(0791), ERROR(0792), ERROR(0793), -ERROR(0794) \ No newline at end of file +ERROR(0794), +ERROR(0802) diff --git a/gcc/testsuite/rust/compile/derive-coerce-pointee1.rs b/gcc/testsuite/rust/compile/derive-coerce-pointee1.rs index ebc9cde10be1..16fee0701ea8 100644 --- a/gcc/testsuite/rust/compile/derive-coerce-pointee1.rs +++ b/gcc/testsuite/rust/compile/derive-coerce-pointee1.rs @@ -9,6 +9,7 @@ #[lang = "sized"] trait Sized {} +#[repr(transparent)] #[derive(CoercePointee)] // { dg-warning "no effect" } // { dg-error "use of unstable library feature" "" { target *-*-* } .-1 } struct Floop<T> { diff --git a/gcc/testsuite/rust/compile/derive-coerce-pointee2.rs b/gcc/testsuite/rust/compile/derive-coerce-pointee2.rs index 8c84a7fb3360..fb0867c2a1be 100644 --- a/gcc/testsuite/rust/compile/derive-coerce-pointee2.rs +++ b/gcc/testsuite/rust/compile/derive-coerce-pointee2.rs @@ -8,6 +8,7 @@ #[lang = "sized"] trait Sized {} +#[repr(transparent)] #[derive(CoercePointee)] // { dg-error "derive.CoercePointee. requires a compatibility mode" "" { target *-*-* } .-1 } // { dg-error "could not resolve trait .CoercePointee." "" { target *-*-* } .-2 } diff --git a/gcc/testsuite/rust/compile/derive-coerce-pointee3.rs b/gcc/testsuite/rust/compile/derive-coerce-pointee3.rs index 8ca8a1bae584..1f471d621a24 100644 --- a/gcc/testsuite/rust/compile/derive-coerce-pointee3.rs +++ b/gcc/testsuite/rust/compile/derive-coerce-pointee3.rs @@ -10,5 +10,6 @@ #[lang = "sized"] trait Sized {} +#[repr(transparent)] #[derive(CoercePointee)] // { dg-warning "no effect" } pub struct Floop<T>(*const T); diff --git a/gcc/testsuite/rust/compile/derive-coerce-pointee4.rs b/gcc/testsuite/rust/compile/derive-coerce-pointee4.rs new file mode 100644 index 000000000000..44d75136e5a1 --- /dev/null +++ b/gcc/testsuite/rust/compile/derive-coerce-pointee4.rs @@ -0,0 +1,17 @@ +// { dg-additional-options "-frust-compat-version=1.84" } + +// check for repr transparent for structs with derive CoercePointee + +#![feature(no_core)] +#![feature(lang_items)] +#![feature(derive_coerce_pointee)] +#![no_core] + +#[lang = "sized"] +trait Sized {} + +#[derive(CoercePointee)] // { dg-warning "no effect" } + // { dg-error "only applicable to struct/tuple with repr.transparent. layout" "" { target *-*-* } .+1 } +struct Bad<T: ?Sized> { + a: *const T, +}
