Skip to content

Commit d22fa2d

Browse files
committed
Auto merge of #56638 - matthewjasper:remove-ref-region, r=nikomatsakis
Remove some `Region`s from HAIR Use `ReErased` for any regions that need to be created in RValue::Ref in MIR generation. We will change them to all to `ReVar` before borrow checking anyway. r? @nikomatsakis
2 parents 2cb7cdc + 4f3c469 commit d22fa2d

File tree

6 files changed

+34
-55
lines changed

6 files changed

+34
-55
lines changed

src/librustc_mir/build/expr/as_rvalue.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,14 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
6767
block.and(Rvalue::Repeat(value_operand, count))
6868
}
6969
ExprKind::Borrow {
70-
region,
7170
borrow_kind,
7271
arg,
7372
} => {
7473
let arg_place = match borrow_kind {
7574
BorrowKind::Shared => unpack!(block = this.as_read_only_place(block, arg)),
7675
_ => unpack!(block = this.as_place(block, arg)),
7776
};
78-
block.and(Rvalue::Ref(region, borrow_kind, arg_place))
77+
block.and(Rvalue::Ref(this.hir.tcx().types.re_erased, borrow_kind, arg_place))
7978
}
8079
ExprKind::Binary { op, lhs, rhs } => {
8180
let lhs = unpack!(block = this.as_operand(block, scope, lhs));
@@ -249,11 +248,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
249248
BorrowKind::Mut {
250249
allow_two_phase_borrow: false,
251250
},
252-
region,
253251
arg,
254252
} => unpack!(
255253
block = this.limit_capture_mutability(
256-
upvar.span, upvar.ty, scope, block, arg, region,
254+
upvar.span, upvar.ty, scope, block, arg,
257255
)
258256
),
259257
_ => unpack!(block = this.as_operand(block, scope, upvar)),
@@ -500,7 +498,6 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
500498
temp_lifetime: Option<region::Scope>,
501499
mut block: BasicBlock,
502500
arg: ExprRef<'tcx>,
503-
region: &'tcx ty::RegionKind,
504501
) -> BlockAnd<Operand<'tcx>> {
505502
let this = self;
506503

@@ -582,7 +579,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
582579
block,
583580
source_info,
584581
&Place::Local(temp),
585-
Rvalue::Ref(region, borrow_kind, arg_place),
582+
Rvalue::Ref(this.hir.tcx().types.re_erased, borrow_kind, arg_place),
586583
);
587584

588585
// In constants, temp_lifetime is None. We should not need to drop

src/librustc_mir/build/matches/mod.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ struct Binding<'tcx> {
640640
var_id: NodeId,
641641
var_ty: Ty<'tcx>,
642642
mutability: Mutability,
643-
binding_mode: BindingMode<'tcx>,
643+
binding_mode: BindingMode,
644644
}
645645

646646
/// Indicates that the type of `source` must be a subtype of the
@@ -1369,7 +1369,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
13691369
// Assign each of the bindings. Since we are binding for a
13701370
// guard expression, this will never trigger moves out of the
13711371
// candidate.
1372-
let re_empty = self.hir.tcx().types.re_empty;
1372+
let re_erased = self.hir.tcx().types.re_erased;
13731373
for binding in bindings {
13741374
let source_info = self.source_info(binding.span);
13751375

@@ -1385,11 +1385,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
13851385
self.schedule_drop_for_binding(binding.var_id, binding.span, RefWithinGuard);
13861386
match binding.binding_mode {
13871387
BindingMode::ByValue => {
1388-
let rvalue = Rvalue::Ref(re_empty, BorrowKind::Shared, binding.source.clone());
1388+
let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, binding.source.clone());
13891389
self.cfg
13901390
.push_assign(block, source_info, &ref_for_guard, rvalue);
13911391
}
1392-
BindingMode::ByRef(region, borrow_kind) => {
1392+
BindingMode::ByRef(borrow_kind) => {
13931393
// Tricky business: For `ref id` and `ref mut id`
13941394
// patterns, we want `id` within the guard to
13951395
// correspond to a temp of type `& &T` or `& &mut
@@ -1429,10 +1429,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
14291429
allow_two_phase_borrow: true,
14301430
},
14311431
};
1432-
let rvalue = Rvalue::Ref(region, borrow_kind, binding.source.clone());
1432+
let rvalue = Rvalue::Ref(re_erased, borrow_kind, binding.source.clone());
14331433
self.cfg
14341434
.push_assign(block, source_info, &val_for_guard, rvalue);
1435-
let rvalue = Rvalue::Ref(region, BorrowKind::Shared, val_for_guard);
1435+
let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, val_for_guard);
14361436
self.cfg
14371437
.push_assign(block, source_info, &ref_for_guard, rvalue);
14381438
}
@@ -1450,6 +1450,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
14501450
block, bindings
14511451
);
14521452

