@@ -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
@@ -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 {}
@@ -521,4 +521,5 @@ ERROR(0790),
ERROR(0791),
ERROR(0792),
ERROR(0793),
-ERROR(0794)
\ No newline at end of file
+ERROR(0794),
+ERROR(0802)
@@ -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> {
@@ -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 }
@@ -10,5 +10,6 @@
#[lang = "sized"]
trait Sized {}
+#[repr(transparent)]
#[derive(CoercePointee)] // { dg-warning "no effect" }
pub struct Floop<T>(*const T);
new file mode 100644
@@ -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,
+}