Skip to content

Commit

Permalink
Better hygiene for core crate naming (#327, #180)
Browse files Browse the repository at this point in the history
## Synopsis

See #180 (comment)
for details.

At the moment, compilation fails in a local crate `core` is present in
workspace or some another crate is renamed as `core`.




## Solution

Use `::derive_more::core` instead of `::core` in macro expansions.




Co-authored-by: Jelte Fennema-Nio <github-tech@jeltef.nl>
  • Loading branch information
tyranron and JelteF authored Dec 26, 2023
1 parent 44edc0c commit 81ede4a
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 81 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
([#284](https://github.com/JelteF/derive_more/pull/284))
- Fix documentation of generated bounds in `Display` derive.
([#297](https://github.com/JelteF/derive_more/pull/297))
- Hygiene of macro expansions in presence of custom `core` crate.
([#327](https://github.com/JelteF/derive_more/pull/327))

## 0.99.10 - 2020-09-11

Expand Down
2 changes: 1 addition & 1 deletion impl/src/add_assign_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream {

quote! {
#[automatically_derived]
impl #impl_generics ::core::ops::#trait_ident for #input_type #ty_generics #where_clause {
impl #impl_generics ::derive_more::#trait_ident for #input_type #ty_generics #where_clause {
#[inline]
fn #method_ident(&mut self, rhs: #input_type #ty_generics) {
#( #exprs; )*
Expand Down
12 changes: 7 additions & 5 deletions impl/src/add_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream {
},
Data::Enum(ref data_enum) => (
quote! {
::core::result::Result<#input_type #ty_generics, ::derive_more::BinaryError>
::derive_more::core::result::Result<#input_type #ty_generics, ::derive_more::BinaryError>
},
enum_content(input_type, data_enum, &method_ident),
),
Expand Down Expand Up @@ -98,7 +98,7 @@ fn enum_content(
let matcher = quote! {
(#subtype(#(#l_vars),*),
#subtype(#(#r_vars),*)) => {
::core::result::Result::Ok(#subtype(#(#l_vars.#method_iter(#r_vars)),*))
::derive_more::core::result::Result::Ok(#subtype(#(#l_vars.#method_iter(#r_vars)),*))
}
};
matches.push(matcher);
Expand All @@ -117,15 +117,17 @@ fn enum_content(
let matcher = quote! {
(#subtype{#(#field_names: #l_vars),*},
#subtype{#(#field_names: #r_vars),*}) => {
::core::result::Result::Ok(#subtype{#(#field_names: #l_vars.#method_iter(#r_vars)),*})
::derive_more::core::result::Result::Ok(#subtype{
#(#field_names: #l_vars.#method_iter(#r_vars)),*
})
}
};
matches.push(matcher);
}
Fields::Unit => {
let operation_name = method_ident.to_string();
matches.push(quote! {
(#subtype, #subtype) => ::core::result::Result::Err(
(#subtype, #subtype) => ::derive_more::core::result::Result::Err(
::derive_more::BinaryError::Unit(
::derive_more::UnitError::new(#operation_name)
)
Expand All @@ -140,7 +142,7 @@ fn enum_content(
// match.
let operation_name = method_ident.to_string();
matches.push(quote! {
_ => ::core::result::Result::Err(::derive_more::BinaryError::Mismatch(
_ => ::derive_more::core::result::Result::Err(::derive_more::BinaryError::Mismatch(
::derive_more::WrongVariantError::new(#operation_name)
))
});
Expand Down
8 changes: 5 additions & 3 deletions impl/src/as/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ impl<'a> ToTokens for Expansion<'a> {
ImplKind::Specialized
};

let trait_ty = quote! { ::core::convert::#trait_ident <#return_ty> };
let trait_ty = quote! {
::derive_more::#trait_ident <#return_ty>
};

let generics = match &impl_kind {
ImplKind::Forwarded => {
Expand All @@ -251,7 +253,7 @@ impl<'a> ToTokens for Expansion<'a> {
if is_blanket {
generics
.params
.push(parse_quote! { #return_ty: ?::core::marker::Sized });
.push(parse_quote! { #return_ty: ?::derive_more::core::marker::Sized });
}
Cow::Owned(generics)
}
Expand All @@ -272,7 +274,7 @@ impl<'a> ToTokens for Expansion<'a> {

let conv =
<::derive_more::__private::Conv<& #mut_ #field_ty, #return_ty>
as ::core::default::Default>::default();
as ::derive_more::core::default::Default>::default();
(&&conv).__extract_ref(#field_ref)
}),
};
Expand Down
12 changes: 8 additions & 4 deletions impl/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn expand(

let provide = provide.map(|provide| {
quote! {
fn provide<'_request>(&'_request self, request: &mut ::core::error::Request<'_request>) {
fn provide<'_request>(&'_request self, request: &mut ::derive_more::core::error::Request<'_request>) {
#provide
}
}
Expand All @@ -61,7 +61,7 @@ pub fn expand(
&generics,
quote! {
where
#ident #ty_generics: ::core::fmt::Debug + ::core::fmt::Display
#ident #ty_generics: ::derive_more::core::fmt::Debug + ::derive_more::core::fmt::Display
},
);
}
Expand All @@ -71,8 +71,12 @@ pub fn expand(
generics = utils::add_extra_where_clauses(
&generics,
quote! {
where
#(#bounds: ::core::fmt::Debug + ::core::fmt::Display + ::derive_more::Error + 'static),*
where #(
#bounds: ::derive_more::core::fmt::Debug
+ ::derive_more::core::fmt::Display
+ ::derive_more::Error
+ 'static
),*
},
);
}
Expand Down
34 changes: 16 additions & 18 deletions impl/src/fmt/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ pub fn expand(input: &syn::DeriveInput, _: &str) -> syn::Result<TokenStream> {

Ok(quote! {
#[automatically_derived]
impl #impl_gens ::core::fmt::Debug for #ident #ty_gens
#where_clause
{
impl #impl_gens ::derive_more::Debug for #ident #ty_gens #where_clause {
fn fmt(
&self, __derive_more_f: &mut ::core::fmt::Formatter<'_>
) -> ::core::fmt::Result {
&self, __derive_more_f: &mut ::derive_more::core::fmt::Formatter<'_>
) -> ::derive_more::core::fmt::Result {
#body
}
}
Expand Down Expand Up @@ -229,17 +227,17 @@ impl<'a> Expansion<'a> {
fn generate_body(&self) -> syn::Result<TokenStream> {
if let Some(fmt) = &self.attr.fmt {
return Ok(if let Some((expr, trait_ident)) = fmt.transparent_call() {
quote! { ::core::fmt::#trait_ident::fmt(&(#expr), __derive_more_f) }
quote! { ::derive_more::core::fmt::#trait_ident::fmt(&(#expr), __derive_more_f) }
} else {
quote! { ::core::write!(__derive_more_f, #fmt) }
quote! { ::derive_more::core::write!(__derive_more_f, #fmt) }
});
};

match self.fields {
syn::Fields::Unit => {
let ident = self.ident.to_string();
Ok(quote! {
::core::fmt::Formatter::write_str(
::derive_more::core::fmt::Formatter::write_str(
__derive_more_f,
#ident,
)
Expand Down Expand Up @@ -268,7 +266,7 @@ impl<'a> Expansion<'a> {
Some(FieldAttribute::Right(fmt_attr)) => Ok(quote! {
::derive_more::__private::DebugTuple::field(
#out,
&::core::format_args!(#fmt_attr),
&::derive_more::core::format_args!(#fmt_attr),
)
}),
None => {
Expand All @@ -291,7 +289,7 @@ impl<'a> Expansion<'a> {
let ident = self.ident.to_string();

let out = quote! {
&mut ::core::fmt::Formatter::debug_struct(
&mut ::derive_more::core::fmt::Formatter::debug_struct(
__derive_more_f,
#ident,
)
Expand All @@ -309,21 +307,21 @@ impl<'a> Expansion<'a> {
Ok::<_, syn::Error>(out)
}
Some(FieldAttribute::Right(fmt_attr)) => Ok(quote! {
::core::fmt::DebugStruct::field(
::derive_more::core::fmt::DebugStruct::field(
#out,
#field_str,
&::core::format_args!(#fmt_attr),
&::derive_more::core::format_args!(#fmt_attr),
)
}),
None => Ok(quote! {
::core::fmt::DebugStruct::field(#out, #field_str, #field_ident)
::derive_more::core::fmt::DebugStruct::field(#out, #field_str, #field_ident)
}),
}
})?;
Ok(if exhaustive {
quote! { ::core::fmt::DebugStruct::finish(#out) }
quote! { ::derive_more::core::fmt::DebugStruct::finish(#out) }
} else {
quote! { ::core::fmt::DebugStruct::finish_non_exhaustive(#out) }
quote! { ::derive_more::core::fmt::DebugStruct::finish_non_exhaustive(#out) }
})
}
}
Expand All @@ -337,7 +335,7 @@ impl<'a> Expansion<'a> {
out.extend(fmt.bounded_types(self.fields).map(|(ty, trait_name)| {
let trait_ident = format_ident!("{trait_name}");

parse_quote! { #ty: ::core::fmt::#trait_ident }
parse_quote! { #ty: ::derive_more::core::fmt::#trait_ident }
}));
Ok(out)
} else {
Expand All @@ -351,12 +349,12 @@ impl<'a> Expansion<'a> {
|(ty, trait_name)| {
let trait_ident = format_ident!("{trait_name}");

parse_quote! { #ty: ::core::fmt::#trait_ident }
parse_quote! { #ty: ::derive_more::core::fmt::#trait_ident }
},
));
}
Some(FieldAttribute::Left(_skip)) => {}
None => out.extend([parse_quote! { #ty: ::core::fmt::Debug }]),
None => out.extend([parse_quote! { #ty: ::derive_more::Debug }]),
}
Ok(out)
})
Expand Down
22 changes: 10 additions & 12 deletions impl/src/fmt/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ pub fn expand(input: &syn::DeriveInput, trait_name: &str) -> syn::Result<TokenSt

Ok(quote! {
#[automatically_derived]
impl #impl_gens ::core::fmt::#trait_ident for #ident #ty_gens
#where_clause
{
impl #impl_gens ::derive_more::#trait_ident for #ident #ty_gens #where_clause {
fn fmt(
&self, __derive_more_f: &mut ::core::fmt::Formatter<'_>
) -> ::core::fmt::Result {
&self, __derive_more_f: &mut ::derive_more::core::fmt::Formatter<'_>
) -> ::derive_more::core::fmt::Result {
#body
}
}
Expand Down Expand Up @@ -190,7 +188,7 @@ fn expand_union(

Ok((
attrs.bounds.0.clone().into_iter().collect(),
quote! { ::core::write!(__derive_more_f, #fmt) },
quote! { ::derive_more::core::write!(__derive_more_f, #fmt) },
))
}

Expand Down Expand Up @@ -231,16 +229,16 @@ impl<'a> Expansion<'a> {
match &self.attrs.fmt {
Some(fmt) => {
Ok(if let Some((expr, trait_ident)) = fmt.transparent_call() {
quote! { ::core::fmt::#trait_ident::fmt(&(#expr), __derive_more_f) }
quote! { ::derive_more::core::fmt::#trait_ident::fmt(&(#expr), __derive_more_f) }
} else {
quote! { ::core::write!(__derive_more_f, #fmt) }
quote! { ::derive_more::core::write!(__derive_more_f, #fmt) }
})
}
None if self.fields.is_empty() => {
let ident_str = self.ident.to_string();

Ok(quote! {
::core::write!(__derive_more_f, #ident_str)
::derive_more::core::write!(__derive_more_f, #ident_str)
})
}
None if self.fields.len() == 1 => {
Expand All @@ -253,7 +251,7 @@ impl<'a> Expansion<'a> {
let trait_ident = self.trait_ident;

Ok(quote! {
::core::fmt::#trait_ident::fmt(#ident, __derive_more_f)
::derive_more::core::fmt::#trait_ident::fmt(#ident, __derive_more_f)
})
}
_ => Err(syn::Error::new(
Expand All @@ -277,7 +275,7 @@ impl<'a> Expansion<'a> {
.map(|f| {
let ty = &f.ty;
let trait_ident = &self.trait_ident;
vec![parse_quote! { #ty: ::core::fmt::#trait_ident }]
vec![parse_quote! { #ty: ::derive_more::core::fmt::#trait_ident }]
})
.unwrap_or_default();
};
Expand All @@ -286,7 +284,7 @@ impl<'a> Expansion<'a> {
.map(|(ty, trait_name)| {
let trait_ident = format_ident!("{trait_name}");

parse_quote! { #ty: ::core::fmt::#trait_ident }
parse_quote! { #ty: ::derive_more::core::fmt::#trait_ident }
})
.chain(self.attrs.bounds.0.clone())
.collect()
Expand Down
18 changes: 6 additions & 12 deletions impl/src/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,15 @@ impl<'a> Expansion<'a> {
let index = index.into_iter();
let from_ty = from_tys.next().unwrap_or_else(|| unreachable!());
quote! {
#( #ident: )* <#ty as ::core::convert::From<#from_ty>>::from(
#( #ident: )* <#ty as ::derive_more::From<#from_ty>>::from(
value #( .#index )*
),
}
});

Ok(quote! {
#[automatically_derived]
impl #impl_gens ::core::convert::From<#ty>
for #ident #ty_gens #where_clause
{
impl #impl_gens ::derive_more::From<#ty> for #ident #ty_gens #where_clause {
#[inline]
fn from(value: #ty) -> Self {
#ident #( :: #variant )* #init
Expand All @@ -195,9 +193,7 @@ impl<'a> Expansion<'a> {

Ok(quote! {
#[automatically_derived]
impl #impl_gens ::core::convert::From<(#( #field_tys ),*)>
for #ident #ty_gens #where_clause
{
impl #impl_gens ::derive_more::From<(#( #field_tys ),*)> for #ident #ty_gens #where_clause {
#[inline]
fn from(value: (#( #field_tys ),*)) -> Self {
#ident #( :: #variant )* #init
Expand All @@ -213,7 +209,7 @@ impl<'a> Expansion<'a> {
let index = index.into_iter();
let gen_ident = format_ident!("__FromT{i}");
let out = quote! {
#( #ident: )* <#ty as ::core::convert::From<#gen_ident>>::from(
#( #ident: )* <#ty as ::derive_more::From<#gen_ident>>::from(
value #( .#index )*
),
};
Expand All @@ -227,7 +223,7 @@ impl<'a> Expansion<'a> {
let mut generics = self.generics.clone();
for (ty, ident) in field_tys.iter().zip(&gen_idents) {
generics.make_where_clause().predicates.push(
parse_quote! { #ty: ::core::convert::From<#ident> },
parse_quote! { #ty: ::derive_more::From<#ident> },
);
generics
.params
Expand All @@ -239,9 +235,7 @@ impl<'a> Expansion<'a> {

Ok(quote! {
#[automatically_derived]
impl #impl_gens ::core::convert::From<(#( #gen_idents ),*)>
for #ident #ty_gens #where_clause
{
impl #impl_gens ::derive_more::From<(#( #gen_idents ),*)> for #ident #ty_gens #where_clause {
#[inline]
fn from(value: (#( #gen_idents ),*)) -> Self {
#ident #(:: #variant)* #init
Expand Down
4 changes: 2 additions & 2 deletions impl/src/from_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn struct_from(state: &State, trait_name: &'static str) -> TokenStream {
type Err = <#field_type as #trait_path>::Err;

#[inline]
fn from_str(src: &str) -> ::core::result::Result<Self, Self::Err> {
fn from_str(src: &str) -> ::derive_more::core::result::Result<Self, Self::Err> {
Ok(#body)
}
}
Expand Down Expand Up @@ -97,7 +97,7 @@ fn enum_from(
type Err = ::derive_more::FromStrError;

#[inline]
fn from_str(src: &str) -> ::core::result::Result<Self, Self::Err> {
fn from_str(src: &str) -> ::derive_more::core::result::Result<Self, Self::Err> {
Ok(match src.to_lowercase().as_str() {
#(#cases)*
_ => return Err(::derive_more::FromStrError::new(#input_type_name)),
Expand Down
4 changes: 2 additions & 2 deletions impl/src/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ impl<'a> Expansion<'a> {

Ok(quote! {
#[automatically_derived]
impl #impl_gens ::core::convert::From<#r #lf #m #input_ident #ty_gens>
impl #impl_gens ::derive_more::core::convert::From<#r #lf #m #input_ident #ty_gens>
for ( #( #r #lf #m #tys ),* ) #where_clause
{
#[inline]
fn from(value: #r #lf #m #input_ident #ty_gens) -> Self {
(#(
<#r #m #tys as ::core::convert::From<_>>::from(
<#r #m #tys as ::derive_more::core::convert::From<_>>::from(
#r #m value. #fields_idents
)
),*)
Expand Down
Loading

0 comments on commit 81ede4a

Please sign in to comment.