| 1 | use quote::ToTokens; |
| 2 | use syn::{spanned::Spanned, Ident, Meta, Variant}; |
| 3 | |
| 4 | #[inline] |
| 5 | pub(crate) fn unit_struct_need_name(name: &Ident) -> syn::Error { |
| 6 | syn::Error::new(name.span(), "a unit struct needs to have a name") |
| 7 | } |
| 8 | |
| 9 | #[inline] |
| 10 | pub(crate) fn unit_variant_need_name(variant: &Variant) -> syn::Error { |
| 11 | syn::Error::new( |
| 12 | variant.span(), |
| 13 | "a unit variant which doesn't use an enum name needs to have a name", |
| 14 | ) |
| 15 | } |
| 16 | |
| 17 | #[inline] |
| 18 | pub(crate) fn unit_enum_need_name(name: &Ident) -> syn::Error { |
| 19 | syn::Error::new(name.span(), "a unit enum needs to have a name") |
| 20 | } |
| 21 | |
| 22 | #[inline] |
| 23 | pub(crate) fn union_without_unsafe(meta: &Meta) -> syn::Error { |
| 24 | let mut s = meta.into_token_stream().to_string().replace(" , ", ", "); |
| 25 | |
| 26 | match s.len() { |
| 27 | 5 => s.push_str("(unsafe)"), |
| 28 | 7 => s.insert_str(6, "unsafe"), |
| 29 | _ => s.insert_str(6, "unsafe, "), |
| 30 | } |
| 31 | |
| 32 | syn::Error::new( |
| 33 | meta.span(), |
| 34 | format!( |
| 35 | "a union's `Debug` implementation may expose uninitialized memory\n* It is \ |
| 36 | recommended that, for a union where `Debug` is implemented, types that allow \ |
| 37 | uninitialized memory should not be used in it.\n* If you can ensure that the union \ |
| 38 | uses no such types, use `#[educe({s})]` to implement the `Debug` trait for it.\n* \ |
| 39 | The `unsafe` keyword should be placed as the first parameter of the `Debug` \ |
| 40 | attribute." |
| 41 | ), |
| 42 | ) |
| 43 | } |