Skip to content

Commit 81ede4a

Browse files
tyranronJelteF
andauthored
Better hygiene for core crate naming (#327, #180)
## 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>
1 parent 44edc0c commit 81ede4a

File tree

16 files changed

+92
-81
lines changed

16 files changed

+92
-81
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
9393
([#284](https://github.com/JelteF/derive_more/pull/284))
9494
- Fix documentation of generated bounds in `Display` derive.
9595
([#297](https://github.com/JelteF/derive_more/pull/297))
96+
- Hygiene of macro expansions in presence of custom `core` crate.
97+
([#327](https://github.com/JelteF/derive_more/pull/327))
9698

9799
## 0.99.10 - 2020-09-11
98100

impl/src/add_assign_like.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream {
2929

3030
quote! {
3131
#[automatically_derived]
32-
impl #impl_generics ::core::ops::#trait_ident for #input_type #ty_generics #where_clause {
32+
impl #impl_generics ::derive_more::#trait_ident for #input_type #ty_generics #where_clause {
3333
#[inline]
3434
fn #method_ident(&mut self, rhs: #input_type #ty_generics) {
3535
#( #exprs; )*

impl/src/add_like.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream {
3232
},
3333
Data::Enum(ref data_enum) => (
3434
quote! {
35-
::core::result::Result<#input_type #ty_generics, ::derive_more::BinaryError>
35+
::derive_more::core::result::Result<#input_type #ty_generics, ::derive_more::BinaryError>
3636
},
3737
enum_content(input_type, data_enum, &method_ident),
3838
),
@@ -98,7 +98,7 @@ fn enum_content(
9898
let matcher = quote! {
9999
(#subtype(#(#l_vars),*),
100100
#subtype(#(#r_vars),*)) => {
101-
::core::result::Result::Ok(#subtype(#(#l_vars.#method_iter(#r_vars)),*))
101+
::derive_more::core::result::Result::Ok(#subtype(#(#l_vars.#method_iter(#r_vars)),*))
102102
}
103103
};
104104
matches.push(matcher);
@@ -117,15 +117,17 @@ fn enum_content(
117117
let matcher = quote! {
118118
(#subtype{#(#field_names: #l_vars),*},
119119
#subtype{#(#field_names: #r_vars),*}) => {
120-
::core::result::Result::Ok(#subtype{#(#field_names: #l_vars.#method_iter(#r_vars)),*})
120+
::derive_more::core::result::Result::Ok(#subtype{
121+
#(#field_names: #l_vars.#method_iter(#r_vars)),*
122+
})
121123
}
122124
};
123125
matches.push(matcher);
124126
}
125127
Fields::Unit => {
126128
let operation_name = method_ident.to_string();
127129
matches.push(quote! {
128-
(#subtype, #subtype) => ::core::result::Result::Err(
130+
(#subtype, #subtype) => ::derive_more::core::result::Result::Err(
129131
::derive_more::BinaryError::Unit(
130132
::derive_more::UnitError::new(#operation_name)
131133
)
@@ -140,7 +142,7 @@ fn enum_content(
140142
// match.
141143
let operation_name = method_ident.to_string();
142144
matches.push(quote! {
143-
_ => ::core::result::Result::Err(::derive_more::BinaryError::Mismatch(
145+
_ => ::derive_more::core::result::Result::Err(::derive_more::BinaryError::Mismatch(
144146
::derive_more::WrongVariantError::new(#operation_name)
145147
))
146148
});

impl/src/as/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ impl<'a> ToTokens for Expansion<'a> {
239239
ImplKind::Specialized
240240
};
241241

242-
let trait_ty = quote! { ::core::convert::#trait_ident <#return_ty> };
242+
let trait_ty = quote! {
243+
::derive_more::#trait_ident <#return_ty>
244+
};
243245

244246
let generics = match &impl_kind {
245247
ImplKind::Forwarded => {
@@ -251,7 +253,7 @@ impl<'a> ToTokens for Expansion<'a> {
251253
if is_blanket {
252254
generics
253255
.params
254-
.push(parse_quote! { #return_ty: ?::core::marker::Sized });
256+
.push(parse_quote! { #return_ty: ?::derive_more::core::marker::Sized });
255257
}
256258
Cow::Owned(generics)
257259
}
@@ -272,7 +274,7 @@ impl<'a> ToTokens for Expansion<'a> {
272274

273275
let conv =
274276
<::derive_more::__private::Conv<& #mut_ #field_ty, #return_ty>
275-
as ::core::default::Default>::default();
277+
as ::derive_more::core::default::Default>::default();
276278
(&&conv).__extract_ref(#field_ref)
277279
}),
278280
};

impl/src/error.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn expand(
4747

4848
let provide = provide.map(|provide| {
4949
quote! {
50-
fn provide<'_request>(&'_request self, request: &mut ::core::error::Request<'_request>) {
50+
fn provide<'_request>(&'_request self, request: &mut ::derive_more::core::error::Request<'_request>) {
5151
#provide
5252
}
5353
}
@@ -61,7 +61,7 @@ pub fn expand(
6161
&generics,
6262
quote! {
6363
where
64-
#ident #ty_generics: ::core::fmt::Debug + ::core::fmt::Display
64+
#ident #ty_generics: ::derive_more::core::fmt::Debug + ::derive_more::core::fmt::Display
6565
},
6666
);
6767
}
@@ -71,8 +71,12 @@ pub fn expand(
7171
generics = utils::add_extra_where_clauses(
7272
&generics,
7373
quote! {
74-
where
75-
#(#bounds: ::core::fmt::Debug + ::core::fmt::Display + ::derive_more::Error + 'static),*
74+
where #(
75+
#bounds: ::derive_more::core::fmt::Debug
76+
+ ::derive_more::core::fmt::Display
77+
+ ::derive_more::Error
78+
+ 'static
79+
),*
7680
},
7781
);
7882
}

impl/src/fmt/debug.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,10 @@ pub fn expand(input: &syn::DeriveInput, _: &str) -> syn::Result<TokenStream> {
4646

4747
Ok(quote! {
4848
#[automatically_derived]
49-
impl #impl_gens ::core::fmt::Debug for #ident #ty_gens
50-
#where_clause
51-
{
49+
impl #impl_gens ::derive_more::Debug for #ident #ty_gens #where_clause {
5250
fn fmt(
53-
&self, __derive_more_f: &mut ::core::fmt::Formatter<'_>
54-
) -> ::core::fmt::Result {
51+
&self, __derive_more_f: &mut ::derive_more::core::fmt::Formatter<'_>
52+
) -> ::derive_more::core::fmt::Result {
5553
#body
5654
}
5755
}
@@ -229,17 +227,17 @@ impl<'a> Expansion<'a> {
229227
fn generate_body(&self) -> syn::Result<TokenStream> {
230228
if let Some(fmt) = &self.attr.fmt {
231229
return Ok(if let Some((expr, trait_ident)) = fmt.transparent_call() {
232-
quote! { ::core::fmt::#trait_ident::fmt(&(#expr), __derive_more_f) }
230+
quote! { ::derive_more::core::fmt::#trait_ident::fmt(&(#expr), __derive_more_f) }
233231
} else {
234-
quote! { ::core::write!(__derive_more_f, #fmt) }
232+
quote! { ::derive_more::core::write!(__derive_more_f, #fmt) }
235233
});
236234
};
237235

238236
match self.fields {
239237
syn::Fields::Unit => {
240238
let ident = self.ident.to_string();
241239
Ok(quote! {
242-
::core::fmt::Formatter::write_str(
240+
::derive_more::core::fmt::Formatter::write_str(
243241
__derive_more_f,
244242
#ident,
245243
)
@@ -268,7 +266,7 @@ impl<'a> Expansion<'a> {
268266
Some(FieldAttribute::Right(fmt_attr)) => Ok(quote! {
269267
::derive_more::__private::DebugTuple::field(
270268
#out,
271-
&::core::format_args!(#fmt_attr),
269+
&::derive_more::core::format_args!(#fmt_attr),
272270
)
273271
}),
274272
None => {
@@ -291,7 +289,7 @@ impl<'a> Expansion<'a> {
291289
let ident = self.ident.to_string();
292290

293291
let out = quote! {
294-
&mut ::core::fmt::Formatter::debug_struct(
292+
&mut ::derive_more::core::fmt::Formatter::debug_struct(
295293
__derive_more_f,
296294
#ident,
297295
)
@@ -309,21 +307,21 @@ impl<'a> Expansion<'a> {
309307
Ok::<_, syn::Error>(out)
310308
}
311309
Some(FieldAttribute::Right(fmt_attr)) => Ok(quote! {
312-
::core::fmt::DebugStruct::field(
310+
::derive_more::core::fmt::DebugStruct::field(
313311
#out,
314312
#field_str,
315-
&::core::format_args!(#fmt_attr),
313+
&::derive_more::core::format_args!(#fmt_attr),
316314
)
317315
}),
318316
None => Ok(quote! {
319-
::core::fmt::DebugStruct::field(#out, #field_str, #field_ident)
317+
::derive_more::core::fmt::DebugStruct::field(#out, #field_str, #field_ident)
320318
}),
321319
}
322320
})?;
323321
Ok(if exhaustive {
324-
quote! { ::core::fmt::DebugStruct::finish(#out) }
322+
quote! { ::derive_more::core::fmt::DebugStruct::finish(#out) }
325323
} else {
326-
quote! { ::core::fmt::DebugStruct::finish_non_exhaustive(#out) }
324+
quote! { ::derive_more::core::fmt::DebugStruct::finish_non_exhaustive(#out) }
327325
})
328326
}
329327
}
@@ -337,7 +335,7 @@ impl<'a> Expansion<'a> {
337335
out.extend(fmt.bounded_types(self.fields).map(|(ty, trait_name)| {
338336
let trait_ident = format_ident!("{trait_name}");
339337

340-
parse_quote! { #ty: ::core::fmt::#trait_ident }
338+
parse_quote! { #ty: ::derive_more::core::fmt::#trait_ident }
341339
}));
342340
Ok(out)
343341
} else {
@@ -351,12 +349,12 @@ impl<'a> Expansion<'a> {
351349
|(ty, trait_name)| {
352350
let trait_ident = format_ident!("{trait_name}");
353351

354-
parse_quote! { #ty: ::core::fmt::#trait_ident }
352+
parse_quote! { #ty: ::derive_more::core::fmt::#trait_ident }
355353
},
356354
));
357355
}
358356
Some(FieldAttribute::Left(_skip)) => {}
359-
None => out.extend([parse_quote! { #ty: ::core::fmt::Debug }]),
357+
None => out.extend([parse_quote! { #ty: ::derive_more::Debug }]),
360358
}
361359
Ok(out)
362360
})

impl/src/fmt/display.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,10 @@ pub fn expand(input: &syn::DeriveInput, trait_name: &str) -> syn::Result<TokenSt
5050

5151
Ok(quote! {
5252
#[automatically_derived]
53-
impl #impl_gens ::core::fmt::#trait_ident for #ident #ty_gens
54-
#where_clause
55-
{
53+
impl #impl_gens ::derive_more::#trait_ident for #ident #ty_gens #where_clause {
5654
fn fmt(
57-
&self, __derive_more_f: &mut ::core::fmt::Formatter<'_>
58-
) -> ::core::fmt::Result {
55+
&self, __derive_more_f: &mut ::derive_more::core::fmt::Formatter<'_>
56+
) -> ::derive_more::core::fmt::Result {
5957
#body
6058
}
6159
}
@@ -190,7 +188,7 @@ fn expand_union(
190188

191189
Ok((
192190
attrs.bounds.0.clone().into_iter().collect(),
193-
quote! { ::core::write!(__derive_more_f, #fmt) },
191+
quote! { ::derive_more::core::write!(__derive_more_f, #fmt) },
194192
))
195193
}
196194

@@ -231,16 +229,16 @@ impl<'a> Expansion<'a> {
231229
match &self.attrs.fmt {
232230
Some(fmt) => {
233231
Ok(if let Some((expr, trait_ident)) = fmt.transparent_call() {
234-
quote! { ::core::fmt::#trait_ident::fmt(&(#expr), __derive_more_f) }
232+
quote! { ::derive_more::core::fmt::#trait_ident::fmt(&(#expr), __derive_more_f) }
235233
} else {
236-
quote! { ::core::write!(__derive_more_f, #fmt) }
234+
quote! { ::derive_more::core::write!(__derive_more_f, #fmt) }
237235
})
238236
}
239237
None if self.fields.is_empty() => {
240238
let ident_str = self.ident.to_string();
241239

242240
Ok(quote! {
243-
::core::write!(__derive_more_f, #ident_str)
241+
::derive_more::core::write!(__derive_more_f, #ident_str)
244242
})
245243
}
246244
None if self.fields.len() == 1 => {
@@ -253,7 +251,7 @@ impl<'a> Expansion<'a> {
253251
let trait_ident = self.trait_ident;
254252

255253
Ok(quote! {
256-
::core::fmt::#trait_ident::fmt(#ident, __derive_more_f)
254+
::derive_more::core::fmt::#trait_ident::fmt(#ident, __derive_more_f)
257255
})
258256
}
259257
_ => Err(syn::Error::new(
@@ -277,7 +275,7 @@ impl<'a> Expansion<'a> {
277275
.map(|f| {
278276
let ty = &f.ty;
279277
let trait_ident = &self.trait_ident;
280-
vec![parse_quote! { #ty: ::core::fmt::#trait_ident }]
278+
vec![parse_quote! { #ty: ::derive_more::core::fmt::#trait_ident }]
281279
})
282280
.unwrap_or_default();
283281
};
@@ -286,7 +284,7 @@ impl<'a> Expansion<'a> {
286284
.map(|(ty, trait_name)| {
287285
let trait_ident = format_ident!("{trait_name}");
288286

289-
parse_quote! { #ty: ::core::fmt::#trait_ident }
287+
parse_quote! { #ty: ::derive_more::core::fmt::#trait_ident }
290288
})
291289
.chain(self.attrs.bounds.0.clone())
292290
.collect()

impl/src/from.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,15 @@ impl<'a> Expansion<'a> {
165165
let index = index.into_iter();
166166
let from_ty = from_tys.next().unwrap_or_else(|| unreachable!());
167167
quote! {
168-
#( #ident: )* <#ty as ::core::convert::From<#from_ty>>::from(
168+
#( #ident: )* <#ty as ::derive_more::From<#from_ty>>::from(
169169
value #( .#index )*
170170
),
171171
}
172172
});
173173

174174
Ok(quote! {
175175
#[automatically_derived]
176-
impl #impl_gens ::core::convert::From<#ty>
177-
for #ident #ty_gens #where_clause
178-
{
176+
impl #impl_gens ::derive_more::From<#ty> for #ident #ty_gens #where_clause {
179177
#[inline]
180178
fn from(value: #ty) -> Self {
181179
#ident #( :: #variant )* #init
@@ -195,9 +193,7 @@ impl<'a> Expansion<'a> {
195193

196194
Ok(quote! {
197195
#[automatically_derived]
198-
impl #impl_gens ::core::convert::From<(#( #field_tys ),*)>
199-
for #ident #ty_gens #where_clause
200-
{
196+
impl #impl_gens ::derive_more::From<(#( #field_tys ),*)> for #ident #ty_gens #where_clause {
201197
#[inline]
202198
fn from(value: (#( #field_tys ),*)) -> Self {
203199
#ident #( :: #variant )* #init
@@ -213,7 +209,7 @@ impl<'a> Expansion<'a> {
213209
let index = index.into_iter();
214210
let gen_ident = format_ident!("__FromT{i}");
215211
let out = quote! {
216-
#( #ident: )* <#ty as ::core::convert::From<#gen_ident>>::from(
212+
#( #ident: )* <#ty as ::derive_more::From<#gen_ident>>::from(
217213
value #( .#index )*
218214
),
219215
};
@@ -227,7 +223,7 @@ impl<'a> Expansion<'a> {
227223
let mut generics = self.generics.clone();
228224
for (ty, ident) in field_tys.iter().zip(&gen_idents) {
229225
generics.make_where_clause().predicates.push(
230-
parse_quote! { #ty: ::core::convert::From<#ident> },
226+
parse_quote! { #ty: ::derive_more::From<#ident> },
231227
);
232228
generics
233229
.params
@@ -239,9 +235,7 @@ impl<'a> Expansion<'a> {
239235

240236
Ok(quote! {
241237
#[automatically_derived]
242-
impl #impl_gens ::core::convert::From<(#( #gen_idents ),*)>
243-
for #ident #ty_gens #where_clause
244-
{
238+
impl #impl_gens ::derive_more::From<(#( #gen_idents ),*)> for #ident #ty_gens #where_clause {
245239
#[inline]
246240
fn from(value: (#( #gen_idents ),*)) -> Self {
247241
#ident #(:: #variant)* #init

impl/src/from_str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn struct_from(state: &State, trait_name: &'static str) -> TokenStream {
4242
type Err = <#field_type as #trait_path>::Err;
4343

4444
#[inline]
45-
fn from_str(src: &str) -> ::core::result::Result<Self, Self::Err> {
45+
fn from_str(src: &str) -> ::derive_more::core::result::Result<Self, Self::Err> {
4646
Ok(#body)
4747
}
4848
}
@@ -97,7 +97,7 @@ fn enum_from(
9797
type Err = ::derive_more::FromStrError;
9898

9999
#[inline]
100-
fn from_str(src: &str) -> ::core::result::Result<Self, Self::Err> {
100+
fn from_str(src: &str) -> ::derive_more::core::result::Result<Self, Self::Err> {
101101
Ok(match src.to_lowercase().as_str() {
102102
#(#cases)*
103103
_ => return Err(::derive_more::FromStrError::new(#input_type_name)),

impl/src/into.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ impl<'a> Expansion<'a> {
179179

180180
Ok(quote! {
181181
#[automatically_derived]
182-
impl #impl_gens ::core::convert::From<#r #lf #m #input_ident #ty_gens>
182+
impl #impl_gens ::derive_more::core::convert::From<#r #lf #m #input_ident #ty_gens>
183183
for ( #( #r #lf #m #tys ),* ) #where_clause
184184
{
185185
#[inline]
186186
fn from(value: #r #lf #m #input_ident #ty_gens) -> Self {
187187
(#(
188-
<#r #m #tys as ::core::convert::From<_>>::from(
188+
<#r #m #tys as ::derive_more::core::convert::From<_>>::from(
189189
#r #m value. #fields_idents
190190
)
191191
),*)

0 commit comments

Comments
 (0)