Skip to content

Commit

Permalink
Auto merge of #56638 - matthewjasper:remove-ref-region, r=nikomatsakis
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jan 8, 2019
2 parents 2cb7cdc + 4f3c469 commit d22fa2d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 55 deletions.
9 changes: 3 additions & 6 deletions src/librustc_mir/build/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,14 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
block.and(Rvalue::Repeat(value_operand, count))
}
ExprKind::Borrow {
region,
borrow_kind,
arg,
} => {
let arg_place = match borrow_kind {
BorrowKind::Shared => unpack!(block = this.as_read_only_place(block, arg)),
_ => unpack!(block = this.as_place(block, arg)),
};
block.and(Rvalue::Ref(region, borrow_kind, arg_place))
block.and(Rvalue::Ref(this.hir.tcx().types.re_erased, borrow_kind, arg_place))
}
ExprKind::Binary { op, lhs, rhs } => {
let lhs = unpack!(block = this.as_operand(block, scope, lhs));
Expand Down Expand Up @@ -249,11 +248,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
BorrowKind::Mut {
allow_two_phase_borrow: false,
},
region,
arg,
} => unpack!(
block = this.limit_capture_mutability(
upvar.span, upvar.ty, scope, block, arg, region,
upvar.span, upvar.ty, scope, block, arg,
)
),
_ => unpack!(block = this.as_operand(block, scope, upvar)),
Expand Down Expand Up @@ -500,7 +498,6 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
temp_lifetime: Option<region::Scope>,
mut block: BasicBlock,
arg: ExprRef<'tcx>,
region: &'tcx ty::RegionKind,
) -> BlockAnd<Operand<'tcx>> {
let this = self;

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

// In constants, temp_lifetime is None. We should not need to drop
Expand Down
24 changes: 13 additions & 11 deletions src/librustc_mir/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ struct Binding<'tcx> {
var_id: NodeId,
var_ty: Ty<'tcx>,
mutability: Mutability,
binding_mode: BindingMode<'tcx>,
binding_mode: BindingMode,
}

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

Expand All @@ -1385,11 +1385,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
self.schedule_drop_for_binding(binding.var_id, binding.span, RefWithinGuard);
match binding.binding_mode {
BindingMode::ByValue => {
let rvalue = Rvalue::Ref(re_empty, BorrowKind::Shared, binding.source.clone());
let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, binding.source.clone());
self.cfg
.push_assign(block, source_info, &ref_for_guard, rvalue);
}
BindingMode::ByRef(region, borrow_kind) => {
BindingMode::ByRef(borrow_kind) => {
// Tricky business: For `ref id` and `ref mut id`
// patterns, we want `id` within the guard to
// correspond to a temp of type `& &T` or `& &mut
Expand Down Expand Up @@ -1429,10 +1429,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
allow_two_phase_borrow: true,
},
};
let rvalue = Rvalue::Ref(region, borrow_kind, binding.source.clone());
let rvalue = Rvalue::Ref(re_erased, borrow_kind, binding.source.clone());
self.cfg
.push_assign(block, source_info, &val_for_guard, rvalue);
let rvalue = Rvalue::Ref(region, BorrowKind::Shared, val_for_guard);
let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, val_for_guard);
self.cfg
.push_assign(block, source_info, &ref_for_guard, rvalue);
}
Expand All @@ -1450,6 +1450,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
block, bindings
);


