@@ -2,7 +2,7 @@ use crate::util::check_builtin_macro_attribute;
22
33use crate :: errors;
44use rustc_ast:: expand:: allocator:: {
5- global_fn_name, AllocatorMethod , AllocatorTy , ALLOCATOR_METHODS ,
5+ global_fn_name, AllocatorMethod , AllocatorMethodInput , AllocatorTy , ALLOCATOR_METHODS ,
66} ;
77use rustc_ast:: ptr:: P ;
88use rustc_ast:: { self as ast, AttrVec , Expr , FnHeader , FnSig , Generics , Param , StmtKind } ;
@@ -70,13 +70,7 @@ struct AllocFnFactory<'a, 'b> {
7070impl AllocFnFactory < ' _ , ' _ > {
7171 fn allocator_fn ( & self , method : & AllocatorMethod ) -> Stmt {
7272 let mut abi_args = ThinVec :: new ( ) ;
73- let mut i = 0 ;
74- let mut mk = || {
75- let name = Ident :: from_str_and_span ( & format ! ( "arg{i}" ) , self . span ) ;
76- i += 1 ;
77- name
78- } ;
79- let args = method. inputs . iter ( ) . map ( |ty| self . arg_ty ( ty, & mut abi_args, & mut mk) ) . collect ( ) ;
73+ let args = method. inputs . iter ( ) . map ( |input| self . arg_ty ( input, & mut abi_args) ) . collect ( ) ;
8074 let result = self . call_allocator ( method. name , args) ;
8175 let output_ty = self . ret_ty ( & method. output ) ;
8276 let decl = self . cx . fn_decl ( abi_args, ast:: FnRetTy :: Ty ( output_ty) ) ;
@@ -113,18 +107,19 @@ impl AllocFnFactory<'_, '_> {
113107 thin_vec ! [ self . cx. attr_word( sym:: rustc_std_internal_symbol, self . span) ]
114108 }
115109
116- fn arg_ty (
117- & self ,
118- ty : & AllocatorTy ,
119- args : & mut ThinVec < Param > ,
120- ident : & mut dyn FnMut ( ) -> Ident ,
121- ) -> P < Expr > {
122- match * ty {
110+ fn arg_ty ( & self , input : & AllocatorMethodInput , args : & mut ThinVec < Param > ) -> P < Expr > {
111+ match input. ty {
123112 AllocatorTy :: Layout => {
113+ // If an allocator method is ever introduced having multiple
114+ // Layout arguments, these argument names need to be
115+ // disambiguated somehow. Currently the generated code would
116+ // fail to compile with "identifier is bound more than once in
117+ // this parameter list".
118+ let size = Ident :: from_str_and_span ( "size" , self . span ) ;
119+ let align = Ident :: from_str_and_span ( "align" , self . span ) ;
120+
124121 let usize = self . cx . path_ident ( self . span , Ident :: new ( sym:: usize, self . span ) ) ;
125122 let ty_usize = self . cx . ty_path ( usize) ;
126- let size = ident ( ) ;
127- let align = ident ( ) ;
128123 args. push ( self . cx . param ( self . span , size, ty_usize. clone ( ) ) ) ;
129124 args. push ( self . cx . param ( self . span , align, ty_usize) ) ;
130125
@@ -138,13 +133,13 @@ impl AllocFnFactory<'_, '_> {
138133 }
139134
140135 AllocatorTy :: Ptr => {
141- let ident = ident ( ) ;
136+ let ident = Ident :: from_str_and_span ( input . name , self . span ) ;
142137 args. push ( self . cx . param ( self . span , ident, self . ptr_u8 ( ) ) ) ;
143138 self . cx . expr_ident ( self . span , ident)
144139 }
145140
146141 AllocatorTy :: Usize => {
147- let ident = ident ( ) ;
142+ let ident = Ident :: from_str_and_span ( input . name , self . span ) ;
148143 args. push ( self . cx . param ( self . span , ident, self . usize ( ) ) ) ;
149144 self . cx . expr_ident ( self . span , ident)
150145 }
0 commit comments