Skip to content

Commit 922ff8e

Browse files
arora-amanroxelo
andcommitted
Refactor hir::Place
For the following code ```rust let c = || bar(foo.x, foo.x) ``` We generate two different `hir::Place`s for both `foo.x`. Handling this adds overhead for analysis we need to do for RFC 2229. We also want to store type information at each Projection to support analysis as part of the RFC. This resembles what we have for `mir::Place` This commit modifies the Place as follows: - Rename to `PlaceWithHirId`, where there `hir_id` is that of the expressioin. - Move any other information that describes the access out to another struct now called `Place`. - Removed `Span`, it can be accessed using the [hir API](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.span) - Modify `Projection` to be a strucutre of its own, that currently only contains the `ProjectionKind`. Adding type information to projections wil be completed as part of rust-lang/project-rfc-2229#5 Closes rust-lang/project-rfc-2229#3 Co-authored-by: Aman Arora <[email protected]> Co-authored-by: Roxane Fruytier <[email protected]>
1 parent 93696f4 commit 922ff8e

File tree

4 files changed

+38
-36
lines changed

4 files changed

+38
-36
lines changed

clippy_lints/src/escape.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_middle::ty::{self, Ty};
66
use rustc_session::{declare_tool_lint, impl_lint_pass};
77
use rustc_span::source_map::Span;
88
use rustc_target::abi::LayoutOf;
9-
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, Place, PlaceBase};
9+
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, PlaceWithHirId, PlaceBase};
1010

1111
use crate::utils::span_lint;
1212

@@ -112,9 +112,9 @@ fn is_argument(map: rustc_middle::hir::map::Map<'_>, id: HirId) -> bool {
112112
}
113113

