Skip to content

Commit

Permalink
expand: Mark some dead code in derive expansion as unreachable
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Nov 19, 2020
1 parent cd2177f commit d575aa4
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 54 deletions.
15 changes: 2 additions & 13 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,7 @@ impl<'a> TraitDef<'a> {
_ => false,
})
}
_ => {
// Non-ADT derive is an error, but it should have been
// set earlier; see
// librustc_expand/expand.rs:MacroExpander::fully_expand_fragment()
// librustc_expand/base.rs:Annotatable::derive_allowed()
return;
}
_ => unreachable!(),
};
let container_id = cx.current_expansion.id.expn_data().parent;
let always_copy = has_no_type_params && cx.resolver.has_derive_copy(container_id);
Expand Down Expand Up @@ -475,12 +469,7 @@ impl<'a> TraitDef<'a> {
);
push(Annotatable::Item(P(ast::Item { attrs, ..(*newitem).clone() })))
}
_ => {
// Non-Item derive is an error, but it should have been
// set earlier; see
// librustc_expand/expand.rs:MacroExpander::fully_expand_fragment()
// librustc_expand/base.rs:Annotatable::derive_allowed()
}
_ => unreachable!(),
}
}

Expand Down
8 changes: 1 addition & 7 deletions compiler/rustc_builtin_macros/src/deriving/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,7 @@ fn inject_impl_of_structural_trait(
) {
let item = match *item {
Annotatable::Item(ref item) => item,
_ => {
// Non-Item derive is an error, but it should have been
// set earlier; see
// librustc_expand/expand.rs:MacroExpander::fully_expand_fragment()
// librustc_expand/base.rs:Annotatable::derive_allowed()
return;
}
_ => unreachable!(),
};

let generics = match item.kind {
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,9 +767,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
InvocationKind::Derive { path, item } => match ext {
SyntaxExtensionKind::Derive(expander)
| SyntaxExtensionKind::LegacyDerive(expander) => {
if !item.derive_allowed() {
return ExpandResult::Ready(fragment_kind.dummy(span));
}
if let SyntaxExtensionKind::Derive(..) = ext {
self.gate_proc_macro_input(&item);
}
Expand Down
33 changes: 2 additions & 31 deletions compiler/rustc_expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,9 @@ impl MultiItemModifier for ProcMacroDerive {
item: Annotatable,
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
let item = match item {
Annotatable::Arm(..)
| Annotatable::Field(..)
| Annotatable::FieldPat(..)
| Annotatable::GenericParam(..)
| Annotatable::Param(..)
| Annotatable::StructField(..)
| Annotatable::Variant(..) => panic!("unexpected annotatable"),
Annotatable::Item(item) => item,
Annotatable::ImplItem(_)
| Annotatable::TraitItem(_)
| Annotatable::ForeignItem(_)
| Annotatable::Stmt(_)
| Annotatable::Expr(_) => {
ecx.span_err(
span,
"proc-macro derives may only be applied to a struct, enum, or union",
);
return ExpandResult::Ready(Vec::new());
}
Annotatable::Item(item) => token::NtItem(item),
_ => unreachable!(),
};
match item.kind {
ItemKind::Struct(..) | ItemKind::Enum(..) | ItemKind::Union(..) => {}
_ => {
ecx.span_err(
span,
"proc-macro derives may only be applied to a struct, enum, or union",
);
return ExpandResult::Ready(Vec::new());
}
}

let item = token::NtItem(item);
let input = if item.pretty_printing_compatibility_hack() {
TokenTree::token(token::Interpolated(Lrc::new(item)), DUMMY_SP).into()
} else {
Expand Down

0 comments on commit d575aa4

Please sign in to comment.