1111use rustc:: middle:: allocator:: AllocatorKind ;
1212use rustc_errors;
1313use syntax:: abi:: Abi ;
14- use syntax:: ast:: { Crate , Attribute , LitKind , StrStyle } ;
15- use syntax:: ast:: { Unsafety , Constness , Generics , Mutability , Ty , Mac , Arg } ;
16- use syntax:: ast:: { self , Ident , Item , ItemKind , TyKind , VisibilityKind , Expr } ;
14+ use syntax:: ast:: { Attribute , Crate , LitKind , StrStyle } ;
15+ use syntax:: ast:: { Arg , Constness , Generics , Mac , Mutability , Ty , Unsafety } ;
16+ use syntax:: ast:: { self , Expr , Ident , Item , ItemKind , TyKind , VisibilityKind } ;
1717use syntax:: attr;
1818use syntax:: codemap:: { dummy_spanned, respan} ;
19- use syntax:: codemap:: { ExpnInfo , NameAndSpan , MacroAttribute } ;
19+ use syntax:: codemap:: { ExpnInfo , MacroAttribute , NameAndSpan } ;
2020use syntax:: ext:: base:: ExtCtxt ;
2121use syntax:: ext:: base:: Resolver ;
2222use syntax:: ext:: build:: AstBuilder ;
@@ -31,10 +31,12 @@ use syntax_pos::{Span, DUMMY_SP};
3131
3232use { AllocatorMethod , AllocatorTy , ALLOCATOR_METHODS } ;
3333
34- pub fn modify ( sess : & ParseSess ,
35- resolver : & mut Resolver ,
36- krate : Crate ,
37- handler : & rustc_errors:: Handler ) -> ast:: Crate {
34+ pub fn modify (
35+ sess : & ParseSess ,
36+ resolver : & mut Resolver ,
37+ krate : Crate ,
38+ handler : & rustc_errors:: Handler ,
39+ ) -> ast:: Crate {
3840 ExpandAllocatorDirectives {
3941 handler,
4042 sess,
@@ -55,20 +57,24 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
5557 let name = if attr:: contains_name ( & item. attrs , "global_allocator" ) {
5658 "global_allocator"
5759 } else {
58- return fold:: noop_fold_item ( item, self )
60+ return fold:: noop_fold_item ( item, self ) ;
5961 } ;
6062 match item. node {
6163 ItemKind :: Static ( ..) => { }
6264 _ => {
63- self . handler . span_err ( item. span , "allocators must be statics" ) ;
64- return SmallVector :: one ( item)
65+ self . handler
66+ . span_err ( item. span , "allocators must be statics" ) ;
67+ return SmallVector :: one ( item) ;
6568 }
6669 }
6770
6871 if self . found {
69- self . handler . span_err ( item. span , "cannot define more than one \
70- #[global_allocator]") ;
71- return SmallVector :: one ( item)
72+ self . handler . span_err (
73+ item. span ,
74+ "cannot define more than one \
75+ #[global_allocator]",
76+ ) ;
77+ return SmallVector :: one ( item) ;
7278 }
7379 self . found = true ;
7480
@@ -80,7 +86,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
8086 span : None ,
8187 allow_internal_unstable : true ,
8288 allow_internal_unsafe : false ,
83- }
89+ } ,
8490 } ) ;
8591 let span = item. span . with_ctxt ( SyntaxContext :: empty ( ) . apply_mark ( mark) ) ;
8692 let ecfg = ExpansionConfig :: default ( name. to_string ( ) ) ;
@@ -91,10 +97,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
9197 core : Ident :: from_str ( "core" ) ,
9298 cx : ExtCtxt :: new ( self . sess , ecfg, self . resolver ) ,
9399 } ;
94- let super_path = f. cx . path ( f. span , vec ! [
95- Ident :: from_str( "super" ) ,
96- f. global,
97- ] ) ;
100+ let super_path = f. cx . path ( f. span , vec ! [ Ident :: from_str( "super" ) , f. global] ) ;
98101 let mut items = vec ! [
99102 f. cx. item_extern_crate( f. span, f. core) ,
100103 f. cx. item_use_simple(
@@ -114,7 +117,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
114117 let mut ret = SmallVector :: new ( ) ;
115118 ret. push ( item) ;
116119 ret. push ( module) ;
117- return ret
120+ return ret;
118121 }
119122
120123 fn fold_mac ( & mut self , mac : Mac ) -> Mac {
@@ -139,30 +142,39 @@ impl<'a> AllocFnFactory<'a> {
139142 i += 1 ;
140143 name
141144 } ;
142- let args = method. inputs . iter ( ) . map ( |ty| {
143- self . arg_ty ( ty, & mut abi_args, mk)
144- } ) . collect ( ) ;
145+ let args = method
146+ . inputs
147+ . iter ( )
148+ . map ( |ty| self . arg_ty ( ty, & mut abi_args, mk) )
149+ . collect ( ) ;
145150 let result = self . call_allocator ( method. name , args) ;
146151 let ( output_ty, output_expr) = self . ret_ty ( & method. output , result) ;
147- let kind = ItemKind :: Fn ( self . cx . fn_decl ( abi_args, ast:: FunctionRetTy :: Ty ( output_ty) ) ,
148- Unsafety :: Unsafe ,
149- dummy_spanned ( Constness :: NotConst ) ,
150- Abi :: Rust ,
151- Generics :: default ( ) ,
152- self . cx . block_expr ( output_expr) ) ;
153- self . cx . item ( self . span ,
154- Ident :: from_str ( & self . kind . fn_name ( method. name ) ) ,
155- self . attrs ( ) ,
156- kind)
152+ let kind = ItemKind :: Fn (
153+ self . cx . fn_decl ( abi_args, ast:: FunctionRetTy :: Ty ( output_ty) ) ,
154+ Unsafety :: Unsafe ,
155+ dummy_spanned ( Constness :: NotConst ) ,
156+ Abi :: Rust ,
157+ Generics :: default ( ) ,
158+ self . cx . block_expr ( output_expr) ,
159+ ) ;
160+ self . cx . item (
161+ self . span ,
162+ Ident :: from_str ( & self . kind . fn_name ( method. name ) ) ,
163+ self . attrs ( ) ,
164+ kind,
165+ )
157166 }
158167
159168 fn call_allocator ( & self , method : & str , mut args : Vec < P < Expr > > ) -> P < Expr > {
160- let method = self . cx . path ( self . span , vec ! [
161- self . core,
162- Ident :: from_str( "alloc" ) ,
163- Ident :: from_str( "GlobalAlloc" ) ,
164- Ident :: from_str( method) ,
165- ] ) ;
169+ let method = self . cx . path (
170+ self . span ,
171+ vec ! [
172+ self . core,
173+ Ident :: from_str( "alloc" ) ,
174+ Ident :: from_str( "GlobalAlloc" ) ,
175+ Ident :: from_str( method) ,
176+ ] ,
177+ ) ;
166178 let method = self . cx . expr_path ( method) ;
167179 let allocator = self . cx . path_ident ( self . span , self . global ) ;
168180 let allocator = self . cx . expr_path ( allocator) ;
@@ -189,10 +201,12 @@ impl<'a> AllocFnFactory<'a> {
189201 ]
190202 }
191203
192- fn arg_ty ( & self ,
193- ty : & AllocatorTy ,
194- args : & mut Vec < Arg > ,
195- ident : & mut FnMut ( ) -> Ident ) -> P < Expr > {
204+ fn arg_ty (
205+ & self ,
206+ ty : & AllocatorTy ,
207+ args : & mut Vec < Arg > ,
208+ ident : & mut FnMut ( ) -> Ident ,
209+ ) -> P < Expr > {
196210 match * ty {
197211 AllocatorTy :: Layout => {
198212 let usize = self . cx . path_ident ( self . span , Ident :: from_str ( "usize" ) ) ;
@@ -202,18 +216,19 @@ impl<'a> AllocFnFactory<'a> {
202216 args. push ( self . cx . arg ( self . span , size, ty_usize. clone ( ) ) ) ;
203217 args. push ( self . cx . arg ( self . span , align, ty_usize) ) ;
204218
205- let layout_new = self . cx . path ( self . span , vec ! [
206- self . core,
207- Ident :: from_str( "alloc" ) ,
208- Ident :: from_str( "Layout" ) ,
209- Ident :: from_str( "from_size_align_unchecked" ) ,
210- ] ) ;
219+ let layout_new = self . cx . path (
220+ self . span ,
221+ vec ! [
222+ self . core,
223+ Ident :: from_str( "alloc" ) ,
224+ Ident :: from_str( "Layout" ) ,
225+ Ident :: from_str( "from_size_align_unchecked" ) ,
226+ ] ,
227+ ) ;
211228 let layout_new = self . cx . expr_path ( layout_new) ;
212229 let size = self . cx . expr_ident ( self . span , size) ;
213230 let align = self . cx . expr_ident ( self . span , align) ;
214- let layout = self . cx . expr_call ( self . span ,
215- layout_new,
216- vec ! [ size, align] ) ;
231+ let layout = self . cx . expr_call ( self . span , layout_new, vec ! [ size, align] ) ;
217232 layout
218233 }
219234
@@ -230,9 +245,7 @@ impl<'a> AllocFnFactory<'a> {
230245 self . cx . expr_ident ( self . span , ident)
231246 }
232247
233- AllocatorTy :: ResultPtr |
234- AllocatorTy :: Bang |
235- AllocatorTy :: Unit => {
248+ AllocatorTy :: ResultPtr | AllocatorTy :: Bang | AllocatorTy :: Unit => {
236249 panic ! ( "can't convert AllocatorTy to an argument" )
237250 }
238251 }
@@ -249,17 +262,11 @@ impl<'a> AllocFnFactory<'a> {
249262 ( self . ptr_u8 ( ) , expr)
250263 }
251264
252- AllocatorTy :: Bang => {
253- ( self . cx . ty ( self . span , TyKind :: Never ) , expr)
254- }
265+ AllocatorTy :: Bang => ( self . cx . ty ( self . span , TyKind :: Never ) , expr) ,
255266
256- AllocatorTy :: Unit => {
257- ( self . cx . ty ( self . span , TyKind :: Tup ( Vec :: new ( ) ) ) , expr)
258- }
267+ AllocatorTy :: Unit => ( self . cx . ty ( self . span , TyKind :: Tup ( Vec :: new ( ) ) ) , expr) ,
259268
260- AllocatorTy :: Layout |
261- AllocatorTy :: Usize |
262- AllocatorTy :: Ptr => {
269+ AllocatorTy :: Layout | AllocatorTy :: Usize | AllocatorTy :: Ptr => {
263270 panic ! ( "can't convert AllocatorTy to an output" )
264271 }
265272 }
@@ -277,11 +284,14 @@ impl<'a> AllocFnFactory<'a> {
277284 }
278285
279286 fn ptr_opaque ( & self ) -> P < Ty > {
280- let opaque = self . cx . path ( self . span , vec ! [
281- self . core,
282- Ident :: from_str( "alloc" ) ,
283- Ident :: from_str( "Opaque" ) ,
284- ] ) ;
287+ let opaque = self . cx . path (
288+ self . span ,
289+ vec ! [
290+ self . core,
291+ Ident :: from_str( "alloc" ) ,
292+ Ident :: from_str( "Opaque" ) ,
293+ ] ,
294+ ) ;
285295 let ty_opaque = self . cx . ty_path ( opaque) ;
286296 self . cx . ty_ptr ( self . span , ty_opaque, Mutability :: Mutable )
287297 }
0 commit comments