1453+
1454+
let re_erased = self.hir.tcx().types.re_erased;
14531455
// Assign each of the bindings. This may trigger moves out of the candidate.
14541456
for binding in bindings {
14551457
let source_info = self.source_info(binding.span);
@@ -1460,8 +1462,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
14601462
BindingMode::ByValue => {
14611463
Rvalue::Use(self.consume_by_copy_or_move(binding.source.clone()))
14621464
}
1463-
BindingMode::ByRef(region, borrow_kind) => {
1464-
Rvalue::Ref(region, borrow_kind, binding.source.clone())
1465+
BindingMode::ByRef(borrow_kind) => {
1466+
Rvalue::Ref(re_erased, borrow_kind, binding.source.clone())
14651467
}
14661468
};
14671469
self.cfg.push_assign(block, source_info, &local, rvalue);
@@ -1507,7 +1509,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
15071509
let tcx = self.hir.tcx();
15081510
let binding_mode = match mode {
15091511
BindingMode::ByValue => ty::BindingMode::BindByValue(mutability.into()),
1510-
BindingMode::ByRef { .. } => ty::BindingMode::BindByReference(mutability.into()),
1512+
BindingMode::ByRef(_) => ty::BindingMode::BindByReference(mutability.into()),
15111513
};
15121514
debug!("declare_binding: user_ty={:?}", user_ty);
15131515
let local = LocalDecl::<'tcx> {
@@ -1545,7 +1547,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
15451547
let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
15461548
// See previous comment.
15471549
mutability: Mutability::Not,
1548-
ty: tcx.mk_imm_ref(tcx.types.re_empty, var_ty),
1550+
ty: tcx.mk_imm_ref(tcx.types.re_erased, var_ty),
15491551
user_ty: UserTypeProjections::none(),
15501552
name: Some(name),
15511553
source_info,
@@ -1614,7 +1616,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
16141616

16151617
for (matched_place, borrow_kind) in all_fake_borrows {
16161618
let borrowed_input =
1617-
Rvalue::Ref(tcx.types.re_empty, borrow_kind, matched_place.clone());
1619+
Rvalue::Ref(tcx.types.re_erased, borrow_kind, matched_place.clone());
16181620
let borrowed_input_ty = borrowed_input.ty(&self.local_decls, tcx);
16191621
let borrowed_input_temp = self.temp(borrowed_input_ty, source_info.span);
16201622
self.cfg.push_assign(

src/librustc_mir/build/matches/test.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -304,17 +304,16 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
304304
let (mty, method) = self.hir.trait_method(eq_def_id, "eq", ty, &[ty.into()]);
305305
let method = self.hir.tcx().intern_lazy_const(ty::LazyConst::Evaluated(method));
306306

307+
let re_erased = self.hir.tcx().types.re_erased;
307308
// take the argument by reference
308-
let region_scope = self.topmost_scope();
309-
let region = self.hir.tcx().mk_region(ty::ReScope(region_scope));
310309
let tam = ty::TypeAndMut {
311310
ty,
312311
mutbl: Mutability::MutImmutable,
313312
};
314-
let ref_ty = self.hir.tcx().mk_ref(region, tam);
313+
let ref_ty = self.hir.tcx().mk_ref(re_erased, tam);
315314

316315
// let lhs_ref_place = &lhs;
317-
let ref_rvalue = Rvalue::Ref(region, BorrowKind::Shared, place);
316+
let ref_rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, place);
318317
let lhs_ref_place = self.temp(ref_ty, test.span);
319318
self.cfg.push_assign(block, source_info, &lhs_ref_place, ref_rvalue);
320319
let val = Operand::Move(lhs_ref_place);
@@ -324,7 +323,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
324323
self.cfg.push_assign(block, source_info, &rhs_place, Rvalue::Use(expect));
325324

326325
// let rhs_ref_place = &rhs_place;
327-
let ref_rvalue = Rvalue::Ref(region, BorrowKind::Shared, rhs_place);
326+
let ref_rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, rhs_place);
328327
let rhs_ref_place = self.temp(ref_ty, test.span);
329328
self.cfg.push_assign(block, source_info, &rhs_ref_place, ref_rvalue);
330329
let expect = Operand::Move(rhs_ref_place);

src/librustc_mir/hair/cx/expr.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -124,40 +124,31 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
124124
}),
125125
span,
126126
kind: ExprKind::Borrow {
127-
region: deref.region,
128127
borrow_kind: deref.mutbl.to_borrow_kind(),
129128
arg: expr.to_ref(),
130129
},
131130
};
132131

