Skip to content

Commit

Permalink
rustfmt relevant files
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Simulacrum committed Feb 21, 2020
1 parent 1059a86 commit 00b07c6
Show file tree
Hide file tree
Showing 6 changed files with 783 additions and 904 deletions.
132 changes: 68 additions & 64 deletions src/librustc/hir/map/blocks.rs
Expand Up @@ -12,9 +12,9 @@
//! for the `Code` associated with a particular NodeId.

use crate::hir as ast;
use crate::hir::intravisit::FnKind;
use crate::hir::map;
use crate::hir::{Expr, FnDecl, Node};
use crate::hir::intravisit::FnKind;
use syntax::ast::{Attribute, Ident};
use syntax_pos::Span;

Expand All @@ -29,11 +29,15 @@ use syntax_pos::Span;
///
/// To construct one, use the `Code::from_node` function.
#[derive(Copy, Clone, Debug)]
pub struct FnLikeNode<'a> { node: Node<'a> }
pub struct FnLikeNode<'a> {
node: Node<'a>,
}

/// MaybeFnLike wraps a method that indicates if an object
/// corresponds to some FnLikeNode.
trait MaybeFnLike { fn is_fn_like(&self) -> bool; }
trait MaybeFnLike {
fn is_fn_like(&self) -> bool;
}

impl MaybeFnLike for ast::Item {
fn is_fn_like(&self) -> bool {
Expand Down Expand Up @@ -96,23 +100,23 @@ impl<'a> Code<'a> {
Code::from_node(map, map.get_parent_node(id))
}
map::Node::Expr(expr) => Some(Code::Expr(expr)),
node => FnLikeNode::from_node(node).map(Code::FnLike)
node => FnLikeNode::from_node(node).map(Code::FnLike),
}
}
}

/// These are all the components one can extract from a fn item for
/// use when implementing FnLikeNode operations.
struct ItemFnParts<'a> {
ident: Ident,
decl: &'a ast::FnDecl,
header: ast::FnHeader,
vis: &'a ast::Visibility,
ident: Ident,
decl: &'a ast::FnDecl,
header: ast::FnHeader,
vis: &'a ast::Visibility,
generics: &'a ast::Generics,
body: ast::BodyId,
id: ast::HirId,
span: Span,
attrs: &'a [Attribute],
body: ast::BodyId,
id: ast::HirId,
span: Span,
attrs: &'a [Attribute],
}

/// These are all the components one can extract from a closure expr
Expand All @@ -127,13 +131,7 @@ struct ClosureParts<'a> {

impl<'a> ClosureParts<'a> {
fn new(d: &'a FnDecl, b: ast::BodyId, id: ast::HirId, s: Span, attrs: &'a [Attribute]) -> Self {
ClosureParts {
decl: d,
body: b,
id,
span: s,
attrs,
}
ClosureParts { decl: d, body: b, id, span: s, attrs }
}
}

Expand All @@ -145,33 +143,41 @@ impl<'a> FnLikeNode<'a> {
map::Node::TraitItem(tm) => tm.is_fn_like(),
map::Node::ImplItem(it) => it.is_fn_like(),
map::Node::Expr(e) => e.is_fn_like(),
_ => false
_ => false,
};
fn_like.then_some(FnLikeNode { node })
}

