Skip to content

Commit d575aa4

Browse files
committed
expand: Mark some dead code in derive expansion as unreachable
1 parent cd2177f commit d575aa4

File tree

4 files changed

+5
-54
lines changed

4 files changed

+5
-54
lines changed

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,7 @@ impl<'a> TraitDef<'a> {
407407
_ => false,
408408
})
409409
}
410-
_ => {
411-
// Non-ADT derive is an error, but it should have been
412-
// set earlier; see
413-
// librustc_expand/expand.rs:MacroExpander::fully_expand_fragment()
414-
// librustc_expand/base.rs:Annotatable::derive_allowed()
415-
return;
416-
}
410+
_ => unreachable!(),
417411
};
418412
let container_id = cx.current_expansion.id.expn_data().parent;
419413
let always_copy = has_no_type_params && cx.resolver.has_derive_copy(container_id);
@@ -475,12 +469,7 @@ impl<'a> TraitDef<'a> {
475469
);
476470
push(Annotatable::Item(P(ast::Item { attrs, ..(*newitem).clone() })))
477471
}
478-
_ => {
479-
// Non-Item derive is an error, but it should have been
480-
// set earlier; see
481-
// librustc_expand/expand.rs:MacroExpander::fully_expand_fragment()
482-
// librustc_expand/base.rs:Annotatable::derive_allowed()
483-
}
472+
_ => unreachable!(),
484473
}
485474
}
486475

compiler/rustc_builtin_macros/src/deriving/mod.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,7 @@ fn inject_impl_of_structural_trait(
9898
) {
9999
let item = match *item {
100100
Annotatable::Item(ref item) => item,
101-
_ => {
102-
// Non-Item derive is an error, but it should have been
103-
// set earlier; see
104-
// librustc_expand/expand.rs:MacroExpander::fully_expand_fragment()
105-
// librustc_expand/base.rs:Annotatable::derive_allowed()
106-
return;
107-
}
101+
_ => unreachable!(),
108102
};
109103

110104
let generics = match item.kind {

compiler/rustc_expand/src/expand.rs

-3
Original file line numberDiff line numberDiff line change
@@ -767,9 +767,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
767767
InvocationKind::Derive { path, item } => match ext {
768768
SyntaxExtensionKind::Derive(expander)
769769
| SyntaxExtensionKind::LegacyDerive(expander) => {
770-
if !item.derive_allowed() {
771-
return ExpandResult::Ready(fragment_kind.dummy(span));
772-
}
773770
if let SyntaxExtensionKind::Derive(..) = ext {
774771
self.gate_proc_macro_input(&item);
775772
}

compiler/rustc_expand/src/proc_macro.rs

+2-31
Original file line numberDiff line numberDiff line change
@@ -75,38 +75,9 @@ impl MultiItemModifier for ProcMacroDerive {
7575
item: Annotatable,
7676
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
7777
let item = match item {
78-
Annotatable::Arm(..)
79-
| Annotatable::Field(..)
80-
| Annotatable::FieldPat(..)
81-
| Annotatable::GenericParam(..)
82-
| Annotatable::Param(..)
83-
| Annotatable::StructField(..)
84-
| Annotatable::Variant(..) => panic!("unexpected annotatable"),
85-
Annotatable::Item(item) => item,
86-
Annotatable::ImplItem(_)
87-
| Annotatable::TraitItem(_)
88-
| Annotatable::ForeignItem(_)
89-
| Annotatable::Stmt(_)
90-
| Annotatable::Expr(_) => {
91-
ecx.span_err(
92-
span,
93-
"proc-macro derives may only be applied to a struct, enum, or union",
94-
);
95-
return ExpandResult::Ready(Vec::new());
96-
}
78+
Annotatable::Item(item) => token::NtItem(item),
79+
_ => unreachable!(),
9780
};
98-
match item.kind {
99-
ItemKind::Struct(..) | ItemKind::Enum(..) | ItemKind::Union(..) => {}
100-
_ => {
101-
ecx.span_err(
102-
span,
103-
"proc-macro derives may only be applied to a struct, enum, or union",
104-
);
105-
return ExpandResult::Ready(Vec::new());
106-
}
107-
}
108-
109-
let item = token::NtItem(item);
11081
let input = if item.pretty_printing_compatibility_hack() {
11182
TokenTree::token(token::Interpolated(Lrc::new(item)), DUMMY_SP).into()
11283
} else {

0 commit comments

Comments
 (0)