114114
impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
115-
fn consume(&mut self, cmt: &Place<'tcx>, mode: ConsumeMode) {
116-
if cmt.projections.is_empty() {
117-
if let PlaceBase::Local(lid) = cmt.base {
115+
fn consume(&mut self, cmt: &PlaceWithHirId<'tcx>, mode: ConsumeMode) {
116+
if cmt.place.projections.is_empty() {
117+
if let PlaceBase::Local(lid) = cmt.place.base {
118118
if let ConsumeMode::Move = mode {
119119
// moved out or in. clearly can't be localized
120120
self.set.remove(&lid);
@@ -132,16 +132,16 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
132132
}
133133
}
134134

135-
fn borrow(&mut self, cmt: &Place<'tcx>, _: ty::BorrowKind) {
136-
if cmt.projections.is_empty() {
137-
if let PlaceBase::Local(lid) = cmt.base {
135+
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: ty::BorrowKind) {
136+
if cmt.place.projections.is_empty() {
137+
if let PlaceBase::Local(lid) = cmt.place.base {
138138
self.set.remove(&lid);
139139
}
140140
}
141141
}
142142

143-
fn mutate(&mut self, cmt: &Place<'tcx>) {
144-
if cmt.projections.is_empty() {
143+
fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>) {
144+
if cmt.place.projections.is_empty() {
145145
let map = &self.cx.tcx.hir();
146146
if is_argument(*map, cmt.hir_id) {
147147
// Skip closure arguments
@@ -150,7 +150,7 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
150150
return;
151151
}
152152

153-
if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
153+
if is_non_trait_box(cmt.place.ty) && !self.is_large_box(cmt.place.ty) {
154154
self.set.insert(cmt.hir_id);
155155
}
156156
return;

clippy_lints/src/loops.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_middle::ty::{self, Ty, TyS};
2828
use rustc_session::{declare_lint_pass, declare_tool_lint};
2929
use rustc_span::source_map::Span;
3030
use rustc_span::symbol::Symbol;
31-
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, Place, PlaceBase};
31+
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, PlaceWithHirId, PlaceBase};
3232
use std::iter::{once, Iterator};
3333
use std::mem;
3434

@@ -1489,42 +1489,43 @@ fn check_for_loop_over_map_kv<'a, 'tcx>(
14891489
}
14901490
}
14911491

1492-
struct MutatePairDelegate {
1492+
struct MutatePairDelegate<'a, 'tcx> {
1493+
cx: &'a LateContext<'a, 'tcx>,
14931494
hir_id_low: Option<HirId>,
14941495
hir_id_high: Option<HirId>,
14951496
span_low: Option<Span>,
14961497
span_high: Option<Span>,
14971498
}
14981499

1499-
impl<'tcx> Delegate<'tcx> for MutatePairDelegate {
1500-
fn consume(&mut self, _: &Place<'tcx>, _: ConsumeMode) {}
1500+
impl<'a, 'tcx> Delegate<'tcx> for MutatePairDelegate<'a, 'tcx> {
1501+
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: ConsumeMode) {}
15011502

1502-
fn borrow(&mut self, cmt: &Place<'tcx>, bk: ty::BorrowKind) {
1503+
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, bk: ty::BorrowKind) {
15031504
if let ty::BorrowKind::MutBorrow = bk {
1504-
if let PlaceBase::Local(id) = cmt.base {
1505+
if let PlaceBase::Local(id) = cmt.place.base {
15051506
if Some(id) == self.hir_id_low {
1506-
self.span_low = Some(cmt.span)
1507+
self.span_low = Some(self.cx.tcx.hir().span(cmt.hir_id))
15071508
}
15081509
if Some(id) == self.hir_id_high {
1509-
self.span_high = Some(cmt.span)
1510+
self.span_high = Some(self.cx.tcx.hir().span(cmt.hir_id))
15101511
}
15111512
}
15121513
}
15131514
}
15141515

1515-
fn mutate(&mut self, cmt: &Place<'tcx>) {
1516-
if let PlaceBase::Local(id) = cmt.base {
1516+
fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>) {
1517+
if let PlaceBase::Local(id) = cmt.place.base {
15171518
if Some(id) == self.hir_id_low {
1518-
self.span_low = Some(cmt.span)
1519+
self.span_low = Some(self.cx.tcx.hir().span(cmt.hir_id))
15191520
}
15201521
if Some(id) == self.hir_id_high {
1521-
self.span_high = Some(cmt.span)
1522+
self.span_high = Some(self.cx.tcx.hir().span(cmt.hir_id))
15221523
}
15231524
}
15241525
}
15251526
}
15261527

1527-
impl<'tcx> MutatePairDelegate {
1528+
impl<'a, 'tcx> MutatePairDelegate<'a, 'tcx> {
15281529
fn mutation_span(&self) -> (Option<Span>, Option<Span>) {
15291530
(self.span_low, self.span_high)
15301531
}
@@ -1579,12 +1580,13 @@ fn check_for_mutability(cx: &LateContext<'_, '_>, bound: &Expr<'_>) -> Option<Hi
15791580
None
15801581
}
15811582

1582-
fn check_for_mutation(
1583-
cx: &LateContext<'_, '_>,
1583+
fn check_for_mutation<'a, 'tcx> (
1584+
cx: &LateContext<'a, 'tcx>,
15841585
body: &Expr<'_>,
15851586
bound_ids: &[Option<HirId>],
15861587
) -> (Option<Span>, Option<Span>) {
15871588
let mut delegate = MutatePairDelegate {
1589+
cx: cx,
15881590
hir_id_low: bound_ids[0],
15891591
hir_id_high: bound_ids[1],
15901592
span_low: None,

clippy_lints/src/needless_pass_by_value.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -326,21 +326,21 @@ struct MovedVariablesCtxt {
326326
}
327327

328328
impl MovedVariablesCtxt {
329-
fn move_common(&mut self, cmt: &euv::Place<'_>) {
330-
if let euv::PlaceBase::Local(vid) = cmt.base {
329+
fn move_common(&mut self, cmt: &euv::PlaceWithHirId<'_>) {
330+
if let euv::PlaceBase::Local(vid) = cmt.place.base {
331331
self.moved_vars.insert(vid);
332332
}
333333
}
334334
}
335335

336336
impl<'tcx> euv::Delegate<'tcx> for MovedVariablesCtxt {
337-
fn consume(&mut self, cmt: &euv::Place<'tcx>, mode: euv::ConsumeMode) {
337+
fn consume(&mut self, cmt: &euv::PlaceWithHirId<'tcx>, mode: euv::ConsumeMode) {
338338
if let euv::ConsumeMode::Move = mode {
339339
self.move_common(cmt);
340340
}
341341
}
342342

343-
fn borrow(&mut self, _: &euv::Place<'tcx>, _: ty::BorrowKind) {}
343+
fn borrow(&mut self, _: &euv::PlaceWithHirId<'tcx>, _: ty::BorrowKind) {}
344344

345-
fn mutate(&mut self, _: &euv::Place<'tcx>) {}
345+
fn mutate(&mut self, _: &euv::PlaceWithHirId<'tcx>) {}
346346
}

clippy_lints/src/utils/usage.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_lint::LateContext;
88
use rustc_middle::hir::map::Map;
99
use rustc_middle::ty;
1010
use rustc_span::symbol::{Ident, Symbol};
11-
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, Place, PlaceBase};
11+
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, PlaceWithHirId, PlaceBase};
1212

1313
/// Returns a set of mutated local variable IDs, or `None` if mutations could not be determined.
1414
pub fn mutated_variables<'a, 'tcx>(expr: &'tcx Expr<'_>, cx: &'a LateContext<'a, 'tcx>) -> Option<FxHashSet<HirId>> {
@@ -46,8 +46,8 @@ struct MutVarsDelegate {
4646

4747
impl<'tcx> MutVarsDelegate {
4848
#[allow(clippy::similar_names)]
49-
fn update(&mut self, cat: &Place<'tcx>) {
50-
match cat.base {
49+
fn update(&mut self, cat: &PlaceWithHirId<'tcx>) {
50+
match cat.place.base {
5151
PlaceBase::Local(id) => {
5252
self.used_mutably.insert(id);
5353
},
@@ -63,15 +63,15 @@ impl<'tcx> MutVarsDelegate {
6363
}
6464

6565
impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
66-
fn consume(&mut self, _: &Place<'tcx>, _: ConsumeMode) {}
66+
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: ConsumeMode) {}
6767

68-
fn borrow(&mut self, cmt: &Place<'tcx>, bk: ty::BorrowKind) {
68+
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, bk: ty::BorrowKind) {
6969
if let ty::BorrowKind::MutBorrow = bk {
7070
self.update(&cmt)
7171
}
7272
}
7373

74-
fn mutate(&mut self, cmt: &Place<'tcx>) {
74+
fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>) {
7575
self.update(&cmt)
7676
}
7777
}

0 commit comments

Comments
 (0)