Skip to content

Commit

Permalink
capture_disjoint_fields(#53488)
Browse files Browse the repository at this point in the history
Refactoring out the HirId of the UpvarId in another struct.
  • Loading branch information
blitzerr committed Nov 15, 2018
1 parent 1ca505a commit 6779bb4
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 30 deletions.
4 changes: 3 additions & 1 deletion src/librustc/ich/impls_ty.rs
Expand Up @@ -216,7 +216,9 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for ty::adjustment::AutoBorrow
}
}

impl_stable_hash_for!(struct ty::UpvarId { var_id, closure_expr_id });
impl_stable_hash_for!(struct ty::UpvarPath { hir_id });

impl_stable_hash_for!(struct ty::UpvarId { var_path, closure_expr_id });

impl_stable_hash_for!(enum ty::BorrowKind {
ImmBorrow,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/error_reporting/mod.rs
Expand Up @@ -1315,7 +1315,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
format!(" for lifetime parameter `{}` in coherence check", name)
}
infer::UpvarRegion(ref upvar_id, _) => {
let var_node_id = self.tcx.hir.hir_to_node_id(upvar_id.var_id);
let var_node_id = self.tcx.hir.hir_to_node_id(upvar_id.var_path.hir_id);
let var_name = self.tcx.hir.name(var_node_id);
format!(" for capture of `{}` by closure", var_name)
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/error_reporting/note.rs
Expand Up @@ -41,7 +41,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
"...so that reference does not outlive borrowed content");
}
infer::ReborrowUpvar(span, ref upvar_id) => {
let var_node_id = self.tcx.hir.hir_to_node_id(upvar_id.var_id);
let var_node_id = self.tcx.hir.hir_to_node_id(upvar_id.var_path.hir_id);
let var_name = self.tcx.hir.name(var_node_id);
err.span_note(span,
&format!("...so that closure can access `{}`", var_name));
Expand Down Expand Up @@ -174,7 +174,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
err
}
infer::ReborrowUpvar(span, ref upvar_id) => {
let var_node_id = self.tcx.hir.hir_to_node_id(upvar_id.var_id);
let var_node_id = self.tcx.hir.hir_to_node_id(upvar_id.var_path.hir_id);
let var_name = self.tcx.hir.name(var_node_id);
let mut err = struct_span_err!(self.tcx.sess,
span,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/expr_use_visitor.rs
Expand Up @@ -938,7 +938,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
let var_hir_id = self.tcx().hir.node_to_hir_id(freevar.var_id());
let closure_def_id = self.tcx().hir.local_def_id(closure_expr.id);
let upvar_id = ty::UpvarId {
var_id: var_hir_id,
var_path: ty::UpvarPath { hir_id: var_hir_id },
closure_expr_id: closure_def_id.to_local(),
};
let upvar_capture = self.mc.tables.upvar_capture(upvar_id);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/mem_categorization.rs
Expand Up @@ -818,7 +818,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
let closure_expr_def_id = self.tcx.hir.local_def_id(fn_node_id);
let var_hir_id = self.tcx.hir.node_to_hir_id(var_id);
let upvar_id = ty::UpvarId {
var_id: var_hir_id,
var_path: ty::UpvarPath { hir_id: var_hir_id },
closure_expr_id: closure_expr_def_id.to_local(),
};

Expand Down
6 changes: 3 additions & 3 deletions src/librustc/ty/context.rs
Expand Up @@ -789,7 +789,7 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {
pat_adjustments.hash_stable(hcx, hasher);
hash_stable_hashmap(hcx, hasher, upvar_capture_map, |up_var_id, hcx| {
let ty::UpvarId {
var_id,
var_path,
closure_expr_id
} = *up_var_id;

Expand All @@ -798,14 +798,14 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {

let var_owner_def_id = DefId {
krate: local_id_root.krate,
index: var_id.owner,
index: var_path.hir_id.owner,
};
let closure_def_id = DefId {
krate: local_id_root.krate,
index: closure_expr_id.to_def_id().index,
};
(hcx.def_path_hash(var_owner_def_id),
var_id.local_id,
var_path.hir_id.local_id,
hcx.def_path_hash(closure_def_id))
});

Expand Down
7 changes: 6 additions & 1 deletion src/librustc/ty/mod.rs
Expand Up @@ -731,12 +731,17 @@ impl<T> List<T> {
}
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub struct UpvarPath {
pub hir_id: hir::HirId,
}

/// Upvars do not get their own node-id. Instead, we use the pair of
/// the original var id (that is, the root variable that is referenced
/// by the upvar) and the id of the closure expression.
#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub struct UpvarId {
pub var_id: hir::HirId,
pub var_path: UpvarPath,
pub closure_expr_id: LocalDefId,
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/util/ppaux.rs
Expand Up @@ -678,8 +678,8 @@ impl<'tcx> fmt::Debug for ty::ClosureUpvar<'tcx> {
impl fmt::Debug for ty::UpvarId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "UpvarId({:?};`{}`;{:?})",
self.var_id,
ty::tls::with(|tcx| tcx.hir.name(tcx.hir.hir_to_node_id(self.var_id))),
self.var_path.hir_id,
ty::tls::with(|tcx| tcx.hir.name(tcx.hir.hir_to_node_id(self.var_path.hir_id))),
self.closure_expr_id)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_borrowck/borrowck/gather_loans/mod.rs
Expand Up @@ -453,8 +453,8 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
}
None
}
LpUpvar(ty::UpvarId{ var_id, closure_expr_id: _ }) => {
self.bccx.used_mut_nodes.borrow_mut().insert(var_id);
LpUpvar(ty::UpvarId{ var_path: ty::UpvarPath { hir_id }, closure_expr_id: _ }) => {
self.bccx.used_mut_nodes.borrow_mut().insert(hir_id);
None
}
LpExtend(ref base, mc::McInherited, LpDeref(pointer_kind)) |
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/gather_loans/move_error.rs
Expand Up @@ -97,7 +97,7 @@ fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, errors: &[MoveErr
}
}
if let NoteClosureEnv(upvar_id) = error.move_from.note {
let var_node_id = bccx.tcx.hir.hir_to_node_id(upvar_id.var_id);
let var_node_id = bccx.tcx.hir.hir_to_node_id(upvar_id.var_path.hir_id);
err.span_label(bccx.tcx.hir.span(var_node_id),
"captured outer variable");
}
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_borrowck/borrowck/mod.rs
Expand Up @@ -846,7 +846,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
MutabilityViolation => {
let mut db = self.cannot_assign(error_span, &descr, Origin::Ast);
if let mc::NoteClosureEnv(upvar_id) = err.cmt.note {
let node_id = self.tcx.hir.hir_to_node_id(upvar_id.var_id);
let node_id = self.tcx.hir.hir_to_node_id(upvar_id.var_path.hir_id);
let sp = self.tcx.hir.span(node_id);
let fn_closure_msg = "`Fn` closures cannot capture their enclosing \
environment for modifications";
Expand Down Expand Up @@ -1415,7 +1415,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
loan_path: &LoanPath<'tcx>,
out: &mut String) {
match loan_path.kind {
LpUpvar(ty::UpvarId { var_id: id, closure_expr_id: _ }) => {
LpUpvar(ty::UpvarId { var_path: ty::UpvarPath { hir_id: id}, closure_expr_id: _ }) => {
out.push_str(&self.tcx.hir.name(self.tcx.hir.hir_to_node_id(id)).as_str());
}
LpVar(id) => {
Expand Down Expand Up @@ -1533,7 +1533,7 @@ impl<'tcx> fmt::Debug for LoanPath<'tcx> {
write!(f, "$({})", ty::tls::with(|tcx| tcx.hir.node_to_string(id)))
}

LpUpvar(ty::UpvarId{ var_id, closure_expr_id }) => {
LpUpvar(ty::UpvarId{ var_path: ty::UpvarPath {hir_id: var_id}, closure_expr_id }) => {
let s = ty::tls::with(|tcx| {
let var_node_id = tcx.hir.hir_to_node_id(var_id);
tcx.hir.node_to_string(var_node_id)
Expand Down Expand Up @@ -1568,9 +1568,9 @@ impl<'tcx> fmt::Display for LoanPath<'tcx> {
write!(f, "$({})", ty::tls::with(|tcx| tcx.hir.node_to_user_string(id)))
}

LpUpvar(ty::UpvarId{ var_id, closure_expr_id: _ }) => {
LpUpvar(ty::UpvarId{ var_path: ty::UpvarPath { hir_id }, closure_expr_id: _ }) => {
let s = ty::tls::with(|tcx| {
let var_node_id = tcx.hir.hir_to_node_id(var_id);
let var_node_id = tcx.hir.hir_to_node_id(hir_id);
tcx.hir.node_to_string(var_node_id)
});
write!(f, "$({} captured by closure)", s)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/mod.rs
Expand Up @@ -612,7 +612,7 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
let var_hir_id = tcx.hir.node_to_hir_id(var_id);
let closure_expr_id = tcx.hir.local_def_id(fn_id);
let capture = hir.tables().upvar_capture(ty::UpvarId {
var_id: var_hir_id,
var_path: ty::UpvarPath {hir_id: var_hir_id},
closure_expr_id: LocalDefId::from_def_id(closure_expr_id),
});
let by_ref = match capture {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/hair/cx/expr.rs
Expand Up @@ -1061,7 +1061,7 @@ fn convert_var<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
// ...but the upvar might be an `&T` or `&mut T` capture, at which
// point we need an implicit deref
let upvar_id = ty::UpvarId {
var_id: var_hir_id,
var_path: ty::UpvarPath {hir_id: var_hir_id},
closure_expr_id: LocalDefId::from_def_id(closure_def_id),
};
match cx.tables().upvar_capture(upvar_id) {
Expand Down Expand Up @@ -1178,7 +1178,7 @@ fn capture_freevar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
-> ExprRef<'tcx> {
let var_hir_id = cx.tcx.hir.node_to_hir_id(freevar.var_id());
let upvar_id = ty::UpvarId {
var_id: var_hir_id,
var_path: ty::UpvarPath { hir_id: var_hir_id },
closure_expr_id: cx.tcx.hir.local_def_id(closure_expr.id).to_local(),
};
let upvar_capture = cx.tables().upvar_capture(upvar_id);
Expand Down
16 changes: 10 additions & 6 deletions src/librustc_typeck/check/upvar.rs
Expand Up @@ -134,7 +134,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
self.tcx.with_freevars(closure_node_id, |freevars| {
for freevar in freevars {
let upvar_id = ty::UpvarId {
var_id: self.tcx.hir.node_to_hir_id(freevar.var_id()),
var_path: ty::UpvarPath {
hir_id : self.tcx.hir.node_to_hir_id(freevar.var_id()),
},
closure_expr_id: LocalDefId::from_def_id(closure_def_id),
};
debug!("seed upvar_id {:?}", upvar_id);
Expand Down Expand Up @@ -248,7 +250,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let var_hir_id = tcx.hir.node_to_hir_id(var_node_id);
let freevar_ty = self.node_ty(var_hir_id);
let upvar_id = ty::UpvarId {
var_id: var_hir_id,
var_path: ty::UpvarPath {
hir_id: var_hir_id,
},
closure_expr_id: LocalDefId::from_def_id(closure_def_index),
};
let capture = self.tables.borrow().upvar_capture(upvar_id);
Expand Down Expand Up @@ -347,7 +351,7 @@ impl<'a, 'gcx, 'tcx> InferBorrowKind<'a, 'gcx, 'tcx> {
upvar_id.closure_expr_id,
ty::ClosureKind::FnOnce,
guarantor.span,
var_name(tcx, upvar_id.var_id),
var_name(tcx, upvar_id.var_path.hir_id),
);

self.adjust_upvar_captures
Expand All @@ -364,7 +368,7 @@ impl<'a, 'gcx, 'tcx> InferBorrowKind<'a, 'gcx, 'tcx> {
upvar_id.closure_expr_id,
ty::ClosureKind::FnOnce,
guarantor.span,
var_name(tcx, upvar_id.var_id),
var_name(tcx, upvar_id.var_path.hir_id),
);
}
mc::NoteIndex | mc::NoteNone => {}
Expand Down Expand Up @@ -465,7 +469,7 @@ impl<'a, 'gcx, 'tcx> InferBorrowKind<'a, 'gcx, 'tcx> {
upvar_id.closure_expr_id,
ty::ClosureKind::FnMut,
cmt.span,
var_name(tcx, upvar_id.var_id),
var_name(tcx, upvar_id.var_path.hir_id),
);

true
Expand All @@ -478,7 +482,7 @@ impl<'a, 'gcx, 'tcx> InferBorrowKind<'a, 'gcx, 'tcx> {
upvar_id.closure_expr_id,
ty::ClosureKind::FnMut,
cmt.span,
var_name(tcx, upvar_id.var_id),
var_name(tcx, upvar_id.var_path.hir_id),
);

true
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/writeback.rs
Expand Up @@ -306,7 +306,7 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
ty::UpvarCapture::ByValue => ty::UpvarCapture::ByValue,
ty::UpvarCapture::ByRef(ref upvar_borrow) => {
let r = upvar_borrow.region;
let r = self.resolve(&r, &upvar_id.var_id);
let r = self.resolve(&r, &upvar_id.var_path.hir_id);
ty::UpvarCapture::ByRef(ty::UpvarBorrow {
kind: upvar_borrow.kind,
region: r,
Expand Down

0 comments on commit 6779bb4

Please sign in to comment.