Skip to content

Commit 4f3c469

Browse files
committed
Remove Region from HAIR
Use `ReErased` for any regions that need to be created in RValue::Ref in MIR generation.
1 parent cae623c commit 4f3c469

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
@@ -617,7 +617,7 @@ struct Binding<'tcx> {
617617
var_id: NodeId,
618618
var_ty: Ty<'tcx>,
619619
mutability: Mutability,
620-
binding_mode: BindingMode<'tcx>,
620+
binding_mode: BindingMode,
621621
}
622622

623623
/// Indicates that the type of `source` must be a subtype of the
@@ -1345,7 +1345,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
13451345
// Assign each of the bindings. Since we are binding for a
13461346
// guard expression, this will never trigger moves out of the
13471347
// candidate.
1348-
let re_empty = self.hir.tcx().types.re_empty;
1348+
let re_erased = self.hir.tcx().types.re_erased;
13491349
for binding in bindings {
13501350
let source_info = self.source_info(binding.span);
13511351

@@ -1361,11 +1361,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
13611361
self.schedule_drop_for_binding(binding.var_id, binding.span, RefWithinGuard);
13621362
match binding.binding_mode {
13631363
BindingMode::ByValue => {
1364-
let rvalue = Rvalue::Ref(re_empty, BorrowKind::Shared, binding.source.clone());
1364+
let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, binding.source.clone());
13651365
self.cfg
13661366
.push_assign(block, source_info, &ref_for_guard, rvalue);
13671367
}
1368-
BindingMode::ByRef(region, borrow_kind) => {
1368+
BindingMode::ByRef(borrow_kind) => {
13691369
// Tricky business: For `ref id` and `ref mut id`
13701370
// patterns, we want `id` within the guard to
13711371
// correspond to a temp of type `& &T` or `& &mut
@@ -1405,10 +1405,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
14051405
allow_two_phase_borrow: true,
14061406
},
14071407
};
1408-
let rvalue = Rvalue::Ref(region, borrow_kind, binding.source.clone());
1408+
let rvalue = Rvalue::Ref(re_erased, borrow_kind, binding.source.clone());
14091409
self.cfg
14101410
.push_assign(block, source_info, &val_for_guard, rvalue);
1411-
let rvalue = Rvalue::Ref(region, BorrowKind::Shared, val_for_guard);
1411+
let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, val_for_guard);
14121412
self.cfg
14131413
.push_assign(block, source_info, &ref_for_guard, rvalue);
14141414
}
@@ -1426,6 +1426,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
14261426
block, bindings
14271427
);
14281428

