Skip to content

Commit 50ca0c6

Browse files
committed
Delegation: self parameter must be named exactly self.
1 parent ad6e587 commit 50ca0c6

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use rustc_errors::ErrorGuaranteed;
4747
use rustc_hir::def_id::DefId;
4848
use rustc_middle::span_bug;
4949
use rustc_middle::ty::{Asyncness, ResolverAstLowering};
50+
use rustc_span::symbol::kw;
5051
use rustc_span::{Ident, Span, Symbol};
5152
use {rustc_ast as ast, rustc_hir as hir};
5253

@@ -101,10 +102,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
101102
let sig_id = self.get_delegation_sig_id(item_id, delegation.id, span, is_in_trait_impl);
102103
match sig_id {
103104
Ok(sig_id) => {
105+
let is_method = self.is_method(sig_id, span);
104106
let (param_count, c_variadic) = self.param_count(sig_id);
105107
let decl = self.lower_delegation_decl(sig_id, param_count, c_variadic, span);
106108
let sig = self.lower_delegation_sig(sig_id, decl, span);
107-
let body_id = self.lower_delegation_body(delegation, param_count, span);
109+
let body_id = self.lower_delegation_body(delegation, is_method, param_count, span);
108110
let ident = self.lower_ident(delegation.ident);
109111
let generics = self.lower_delegation_generics(span);
110112
DelegationResults { body_id, sig, ident, generics }
@@ -234,10 +236,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
234236
hir::FnSig { decl, header, span }
235237
}
236238

237-
fn generate_param(&mut self, idx: usize, span: Span) -> (hir::Param<'hir>, NodeId) {
239+
fn generate_param(
240+
&mut self,
241+
is_method: bool,
242+
idx: usize,
243+
span: Span,
244+
) -> (hir::Param<'hir>, NodeId) {
238245
let pat_node_id = self.next_node_id();
239246
let pat_id = self.lower_node_id(pat_node_id);
240-
let ident = Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}")));
247+
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
248+
let name = if is_method && idx == 0 {
249+
kw::SelfLower
250+
} else {
251+
Symbol::intern(&format!("arg{idx}"))
252+
};
253+
let ident = Ident::with_dummy_span(name);
241254
let pat = self.arena.alloc(hir::Pat {
242255
hir_id: pat_id,
243256
kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None),
@@ -248,9 +261,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
248261
(hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id)
249262
}
250263

251-
fn generate_arg(&mut self, idx: usize, param_id: HirId, span: Span) -> hir::Expr<'hir> {
264+
fn generate_arg(
265+
&mut self,
266+
is_method: bool,
267+
idx: usize,
268+
param_id: HirId,
269+
span: Span,
270+
) -> hir::Expr<'hir> {
271+
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
272+
let name = if is_method && idx == 0 {
273+
kw::SelfLower
274+
} else {
275+
Symbol::intern(&format!("arg{idx}"))
276+
};
252277
let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment {
253-
ident: Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}"))),
278+
ident: Ident::with_dummy_span(name),
254279
hir_id: self.next_id(),
255280
res: Res::Local(param_id),
256281
args: None,
@@ -264,6 +289,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
264289
fn lower_delegation_body(
265290
&mut self,
266291
delegation: &Delegation,
292+
is_method: bool,
267293
param_count: usize,
268294
span: Span,
269295
) -> BodyId {
@@ -274,7 +300,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
274300
let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);
275301

276302
for idx in 0..param_count {
277-
let (param, pat_node_id) = this.generate_param(idx, span);
303+
let (param, pat_node_id) = this.generate_param(is_method, idx, span);
278304
parameters.push(param);
279305

280306
let arg = if let Some(block) = block
@@ -290,7 +316,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
290316
this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
291317
this.lower_target_expr(&block)
292318
} else {
293-
this.generate_arg(idx, param.pat.hir_id, span)
319+
this.generate_arg(is_method, idx, param.pat.hir_id, span)
294320
};
295321
args.push(arg);
296322
}

0 commit comments

Comments
 (0)