Skip to content

Commit b44197a

Browse files
committed
Auto merge of rust-lang#101261 - TaKO8Ki:separate-receiver-from-arguments-in-hir, r=cjgillot
Separate the receiver from arguments in HIR Related to rust-lang#100232 cc `@cjgillot`
2 parents 2dc703f + 9cde34e commit b44197a

File tree

140 files changed

+815
-720
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+815
-720
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
6868
ParenthesizedGenericArgs::Err,
6969
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
7070
));
71-
let args = self.arena.alloc_from_iter(
72-
[&*receiver].into_iter().chain(args.iter()).map(|x| self.lower_expr_mut(x)),
73-
);
74-
hir::ExprKind::MethodCall(hir_seg, args, self.lower_span(span))
71+
let receiver = self.lower_expr(receiver);
72+
let args =
73+
self.arena.alloc_from_iter(args.iter().map(|x| self.lower_expr_mut(x)));
74+
hir::ExprKind::MethodCall(hir_seg, receiver, args, self.lower_span(span))
7575
}
7676
ExprKind::Binary(binop, ref lhs, ref rhs) => {
7777
let binop = self.lower_binop(binop);

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
711711
Applicability::MachineApplicable,
712712
);
713713
self.suggested = true;
714-
} else if let hir::ExprKind::MethodCall(_path, args @ [_, ..], sp) = expr.kind
715-
&& let hir::ExprKind::Index(val, index) = args[0].kind
714+
} else if let hir::ExprKind::MethodCall(_path, receiver, _, sp) = expr.kind
715+
&& let hir::ExprKind::Index(val, index) = receiver.kind
716716
&& expr.span == self.assign_span
717717
{
718718
// val[index].path(args..);
@@ -724,7 +724,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
724724
".get_mut(".to_string(),
725725
),
726726
(
727-
index.span.shrink_to_hi().with_hi(args[0].span.hi()),
727+
index.span.shrink_to_hi().with_hi(receiver.span.hi()),
728728
").map(|val| val".to_string(),
729729
),
730730
(sp.shrink_to_hi(), ")".to_string()),
@@ -911,11 +911,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
911911
[
912912
Expr {
913913
kind:
914-
MethodCall(
915-
path_segment,
916-
_args,
917-
span,
918-
),
914+
MethodCall(path_segment, _, _, span),
919915
hir_id,
920916
..
921917
},

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -900,14 +900,13 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
900900
let mut closure_span = None::<rustc_span::Span>;
901901
match expr.kind {
902902
hir::ExprKind::MethodCall(.., args, _) => {
903-
// only the first closre parameter of the method. args[0] is MethodCall PathSegment
904-
for i in 1..args.len() {
903+
for arg in args {
905904
if let hir::ExprKind::Closure(hir::Closure {
906905
capture_clause: hir::CaptureBy::Ref,
907906
..
908-
}) = args[i].kind
907+
}) = arg.kind
909908
{
910-
closure_span = Some(args[i].span.shrink_to_lo());
909+
closure_span = Some(arg.span.shrink_to_lo());
911910
break;
912911
}
913912
}

compiler/rustc_hir/src/hir.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,19 +1876,19 @@ pub enum ExprKind<'hir> {
18761876
///
18771877
/// The `PathSegment` represents the method name and its generic arguments
18781878
/// (within the angle brackets).
1879-
/// The first element of the `&[Expr]` is the expression that evaluates
1879+
/// The `&Expr` is the expression that evaluates
18801880
/// to the object on which the method is being called on (the receiver),
1881-
/// and the remaining elements are the rest of the arguments.
1881+
/// and the `&[Expr]` is the rest of the arguments.
18821882
/// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
1883-
/// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, [x, a, b, c, d], span)`.
1883+
/// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, x, [a, b, c, d], span)`.
18841884
/// The final `Span` represents the span of the function and arguments
18851885
/// (e.g. `foo::<Bar, Baz>(a, b, c, d)` in `x.foo::<Bar, Baz>(a, b, c, d)`
18861886
///
18871887
/// To resolve the called method to a `DefId`, call [`type_dependent_def_id`] with
18881888
/// the `hir_id` of the `MethodCall` node itself.
18891889
///
18901890
/// [`type_dependent_def_id`]: ../../rustc_middle/ty/struct.TypeckResults.html#method.type_dependent_def_id
1891-
MethodCall(&'hir PathSegment<'hir>, &'hir [Expr<'hir>], Span),
1891+
MethodCall(&'hir PathSegment<'hir>, &'hir Expr<'hir>, &'hir [Expr<'hir>], Span),
18921892
/// A tuple (e.g., `(a, b, c, d)`).
18931893
Tup(&'hir [Expr<'hir>]),
18941894
/// A binary operation (e.g., `a + b`, `a * b`).
@@ -3492,8 +3492,8 @@ mod size_asserts {
34923492
// These are in alphabetical order, which is easy to maintain.
34933493
static_assert_size!(Block<'_>, 48);
34943494
static_assert_size!(Body<'_>, 32);
3495-
static_assert_size!(Expr<'_>, 56);
3496-
static_assert_size!(ExprKind<'_>, 40);
3495+
static_assert_size!(Expr<'_>, 64);
3496+
static_assert_size!(ExprKind<'_>, 48);
34973497
static_assert_size!(FnDecl<'_>, 40);
34983498
static_assert_size!(ForeignItem<'_>, 72);
34993499
static_assert_size!(ForeignItemKind<'_>, 40);

compiler/rustc_hir/src/intravisit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,8 +1094,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
10941094
visitor.visit_expr(callee_expression);
10951095
walk_list!(visitor, visit_expr, arguments);
10961096
}
1097-
ExprKind::MethodCall(ref segment, arguments, _) => {
1097+
ExprKind::MethodCall(ref segment, receiver, arguments, _) => {
10981098
visitor.visit_path_segment(expression.span, segment);
1099+
visitor.visit_expr(receiver);
10991100
walk_list!(visitor, visit_expr, arguments);
11001101
}
11011102
ExprKind::Binary(_, ref left_expression, ref right_expression) => {

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,9 +1181,14 @@ impl<'a> State<'a> {
11811181
self.print_call_post(args)
11821182
}
11831183

1184-
fn print_expr_method_call(&mut self, segment: &hir::PathSegment<'_>, args: &[hir::Expr<'_>]) {
1185-
let base_args = &args[1..];
1186-
self.print_expr_maybe_paren(&args[0], parser::PREC_POSTFIX);
1184+
fn print_expr_method_call(
1185+
&mut self,
1186+
segment: &hir::PathSegment<'_>,
1187+
receiver: &hir::Expr<'_>,
1188+
args: &[hir::Expr<'_>],
1189+
) {
1190+
let base_args = args;
1191+
self.print_expr_maybe_paren(&receiver, parser::PREC_POSTFIX);
11871192
self.word(".");
11881193
self.print_ident(segment.ident);
11891194

@@ -1394,8 +1399,8 @@ impl<'a> State<'a> {
13941399
hir::ExprKind::Call(func, args) => {
13951400
self.print_expr_call(func, args);
13961401
}
1397-
hir::ExprKind::MethodCall(segment, args, _) => {
1398-
self.print_expr_method_call(segment, args);
1402+
hir::ExprKind::MethodCall(segment, receiver, args, _) => {
1403+
self.print_expr_method_call(segment, receiver, args);
13991404
}
14001405
hir::ExprKind::Binary(op, lhs, rhs) => {
14011406
self.print_expr_binary(op, lhs, rhs);
@@ -2393,9 +2398,9 @@ fn contains_exterior_struct_lit(value: &hir::Expr<'_>) -> bool {
23932398
contains_exterior_struct_lit(x)
23942399
}
23952400

2396-
hir::ExprKind::MethodCall(.., exprs, _) => {
2401+
hir::ExprKind::MethodCall(_, receiver, ..) => {
23972402
// `X { y: 1 }.bar(...)`
2398-
contains_exterior_struct_lit(&exprs[0])
2403+
contains_exterior_struct_lit(receiver)
23992404
}
24002405

24012406
_ => false,

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
901901
}
902902
}
903903
}
904-
hir::ExprKind::MethodCall(segment, _, _) => {
904+
hir::ExprKind::MethodCall(segment, ..) => {
905905
if let Some(def_id) = self.typeck_results.type_dependent_def_id(expr.hir_id) {
906906
let generics = tcx.generics_of(def_id);
907907
let insertable: Option<_> = try {
@@ -1132,7 +1132,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
11321132
let generic_args = &generics.own_substs_no_defaults(tcx, substs)
11331133
[generics.own_counts().lifetimes..];
11341134
let span = match expr.kind {
1135-
ExprKind::MethodCall(path, _, _) => path.ident.span,
1135+
ExprKind::MethodCall(path, ..) => path.ident.span,
11361136
_ => expr.span,
11371137
};
11381138

@@ -1181,20 +1181,20 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
11811181
})
11821182
.any(|generics| generics.has_impl_trait())
11831183
};
1184-
if let ExprKind::MethodCall(path, args, span) = expr.kind
1184+
if let ExprKind::MethodCall(path, receiver, args, span) = expr.kind
11851185
&& let Some(substs) = self.node_substs_opt(expr.hir_id)
11861186
&& substs.iter().any(|arg| self.generic_arg_contains_target(arg))
11871187
&& let Some(def_id) = self.typeck_results.type_dependent_def_id(expr.hir_id)
11881188
&& self.infcx.tcx.trait_of_item(def_id).is_some()
11891189
&& !has_impl_trait(def_id)
11901190
{
11911191
let successor =
1192-
args.get(1).map_or_else(|| (")", span.hi()), |arg| (", ", arg.span.lo()));
1192+
args.get(0).map_or_else(|| (")", span.hi()), |arg| (", ", arg.span.lo()));
11931193
let substs = self.infcx.resolve_vars_if_possible(substs);
11941194
self.update_infer_source(InferSource {
11951195
span: path.ident.span,
11961196
kind: InferSourceKind::FullyQualifiedMethodCall {
1197-
receiver: args.first().unwrap(),
1197+
receiver,
11981198
successor,
11991199
substs,
12001200
def_id,

compiler/rustc_lint/src/array_into_iter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter {
6161
}
6262

6363
// We only care about method call expressions.
64-
if let hir::ExprKind::MethodCall(call, args, _) = &expr.kind {
64+
if let hir::ExprKind::MethodCall(call, receiver_arg, ..) = &expr.kind {
6565
if call.ident.name != sym::into_iter {
6666
return;
6767
}
@@ -75,7 +75,6 @@ impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter {
7575
};
7676

7777
// As this is a method call expression, we have at least one argument.
78-
let receiver_arg = &args[0];
7978
let receiver_ty = cx.typeck_results().expr_ty(receiver_arg);
8079
let adjustments = cx.typeck_results().expr_adjustments(receiver_arg);
8180

compiler/rustc_lint/src/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,13 +2412,13 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
24122412
_ => {}
24132413
}
24142414
}
2415-
} else if let hir::ExprKind::MethodCall(_, ref args, _) = expr.kind {
2415+
} else if let hir::ExprKind::MethodCall(_, receiver, ..) = expr.kind {
24162416
// Find problematic calls to `MaybeUninit::assume_init`.
24172417
let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?;
24182418
if cx.tcx.is_diagnostic_item(sym::assume_init, def_id) {
24192419
// This is a call to *some* method named `assume_init`.
24202420
// See if the `self` parameter is one of the dangerous constructors.
2421-
if let hir::ExprKind::Call(ref path_expr, _) = args[0].kind {
2421+
if let hir::ExprKind::Call(ref path_expr, _) = receiver.kind {
24222422
if let hir::ExprKind::Path(ref qpath) = path_expr.kind {
24232423
let def_id = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()?;
24242424
match cx.tcx.get_diagnostic_name(def_id) {

compiler/rustc_lint/src/internal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn typeck_results_of_method_fn<'tcx>(
5151
expr: &Expr<'_>,
5252
) -> Option<(Span, DefId, ty::subst::SubstsRef<'tcx>)> {
5353
match expr.kind {
54-
ExprKind::MethodCall(segment, _, _)
54+
ExprKind::MethodCall(segment, ..)
5555
if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) =>
5656
{
5757
Some((segment.ident.span, def_id, cx.typeck_results().node_substs(expr.hir_id)))

0 commit comments

Comments
 (0)