pub fn body(self) -> ast::BodyId {
self.handle(|i: ItemFnParts<'a>| i.body,
|_, _, _: &'a ast::FnSig, _, body: ast::BodyId, _, _| body,
|c: ClosureParts<'a>| c.body)
self.handle(
|i: ItemFnParts<'a>| i.body,
|_, _, _: &'a ast::FnSig, _, body: ast::BodyId, _, _| body,
|c: ClosureParts<'a>| c.body,
)
}

pub fn decl(self) -> &'a FnDecl {
self.handle(|i: ItemFnParts<'a>| &*i.decl,
|_, _, sig: &'a ast::FnSig, _, _, _, _| &sig.decl,
|c: ClosureParts<'a>| c.decl)
self.handle(
|i: ItemFnParts<'a>| &*i.decl,
|_, _, sig: &'a ast::FnSig, _, _, _, _| &sig.decl,
|c: ClosureParts<'a>| c.decl,
)
}

pub fn span(self) -> Span {
self.handle(|i: ItemFnParts<'_>| i.span,
|_, _, _: &'a ast::FnSig, _, _, span, _| span,
|c: ClosureParts<'_>| c.span)
self.handle(
|i: ItemFnParts<'_>| i.span,
|_, _, _: &'a ast::FnSig, _, _, span, _| span,
|c: ClosureParts<'_>| c.span,
)
}

pub fn id(self) -> ast::HirId {
self.handle(|i: ItemFnParts<'_>| i.id,
|id, _, _: &'a ast::FnSig, _, _, _, _| id,
|c: ClosureParts<'_>| c.id)
self.handle(
|i: ItemFnParts<'_>| i.id,
|id, _, _: &'a ast::FnSig, _, _, _, _| id,
|c: ClosureParts<'_>| c.id,
)
}

pub fn constness(self) -> ast::Constness {
Expand All @@ -190,41 +196,40 @@ impl<'a> FnLikeNode<'a> {
let item = |p: ItemFnParts<'a>| -> FnKind<'a> {
FnKind::ItemFn(p.ident, p.generics, p.header, p.vis, p.attrs)
};
let closure = |c: ClosureParts<'a>| {
FnKind::Closure(c.attrs)
};
let closure = |c: ClosureParts<'a>| FnKind::Closure(c.attrs);
let method = |_, ident: Ident, sig: &'a ast::FnSig, vis, _, _, attrs| {
FnKind::Method(ident, sig, vis, attrs)
};
self.handle(item, method, closure)
}

fn handle<A, I, M, C>(self, item_fn: I, method: M, closure: C) -> A where
fn handle<A, I, M, C>(self, item_fn: I, method: M, closure: C) -> A
where
I: FnOnce(ItemFnParts<'a>) -> A,
M: FnOnce(ast::HirId,
Ident,
&'a ast::FnSig,
Option<&'a ast::Visibility>,
ast::BodyId,
Span,
&'a [Attribute])
-> A,
M: FnOnce(
ast::HirId,
Ident,
&'a ast::FnSig,
Option<&'a ast::Visibility>,
ast::BodyId,
Span,
&'a [Attribute],
) -> A,
C: FnOnce(ClosureParts<'a>) -> A,
{
match self.node {
map::Node::Item(i) => match i.kind {
ast::ItemKind::Fn(ref sig, ref generics, block) =>
item_fn(ItemFnParts {
id: i.hir_id,
ident: i.ident,
decl: &sig.decl,
body: block,
vis: &i.vis,
span: i.span,
attrs: &i.attrs,
header: sig.header,
generics,
}),
ast::ItemKind::Fn(ref sig, ref generics, block) => item_fn(ItemFnParts {
id: i.hir_id,
ident: i.ident,
decl: &sig.decl,
body: block,
vis: &i.vis,
span: i.span,
attrs: &i.attrs,
header: sig.header,
generics,
}),
_ => bug!("item FnLikeNode that is not fn-like"),
},
map::Node::TraitItem(ti) => match ti.kind {
Expand All @@ -233,17 +238,16 @@ impl<'a> FnLikeNode<'a> {
}
_ => bug!("trait method FnLikeNode that is not fn-like"),
},
map::Node::ImplItem(ii) => {
match ii.kind {
ast::ImplItemKind::Method(ref sig, body) => {
method(ii.hir_id, ii.ident, sig, Some(&ii.vis), body, ii.span, &ii.attrs)
}
_ => bug!("impl method FnLikeNode that is not fn-like")
map::Node::ImplItem(ii) => match ii.kind {
ast::ImplItemKind::Method(ref sig, body) => {
method(ii.hir_id, ii.ident, sig, Some(&ii.vis), body, ii.span, &ii.attrs)
}
_ => bug!("impl method FnLikeNode that is not fn-like"),
},
map::Node::Expr(e) => match e.kind {
ast::ExprKind::Closure(_, ref decl, block, _fn_decl_span, _gen) =>
closure(ClosureParts::new(&decl, block, e.hir_id, e.span, &e.attrs)),
ast::ExprKind::Closure(_, ref decl, block, _fn_decl_span, _gen) => {
closure(ClosureParts::new(&decl, block, e.hir_id, e.span, &e.attrs))
}
_ => bug!("expr FnLikeNode that is not fn-like"),
},
_ => bug!("other FnLikeNode that is not fn-like"),
Expand Down

0 comments on commit 00b07c6

Please sign in to comment.