1429+
1430+
let re_erased = self.hir.tcx().types.re_erased;
14291431
// Assign each of the bindings. This may trigger moves out of the candidate.
14301432
for binding in bindings {
14311433
let source_info = self.source_info(binding.span);
@@ -1436,8 +1438,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
14361438
BindingMode::ByValue => {
14371439
Rvalue::Use(self.consume_by_copy_or_move(binding.source.clone()))
14381440
}
1439-
BindingMode::ByRef(region, borrow_kind) => {
1440-
Rvalue::Ref(region, borrow_kind, binding.source.clone())
1441+
BindingMode::ByRef(borrow_kind) => {
1442+
Rvalue::Ref(re_erased, borrow_kind, binding.source.clone())
14411443
}
14421444
};
14431445
self.cfg.push_assign(block, source_info, &local, rvalue);
@@ -1483,7 +1485,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
14831485
let tcx = self.hir.tcx();
14841486
let binding_mode = match mode {
14851487
BindingMode::ByValue => ty::BindingMode::BindByValue(mutability.into()),
1486-
BindingMode::ByRef { .. } => ty::BindingMode::BindByReference(mutability.into()),
1488+
BindingMode::ByRef(_) => ty::BindingMode::BindByReference(mutability.into()),
14871489
};
14881490
debug!("declare_binding: user_ty={:?}", user_ty);
14891491
let local = LocalDecl::<'tcx> {
@@ -1521,7 +1523,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
15211523
let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
15221524
// See previous comment.
15231525
mutability: Mutability::Not,
1524-
ty: tcx.mk_imm_ref(tcx.types.re_empty, var_ty),
1526+
ty: tcx.mk_imm_ref(tcx.types.re_erased, var_ty),
15251527
user_ty: UserTypeProjections::none(),
15261528
name: Some(name),
15271529
source_info,
@@ -1590,7 +1592,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
15901592

15911593
for (matched_place, borrow_kind) in all_fake_borrows {
15921594
let borrowed_input =
1593-
Rvalue::Ref(tcx.types.re_empty, borrow_kind, matched_place.clone());
1595+
Rvalue::Ref(tcx.types.re_erased, borrow_kind, matched_place.clone());
15941596
let borrowed_input_ty = borrowed_input.ty(&self.local_decls, tcx);
15951597
let borrowed_input_temp = self.temp(borrowed_input_ty, source_info.span);
15961598
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)]
@@ -98,7 +98,7 @@ pub enum PatternKind<'tcx> {
9898
Binding {
9999
mutability: Mutability,
100100
name: ast::Name,
101-
mode: BindingMode<'tcx>,
101+
mode: BindingMode,
102102
var: ast::NodeId,
103103
ty: Ty<'tcx>,
104104
subpattern: Option<Pattern<'tcx>>,
@@ -162,7 +162,7 @@ impl<'tcx> fmt::Display for Pattern<'tcx> {
162162
PatternKind::Binding { mutability, name, mode, ref subpattern, .. } => {
163163
let is_mut = match mode {
164164
BindingMode::ByValue => mutability == Mutability::Mut,
165-
BindingMode::ByRef(_, bk) => {
165+
BindingMode::ByRef(bk) => {
166166
write!(f, "ref ")?;
167167
match bk { BorrowKind::Mut { .. } => true, _ => false }
168168
}
@@ -493,12 +493,9 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
493493

494494
PatKind::Binding(_, id, ident, ref sub) => {
495495
let var_ty = self.tables.node_id_to_type(pat.hir_id);
496-
let region = match var_ty.sty {
497-
ty::Ref(r, _, _) => Some(r),
498-
ty::Error => { // Avoid ICE
499-
return Pattern { span: pat.span, ty, kind: Box::new(PatternKind::Wild) };
500-
}
501-
_ => None,
496+
if let ty::Error = var_ty.sty {
497+
// Avoid ICE
498+
return Pattern { span: pat.span, ty, kind: Box::new(PatternKind::Wild) };
502499
};
503500
let bm = *self.tables.pat_binding_modes().get(pat.hir_id)
504501
.expect("missing binding mode");
@@ -509,10 +506,10 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
509506
(Mutability::Not, BindingMode::ByValue),
510507
ty::BindByReference(hir::MutMutable) =>
511508
(Mutability::Not, BindingMode::ByRef(
512-
region.unwrap(), BorrowKind::Mut { allow_two_phase_borrow: false })),
509+
BorrowKind::Mut { allow_two_phase_borrow: false })),
513510
ty::BindByReference(hir::MutImmutable) =>
514511
(Mutability::Not, BindingMode::ByRef(
515-
region.unwrap(), BorrowKind::Shared)),
512+
BorrowKind::Shared)),
516513
};
517514

518515
// A ref x pattern is the same node used for x, and as such it has
@@ -1019,7 +1016,7 @@ macro_rules! CloneImpls {
10191016

10201017
CloneImpls!{ <'tcx>
10211018
Span, Field, Mutability, ast::Name, ast::NodeId, usize, ty::Const<'tcx>,
1022-
Region<'tcx>, Ty<'tcx>, BindingMode<'tcx>, &'tcx AdtDef,
1019+
Region<'tcx>, Ty<'tcx>, BindingMode, &'tcx AdtDef,
10231020
&'tcx Substs<'tcx>, &'tcx Kind<'tcx>, UserTypeAnnotation<'tcx>,
10241021
UserTypeProjection<'tcx>, PatternTypeProjection<'tcx>
10251022
}

0 commit comments

Comments
 (0)