Skip to content

Commit 1408173

Browse files
committed
Move find_self_call.
It's a function that does stuff with MIR and yet it weirdly has its own module in `rustc_middle::util`. This commit moves it into `rustc_middle::mir`, a more sensible home.
1 parent 3f5e218 commit 1408173

File tree

6 files changed

+48
-60
lines changed

6 files changed

+48
-60
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3777,7 +3777,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
37773777
let tcx = self.infcx.tcx;
37783778
if let Some(Terminator { kind: TerminatorKind::Call { call_source, fn_span, .. }, .. }) =
37793779
&self.body[loan.reserve_location.block].terminator
3780-
&& let Some((method_did, method_args)) = rustc_middle::util::find_self_call(
3780+
&& let Some((method_did, method_args)) = mir::find_self_call(
37813781
tcx,
37823782
self.body,
37833783
loan.assigned_place.local,

Diff for: compiler/rustc_borrowck/src/diagnostics/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::mir::tcx::PlaceTy;
1717
use rustc_middle::mir::{
1818
AggregateKind, CallSource, ConstOperand, ConstraintCategory, FakeReadCause, Local, LocalInfo,
1919
LocalKind, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement,
20-
StatementKind, Terminator, TerminatorKind,
20+
StatementKind, Terminator, TerminatorKind, find_self_call,
2121
};
2222
use rustc_middle::ty::print::Print;
2323
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -1016,12 +1016,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
10161016
kind: TerminatorKind::Call { fn_span, call_source, .. }, ..
10171017
}) = &self.body[location.block].terminator
10181018
{
1019-
let Some((method_did, method_args)) = rustc_middle::util::find_self_call(
1020-
self.infcx.tcx,
1021-
self.body,
1022-
target_temp,
1023-
location.block,
1024-
) else {
1019+
let Some((method_did, method_args)) =
1020+
find_self_call(self.infcx.tcx, self.body, target_temp, location.block)
1021+
else {
10251022
return normal_ret;
10261023
};
10271024

Diff for: compiler/rustc_middle/src/mir/mod.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisit
2727
use rustc_serialize::{Decodable, Encodable};
2828
use rustc_span::source_map::Spanned;
2929
use rustc_span::{DUMMY_SP, Span, Symbol};
30-
use tracing::trace;
30+
use tracing::{debug, trace};
3131

3232
pub use self::query::*;
3333
use self::visit::TyContext;
@@ -1796,6 +1796,47 @@ impl DefLocation {
17961796
}
17971797
}
17981798

1799+
/// Checks if the specified `local` is used as the `self` parameter of a method call
1800+
/// in the provided `BasicBlock`. If it is, then the `DefId` of the called method is
1801+
/// returned.
1802+
pub fn find_self_call<'tcx>(
1803+
tcx: TyCtxt<'tcx>,
1804+
body: &Body<'tcx>,
1805+
local: Local,
1806+
block: BasicBlock,
1807+
) -> Option<(DefId, GenericArgsRef<'tcx>)> {
1808+
debug!("find_self_call(local={:?}): terminator={:?}", local, body[block].terminator);
1809+
if let Some(Terminator { kind: TerminatorKind::Call { func, args, .. }, .. }) =
1810+
&body[block].terminator
1811+
&& let Operand::Constant(box ConstOperand { const_, .. }) = func
1812+
&& let ty::FnDef(def_id, fn_args) = *const_.ty().kind()
1813+
&& let Some(ty::AssocItem { fn_has_self_parameter: true, .. }) =
1814+
tcx.opt_associated_item(def_id)
1815+
&& let [Spanned { node: Operand::Move(self_place) | Operand::Copy(self_place), .. }, ..] =
1816+
**args
1817+
{
1818+
if self_place.as_local() == Some(local) {
1819+
return Some((def_id, fn_args));
1820+
}
1821+
1822+
// Handle the case where `self_place` gets reborrowed.
1823+
// This happens when the receiver is `&T`.
1824+
for stmt in &body[block].statements {
1825+
if let StatementKind::Assign(box (place, rvalue)) = &stmt.kind
1826+
&& let Some(reborrow_local) = place.as_local()
1827+
&& self_place.as_local() == Some(reborrow_local)
1828+
&& let Rvalue::Ref(_, _, deref_place) = rvalue
1829+
&& let PlaceRef { local: deref_local, projection: [ProjectionElem::Deref] } =
1830+
deref_place.as_ref()
1831+
&& deref_local == local
1832+
{
1833+
return Some((def_id, fn_args));
1834+
}
1835+
}
1836+
}
1837+
None
1838+
}
1839+
17991840
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
18001841
#[cfg(target_pointer_width = "64")]
18011842
mod size_asserts {

Diff for: compiler/rustc_middle/src/util/find_self_call.rs

-47
This file was deleted.

Diff for: compiler/rustc_middle/src/util/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
pub mod bug;
22
pub mod common;
3-
pub mod find_self_call;
4-
5-
pub use find_self_call::find_self_call;
63

74
#[derive(Default, Copy, Clone)]
85
pub struct Providers {

Diff for: compiler/rustc_mir_transform/src/check_const_item_mutation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'tcx> Visitor<'tcx> for ConstMutationChecker<'_, 'tcx> {
133133
// the `self` parameter of a method call (as the terminator of our current
134134
// BasicBlock). If so, we emit a more specific lint.
135135
let method_did = self.target_local.and_then(|target_local| {
136-
rustc_middle::util::find_self_call(self.tcx, self.body, target_local, loc.block)
136+
find_self_call(self.tcx, self.body, target_local, loc.block)
137137
});
138138
let lint_loc =
139139
if method_did.is_some() { self.body.terminator_loc(loc.block) } else { loc };

0 commit comments

Comments
 (0)