Skip to content

Commit a88182f

Browse files
authored
Rollup merge of #73489 - sexxi-goose:init_place_refactor, r=nikomatsakis
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
2 parents d2272d4 + 3659406 commit a88182f

File tree

8 files changed

+222
-195
lines changed

8 files changed

+222
-195
lines changed

src/librustc_typeck/check/regionck.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,10 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
439439

440440
/// Invoked on any adjustments that occur. Checks that if this is a region pointer being
441441
/// dereferenced, the lifetime of the pointer includes the deref expr.
442-
fn constrain_adjustments(&mut self, expr: &hir::Expr<'_>) -> mc::McResult<mc::Place<'tcx>> {
442+
fn constrain_adjustments(
443+
&mut self,
444+
expr: &hir::Expr<'_>,
445+
) -> mc::McResult<mc::PlaceWithHirId<'tcx>> {
443446
debug!("constrain_adjustments(expr={:?})", expr);
444447

445448
let mut place = self.with_mc(|mc| mc.cat_expr_unadjusted(expr))?;
@@ -480,12 +483,12 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
480483

481484
fn check_safety_of_rvalue_destructor_if_necessary(
482485
&mut self,
483-
place: &mc::Place<'tcx>,
486+
place_with_id: &mc::PlaceWithHirId<'tcx>,
484487
span: Span,
485488
) {
486-
if let mc::PlaceBase::Rvalue = place.base {
487-
if place.projections.is_empty() {
488-
let typ = self.resolve_type(place.ty);
489+
if let mc::PlaceBase::Rvalue = place_with_id.place.base {
490+
if place_with_id.place.projections.is_empty() {
491+
let typ = self.resolve_type(place_with_id.place.ty);
489492
let body_id = self.body_id;
490493
let _ = dropck::check_drop_obligations(self, typ, span, body_id);
491494
}
@@ -570,7 +573,7 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
570573

571574
/// Link lifetimes of any ref bindings in `root_pat` to the pointers found
572575
/// in the discriminant, if needed.
573-
fn link_pattern(&self, discr_cmt: mc::Place<'tcx>, root_pat: &hir::Pat<'_>) {
576+
fn link_pattern(&self, discr_cmt: mc::PlaceWithHirId<'tcx>, root_pat: &hir::Pat<'_>) {
574577
debug!("link_pattern(discr_cmt={:?}, root_pat={:?})", discr_cmt, root_pat);
575578
ignore_err!(self.with_mc(|mc| {
576579
mc.cat_pattern(discr_cmt, root_pat, |sub_cmt, hir::Pat { kind, span, hir_id }| {
@@ -591,7 +594,7 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
591594
fn link_autoref(
592595
&self,
593596
expr: &hir::Expr<'_>,
594-
expr_cmt: &mc::Place<'tcx>,
597+
expr_cmt: &mc::PlaceWithHirId<'tcx>,
595598
autoref: &adjustment::AutoBorrow<'tcx>,
596599
) {
597600
debug!("link_autoref(autoref={:?}, expr_cmt={:?})", autoref, expr_cmt);
@@ -612,7 +615,7 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
612615
span: Span,
613616
id: hir::HirId,
614617
mutbl: hir::Mutability,
615-
cmt_borrowed: &mc::Place<'tcx>,
618+
cmt_borrowed: &mc::PlaceWithHirId<'tcx>,
616619
) {
617620
debug!(
618621
"link_region_from_node_type(id={:?}, mutbl={:?}, cmt_borrowed={:?})",
@@ -635,12 +638,12 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
635638
span: Span,
636639
borrow_region: ty::Region<'tcx>,
637640
borrow_kind: ty::BorrowKind,
638-
borrow_place: &mc::Place<'tcx>,
641+
borrow_place: &mc::PlaceWithHirId<'tcx>,
639642
) {
640-
let origin = infer::DataBorrowed(borrow_place.ty, span);
641-
self.type_must_outlive(origin, borrow_place.ty, borrow_region);
643+
let origin = infer::DataBorrowed(borrow_place.place.ty, span);
644+
self.type_must_outlive(origin, borrow_place.place.ty, borrow_region);
642645

643-
for pointer_ty in borrow_place.deref_tys() {
646+
for pointer_ty in borrow_place.place.deref_tys() {
644647
debug!(
645648
"link_region(borrow_region={:?}, borrow_kind={:?}, pointer_ty={:?})",
646649
borrow_region, borrow_kind, borrow_place
@@ -656,7 +659,7 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
656659
_ => assert!(pointer_ty.is_box(), "unexpected built-in deref type {}", pointer_ty),
657660
}
658661
}
659-
if let mc::PlaceBase::Upvar(upvar_id) = borrow_place.base {
662+
if let mc::PlaceBase::Upvar(upvar_id) = borrow_place.place.base {
660663
self.link_upvar_region(span, borrow_region, upvar_id);
661664
}
662665
}

src/librustc_typeck/check/upvar.rs

+34-23
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,13 @@ struct InferBorrowKind<'a, 'tcx> {
270270
impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> {
271271
fn adjust_upvar_borrow_kind_for_consume(
272272
&mut self,
273-
place: &mc::Place<'tcx>,
273+
place_with_id: &mc::PlaceWithHirId<'tcx>,
274274
mode: euv::ConsumeMode,
275275
) {
276-
debug!("adjust_upvar_borrow_kind_for_consume(place={:?}, mode={:?})", place, mode);
276+
debug!(
277+
"adjust_upvar_borrow_kind_for_consume(place_with_id={:?}, mode={:?})",
278+
place_with_id, mode
279+
);
277280

278281
// we only care about moves
279282
match mode {
@@ -284,7 +287,7 @@ impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> {
284287
}
285288

286289
let tcx = self.fcx.tcx;
287-
let upvar_id = if let PlaceBase::Upvar(upvar_id) = place.base {
290+
let upvar_id = if let PlaceBase::Upvar(upvar_id) = place_with_id.place.base {
288291
upvar_id
289292
} else {
290293
return;
@@ -296,22 +299,22 @@ impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> {
296299
self.adjust_closure_kind(
297300
upvar_id.closure_expr_id,
298301
ty::ClosureKind::FnOnce,
299-
place.span,
302+
tcx.hir().span(place_with_id.hir_id),
300303
var_name(tcx, upvar_id.var_path.hir_id),
301304
);
302305

303306
self.adjust_upvar_captures.insert(upvar_id, ty::UpvarCapture::ByValue);
304307
}
305308

306-
/// Indicates that `place` is being directly mutated (e.g., assigned
309+
/// Indicates that `place_with_id` is being directly mutated (e.g., assigned
307310
/// to). If the place is based on a by-ref upvar, this implies that
308311
/// the upvar must be borrowed using an `&mut` borrow.
309-
fn adjust_upvar_borrow_kind_for_mut(&mut self, place: &mc::Place<'tcx>) {
310-
debug!("adjust_upvar_borrow_kind_for_mut(place={:?})", place);
312+
fn adjust_upvar_borrow_kind_for_mut(&mut self, place_with_id: &mc::PlaceWithHirId<'tcx>) {
313+
debug!("adjust_upvar_borrow_kind_for_mut(place_with_id={:?})", place_with_id);
311314

312-
if let PlaceBase::Upvar(upvar_id) = place.base {
315+
if let PlaceBase::Upvar(upvar_id) = place_with_id.place.base {
313316
let mut borrow_kind = ty::MutBorrow;
314-
for pointer_ty in place.deref_tys() {
317+
for pointer_ty in place_with_id.place.deref_tys() {
315318
match pointer_ty.kind {
316319
// Raw pointers don't inherit mutability.
317320
ty::RawPtr(_) => return,
@@ -323,20 +326,28 @@ impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> {
323326
_ => (),
324327
}
325328
}
326-
self.adjust_upvar_deref(upvar_id, place.span, borrow_kind);
329+
self.adjust_upvar_deref(
330+
upvar_id,
331+
self.fcx.tcx.hir().span(place_with_id.hir_id),
332+
borrow_kind,
333+
);
327334
}
328335
}
329336

330-
fn adjust_upvar_borrow_kind_for_unique(&mut self, place: &mc::Place<'tcx>) {
331-
debug!("adjust_upvar_borrow_kind_for_unique(place={:?})", place);
337+
fn adjust_upvar_borrow_kind_for_unique(&mut self, place_with_id: &mc::PlaceWithHirId<'tcx>) {
338+
debug!("adjust_upvar_borrow_kind_for_unique(place_with_id={:?})", place_with_id);
332339

333-
if let PlaceBase::Upvar(upvar_id) = place.base {
334-
if place.deref_tys().any(ty::TyS::is_unsafe_ptr) {
340+
if let PlaceBase::Upvar(upvar_id) = place_with_id.place.base {
341+
if place_with_id.place.deref_tys().any(ty::TyS::is_unsafe_ptr) {
335342
// Raw pointers don't inherit mutability.
336343
return;
337344
}
338345
// for a borrowed pointer to be unique, its base must be unique
339-
self.adjust_upvar_deref(upvar_id, place.span, ty::UniqueImmBorrow);
346+
self.adjust_upvar_deref(
347+
upvar_id,
348+
self.fcx.tcx.hir().span(place_with_id.hir_id),
349+
ty::UniqueImmBorrow,
350+
);
340351
}
341352
}
342353

@@ -453,26 +464,26 @@ impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> {
453464
}
454465

455466
impl<'a, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'tcx> {
456-
fn consume(&mut self, place: &mc::Place<'tcx>, mode: euv::ConsumeMode) {
457-
debug!("consume(place={:?},mode={:?})", place, mode);
458-
self.adjust_upvar_borrow_kind_for_consume(place, mode);
467+
fn consume(&mut self, place_with_id: &mc::PlaceWithHirId<'tcx>, mode: euv::ConsumeMode) {
468+
debug!("consume(place_with_id={:?},mode={:?})", place_with_id, mode);
469+
self.adjust_upvar_borrow_kind_for_consume(place_with_id, mode);
459470
}
460471

461-
fn borrow(&mut self, place: &mc::Place<'tcx>, bk: ty::BorrowKind) {
462-
debug!("borrow(place={:?}, bk={:?})", place, bk);
472+
fn borrow(&mut self, place_with_id: &mc::PlaceWithHirId<'tcx>, bk: ty::BorrowKind) {
473+
debug!("borrow(place_with_id={:?}, bk={:?})", place_with_id, bk);
463474

464475
match bk {
465476
ty::ImmBorrow => {}
466477
ty::UniqueImmBorrow => {
467-
self.adjust_upvar_borrow_kind_for_unique(place);
478+
self.adjust_upvar_borrow_kind_for_unique(place_with_id);
468479
}
469480
ty::MutBorrow => {
470-
self.adjust_upvar_borrow_kind_for_mut(place);
481+
self.adjust_upvar_borrow_kind_for_mut(place_with_id);
471482
}
472483
}
473484
}
474485

475-
fn mutate(&mut self, assignee_place: &mc::Place<'tcx>) {
486+
fn mutate(&mut self, assignee_place: &mc::PlaceWithHirId<'tcx>) {
476487
debug!("mutate(assignee_place={:?})", assignee_place);
477488

478489
self.adjust_upvar_borrow_kind_for_mut(assignee_place);

src/librustc_typeck/expr_use_visitor.rs

+36-28
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
pub use self::ConsumeMode::*;
66

77
// Export these here so that Clippy can use them.
8-
pub use mc::{Place, PlaceBase, Projection};
8+
pub use mc::{PlaceBase, PlaceWithHirId, Projection};
99

1010
use rustc_hir as hir;
1111
use rustc_hir::def::Res;
@@ -25,13 +25,13 @@ use rustc_span::Span;
2525
pub trait Delegate<'tcx> {
2626
// The value found at `place` is either copied or moved, depending
2727
// on mode.
28-
fn consume(&mut self, place: &mc::Place<'tcx>, mode: ConsumeMode);
28+
fn consume(&mut self, place_with_id: &mc::PlaceWithHirId<'tcx>, mode: ConsumeMode);
2929

3030
// The value found at `place` is being borrowed with kind `bk`.
31-
fn borrow(&mut self, place: &mc::Place<'tcx>, bk: ty::BorrowKind);
31+
fn borrow(&mut self, place_with_id: &mc::PlaceWithHirId<'tcx>, bk: ty::BorrowKind);
3232

33-
// The path at `place` is being assigned to.
34-
fn mutate(&mut self, assignee_place: &mc::Place<'tcx>);
33+
// The path at `place_with_id` is being assigned to.
34+
fn mutate(&mut self, assignee_place: &mc::PlaceWithHirId<'tcx>);
3535
}
3636

3737
#[derive(Copy, Clone, PartialEq, Debug)]
@@ -113,11 +113,11 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
113113
self.mc.tcx()
114114
}
115115

116-
fn delegate_consume(&mut self, place: &Place<'tcx>) {
117-
debug!("delegate_consume(place={:?})", place);
116+
fn delegate_consume(&mut self, place_with_id: &PlaceWithHirId<'tcx>) {
117+
debug!("delegate_consume(place_with_id={:?})", place_with_id);
118118

119-
let mode = copy_or_move(&self.mc, place);
120-
self.delegate.consume(place, mode);
119+
let mode = copy_or_move(&self.mc, place_with_id);
120+
self.delegate.consume(place_with_id, mode);
121121
}
122122

123123
fn consume_exprs(&mut self, exprs: &[hir::Expr<'_>]) {
@@ -129,22 +129,22 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
129129
pub fn consume_expr(&mut self, expr: &hir::Expr<'_>) {
130130
debug!("consume_expr(expr={:?})", expr);
131131

132-
let place = return_if_err!(self.mc.cat_expr(expr));
133-
self.delegate_consume(&place);
132+
let place_with_id = return_if_err!(self.mc.cat_expr(expr));
133+
self.delegate_consume(&place_with_id);
134134
self.walk_expr(expr);
135135
}
136136

137137
fn mutate_expr(&mut self, expr: &hir::Expr<'_>) {
138-
let place = return_if_err!(self.mc.cat_expr(expr));
139-
self.delegate.mutate(&place);
138+
let place_with_id = return_if_err!(self.mc.cat_expr(expr));
139+
self.delegate.mutate(&place_with_id);
140140
self.walk_expr(expr);
141141
}
142142

143143
fn borrow_expr(&mut self, expr: &hir::Expr<'_>, bk: ty::BorrowKind) {
144144
debug!("borrow_expr(expr={:?}, bk={:?})", expr, bk);
145145

146-
let place = return_if_err!(self.mc.cat_expr(expr));
147-
self.delegate.borrow(&place, bk);
146+
let place_with_id = return_if_err!(self.mc.cat_expr(expr));
147+
self.delegate.borrow(&place_with_id, bk);
148148

149149
self.walk_expr(expr)
150150
}
@@ -384,7 +384,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
384384

385385
// Select just those fields of the `with`
386386
// expression that will actually be used
387-
match with_place.ty.kind {
387+
match with_place.place.ty.kind {
388388
ty::Adt(adt, substs) if adt.is_struct() => {
389389
// Consume those fields of the with expression that are needed.
390390
for (f_index, with_field) in adt.non_enum_variant().fields.iter().enumerate() {
@@ -422,14 +422,14 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
422422
// process.
423423
fn walk_adjustment(&mut self, expr: &hir::Expr<'_>) {
424424
let adjustments = self.mc.tables.expr_adjustments(expr);
425-
let mut place = return_if_err!(self.mc.cat_expr_unadjusted(expr));
425+
let mut place_with_id = return_if_err!(self.mc.cat_expr_unadjusted(expr));
426426
for adjustment in adjustments {
427427
debug!("walk_adjustment expr={:?} adj={:?}", expr, adjustment);
428428
match adjustment.kind {
429429
adjustment::Adjust::NeverToAny | adjustment::Adjust::Pointer(_) => {
430430
// Creating a closure/fn-pointer or unsizing consumes
431431
// the input and stores it into the resulting rvalue.
432-
self.delegate_consume(&place);
432+
self.delegate_consume(&place_with_id);
433433
}
434434

435435
adjustment::Adjust::Deref(None) => {}
@@ -441,14 +441,15 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
441441
// this is an autoref of `x`.
442442
adjustment::Adjust::Deref(Some(ref deref)) => {
443443
let bk = ty::BorrowKind::from_mutbl(deref.mutbl);
444-
self.delegate.borrow(&place, bk);
444+
self.delegate.borrow(&place_with_id, bk);
445445
}
446446

447447
adjustment::Adjust::Borrow(ref autoref) => {
448-
self.walk_autoref(expr, &place, autoref);
448+
self.walk_autoref(expr, &place_with_id, autoref);
449449
}
450450
}
451-
place = return_if_err!(self.mc.cat_expr_adjusted(expr, place, &adjustment));
451+
place_with_id =
452+
return_if_err!(self.mc.cat_expr_adjusted(expr, place_with_id, &adjustment));
452453
}
453454
}
454455

@@ -458,7 +459,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
458459
fn walk_autoref(
459460
&mut self,
460461
expr: &hir::Expr<'_>,
461-
base_place: &mc::Place<'tcx>,
462+
base_place: &mc::PlaceWithHirId<'tcx>,
462463
autoref: &adjustment::AutoBorrow<'tcx>,
463464
) {
464465
debug!(
@@ -479,7 +480,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
479480
}
480481
}
481482

482-
fn walk_arm(&mut self, discr_place: &Place<'tcx>, arm: &hir::Arm<'_>) {
483+
fn walk_arm(&mut self, discr_place: &PlaceWithHirId<'tcx>, arm: &hir::Arm<'_>) {
483484
self.walk_pat(discr_place, &arm.pat);
484485

485486
if let Some(hir::Guard::If(ref e)) = arm.guard {
@@ -491,12 +492,12 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
491492

492493
/// Walks a pat that occurs in isolation (i.e., top-level of fn argument or
493494
/// let binding, and *not* a match arm or nested pat.)
494-
fn walk_irrefutable_pat(&mut self, discr_place: &Place<'tcx>, pat: &hir::Pat<'_>) {
495+
fn walk_irrefutable_pat(&mut self, discr_place: &PlaceWithHirId<'tcx>, pat: &hir::Pat<'_>) {
495496
self.walk_pat(discr_place, pat);
496497
}
497498

498499
/// The core driver for walking a pattern
499-
fn walk_pat(&mut self, discr_place: &Place<'tcx>, pat: &hir::Pat<'_>) {
500+
fn walk_pat(&mut self, discr_place: &PlaceWithHirId<'tcx>, pat: &hir::Pat<'_>) {
500501
debug!("walk_pat(discr_place={:?}, pat={:?})", discr_place, pat);
501502

502503
let tcx = self.tcx();
@@ -569,7 +570,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
569570
closure_hir_id: hir::HirId,
570571
closure_span: Span,
571572
var_id: hir::HirId,
572-
) -> mc::McResult<mc::Place<'tcx>> {
573+
) -> mc::McResult<mc::PlaceWithHirId<'tcx>> {
573574
// Create the place for the variable being borrowed, from the
574575
// perspective of the creator (parent) of the closure.
575576
let var_ty = self.mc.node_ty(var_id)?;
@@ -579,7 +580,14 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
579580

580581
fn copy_or_move<'a, 'tcx>(
581582
mc: &mc::MemCategorizationContext<'a, 'tcx>,
582-
place: &Place<'tcx>,
583+
place_with_id: &PlaceWithHirId<'tcx>,
583584
) -> ConsumeMode {
584-
if !mc.type_is_copy_modulo_regions(place.ty, place.span) { Move } else { Copy }
585+
if !mc.type_is_copy_modulo_regions(
586+
place_with_id.place.ty,
587+
mc.tcx().hir().span(place_with_id.hir_id),
588+
) {
589+
Move
590+
} else {
591+
Copy
592+
}
585593
}

0 commit comments

Comments
 (0)