Skip to content

Commit 1768030

Browse files
committed
rustc: track the body owner DefId in MC and EUV.
1 parent 961fe54 commit 1768030

File tree

8 files changed

+43
-9
lines changed

8 files changed

+43
-9
lines changed

src/librustc/middle/expr_use_visitor.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx, 'tcx> {
268268
/// See also `with_infer`, which is used *during* typeck.
269269
pub fn new(delegate: &'a mut (dyn Delegate<'tcx>+'a),
270270
tcx: TyCtxt<'a, 'tcx, 'tcx>,
271+
body_owner: DefId,
271272
param_env: ty::ParamEnv<'tcx>,
272273
region_scope_tree: &'a region::ScopeTree,
273274
tables: &'a ty::TypeckTables<'tcx>,
@@ -276,6 +277,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx, 'tcx> {
276277
{
277278
ExprUseVisitor {
278279
mc: mc::MemCategorizationContext::new(tcx,
280+
body_owner,
279281
region_scope_tree,
280282
tables,
281283
rvalue_promotable_map),
@@ -288,13 +290,19 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx, 'tcx> {
288290
impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
289291
pub fn with_infer(delegate: &'a mut (dyn Delegate<'tcx>+'a),
290292
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
293+
body_owner: DefId,
291294
param_env: ty::ParamEnv<'tcx>,
292295
region_scope_tree: &'a region::ScopeTree,
293296
tables: &'a ty::TypeckTables<'tcx>)
294297
-> Self
295298
{
296299
ExprUseVisitor {
297-
mc: mc::MemCategorizationContext::with_infer(infcx, region_scope_tree, tables),
300+
mc: mc::MemCategorizationContext::with_infer(
301+
infcx,
302+
body_owner,
303+
region_scope_tree,
304+
tables,
305+
),
298306
delegate,
299307
param_env,
300308
}
@@ -965,10 +973,9 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
965973
// caller's perspective
966974
if upvar.has_parent {
967975
let closure_def_id = self.tcx().hir().local_def_id_from_hir_id(closure_hir_id);
968-
let parent_def_id = self.tcx().parent(closure_def_id).unwrap();
969-
assert!(self.tcx().is_closure(parent_def_id));
976+
assert_eq!(self.mc.body_owner, self.tcx().parent(closure_def_id).unwrap());
970977
let var_nid = self.tcx().hir().hir_to_node_id(var_id);
971-
self.mc.cat_upvar(closure_hir_id, closure_span, var_nid, parent_def_id)
978+
self.mc.cat_upvar(closure_hir_id, closure_span, var_nid)
972979
} else {
973980
let var_ty = self.mc.node_ty(var_id)?;
974981
self.mc.cat_res(closure_hir_id, closure_span, var_ty, Res::Local(var_id))

src/librustc/middle/mem_categorization.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ impl HirNode for hir::Pat {
288288
#[derive(Clone)]
289289
pub struct MemCategorizationContext<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
290290
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
291+
pub body_owner: DefId,
291292
pub region_scope_tree: &'a region::ScopeTree,
292293
pub tables: &'a ty::TypeckTables<'tcx>,
293294
rvalue_promotable_map: Option<&'tcx ItemLocalSet>,
@@ -398,12 +399,14 @@ impl MutabilityCategory {
398399

399400
impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx, 'tcx> {
400401
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>,
402+
body_owner: DefId,
401403
region_scope_tree: &'a region::ScopeTree,
402404
tables: &'a ty::TypeckTables<'tcx>,
403405
rvalue_promotable_map: Option<&'tcx ItemLocalSet>)
404406
-> MemCategorizationContext<'a, 'tcx, 'tcx> {
405407
MemCategorizationContext {
406408
tcx,
409+
body_owner,
407410
region_scope_tree,
408411
tables,
409412
rvalue_promotable_map,
@@ -423,6 +426,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
423426
/// - similarly, as the results of upvar analysis are not yet
424427
/// known, the results around upvar accesses may be incorrect.
425428
pub fn with_infer(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
429+
body_owner: DefId,
426430
region_scope_tree: &'a region::ScopeTree,
427431
tables: &'a ty::TypeckTables<'tcx>)
428432
-> MemCategorizationContext<'a, 'gcx, 'tcx> {
@@ -436,6 +440,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
436440

437441
MemCategorizationContext {
438442
tcx,
443+
body_owner,
439444
region_scope_tree,
440445
tables,
441446
rvalue_promotable_map,
@@ -739,8 +744,10 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
739744

740745
Res::Upvar(var_id, closure_node_id) => {
741746
let var_nid = self.tcx.hir().hir_to_node_id(var_id);
742-
let closure_expr_def_id = self.tcx.hir().local_def_id(closure_node_id);
743-
self.cat_upvar(hir_id, span, var_nid, closure_expr_def_id)
747+
let closure_def_id = self.tcx.hir().local_def_id(closure_node_id);
748+
assert_eq!(self.body_owner, closure_def_id);
749+
750+
self.cat_upvar(hir_id, span, var_nid)
744751
}
745752

746753
Res::Local(vid) => {
@@ -766,7 +773,6 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
766773
hir_id: hir::HirId,
767774
span: Span,
768775
var_id: ast::NodeId,
769-
closure_expr_def_id: DefId,
770776
) -> McResult<cmt_<'tcx>> {
771777
// An upvar can have up to 3 components. We translate first to a
772778
// `Categorization::Upvar`, which is itself a fiction -- it represents the reference to the
@@ -791,6 +797,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
791797
// FnMut | copied -> &'env mut | upvar -> &'env mut -> &'up bk
792798
// FnOnce | copied | upvar -> &'up bk
793799

800+
let closure_expr_def_id = self.body_owner;
794801
let fn_hir_id = self.tcx.hir().local_def_id_to_hir_id(
795802
LocalDefId::from_def_id(closure_expr_def_id),
796803
);

src/librustc_borrowck/borrowck/check_loans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ pub fn check_loans<'a, 'b, 'c, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
208208
let rvalue_promotable_map = bccx.tcx.rvalue_promotable_map(def_id);
209209
euv::ExprUseVisitor::new(&mut clcx,
210210
bccx.tcx,
211+
def_id,
211212
param_env,
212213
&bccx.region_scope_tree,
213214
bccx.tables,

src/librustc_borrowck/borrowck/gather_loans/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub fn gather_loans_in_fn<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
4444
let rvalue_promotable_map = bccx.tcx.rvalue_promotable_map(def_id);
4545
euv::ExprUseVisitor::new(&mut glcx,
4646
bccx.tcx,
47+
def_id,
4748
param_env,
4849
&bccx.region_scope_tree,
4950
bccx.tables,

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(crate) fn check_match<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
3535

3636
MatchVisitor {
3737
tcx,
38+
body_owner: def_id,
3839
tables: tcx.body_tables(body_id),
3940
region_scope_tree: &tcx.region_scope_tree(def_id),
4041
param_env: tcx.param_env(def_id),
@@ -48,6 +49,7 @@ fn create_e0004<'a>(sess: &'a Session, sp: Span, error_message: String) -> Diagn
4849

4950
struct MatchVisitor<'a, 'tcx: 'a> {
5051
tcx: TyCtxt<'a, 'tcx, 'tcx>,
52+
body_owner: DefId,
5153
tables: &'a ty::TypeckTables<'tcx>,
5254
param_env: ty::ParamEnv<'tcx>,
5355
identity_substs: SubstsRef<'tcx>,
@@ -632,6 +634,7 @@ fn check_for_mutation_in_guard(cx: &MatchVisitor<'_, '_>, guard: &hir::Guard) {
632634
hir::Guard::If(expr) =>
633635
ExprUseVisitor::new(&mut checker,
634636
cx.tcx,
637+
cx.body_owner,
635638
cx.param_env,
636639
cx.region_scope_tree,
637640
cx.tables,

src/librustc_passes/rvalue_promotion.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,15 @@ impl<'a, 'tcx> CheckCrateVisitor<'a, 'tcx> {
199199
let param_env = self.param_env;
200200
let region_scope_tree = self.tcx.region_scope_tree(item_def_id);
201201
let tables = self.tables;
202-
euv::ExprUseVisitor::new(self, tcx, param_env, &region_scope_tree, tables, None)
203-
.consume_body(body);
202+
euv::ExprUseVisitor::new(
203+
self,
204+
tcx,
205+
item_def_id,
206+
param_env,
207+
&region_scope_tree,
208+
tables,
209+
None,
210+
).consume_body(body);
204211

205212
let body_promotable = self.check_expr(&body.value);
206213
self.in_fn = outer_in_fn;

src/librustc_typeck/check/regionck.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ pub struct RegionCtxt<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
200200

201201
// id of innermost fn body id
202202
body_id: hir::HirId,
203+
body_owner: DefId,
203204

204205
// call_site scope of innermost fn
205206
call_site_scope: Option<region::Scope>,
@@ -236,6 +237,7 @@ impl<'a, 'gcx, 'tcx> RegionCtxt<'a, 'gcx, 'tcx> {
236237
region_scope_tree,
237238
repeating_scope: initial_repeating_scope,
238239
body_id: initial_body_id,
240+
body_owner: subject,
239241
call_site_scope: None,
240242
subject_def_id: subject,
241243
outlives_environment,
@@ -308,6 +310,7 @@ impl<'a, 'gcx, 'tcx> RegionCtxt<'a, 'gcx, 'tcx> {
308310

309311
let body_id = body.id();
310312
self.body_id = body_id.hir_id;
313+
self.body_owner = self.tcx.hir().body_owner_def_id(body_id);
311314

312315
let call_site = region::Scope {
313316
id: body.value.hir_id.local_id,
@@ -466,6 +469,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for RegionCtxt<'a, 'gcx, 'tcx> {
466469
// Save state of current function before invoking
467470
// `visit_fn_body`. We will restore afterwards.
468471
let old_body_id = self.body_id;
472+
let old_body_owner = self.body_owner;
469473
let old_call_site_scope = self.call_site_scope;
470474
let env_snapshot = self.outlives_environment.push_snapshot_pre_closure();
471475

@@ -477,6 +481,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for RegionCtxt<'a, 'gcx, 'tcx> {
477481
.pop_snapshot_post_closure(env_snapshot);
478482
self.call_site_scope = old_call_site_scope;
479483
self.body_id = old_body_id;
484+
self.body_owner = old_body_owner;
480485
}
481486

482487
//visit_pat: visit_pat, // (..) see above
@@ -829,6 +834,7 @@ impl<'a, 'gcx, 'tcx> RegionCtxt<'a, 'gcx, 'tcx> {
829834
{
830835
f(mc::MemCategorizationContext::with_infer(
831836
&self.infcx,
837+
self.body_owner,
832838
&self.region_scope_tree,
833839
&self.tables.borrow(),
834840
))

src/librustc_typeck/check/upvar.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
167167
}
168168

169169
let body_owner_def_id = self.tcx.hir().body_owner_def_id(body.id());
170+
assert_eq!(body_owner_def_id, closure_def_id);
170171
let region_scope_tree = &self.tcx.region_scope_tree(body_owner_def_id);
171172
let mut delegate = InferBorrowKind {
172173
fcx: self,
@@ -178,6 +179,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
178179
euv::ExprUseVisitor::with_infer(
179180
&mut delegate,
180181
&self.infcx,
182+
body_owner_def_id,
181183
self.param_env,
182184
region_scope_tree,
183185
&self.tables.borrow(),

0 commit comments

Comments
 (0)