Skip to content

Make many MacResult methods unreachable. #142061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 16 additions & 63 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,37 +390,37 @@ macro_rules! make_stmts_default {
pub trait MacResult {
/// Creates an expression.
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
None
unreachable!();
}

/// Creates zero or more items.
fn make_items(self: Box<Self>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
None
unreachable!();
}

/// Creates zero or more impl items.
fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
None
unreachable!();
}

/// Creates zero or more impl items.
fn make_trait_impl_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
None
unreachable!();
}

/// Creates zero or more trait items.
fn make_trait_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
None
unreachable!();
}

/// Creates zero or more items in an `extern {}` block
fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[P<ast::ForeignItem>; 1]>> {
None
unreachable!();
}

/// Creates a pattern.
fn make_pat(self: Box<Self>) -> Option<P<ast::Pat>> {
None
unreachable!();
}

/// Creates zero or more statements.
Expand All @@ -432,39 +432,39 @@ pub trait MacResult {
}

fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
None
unreachable!();
}

fn make_arms(self: Box<Self>) -> Option<SmallVec<[ast::Arm; 1]>> {
None
unreachable!();
}

fn make_expr_fields(self: Box<Self>) -> Option<SmallVec<[ast::ExprField; 1]>> {
None
unreachable!();
}

fn make_pat_fields(self: Box<Self>) -> Option<SmallVec<[ast::PatField; 1]>> {
None
unreachable!();
}

fn make_generic_params(self: Box<Self>) -> Option<SmallVec<[ast::GenericParam; 1]>> {
None
unreachable!();
}

fn make_params(self: Box<Self>) -> Option<SmallVec<[ast::Param; 1]>> {
None
unreachable!();
}

fn make_field_defs(self: Box<Self>) -> Option<SmallVec<[ast::FieldDef; 1]>> {
None
unreachable!();
}

fn make_variants(self: Box<Self>) -> Option<SmallVec<[ast::Variant; 1]>> {
None
unreachable!();
}

fn make_where_predicates(self: Box<Self>) -> Option<SmallVec<[ast::WherePredicate; 1]>> {
None
unreachable!();
}

fn make_crate(self: Box<Self>) -> Option<ast::Crate> {
Expand Down Expand Up @@ -501,9 +501,6 @@ make_MacEager! {
expr: P<ast::Expr>,
pat: P<ast::Pat>,
items: SmallVec<[P<ast::Item>; 1]>,
impl_items: SmallVec<[P<ast::AssocItem>; 1]>,
trait_items: SmallVec<[P<ast::AssocItem>; 1]>,
foreign_items: SmallVec<[P<ast::ForeignItem>; 1]>,
stmts: SmallVec<[ast::Stmt; 1]>,
ty: P<ast::Ty>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MacEager can certainly be trimmed further, only MacEager::{expr, items, ty} are actually used in built-in macros, the other fields are never set to anything other than None and their uses can be replaced with None.

}
Expand All @@ -517,22 +514,6 @@ impl MacResult for MacEager {
self.items
}

fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
self.impl_items
}

fn make_trait_impl_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
self.impl_items
}

fn make_trait_items(self: Box<Self>) -> Option<SmallVec<[P<ast::AssocItem>; 1]>> {
self.trait_items
}

fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[P<ast::ForeignItem>; 1]>> {
self.foreign_items
}

fn make_stmts(self: Box<Self>) -> Option<SmallVec<[ast::Stmt; 1]>> {
match self.stmts.as_ref().map_or(0, |s| s.len()) {
0 => make_stmts_default!(self),
Expand Down Expand Up @@ -654,34 +635,6 @@ impl MacResult for DummyResult {
}))
}

fn make_arms(self: Box<DummyResult>) -> Option<SmallVec<[ast::Arm; 1]>> {
Some(SmallVec::new())
}

fn make_expr_fields(self: Box<DummyResult>) -> Option<SmallVec<[ast::ExprField; 1]>> {
Some(SmallVec::new())
}

fn make_pat_fields(self: Box<DummyResult>) -> Option<SmallVec<[ast::PatField; 1]>> {
Some(SmallVec::new())
}

fn make_generic_params(self: Box<DummyResult>) -> Option<SmallVec<[ast::GenericParam; 1]>> {
Some(SmallVec::new())
}

fn make_params(self: Box<DummyResult>) -> Option<SmallVec<[ast::Param; 1]>> {
Some(SmallVec::new())
}

fn make_field_defs(self: Box<DummyResult>) -> Option<SmallVec<[ast::FieldDef; 1]>> {
Some(SmallVec::new())
}

fn make_variants(self: Box<DummyResult>) -> Option<SmallVec<[ast::Variant; 1]>> {
Some(SmallVec::new())
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need

  • a test using #[rustfmt::skip] in all possible attribute positions,
  • a test using log_syntax!() in all possible fn-like macro positions,
  • and a test using include!() or concat_idents!() in all possible fn-like macro positions.

If all of those do not crash, then we can keep the unreachable!s.


fn make_crate(self: Box<DummyResult>) -> Option<ast::Crate> {
Some(ast::Crate {
attrs: Default::default(),
Expand Down
Loading