Skip to content

Commit 64ea295

Browse files
committed
expand: extract error_derive_forbidden_on_non_adt
1 parent b8b32a9 commit 64ea295

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/librustc_expand/expand.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -451,28 +451,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
451451
_ => unreachable!(),
452452
};
453453
if !item.derive_allowed() {
454-
let attr = attr::find_by_name(item.attrs(), sym::derive)
455-
.expect("`derive` attribute should exist");
456-
let span = attr.span;
457-
let mut err = self.cx.struct_span_err(
458-
span,
459-
"`derive` may only be applied to structs, enums and unions",
460-
);
461-
if let ast::AttrStyle::Inner = attr.style {
462-
let trait_list = derives
463-
.iter()
464-
.map(|t| pprust::path_to_string(t))
465-
.collect::<Vec<_>>();
466-
let suggestion = format!("#[derive({})]", trait_list.join(", "));
467-
err.span_suggestion(
468-
span,
469-
"try an outer attribute",
470-
suggestion,
471-
// We don't 𝑘𝑛𝑜𝑤 that the following item is an ADT
472-
Applicability::MaybeIncorrect,
473-
);
474-
}
475-
err.emit();
454+
self.error_derive_forbidden_on_non_adt(&derives, &item);
476455
}
477456

478457
let mut item = self.fully_configure(item);
@@ -521,6 +500,27 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
521500
fragment_with_placeholders
522501
}
523502

503+
fn error_derive_forbidden_on_non_adt(&self, derives: &[Path], item: &Annotatable) {
504+
let attr =
505+
attr::find_by_name(item.attrs(), sym::derive).expect("`derive` attribute should exist");
506+
let span = attr.span;
507+
let mut err = self
508+
.cx
509+
.struct_span_err(span, "`derive` may only be applied to structs, enums and unions");
510+
if let ast::AttrStyle::Inner = attr.style {
511+
let trait_list = derives.iter().map(|t| pprust::path_to_string(t)).collect::<Vec<_>>();
512+
let suggestion = format!("#[derive({})]", trait_list.join(", "));
513+
err.span_suggestion(
514+
span,
515+
"try an outer attribute",
516+
suggestion,
517+
// We don't 𝑘𝑛𝑜𝑤 that the following item is an ADT
518+
Applicability::MaybeIncorrect,
519+
);
520+
}
521+
err.emit();
522+
}
523+
524524
fn resolve_imports(&mut self) {
525525
if self.monotonic {
526526
self.cx.resolver.resolve_imports();

0 commit comments

Comments
 (0)