Skip to content

Commit 704aa56

Browse files
committed
Generate better function argument names in global_allocator expansion
1 parent bc720ad commit 704aa56

File tree

5 files changed

+37
-30
lines changed

5 files changed

+37
-30
lines changed

compiler/rustc_ast/src/expand/allocator.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,41 @@ pub enum AllocatorTy {
3333

3434
pub struct AllocatorMethod {
3535
pub name: Symbol,
36-
pub inputs: &'static [AllocatorTy],
36+
pub inputs: &'static [AllocatorMethodInput],
3737
pub output: AllocatorTy,
3838
}
3939

40+
pub struct AllocatorMethodInput {
41+
pub name: &'static str,
42+
pub ty: AllocatorTy,
43+
}
44+
4045
pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
4146
AllocatorMethod {
4247
name: sym::alloc,
43-
inputs: &[AllocatorTy::Layout],
48+
inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],
4449
output: AllocatorTy::ResultPtr,
4550
},
4651
AllocatorMethod {
4752
name: sym::dealloc,
48-
inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout],
53+
inputs: &[
54+
AllocatorMethodInput { name: "ptr", ty: AllocatorTy::Ptr },
55+
AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout },
56+
],
4957
output: AllocatorTy::Unit,
5058
},
5159
AllocatorMethod {
5260
name: sym::realloc,
53-
inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout, AllocatorTy::Usize],
61+
inputs: &[
62+
AllocatorMethodInput { name: "ptr", ty: AllocatorTy::Ptr },
63+
AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout },
64+
AllocatorMethodInput { name: "new_size", ty: AllocatorTy::Usize },
65+
],
5466
output: AllocatorTy::ResultPtr,
5567
},
5668
AllocatorMethod {
5769
name: sym::alloc_zeroed,
58-
inputs: &[AllocatorTy::Layout],
70+
inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],
5971
output: AllocatorTy::ResultPtr,
6072
},
6173
];

compiler/rustc_builtin_macros/src/global_allocator.rs

+14-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::util::check_builtin_macro_attribute;
22

33
use crate::errors;
44
use rustc_ast::expand::allocator::{
5-
global_fn_name, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS,
5+
global_fn_name, AllocatorMethod, AllocatorMethodInput, AllocatorTy, ALLOCATOR_METHODS,
66
};
77
use rustc_ast::ptr::P;
88
use rustc_ast::{self as ast, AttrVec, Expr, FnHeader, FnSig, Generics, Param, StmtKind};
@@ -70,13 +70,7 @@ struct AllocFnFactory<'a, 'b> {
7070
impl 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
}

compiler/rustc_codegen_cranelift/src/allocator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ fn codegen_inner(
3939
if kind == AllocatorKind::Default {
4040
for method in ALLOCATOR_METHODS {
4141
let mut arg_tys = Vec::with_capacity(method.inputs.len());
42-
for ty in method.inputs.iter() {
43-
match *ty {
42+
for input in method.inputs.iter() {
43+
match input.ty {
4444
AllocatorTy::Layout => {
4545
arg_tys.push(usize_ty); // size
4646
arg_tys.push(usize_ty); // align

compiler/rustc_codegen_gcc/src/allocator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
2727
if kind == AllocatorKind::Default {
2828
for method in ALLOCATOR_METHODS {
2929
let mut types = Vec::with_capacity(method.inputs.len());
30-
for ty in method.inputs.iter() {
31-
match *ty {
30+
for input in method.inputs.iter() {
31+
match input.ty {
3232
AllocatorTy::Layout => {
3333
types.push(usize);
3434
types.push(usize);

compiler/rustc_codegen_llvm/src/allocator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ pub(crate) unsafe fn codegen(
3434
if kind == AllocatorKind::Default {
3535
for method in ALLOCATOR_METHODS {
3636
let mut args = Vec::with_capacity(method.inputs.len());
37-
for ty in method.inputs.iter() {
38-
match *ty {
37+
for input in method.inputs.iter() {
38+
match input.ty {
3939
AllocatorTy::Layout => {
4040
args.push(usize); // size
4141
args.push(usize); // align

0 commit comments

Comments
 (0)