From: Wesley Lewis <[email protected]>
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]>
---
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/74386d7d16cc7f8218cacb922c1f3a77627c7ead
The commit has been mentioned in the following issue(s):
- Rust-GCC/gccrs#4914: https://github.com/Rust-GCC/gccrs/issues/4914
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/4950
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(-)
create mode 100644 gcc/testsuite/rust/compile/derive-coerce-pointee4.rs
diff --git a/gcc/rust/expand/rust-derive-coerce-pointee.cc
b/gcc/rust/expand/rust-derive-coerce-pointee.cc
index 51bb7d3e4..50386b48f 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 b6f5c2b46..b18704782 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 7de1e3d8d..a5086f1f4 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 ebc9cde10..16fee0701 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 8c84a7fb3..fb0867c2a 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 8ca8a1bae..1f471d621 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 000000000..44d75136e
--- /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,
+}
base-commit: 5efbf3f97649c43076f7298e52807f505ed2b2e5
--
2.56.0