about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorCamille GILLOT <gillot.camille@gmail.com>2025-07-13 13:43:10 +0000
committerCamille GILLOT <gillot.camille@gmail.com>2025-07-13 13:50:00 +0000
commit50ca0c6ab8eb742c88ed24018ff78973a1f68d37 (patch)
tree6da866fc4bf6553a4bdc2890f5bd121711c84bd9 /compiler
parentad6e587e838b1197596063f7efca1c65978a985e (diff)
downloadrust-50ca0c6ab8eb742c88ed24018ff78973a1f68d37.tar.gz
rust-50ca0c6ab8eb742c88ed24018ff78973a1f68d37.zip
Delegation: self parameter must be named exactly `self`.
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_ast_lowering/src/delegation.rs40
1 files changed, 33 insertions, 7 deletions
diff --git a/compiler/rustc_ast_lowering/src/delegation.rs b/compiler/rustc_ast_lowering/src/delegation.rs
index 42d25b512f5..a8a8b2d6e1b 100644
--- a/compiler/rustc_ast_lowering/src/delegation.rs
+++ b/compiler/rustc_ast_lowering/src/delegation.rs
@@ -47,6 +47,7 @@ use rustc_errors::ErrorGuaranteed;
 use rustc_hir::def_id::DefId;
 use rustc_middle::span_bug;
 use rustc_middle::ty::{Asyncness, ResolverAstLowering};
+use rustc_span::symbol::kw;
 use rustc_span::{Ident, Span, Symbol};
 use {rustc_ast as ast, rustc_hir as hir};
 
@@ -101,10 +102,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
         let sig_id = self.get_delegation_sig_id(item_id, delegation.id, span, is_in_trait_impl);
         match sig_id {
             Ok(sig_id) => {
+                let is_method = self.is_method(sig_id, span);
                 let (param_count, c_variadic) = self.param_count(sig_id);
                 let decl = self.lower_delegation_decl(sig_id, param_count, c_variadic, span);
                 let sig = self.lower_delegation_sig(sig_id, decl, span);
-                let body_id = self.lower_delegation_body(delegation, param_count, span);
+                let body_id = self.lower_delegation_body(delegation, is_method, param_count, span);
                 let ident = self.lower_ident(delegation.ident);
                 let generics = self.lower_delegation_generics(span);
                 DelegationResults { body_id, sig, ident, generics }
@@ -234,10 +236,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
         hir::FnSig { decl, header, span }
     }
 
-    fn generate_param(&mut self, idx: usize, span: Span) -> (hir::Param<'hir>, NodeId) {
+    fn generate_param(
+        &mut self,
+        is_method: bool,
+        idx: usize,
+        span: Span,
+    ) -> (hir::Param<'hir>, NodeId) {
         let pat_node_id = self.next_node_id();
         let pat_id = self.lower_node_id(pat_node_id);
-        let ident = Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}")));
+        // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
+        let name = if is_method && idx == 0 {
+            kw::SelfLower
+        } else {
+            Symbol::intern(&format!("arg{idx}"))
+        };
+        let ident = Ident::with_dummy_span(name);
         let pat = self.arena.alloc(hir::Pat {
             hir_id: pat_id,
             kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None),
@@ -248,9 +261,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
         (hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id)
     }
 
-    fn generate_arg(&mut self, idx: usize, param_id: HirId, span: Span) -> hir::Expr<'hir> {
+    fn generate_arg(
+        &mut self,
+        is_method: bool,
+        idx: usize,
+        param_id: HirId,
+        span: Span,
+    ) -> hir::Expr<'hir> {
+        // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
+        let name = if is_method && idx == 0 {
+            kw::SelfLower
+        } else {
+            Symbol::intern(&format!("arg{idx}"))
+        };
         let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment {
-            ident: Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}"))),
+            ident: Ident::with_dummy_span(name),
             hir_id: self.next_id(),
             res: Res::Local(param_id),
             args: None,
@@ -264,6 +289,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
     fn lower_delegation_body(
         &mut self,
         delegation: &Delegation,
+        is_method: bool,
         param_count: usize,
         span: Span,
     ) -> BodyId {
@@ -274,7 +300,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
             let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);
 
             for idx in 0..param_count {
-                let (param, pat_node_id) = this.generate_param(idx, span);
+                let (param, pat_node_id) = this.generate_param(is_method, idx, span);
                 parameters.push(param);
 
                 let arg = if let Some(block) = block
@@ -290,7 +316,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
                     this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
                     this.lower_target_expr(&block)
                 } else {
-                    this.generate_arg(idx, param.pat.hir_id, span)
+                    this.generate_arg(is_method, idx, param.pat.hir_id, span)
                 };
                 args.push(arg);
             }