about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@gmail.com>2023-08-06 07:20:31 -0700
committerDavid Tolnay <dtolnay@gmail.com>2023-08-06 07:36:05 -0700
commit704aa56ba0f3b42499f97b14b2f7a2bfb0761b0e (patch)
tree43bb98183e51037ea2201f70b24044698454b33c /compiler
parentbc720ad36b74c428b210c71c66e4490cec12b023 (diff)
downloadrust-704aa56ba0f3b42499f97b14b2f7a2bfb0761b0e.tar.gz
rust-704aa56ba0f3b42499f97b14b2f7a2bfb0761b0e.zip
Generate better function argument names in global_allocator expansion
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_ast/src/expand/allocator.rs22
-rw-r--r--compiler/rustc_builtin_macros/src/global_allocator.rs33
-rw-r--r--compiler/rustc_codegen_cranelift/src/allocator.rs4
-rw-r--r--compiler/rustc_codegen_gcc/src/allocator.rs4
-rw-r--r--compiler/rustc_codegen_llvm/src/allocator.rs4
5 files changed, 37 insertions, 30 deletions
diff --git a/compiler/rustc_ast/src/expand/allocator.rs b/compiler/rustc_ast/src/expand/allocator.rs
index e87f6e820a1..f825b10f489 100644
--- a/compiler/rustc_ast/src/expand/allocator.rs
+++ b/compiler/rustc_ast/src/expand/allocator.rs
@@ -33,29 +33,41 @@ pub enum AllocatorTy {
 
 pub struct AllocatorMethod {
     pub name: Symbol,
-    pub inputs: &'static [AllocatorTy],
+    pub inputs: &'static [AllocatorMethodInput],
     pub output: AllocatorTy,
 }
 
+pub struct AllocatorMethodInput {
+    pub name: &'static str,
+    pub ty: AllocatorTy,
+}
+
 pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
     AllocatorMethod {
         name: sym::alloc,
-        inputs: &[AllocatorTy::Layout],
+        inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],
         output: AllocatorTy::ResultPtr,
     },
     AllocatorMethod {
         name: sym::dealloc,
-        inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout],
+        inputs: &[
+            AllocatorMethodInput { name: "ptr", ty: AllocatorTy::Ptr },
+            AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout },
+        ],
         output: AllocatorTy::Unit,
     },
     AllocatorMethod {
         name: sym::realloc,
-        inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout, AllocatorTy::Usize],
+        inputs: &[
+            AllocatorMethodInput { name: "ptr", ty: AllocatorTy::Ptr },
+            AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout },
+            AllocatorMethodInput { name: "new_size", ty: AllocatorTy::Usize },
+        ],
         output: AllocatorTy::ResultPtr,
     },
     AllocatorMethod {
         name: sym::alloc_zeroed,
-        inputs: &[AllocatorTy::Layout],
+        inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],
         output: AllocatorTy::ResultPtr,
     },
 ];
diff --git a/compiler/rustc_builtin_macros/src/global_allocator.rs b/compiler/rustc_builtin_macros/src/global_allocator.rs
index 7bded6b1d82..1bec00add55 100644
--- a/compiler/rustc_builtin_macros/src/global_allocator.rs
+++ b/compiler/rustc_builtin_macros/src/global_allocator.rs
@@ -2,7 +2,7 @@ use crate::util::check_builtin_macro_attribute;
 
 use crate::errors;
 use rustc_ast::expand::allocator::{
-    global_fn_name, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS,
+    global_fn_name, AllocatorMethod, AllocatorMethodInput, AllocatorTy, ALLOCATOR_METHODS,
 };
 use rustc_ast::ptr::P;
 use rustc_ast::{self as ast, AttrVec, Expr, FnHeader, FnSig, Generics, Param, StmtKind};
