Skip to content

Commit 1c43cab

Browse files
authored
Rollup merge of #100232 - cjgillot:no-desugar-methodcall, r=nagisa
Do not consider method call receiver as an argument in AST. Fixes #73663
2 parents 32bd147 + 69205db commit 1c43cab

File tree

20 files changed

+109
-57
lines changed

20 files changed

+109
-57
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,14 +1338,13 @@ pub enum ExprKind {
13381338
///
13391339
/// The `PathSegment` represents the method name and its generic arguments
13401340
/// (within the angle brackets).
1341-
/// The first element of the vector of an `Expr` is the expression that evaluates
1342-
/// to the object on which the method is being called on (the receiver),
1343-
/// and the remaining elements are the rest of the arguments.
1344-
/// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
1345-
/// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, [x, a, b, c, d])`.
1341+
/// The standalone `Expr` is the receiver expression.
1342+
/// The vector of `Expr` is the arguments.
1343+
/// `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
1344+
/// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, x, [a, b, c, d])`.
13461345
/// This `Span` is the span of the function, without the dot and receiver
13471346
/// (e.g. `foo(a, b)` in `x.foo(a, b)`
1348-
MethodCall(PathSegment, Vec<P<Expr>>, Span),
1347+
MethodCall(PathSegment, P<Expr>, Vec<P<Expr>>, Span),
13491348
/// A tuple (e.g., `(a, b, c, d)`).
13501349
Tup(Vec<P<Expr>>),
13511350
/// A binary operation (e.g., `a + b`, `a * b`).

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,10 +1302,11 @@ pub fn noop_visit_expr<T: MutVisitor>(
13021302
vis.visit_expr(f);
13031303
visit_exprs(args, vis);
13041304
}
1305-
ExprKind::MethodCall(PathSegment { ident, id, args }, exprs, span) => {
1305+
ExprKind::MethodCall(PathSegment { ident, id, args }, receiver, exprs, span) => {
13061306
vis.visit_ident(ident);
13071307
vis.visit_id(id);
13081308
visit_opt(args, |args| vis.visit_generic_args(args));
1309+
vis.visit_expr(receiver);
13091310
visit_exprs(exprs, vis);
13101311
vis.visit_span(span);
13111312
}

compiler/rustc_ast/src/util/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,9 @@ pub fn contains_exterior_struct_lit(value: &ast::Expr) -> bool {
396396
contains_exterior_struct_lit(&x)
397397
}
398398

399-
ast::ExprKind::MethodCall(.., ref exprs, _) => {
399+
ast::ExprKind::MethodCall(_, ref receiver, _, _) => {
400400
// X { y: 1 }.bar(...)
401-
contains_exterior_struct_lit(&exprs[0])
401+
contains_exterior_struct_lit(&receiver)
402402
}
403403

404404
_ => false,

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,8 +813,9 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
813813
visitor.visit_expr(callee_expression);
814814
walk_list!(visitor, visit_expr, arguments);
815815
}
816-
ExprKind::MethodCall(ref segment, ref arguments, _span) => {
816+
ExprKind::MethodCall(ref segment, ref receiver, ref arguments, _span) => {
817817
visitor.visit_path_segment(expression.span, segment);
818+
visitor.visit_expr(receiver);
818819
walk_list!(visitor, visit_expr, arguments);
819820
}
820821
ExprKind::Binary(_, ref left_expression, ref right_expression) => {

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
6262
hir::ExprKind::Call(f, self.lower_exprs(args))
6363
}
6464
}
65-
ExprKind::MethodCall(ref seg, ref args, span) => {
65+
ExprKind::MethodCall(ref seg, ref receiver, ref args, span) => {
6666
let hir_seg = self.arena.alloc(self.lower_path_segment(
6767
e.span,
6868
seg,
6969
ParamMode::Optional,
7070
ParenthesizedGenericArgs::Err,
7171
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
7272
));
73-
let args = self.lower_exprs(args);
73+
let args = self.arena.alloc_from_iter(
74+
[&*receiver].into_iter().chain(args.iter()).map(|x| self.lower_expr_mut(x)),
75+
);
7476
hir::ExprKind::MethodCall(hir_seg, args, self.lower_span(span))
7577
}
7678
ExprKind::Binary(binop, ref lhs, ref rhs) => {

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,13 @@ impl<'a> State<'a> {
193193
self.print_call_post(args)
194194
}
195195

196-
fn print_expr_method_call(&mut self, segment: &ast::PathSegment, args: &[P<ast::Expr>]) {
197-
let base_args = &args[1..];
198-
self.print_expr_maybe_paren(&args[0], parser::PREC_POSTFIX);
196+
fn print_expr_method_call(
197+
&mut self,
198+
segment: &ast::PathSegment,
199+
receiver: &ast::Expr,
200+
base_args: &[P<ast::Expr>],
201+
) {
202+
self.print_expr_maybe_paren(receiver, parser::PREC_POSTFIX);
199203
self.word(".");
200204
self.print_ident(segment.ident);
201205
if let Some(ref args) = segment.args {
@@ -303,8 +307,8 @@ impl<'a> State<'a> {
303307
ast::ExprKind::Call(ref func, ref args) => {
304308
self.print_expr_call(func, &args);
305309
}
306-
ast::ExprKind::MethodCall(ref segment, ref args, _) => {
307-
self.print_expr_method_call(segment, &args);
310+
ast::ExprKind::MethodCall(ref segment, ref receiver, ref args, _) => {
311+
self.print_expr_method_call(segment, &receiver, &args);
308312
}
309313
ast::ExprKind::Binary(op, ref lhs, ref rhs) => {
310314
self.print_expr_binary(op, lhs, rhs);

compiler/rustc_builtin_macros/src/assert/context.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ impl<'cx, 'a> Context<'cx, 'a> {
240240
self.manage_cond_expr(prefix);
241241
self.manage_cond_expr(suffix);
242242
}
243-
ExprKind::MethodCall(_, ref mut local_exprs, _) => {
244-
for local_expr in local_exprs.iter_mut().skip(1) {
243+
ExprKind::MethodCall(_, _,ref mut local_exprs, _) => {
244+
for local_expr in local_exprs.iter_mut() {
245245
self.manage_cond_expr(local_expr);
246246
}
247247
}
@@ -377,14 +377,12 @@ impl<'cx, 'a> Context<'cx, 'a> {
377377
id: DUMMY_NODE_ID,
378378
ident: Ident::new(sym::try_capture, self.span),
379379
},
380-
vec![
381-
expr_paren(self.cx, self.span, self.cx.expr_addr_of(self.span, wrapper)),
382-
expr_addr_of_mut(
383-
self.cx,
384-
self.span,
385-
self.cx.expr_path(Path::from_ident(capture)),
386-
),
387-
],
380+
expr_paren(self.cx, self.span, self.cx.expr_addr_of(self.span, wrapper)),
381+
vec![expr_addr_of_mut(
382+
self.cx,
383+
self.span,
384+
self.cx.expr_path(Path::from_ident(capture)),
385+
)],
388386
self.span,
389387
))
390388
.add_trailing_semicolon();
@@ -442,10 +440,11 @@ fn expr_addr_of_mut(cx: &ExtCtxt<'_>, sp: Span, e: P<Expr>) -> P<Expr> {
442440
fn expr_method_call(
443441
cx: &ExtCtxt<'_>,
444442
path: PathSegment,
443+
receiver: P<Expr>,
445444
args: Vec<P<Expr>>,
446445
span: Span,
447446
) -> P<Expr> {
448-
cx.expr(span, ExprKind::MethodCall(path, args, span))
447+
cx.expr(span, ExprKind::MethodCall(path, receiver, args, span))
449448
}
450449

451450
fn expr_paren(cx: &ExtCtxt<'_>, sp: Span, e: P<Expr>) -> P<Expr> {

compiler/rustc_lint/src/unused.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,7 @@ trait UnusedDelimLint {
608608
ref call_or_other => {
609609
let (args_to_check, ctx) = match *call_or_other {
610610
Call(_, ref args) => (&args[..], UnusedDelimsCtx::FunctionArg),
611-
// first "argument" is self (which sometimes needs delims)
612-
MethodCall(_, ref args, _) => (&args[1..], UnusedDelimsCtx::MethodArg),
611+
MethodCall(_, _, ref args, _) => (&args[..], UnusedDelimsCtx::MethodArg),
613612
// actual catch-all arm
614613
_ => {
615614
return;

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ impl<'a> Parser<'a> {
850850
ExprKind::Index(_, _) => "indexing",
851851
ExprKind::Try(_) => "`?`",
852852
ExprKind::Field(_, _) => "a field access",
853-
ExprKind::MethodCall(_, _, _) => "a method call",
853+
ExprKind::MethodCall(_, _, _, _) => "a method call",
854854
ExprKind::Call(_, _) => "a function call",
855855
ExprKind::Await(_) => "`.await`",
856856
ExprKind::Err => return Ok(with_postfix),
@@ -1280,12 +1280,14 @@ impl<'a> Parser<'a> {
12801280

12811281
if self.check(&token::OpenDelim(Delimiter::Parenthesis)) {
12821282
// Method call `expr.f()`
1283-
let mut args = self.parse_paren_expr_seq()?;
1284-
args.insert(0, self_arg);
1285-
1283+
let args = self.parse_paren_expr_seq()?;
12861284
let fn_span = fn_span_lo.to(self.prev_token.span);
12871285
let span = lo.to(self.prev_token.span);
1288-
Ok(self.mk_expr(span, ExprKind::MethodCall(segment, args, fn_span), AttrVec::new()))
1286+
Ok(self.mk_expr(
1287+
span,
1288+
ExprKind::MethodCall(segment, self_arg, args, fn_span),
1289+
AttrVec::new(),
1290+
))
12891291
} else {
12901292
// Field access `expr.f`
12911293
if let Some(args) = segment.args {

compiler/rustc_resolve/src/late.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3796,9 +3796,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
37963796
ExprKind::Field(ref subexpression, _) => {
37973797
self.resolve_expr(subexpression, Some(expr));
37983798
}
3799-
ExprKind::MethodCall(ref segment, ref arguments, _) => {
3800-
let mut arguments = arguments.iter();
3801-
self.resolve_expr(arguments.next().unwrap(), Some(expr));
3799+
ExprKind::MethodCall(ref segment, ref receiver, ref arguments, _) => {
3800+
self.resolve_expr(receiver, Some(expr));
38023801
for argument in arguments {
38033802
self.resolve_expr(argument, None);
38043803
}

0 commit comments

Comments
 (0)