let re_erased = self.hir.tcx().types.re_erased;
// Assign each of the bindings. This may trigger moves out of the candidate.
for binding in bindings {
let source_info = self.source_info(binding.span);
Expand All @@ -1460,8 +1462,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
BindingMode::ByValue => {
Rvalue::Use(self.consume_by_copy_or_move(binding.source.clone()))
}
BindingMode::ByRef(region, borrow_kind) => {
Rvalue::Ref(region, borrow_kind, binding.source.clone())
BindingMode::ByRef(borrow_kind) => {
Rvalue::Ref(re_erased, borrow_kind, binding.source.clone())
}
};
self.cfg.push_assign(block, source_info, &local, rvalue);
Expand Down Expand Up @@ -1507,7 +1509,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
let tcx = self.hir.tcx();
let binding_mode = match mode {
BindingMode::ByValue => ty::BindingMode::BindByValue(mutability.into()),
BindingMode::ByRef { .. } => ty::BindingMode::BindByReference(mutability.into()),
BindingMode::ByRef(_) => ty::BindingMode::BindByReference(mutability.into()),
};
debug!("declare_binding: user_ty={:?}", user_ty);
let local = LocalDecl::<'tcx> {
Expand Down Expand Up @@ -1545,7 +1547,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
// See previous comment.
mutability: Mutability::Not,
ty: tcx.mk_imm_ref(tcx.types.re_empty, var_ty),
ty: tcx.mk_imm_ref(tcx.types.re_erased, var_ty),
user_ty: UserTypeProjections::none(),
name: Some(name),
source_info,
Expand Down Expand Up @@ -1614,7 +1616,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {

for (matched_place, borrow_kind) in all_fake_borrows {
let borrowed_input =
Rvalue::Ref(tcx.types.re_empty, borrow_kind, matched_place.clone());
Rvalue::Ref(tcx.types.re_erased, borrow_kind, matched_place.clone());
let borrowed_input_ty = borrowed_input.ty(&self.local_decls, tcx);
let borrowed_input_temp = self.temp(borrowed_input_ty, source_info.span);
self.cfg.push_assign(
Expand Down
9 changes: 4 additions & 5 deletions src/librustc_mir/build/matches/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,17 +304,16 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
let (mty, method) = self.hir.trait_method(eq_def_id, "eq", ty, &[ty.into()]);
let method = self.hir.tcx().intern_lazy_const(ty::LazyConst::Evaluated(method));

let re_erased = self.hir.tcx().types.re_erased;
// take the argument by reference
let region_scope = self.topmost_scope();
let region = self.hir.tcx().mk_region(ty::ReScope(region_scope));
let tam = ty::TypeAndMut {
ty,
mutbl: Mutability::MutImmutable,
};
let ref_ty = self.hir.tcx().mk_ref(region, tam);
let ref_ty = self.hir.tcx().mk_ref(re_erased, tam);

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

// let rhs_ref_place = &rhs_place;
let ref_rvalue = Rvalue::Ref(region, BorrowKind::Shared, rhs_place);
let ref_rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, rhs_place);
let rhs_ref_place = self.temp(ref_ty, test.span);
self.cfg.push_assign(block, source_info, &rhs_ref_place, ref_rvalue);
let expect = Operand::Move(rhs_ref_place);
Expand Down
21 changes: 3 additions & 18 deletions src/librustc_mir/hair/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,40 +124,31 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
}),
span,
kind: ExprKind::Borrow {
region: deref.region,
borrow_kind: deref.mutbl.to_borrow_kind(),
arg: expr.to_ref(),
},
};