@@ -70,13 +70,7 @@ struct AllocFnFactory<'a, 'b> {
 impl AllocFnFactory<'_, '_> {
     fn allocator_fn(&self, method: &AllocatorMethod) -> Stmt {
         let mut abi_args = ThinVec::new();
-        let mut i = 0;
-        let mut mk = || {
-            let name = Ident::from_str_and_span(&format!("arg{i}"), self.span);
-            i += 1;
-            name
-        };
-        let args = method.inputs.iter().map(|ty| self.arg_ty(ty, &mut abi_args, &mut mk)).collect();
+        let args = method.inputs.iter().map(|input| self.arg_ty(input, &mut abi_args)).collect();
         let result = self.call_allocator(method.name, args);
         let output_ty = self.ret_ty(&method.output);
         let decl = self.cx.fn_decl(abi_args, ast::FnRetTy::Ty(output_ty));
@@ -113,18 +107,19 @@ impl AllocFnFactory<'_, '_> {
         thin_vec![self.cx.attr_word(sym::rustc_std_internal_symbol, self.span)]
     }
 
-    fn arg_ty(
-        &self,
-        ty: &AllocatorTy,
-        args: &mut ThinVec<Param>,
-        ident: &mut dyn FnMut() -> Ident,
-    ) -> P<Expr> {
-        match *ty {
+    fn arg_ty(&self, input: &AllocatorMethodInput, args: &mut ThinVec<Param>) -> P<Expr> {
+        match input.ty {
             AllocatorTy::Layout => {
+                // If an allocator method is ever introduced having multiple
+                // Layout arguments, these argument names need to be
+                // disambiguated somehow. Currently the generated code would
+                // fail to compile with "identifier is bound more than once in
+                // this parameter list".
+                let size = Ident::from_str_and_span("size", self.span);
+                let align = Ident::from_str_and_span("align", self.span);
+
                 let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
                 let ty_usize = self.cx.ty_path(usize);
-                let size = ident();
-                let align = ident();
                 args.push(self.cx.param(self.span, size, ty_usize.clone()));
                 args.push(self.cx.param(self.span, align, ty_usize));
 
@@ -138,13 +133,13 @@ impl AllocFnFactory<'_, '_> {
             }
 
             AllocatorTy::Ptr => {
-                let ident = ident();
+                let ident = Ident::from_str_and_span(input.name, self.span);
                 args.push(self.cx.param(self.span, ident, self.ptr_u8()));
                 self.cx.expr_ident(self.span, ident)
             }
 
             AllocatorTy::Usize => {
-                let ident = ident();
+                let ident = Ident::from_str_and_span(input.name, self.span);
                 args.push(self.cx.param(self.span, ident, self.usize()));
                 self.cx.expr_ident(self.span, ident)
             }
diff --git a/compiler/rustc_codegen_cranelift/src/allocator.rs b/compiler/rustc_codegen_cranelift/src/allocator.rs
index e92280b26b0..4e4c595de82 100644
--- a/compiler/rustc_codegen_cranelift/src/allocator.rs
+++ b/compiler/rustc_codegen_cranelift/src/allocator.rs
@@ -39,8 +39,8 @@ fn codegen_inner(
     if kind == AllocatorKind::Default {
         for method in ALLOCATOR_METHODS {
             let mut arg_tys = Vec::with_capacity(method.inputs.len());
-            for ty in method.inputs.iter() {
-                match *ty {
+            for input in method.inputs.iter() {
+                match input.ty {
                     AllocatorTy::Layout => {
                         arg_tys.push(usize_ty); // size
                         arg_tys.push(usize_ty); // align
diff --git a/compiler/rustc_codegen_gcc/src/allocator.rs b/compiler/rustc_codegen_gcc/src/allocator.rs
index 13f88192bbc..edd7ab722f6 100644
--- a/compiler/rustc_codegen_gcc/src/allocator.rs
+++ b/compiler/rustc_codegen_gcc/src/allocator.rs
@@ -27,8 +27,8 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
     if kind == AllocatorKind::Default {
         for method in ALLOCATOR_METHODS {
             let mut types = Vec::with_capacity(method.inputs.len());
-            for ty in method.inputs.iter() {
-                match *ty {
+            for input in method.inputs.iter() {
+                match input.ty {
                     AllocatorTy::Layout => {
                         types.push(usize);
                         types.push(usize);
diff --git a/compiler/rustc_codegen_llvm/src/allocator.rs b/compiler/rustc_codegen_llvm/src/allocator.rs
index ca123334fca..8bb93025c45 100644
--- a/compiler/rustc_codegen_llvm/src/allocator.rs
+++ b/compiler/rustc_codegen_llvm/src/allocator.rs
@@ -34,8 +34,8 @@ pub(crate) unsafe fn codegen(
     if kind == AllocatorKind::Default {
         for method in ALLOCATOR_METHODS {
             let mut args = Vec::with_capacity(method.inputs.len());
-            for ty in method.inputs.iter() {
-                match *ty {
+            for input in method.inputs.iter() {
+                match input.ty {
                     AllocatorTy::Layout => {
                         args.push(usize); // size
                         args.push(usize); // align