@@ -2,7 +2,7 @@ use crate::util::check_builtin_macro_attribute;
2
2
3
3
use crate :: errors;
4
4
use rustc_ast:: expand:: allocator:: {
5
- global_fn_name, AllocatorMethod , AllocatorTy , ALLOCATOR_METHODS ,
5
+ global_fn_name, AllocatorMethod , AllocatorMethodInput , AllocatorTy , ALLOCATOR_METHODS ,
6
6
} ;
7
7
use rustc_ast:: ptr:: P ;
8
8
use rustc_ast:: { self as ast, AttrVec , Expr , FnHeader , FnSig , Generics , Param , StmtKind } ;
@@ -70,13 +70,7 @@ struct AllocFnFactory<'a, 'b> {
70
70
impl AllocFnFactory < ' _ , ' _ > {
71
71
fn allocator_fn ( & self , method : & AllocatorMethod ) -> Stmt {
72
72
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 ( ) ;
80
74
let result = self . call_allocator ( method. name , args) ;
81
75
let output_ty = self . ret_ty ( & method. output ) ;
82
76
let decl = self . cx . fn_decl ( abi_args, ast:: FnRetTy :: Ty ( output_ty) ) ;
@@ -113,18 +107,19 @@ impl AllocFnFactory<'_, '_> {
113
107
thin_vec ! [ self . cx. attr_word( sym:: rustc_std_internal_symbol, self . span) ]
114
108
}
115
109
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 {
123
112
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
+
124
121
let usize = self . cx . path_ident ( self . span , Ident :: new ( sym:: usize, self . span ) ) ;
125
122
let ty_usize = self . cx . ty_path ( usize) ;
126
- let size = ident ( ) ;
127
- let align = ident ( ) ;
128
123
args. push ( self . cx . param ( self . span , size, ty_usize. clone ( ) ) ) ;
129
124
args. push ( self . cx . param ( self . span , align, ty_usize) ) ;
130
125
@@ -138,13 +133,13 @@ impl AllocFnFactory<'_, '_> {
138
133
}
139
134
140
135
AllocatorTy :: Ptr => {
141
- let ident = ident ( ) ;
136
+ let ident = Ident :: from_str_and_span ( input . name , self . span ) ;
142
137
args. push ( self . cx . param ( self . span , ident, self . ptr_u8 ( ) ) ) ;
143
138
self . cx . expr_ident ( self . span , ident)
144
139
}
145
140
146
141
AllocatorTy :: Usize => {
147
- let ident = ident ( ) ;
142
+ let ident = Ident :: from_str_and_span ( input . name , self . span ) ;
148
143
args. push ( self . cx . param ( self . span , ident, self . usize ( ) ) ) ;
149
144
self . cx . expr_ident ( self . span , ident)
150
145
}
0 commit comments