133132
overloaded_place(cx, hir_expr, adjustment.target, Some(call), vec![expr.to_ref()])
134133
}
135-
Adjust::Borrow(AutoBorrow::Ref(r, m)) => {
134+
Adjust::Borrow(AutoBorrow::Ref(_, m)) => {
136135
ExprKind::Borrow {
137-
region: r,
138136
borrow_kind: m.to_borrow_kind(),
139137
arg: expr.to_ref(),
140138
}
141139
}
142140
Adjust::Borrow(AutoBorrow::RawPtr(m)) => {
143141
// Convert this to a suitable `&foo` and
144-
// then an unsafe coercion. Limit the region to be just this
145-
// expression.
146-
let region = ty::ReScope(region::Scope {
147-
id: hir_expr.hir_id.local_id,
148-
data: region::ScopeData::Node
149-
});
150-
let region = cx.tcx.mk_region(region);
142+
// then an unsafe coercion.
151143
expr = Expr {
152144
temp_lifetime,
153-
ty: cx.tcx.mk_ref(region,
145+
ty: cx.tcx.mk_ref(cx.tcx.types.re_erased,
154146
ty::TypeAndMut {
155147
ty: expr.ty,
156148
mutbl: m,
157149
}),
158150
span,
159151
kind: ExprKind::Borrow {
160-
region,
161152
borrow_kind: m.to_borrow_kind(),
162153
arg: expr.to_ref(),
163154
},
@@ -323,12 +314,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
323314
}
324315

325316
hir::ExprKind::AddrOf(mutbl, ref expr) => {
326-
let region = match expr_ty.sty {
327-
ty::Ref(r, _, _) => r,
328-
_ => span_bug!(expr.span, "type of & not region"),
329-
};
330317
ExprKind::Borrow {
331-
region,
332318
borrow_kind: mutbl.to_borrow_kind(),
333319
arg: expr.to_ref(),
334320
}
@@ -1222,7 +1208,6 @@ fn capture_freevar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
12221208
ty: freevar_ty,
12231209
span: closure_expr.span,
12241210
kind: ExprKind::Borrow {
1225-
region: upvar_borrow.region,
12261211
borrow_kind,
12271212
arg: captured_var.to_ref(),
12281213
},

src/librustc_mir/hair/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc::hir::def_id::DefId;
99
use rustc::infer::canonical::Canonical;
1010
use rustc::middle::region;
1111
use rustc::ty::subst::Substs;
12-
use rustc::ty::{AdtDef, UpvarSubsts, Region, Ty, Const, LazyConst, UserTypeAnnotation};
12+
use rustc::ty::{AdtDef, UpvarSubsts, Ty, Const, LazyConst, UserTypeAnnotation};
1313
use rustc::ty::layout::VariantIdx;
1414
use rustc::hir;
1515
use syntax::ast;
@@ -235,7 +235,6 @@ pub enum ExprKind<'tcx> {
235235
id: DefId,
236236
},
237237
Borrow {
238-
region: Region<'tcx>,
239238
borrow_kind: BorrowKind,
240239
arg: ExprRef<'tcx>,
241240
},

src/librustc_mir/hair/pattern/mod.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ pub enum PatternError {
3939
}
4040

4141
#[derive(Copy, Clone, Debug)]
42-
pub enum BindingMode<'tcx> {
42+
pub enum BindingMode {
4343
ByValue,
44-
ByRef(Region<'tcx>, BorrowKind),
44+
ByRef(BorrowKind),
4545
}
4646

4747
#[derive(Clone, Debug)]
@@ -117,7 +117,7 @@ pub enum PatternKind<'tcx> {
117117
Binding {
118118
mutability: Mutability,
119119
name: ast::Name,
120-
mode: BindingMode<'tcx>,
120+
mode: BindingMode,
121121
var: ast::NodeId,
122122
ty: Ty<'tcx>,
123123
subpattern: Option<Pattern<'tcx>>,
@@ -181,7 +181,7 @@ impl<'tcx> fmt::Display for Pattern<'tcx> {
181181
PatternKind::Binding { mutability, name, mode, ref subpattern, .. } => {
182182
let is_mut = match mode {
183183
BindingMode::ByValue => mutability == Mutability::Mut,
184-
BindingMode::ByRef(_, bk) => {
184+
BindingMode::ByRef(bk) => {
185185
write!(f, "ref ")?;
186186
match bk { BorrowKind::Mut { .. } => true, _ => false }
187187
}
@@ -512,12 +512,9 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
512512

513513
PatKind::Binding(_, id, ident, ref sub) => {
514514
let var_ty = self.tables.node_id_to_type(pat.hir_id);
515-
let region = match var_ty.sty {
516-
ty::Ref(r, _, _) => Some(r),
517-
ty::Error => { // Avoid ICE
518-
return Pattern { span: pat.span, ty, kind: Box::new(PatternKind::Wild) };
519-
}
520-
_ => None,
515+
if let ty::Error = var_ty.sty {
516+
// Avoid ICE
517+
return Pattern { span: pat.span, ty, kind: Box::new(PatternKind::Wild) };
521518
};
522519
let bm = *self.tables.pat_binding_modes().get(pat.hir_id)
523520
.expect("missing binding mode");
@@ -528,10 +525,10 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
528525
(Mutability::Not, BindingMode::ByValue),
529526
ty::BindByReference(hir::MutMutable) =>
530527
(Mutability::Not, BindingMode::ByRef(
531-
region.unwrap(), BorrowKind::Mut { allow_two_phase_borrow: false })),
528+
BorrowKind::Mut { allow_two_phase_borrow: false })),
532529
ty::BindByReference(hir::MutImmutable) =>
533530
(Mutability::Not, BindingMode::ByRef(
534-
region.unwrap(), BorrowKind::Shared)),
531+
BorrowKind::Shared)),
535532
};
536533

537534
// A ref x pattern is the same node used for x, and as such it has
@@ -1042,7 +1039,7 @@ macro_rules! CloneImpls {
10421039

10431040
CloneImpls!{ <'tcx>
10441041
Span, Field, Mutability, ast::Name, ast::NodeId, usize, ty::Const<'tcx>,
1045-
Region<'tcx>, Ty<'tcx>, BindingMode<'tcx>, &'tcx AdtDef,
1042+
Region<'tcx>, Ty<'tcx>, BindingMode, &'tcx AdtDef,
10461043
&'tcx Substs<'tcx>, &'tcx Kind<'tcx>, UserTypeAnnotation<'tcx>,
10471044
UserTypeProjection<'tcx>, PatternTypeProjection<'tcx>
10481045
}

0 commit comments

Comments
 (0)