1
Fork 0

Run rustfmt

This commit is contained in:
Mark Mansi 2018-04-13 15:58:16 -05:00
parent d703622ce0
commit 3d6d9f98d1

View file

@ -11,12 +11,12 @@
use rustc::middle::allocator::AllocatorKind; use rustc::middle::allocator::AllocatorKind;
use rustc_errors; use rustc_errors;
use syntax::abi::Abi; use syntax::abi::Abi;
use syntax::ast::{Crate, Attribute, LitKind, StrStyle}; use syntax::ast::{Attribute, Crate, LitKind, StrStyle};
use syntax::ast::{Unsafety, Constness, Generics, Mutability, Ty, Mac, Arg}; use syntax::ast::{Arg, Constness, Generics, Mac, Mutability, Ty, Unsafety};
use syntax::ast::{self, Ident, Item, ItemKind, TyKind, VisibilityKind, Expr}; use syntax::ast::{self, Expr, Ident, Item, ItemKind, TyKind, VisibilityKind};
use syntax::attr; use syntax::attr;
use syntax::codemap::{dummy_spanned, respan}; use syntax::codemap::{dummy_spanned, respan};
use syntax::codemap::{ExpnInfo, NameAndSpan, MacroAttribute}; use syntax::codemap::{ExpnInfo, MacroAttribute, NameAndSpan};
use syntax::ext::base::ExtCtxt; use syntax::ext::base::ExtCtxt;
use syntax::ext::base::Resolver; use syntax::ext::base::Resolver;
use syntax::ext::build::AstBuilder; use syntax::ext::build::AstBuilder;
@ -31,10 +31,12 @@ use syntax_pos::{Span, DUMMY_SP};
use {AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS}; use {AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS};
pub fn modify(sess: &ParseSess, pub fn modify(
sess: &ParseSess,
resolver: &mut Resolver, resolver: &mut Resolver,
krate: Crate, krate: Crate,
handler: &rustc_errors::Handler) -> ast::Crate { handler: &rustc_errors::Handler,
) -> ast::Crate {
ExpandAllocatorDirectives { ExpandAllocatorDirectives {
handler, handler,
sess, sess,
@ -55,20 +57,24 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
let name = if attr::contains_name(&item.attrs, "global_allocator") { let name = if attr::contains_name(&item.attrs, "global_allocator") {
"global_allocator" "global_allocator"
} else { } else {
return fold::noop_fold_item(item, self) return fold::noop_fold_item(item, self);
}; };
match item.node { match item.node {
ItemKind::Static(..) => {} ItemKind::Static(..) => {}
_ => { _ => {
self.handler.span_err(item.span, "allocators must be statics"); self.handler
return SmallVector::one(item) .span_err(item.span, "allocators must be statics");
return SmallVector::one(item);
} }
} }
if self.found { if self.found {
self.handler.span_err(item.span, "cannot define more than one \ self.handler.span_err(
#[global_allocator]"); item.span,
return SmallVector::one(item) "cannot define more than one \
#[global_allocator]",
);
return SmallVector::one(item);
} }
self.found = true; self.found = true;
@ -80,7 +86,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
span: None, span: None,
allow_internal_unstable: true, allow_internal_unstable: true,
allow_internal_unsafe: false, allow_internal_unsafe: false,
} },
}); });
let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark)); let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
let ecfg = ExpansionConfig::default(name.to_string()); let ecfg = ExpansionConfig::default(name.to_string());
@ -91,10 +97,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
core: Ident::from_str("core"), core: Ident::from_str("core"),
cx: ExtCtxt::new(self.sess, ecfg, self.resolver), cx: ExtCtxt::new(self.sess, ecfg, self.resolver),
}; };
let super_path = f.cx.path(f.span, vec![ let super_path = f.cx.path(f.span, vec![Ident::from_str("super"), f.global]);
Ident::from_str("super"),
f.global,
]);
let mut items = vec![ let mut items = vec![
f.cx.item_extern_crate(f.span, f.core), f.cx.item_extern_crate(f.span, f.core),
f.cx.item_use_simple( f.cx.item_use_simple(
@ -114,7 +117,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
let mut ret = SmallVector::new(); let mut ret = SmallVector::new();
ret.push(item); ret.push(item);
ret.push(module); ret.push(module);
return ret return ret;
} }
fn fold_mac(&mut self, mac: Mac) -> Mac { fn fold_mac(&mut self, mac: Mac) -> Mac {
@ -139,30 +142,39 @@ impl<'a> AllocFnFactory<'a> {
i += 1; i += 1;
name name
}; };
let args = method.inputs.iter().map(|ty| { let args = method
self.arg_ty(ty, &mut abi_args, mk) .inputs
}).collect(); .iter()
.map(|ty| self.arg_ty(ty, &mut abi_args, mk))
.collect();
let result = self.call_allocator(method.name, args); let result = self.call_allocator(method.name, args);
let (output_ty, output_expr) = self.ret_ty(&method.output, result); let (output_ty, output_expr) = self.ret_ty(&method.output, result);
let kind = ItemKind::Fn(self.cx.fn_decl(abi_args, ast::FunctionRetTy::Ty(output_ty)), let kind = ItemKind::Fn(
self.cx.fn_decl(abi_args, ast::FunctionRetTy::Ty(output_ty)),
Unsafety::Unsafe, Unsafety::Unsafe,
dummy_spanned(Constness::NotConst), dummy_spanned(Constness::NotConst),
Abi::Rust, Abi::Rust,
Generics::default(), Generics::default(),
self.cx.block_expr(output_expr)); self.cx.block_expr(output_expr),
self.cx.item(self.span, );
self.cx.item(
self.span,
Ident::from_str(&self.kind.fn_name(method.name)), Ident::from_str(&self.kind.fn_name(method.name)),
self.attrs(), self.attrs(),
kind) kind,
)
} }
fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> { fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> {
let method = self.cx.path(self.span, vec![ let method = self.cx.path(
self.span,
vec![
self.core, self.core,
Ident::from_str("alloc"), Ident::from_str("alloc"),
Ident::from_str("GlobalAlloc"), Ident::from_str("GlobalAlloc"),
Ident::from_str(method), Ident::from_str(method),
]); ],
);
let method = self.cx.expr_path(method); let method = self.cx.expr_path(method);
let allocator = self.cx.path_ident(self.span, self.global); let allocator = self.cx.path_ident(self.span, self.global);
let allocator = self.cx.expr_path(allocator); let allocator = self.cx.expr_path(allocator);
@ -189,10 +201,12 @@ impl<'a> AllocFnFactory<'a> {
] ]
} }
fn arg_ty(&self, fn arg_ty(
&self,
ty: &AllocatorTy, ty: &AllocatorTy,
args: &mut Vec<Arg>, args: &mut Vec<Arg>,
ident: &mut FnMut() -> Ident) -> P<Expr> { ident: &mut FnMut() -> Ident,
) -> P<Expr> {
match *ty { match *ty {
AllocatorTy::Layout => { AllocatorTy::Layout => {
let usize = self.cx.path_ident(self.span, Ident::from_str("usize")); let usize = self.cx.path_ident(self.span, Ident::from_str("usize"));
@ -202,18 +216,19 @@ impl<'a> AllocFnFactory<'a> {
args.push(self.cx.arg(self.span, size, ty_usize.clone())); args.push(self.cx.arg(self.span, size, ty_usize.clone()));
args.push(self.cx.arg(self.span, align, ty_usize)); args.push(self.cx.arg(self.span, align, ty_usize));
let layout_new = self.cx.path(self.span, vec![ let layout_new = self.cx.path(
self.span,
vec![
self.core, self.core,
Ident::from_str("alloc"), Ident::from_str("alloc"),
Ident::from_str("Layout"), Ident::from_str("Layout"),
Ident::from_str("from_size_align_unchecked"), Ident::from_str("from_size_align_unchecked"),
]); ],
);
let layout_new = self.cx.expr_path(layout_new); let layout_new = self.cx.expr_path(layout_new);
let size = self.cx.expr_ident(self.span, size); let size = self.cx.expr_ident(self.span, size);
let align = self.cx.expr_ident(self.span, align); let align = self.cx.expr_ident(self.span, align);
let layout = self.cx.expr_call(self.span, let layout = self.cx.expr_call(self.span, layout_new, vec![size, align]);
layout_new,
vec![size, align]);
layout layout
} }
@ -230,9 +245,7 @@ impl<'a> AllocFnFactory<'a> {
self.cx.expr_ident(self.span, ident) self.cx.expr_ident(self.span, ident)
} }
AllocatorTy::ResultPtr | AllocatorTy::ResultPtr | AllocatorTy::Bang | AllocatorTy::Unit => {
AllocatorTy::Bang |
AllocatorTy::Unit => {
panic!("can't convert AllocatorTy to an argument") panic!("can't convert AllocatorTy to an argument")
} }
} }
@ -249,17 +262,11 @@ impl<'a> AllocFnFactory<'a> {
(self.ptr_u8(), expr) (self.ptr_u8(), expr)
} }
AllocatorTy::Bang => { AllocatorTy::Bang => (self.cx.ty(self.span, TyKind::Never), expr),
(self.cx.ty(self.span, TyKind::Never), expr)
}
AllocatorTy::Unit => { AllocatorTy::Unit => (self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr),
(self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr)
}
AllocatorTy::Layout | AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
AllocatorTy::Usize |
AllocatorTy::Ptr => {
panic!("can't convert AllocatorTy to an output") panic!("can't convert AllocatorTy to an output")
} }
} }
@ -277,11 +284,14 @@ impl<'a> AllocFnFactory<'a> {
} }
fn ptr_opaque(&self) -> P<Ty> { fn ptr_opaque(&self) -> P<Ty> {
let opaque = self.cx.path(self.span, vec![ let opaque = self.cx.path(
self.span,
vec![
self.core, self.core,
Ident::from_str("alloc"), Ident::from_str("alloc"),
Ident::from_str("Opaque"), Ident::from_str("Opaque"),
]); ],
);
let ty_opaque = self.cx.ty_path(opaque); let ty_opaque = self.cx.ty_path(opaque);
self.cx.ty_ptr(self.span, ty_opaque, Mutability::Mutable) self.cx.ty_ptr(self.span, ty_opaque, Mutability::Mutable)
} }