Skip to content

Commit

Permalink
Switch to anon-const for wrapping derived impls
Browse files Browse the repository at this point in the history
Fixes a non_local_definitions warning in the derived code.

    warning: non-local `impl` definition, they should be avoided as they go against expectation
     --> tests/test_derive.rs:5:28
      |
    5 | #[derive(PartialEq, Debug, Serialize, Deserialize)]
      |                            ^^^^^^^^^
      |
      = help: move this `impl` block outside the of the current constant `_IMPL_MINISERIALIZE_FOR_Tag`
      = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
      = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
      = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
      = note: the derive macro `Serialize` may come from an old version of the `mini_internal` crate, try updating your dependency with `cargo update -p mini_internal`
      = note: `#[warn(non_local_definitions)]` on by default
      = note: this warning originates in the derive macro `Serialize` (in Nightly builds, run with -Z macro-backtrace for more info)
  • Loading branch information
dtolnay committed Feb 26, 2024
1 parent 0240792 commit d9faf43
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
14 changes: 3 additions & 11 deletions derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{attr, bound};
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
parse_quote, Data, DataEnum, DataStruct, DeriveInput, Error, Fields, FieldsNamed, Ident, Result,
parse_quote, Data, DataEnum, DataStruct, DeriveInput, Error, Fields, FieldsNamed, Result,
};

pub fn derive(input: DeriveInput) -> Result<TokenStream> {
Expand All @@ -26,10 +26,6 @@ pub fn derive(input: DeriveInput) -> Result<TokenStream> {
pub fn derive_struct(input: &DeriveInput, fields: &FieldsNamed) -> Result<TokenStream> {
let ident = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let dummy = Ident::new(
&format!("_IMPL_MINIDESERIALIZE_FOR_{}", ident),
Span::call_site(),
);

let fieldname = fields.named.iter().map(|f| &f.ident).collect::<Vec<_>>();
let fieldty = fields.named.iter().map(|f| &f.ty);
Expand All @@ -46,7 +42,7 @@ pub fn derive_struct(input: &DeriveInput, fields: &FieldsNamed) -> Result<TokenS

Ok(quote! {
#[allow(non_upper_case_globals)]
const #dummy: () = {
const _: () = {
#[repr(C)]
struct __Visitor #impl_generics #where_clause {
__out: miniserde::__private::Option<#ident #ty_generics>,
Expand Down Expand Up @@ -117,10 +113,6 @@ pub fn derive_enum(input: &DeriveInput, enumeration: &DataEnum) -> Result<TokenS
}

let ident = &input.ident;
let dummy = Ident::new(
&format!("_IMPL_MINIDESERIALIZE_FOR_{}", ident),
Span::call_site(),
);

let var_idents = enumeration
.variants
Expand All @@ -141,7 +133,7 @@ pub fn derive_enum(input: &DeriveInput, enumeration: &DataEnum) -> Result<TokenS

Ok(quote! {
#[allow(non_upper_case_globals)]
const #dummy: () = {
const _: () = {
#[repr(C)]
struct __Visitor {
__out: miniserde::__private::Option<#ident>,
Expand Down
14 changes: 3 additions & 11 deletions derive/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{attr, bound};
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
parse_quote, Data, DataEnum, DataStruct, DeriveInput, Error, Fields, FieldsNamed, Ident, Result,
parse_quote, Data, DataEnum, DataStruct, DeriveInput, Error, Fields, FieldsNamed, Result,
};

pub fn derive(input: DeriveInput) -> Result<TokenStream> {
Expand All @@ -26,10 +26,6 @@ pub fn derive(input: DeriveInput) -> Result<TokenStream> {
fn derive_struct(input: &DeriveInput, fields: &FieldsNamed) -> Result<TokenStream> {
let ident = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let dummy = Ident::new(
&format!("_IMPL_MINISERIALIZE_FOR_{}", ident),
Span::call_site(),
);

let fieldname = &fields.named.iter().map(|f| &f.ident).collect::<Vec<_>>();
let fieldstr = fields
Expand All @@ -46,7 +42,7 @@ fn derive_struct(input: &DeriveInput, fields: &FieldsNamed) -> Result<TokenStrea

Ok(quote! {
#[allow(non_upper_case_globals)]
const #dummy: () = {
const _: () = {
impl #impl_generics miniserde::Serialize for #ident #ty_generics #bounded_where_clause {
fn begin(&self) -> miniserde::ser::Fragment {
miniserde::ser::Fragment::Map(miniserde::__private::Box::new(__Map {
Expand Down Expand Up @@ -89,10 +85,6 @@ fn derive_enum(input: &DeriveInput, enumeration: &DataEnum) -> Result<TokenStrea
}

let ident = &input.ident;
let dummy = Ident::new(
&format!("_IMPL_MINISERIALIZE_FOR_{}", ident),
Span::call_site(),
);

let var_idents = enumeration
.variants
Expand All @@ -113,7 +105,7 @@ fn derive_enum(input: &DeriveInput, enumeration: &DataEnum) -> Result<TokenStrea

Ok(quote! {
#[allow(non_upper_case_globals)]
const #dummy: () = {
const _: () = {
impl miniserde::Serialize for #ident {
fn begin(&self) -> miniserde::ser::Fragment {
match self {
Expand Down

0 comments on commit d9faf43

Please sign in to comment.