overloaded_place(cx, hir_expr, adjustment.target, Some(call), vec![expr.to_ref()])
}
Adjust::Borrow(AutoBorrow::Ref(r, m)) => {
Adjust::Borrow(AutoBorrow::Ref(_, m)) => {
ExprKind::Borrow {
region: r,
borrow_kind: m.to_borrow_kind(),
arg: expr.to_ref(),
}
}
Adjust::Borrow(AutoBorrow::RawPtr(m)) => {
// Convert this to a suitable `&foo` and
// then an unsafe coercion. Limit the region to be just this
// expression.
let region = ty::ReScope(region::Scope {
id: hir_expr.hir_id.local_id,
data: region::ScopeData::Node
});
let region = cx.tcx.mk_region(region);
// then an unsafe coercion.
expr = Expr {
temp_lifetime,
ty: cx.tcx.mk_ref(region,
ty: cx.tcx.mk_ref(cx.tcx.types.re_erased,
ty::TypeAndMut {
ty: expr.ty,
mutbl: m,
}),
span,
kind: ExprKind::Borrow {
region,
borrow_kind: m.to_borrow_kind(),
arg: expr.to_ref(),
},
Expand Down Expand Up @@ -323,12 +314,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
}

hir::ExprKind::AddrOf(mutbl, ref expr) => {
let region = match expr_ty.sty {
ty::Ref(r, _, _) => r,
_ => span_bug!(expr.span, "type of & not region"),
};
ExprKind::Borrow {
region,
borrow_kind: mutbl.to_borrow_kind(),
arg: expr.to_ref(),
}
Expand Down Expand Up @@ -1222,7 +1208,6 @@ fn capture_freevar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
ty: freevar_ty,
span: closure_expr.span,
kind: ExprKind::Borrow {
region: upvar_borrow.region,
borrow_kind,
arg: captured_var.to_ref(),
},
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_mir/hair/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc::hir::def_id::DefId;
use rustc::infer::canonical::Canonical;
use rustc::middle::region;
use rustc::ty::subst::Substs;
use rustc::ty::{AdtDef, UpvarSubsts, Region, Ty, Const, LazyConst, UserTypeAnnotation};
use rustc::ty::{AdtDef, UpvarSubsts, Ty, Const, LazyConst, UserTypeAnnotation};
use rustc::ty::layout::VariantIdx;
use rustc::hir;
use syntax::ast;
Expand Down Expand Up @@ -235,7 +235,6 @@ pub enum ExprKind<'tcx> {
id: DefId,
},
Borrow {
region: Region<'tcx>,
borrow_kind: BorrowKind,
arg: ExprRef<'tcx>,
},
Expand Down
23 changes: 10 additions & 13 deletions src/librustc_mir/hair/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ pub enum PatternError {
}

#[derive(Copy, Clone, Debug)]
pub enum BindingMode<'tcx> {
pub enum BindingMode {
ByValue,
ByRef(Region<'tcx>, BorrowKind),
ByRef(BorrowKind),
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -117,7 +117,7 @@ pub enum PatternKind<'tcx> {
Binding {
mutability: Mutability,
name: ast::Name,
mode: BindingMode<'tcx>,
mode: BindingMode,
var: ast::NodeId,
ty: Ty<'tcx>,
subpattern: Option<Pattern<'tcx>>,
Expand Down Expand Up @@ -181,7 +181,7 @@ impl<'tcx> fmt::Display for Pattern<'tcx> {
PatternKind::Binding { mutability, name, mode, ref subpattern, .. } => {
let is_mut = match mode {
BindingMode::ByValue => mutability == Mutability::Mut,
BindingMode::ByRef(_, bk) => {
BindingMode::ByRef(bk) => {
write!(f, "ref ")?;
match bk { BorrowKind::Mut { .. } => true, _ => false }
}
Expand Down Expand Up @@ -512,12 +512,9 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {

PatKind::Binding(_, id, ident, ref sub) => {
let var_ty = self.tables.node_id_to_type(pat.hir_id);
let region = match var_ty.sty {
ty::Ref(r, _, _) => Some(r),
ty::Error => { // Avoid ICE
return Pattern { span: pat.span, ty, kind: Box::new(PatternKind::Wild) };
}
_ => None,
if let ty::Error = var_ty.sty {
// Avoid ICE
return Pattern { span: pat.span, ty, kind: Box::new(PatternKind::Wild) };
};
let bm = *self.tables.pat_binding_modes().get(pat.hir_id)
.expect("missing binding mode");
Expand All @@ -528,10 +525,10 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
(Mutability::Not, BindingMode::ByValue),
ty::BindByReference(hir::MutMutable) =>
(Mutability::Not, BindingMode::ByRef(
region.unwrap(), BorrowKind::Mut { allow_two_phase_borrow: false })),
BorrowKind::Mut { allow_two_phase_borrow: false })),
ty::BindByReference(hir::MutImmutable) =>
(Mutability::Not, BindingMode::ByRef(
region.unwrap(), BorrowKind::Shared)),
BorrowKind::Shared)),
};

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

CloneImpls!{ <'tcx>
Span, Field, Mutability, ast::Name, ast::NodeId, usize, ty::Const<'tcx>,
Region<'tcx>, Ty<'tcx>, BindingMode<'tcx>, &'tcx AdtDef,
Region<'tcx>, Ty<'tcx>, BindingMode, &'tcx AdtDef,
&'tcx Substs<'tcx>, &'tcx Kind<'tcx>, UserTypeAnnotation<'tcx>,
UserTypeProjection<'tcx>, PatternTypeProjection<'tcx>
}
Expand Down

0 comments on commit d22fa2d

Please sign in to comment.