Email me

Rust errors / Attributes, macros and features

Rust compiler error · checked with rustc 1.99.0

Rust error E0802: `CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`

What it meansThe target of derive(CoercePointee) macro has inadmissible specification for a meaningful use.

What rustc prints

error[E0802]: `CoercePointee` can only be derived on `struct`s with `#[repr(transparent)]`
 --> main.rs:3:10
  |
3 | #[derive(CoercePointee)]
  |          ^^^^^^^^^^^^^

Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.

E0802Attributes, macros and features
Activein rustc 1.99.0
4related errors linked below

The compiler team's explanation

The target of derive(CoercePointee) macro has inadmissible specification for a meaningful use.

Erroneous code examples:

The target data is not a struct.

#![feature(coerce_pointee)]
use std::marker::CoercePointee;
#[derive(CoercePointee)]
enum NotStruct<'a, T: ?Sized> {
    Variant(&'a T),
}

The target data has a layout that is not transparent, or repr(transparent) in other words.

#![feature(coerce_pointee)]
use std::marker::CoercePointee;
#[derive(CoercePointee)]
struct NotTransparent<'a, #[pointee] T: ?Sized> {
    ptr: &'a T,
}

The target data has no data field.

#![feature(coerce_pointee)]
use std::marker::CoercePointee;
#[derive(CoercePointee)]
#[repr(transparent)]
struct NoField<'a, #[pointee] T: ?Sized> {}

The target data is not generic over any data, or has no generic type parameter.

#![feature(coerce_pointee)]
use std::marker::CoercePointee;
#[derive(CoercePointee)]
#[repr(transparent)]
struct NoGeneric<'a>(&'a u8);

The target data has multiple generic type parameters, but none is designated as a pointee for coercion.

#![feature(coerce_pointee)]
use std::marker::CoercePointee;
#[derive(CoercePointee)]
#[repr(transparent)]
struct AmbiguousPointee<'a, T1: ?Sized, T2: ?Sized> {
    a: (&'a T1, &'a T2),
}

The target data has multiple generic type parameters that are designated as pointees for coercion.

#![feature(coerce_pointee)]
use std::marker::CoercePointee;
#[derive(CoercePointee)]
#[repr(transparent)]
struct TooManyPointees<
    'a,
    #[pointee] A: ?Sized,
    #[pointee] B: ?Sized>
((&'a A, &'a B));

The type parameter that is designated as a pointee is not marked ?Sized.

#![feature(coerce_pointee)]
use std::marker::CoercePointee;
#[derive(CoercePointee)]
#[repr(transparent)]
struct NoMaybeSized<'a, #[pointee] T> {
    ptr: &'a T,
}

In summary, the CoercePointee macro demands the type to be a struct that is generic over at least one type or over more types, one of which is marked with #[pointee], and has at least one data field and adopts a repr(transparent) layout. The only generic type or the type marked with #[pointee] has to be also marked as ?Sized.

From rustc --explain E0802 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.

Related errors

All 518 Rust error codes · Rust crate data · Glossary

Up next

Often hit nextE0658: is unstableA related error from the same family.Read it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