Skip to content

Commit 2d280a5

Browse files
committed
more LocalDefId cleanup
1 parent 1875c79 commit 2d280a5

File tree

7 files changed

+259
-260
lines changed

7 files changed

+259
-260
lines changed

src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs

+19-22
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,27 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
2828
br: &ty::BoundRegion,
2929
) -> Option<(&hir::Ty<'tcx>, &hir::FnDecl<'tcx>)> {
3030
if let Some(anon_reg) = self.tcx().is_suitable_region(region) {
31-
let def_id = anon_reg.def_id;
32-
if let Some(def_id) = def_id.as_local() {
33-
let hir_id = self.tcx().hir().as_local_hir_id(def_id);
34-
let fndecl = match self.tcx().hir().get(hir_id) {
35-
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. })
36-
| Node::TraitItem(&hir::TraitItem {
37-
kind: hir::TraitItemKind::Fn(ref m, ..),
38-
..
39-
})
40-
| Node::ImplItem(&hir::ImplItem {
41-
kind: hir::ImplItemKind::Fn(ref m, ..),
42-
..
43-
}) => &m.decl,
44-
_ => return None,
45-
};
31+
let hir_id = self.tcx().hir().as_local_hir_id(anon_reg.def_id);
32+
let fndecl = match self.tcx().hir().get(hir_id) {
33+
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. })
34+
| Node::TraitItem(&hir::TraitItem {
35+
kind: hir::TraitItemKind::Fn(ref m, ..),
36+
..
37+
})
38+
| Node::ImplItem(&hir::ImplItem {
39+
kind: hir::ImplItemKind::Fn(ref m, ..), ..
40+
}) => &m.decl,
41+
_ => return None,
42+
};
4643

47-
return fndecl
48-
.inputs
49-
.iter()
50-
.find_map(|arg| self.find_component_for_bound_region(arg, br))
51-
.map(|ty| (ty, &**fndecl));
52-
}
44+
fndecl
45+
.inputs
46+
.iter()
47+
.find_map(|arg| self.find_component_for_bound_region(arg, br))
48+
.map(|ty| (ty, &**fndecl))
49+
} else {
50+
None
5351
}
54-
None
5552
}
5653

5754
// This method creates a FindNestedTypeVisitor which returns the type corresponding

src/librustc_infer/infer/error_reporting/nice_region_error/named_anon_conflict.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
7575
}
7676

7777
if let Some((_, fndecl)) = self.find_anon_type(anon, &br) {
78-
let is_self_anon = self.is_self_anon(is_first, scope_def_id);
79-
if is_self_anon {
78+
if self.is_self_anon(is_first, scope_def_id) {
8079
return None;
8180
}
8281

src/librustc_infer/infer/error_reporting/nice_region_error/static_impl_trait.rs

+196-199
Large diffs are not rendered by default.

src/librustc_infer/infer/error_reporting/nice_region_error/util.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
55
use rustc_hir as hir;
6-
use rustc_hir::def_id::DefId;
6+
use rustc_hir::def_id::LocalDefId;
77
use rustc_middle::ty::{self, DefIdTree, Region, Ty};
88
use rustc_span::Span;
99

@@ -92,7 +92,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
9292
// FIXME(#42703) - Need to handle certain cases here.
9393
pub(super) fn is_return_type_anon(
9494
&self,
95-
scope_def_id: DefId,
95+
scope_def_id: LocalDefId,
9696
br: ty::BoundRegion,
9797
decl: &hir::FnDecl<'_>,
9898
) -> Option<Span> {
@@ -112,9 +112,12 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
112112
// corresponds to self and if yes, we display E0312.
113113
// FIXME(#42700) - Need to format self properly to
114114
// enable E0621 for it.
115-
pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: DefId) -> bool {
115+
pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: LocalDefId) -> bool {
116116
is_first
117-
&& self.tcx().opt_associated_item(scope_def_id).map(|i| i.fn_has_self_parameter)
117+
&& self
118+
.tcx()
119+
.opt_associated_item(scope_def_id.to_def_id())
120+
.map(|i| i.fn_has_self_parameter)
118121
== Some(true)
119122
}
120123
}

src/librustc_middle/ty/context.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -873,8 +873,8 @@ impl<'tcx> CommonConsts<'tcx> {
873873
// conflict.
874874
#[derive(Debug)]
875875
pub struct FreeRegionInfo {
876-
// def id corresponding to FreeRegion
877-
pub def_id: DefId,
876+
// `LocalDefId` corresponding to FreeRegion
877+
pub def_id: LocalDefId,
878878
// the bound region corresponding to FreeRegion
879879
pub boundregion: ty::BoundRegion,
880880
// checks if bound region is in Impl Item
@@ -1412,14 +1412,17 @@ impl<'tcx> TyCtxt<'tcx> {
14121412
// Returns the `DefId` and the `BoundRegion` corresponding to the given region.
14131413
pub fn is_suitable_region(&self, region: Region<'tcx>) -> Option<FreeRegionInfo> {
14141414
let (suitable_region_binding_scope, bound_region) = match *region {
1415-
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
1416-
ty::ReEarlyBound(ref ebr) => {
1417-
(self.parent(ebr.def_id).unwrap(), ty::BoundRegion::BrNamed(ebr.def_id, ebr.name))
1415+
ty::ReFree(ref free_region) => {
1416+
(free_region.scope.expect_local(), free_region.bound_region)
14181417
}
1418+
ty::ReEarlyBound(ref ebr) => (
1419+
self.parent(ebr.def_id).unwrap().expect_local(),
1420+
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
1421+
),
14191422
_ => return None, // not a free region
14201423
};
14211424

1422-
let hir_id = self.hir().as_local_hir_id(suitable_region_binding_scope.expect_local());
1425+
let hir_id = self.hir().as_local_hir_id(suitable_region_binding_scope);
14231426
let is_impl_item = match self.hir().find(hir_id) {
14241427
Some(Node::Item(..) | Node::TraitItem(..)) => false,
14251428
Some(Node::ImplItem(..)) => {
@@ -1515,8 +1518,9 @@ impl<'tcx> TyCtxt<'tcx> {
15151518
}
15161519

15171520
// Checks if the bound region is in Impl Item.
1518-
pub fn is_bound_region_in_impl_item(&self, suitable_region_binding_scope: DefId) -> bool {
1519-
let container_id = self.associated_item(suitable_region_binding_scope).container.id();
1521+
pub fn is_bound_region_in_impl_item(&self, suitable_region_binding_scope: LocalDefId) -> bool {
1522+
let container_id =
1523+
self.associated_item(suitable_region_binding_scope.to_def_id()).container.id();
15201524
if self.impl_trait_ref(container_id).is_some() {
15211525
// For now, we do not try to target impls of traits. This is
15221526
// because this message is going to suggest that the user

src/librustc_mir/borrow_check/diagnostics/region_errors.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
579579
if let (Some(f), Some(ty::RegionKind::ReStatic)) =
580580
(self.to_error_region(fr), self.to_error_region(outlived_fr))
581581
{
582-
if let Some((ty::TyS { kind: ty::Opaque(did, substs), .. }, _)) = self
582+
if let Some((&ty::TyS { kind: ty::Opaque(did, substs), .. }, _)) = self
583583
.infcx
584584
.tcx
585585
.is_suitable_region(f)
586-
.map(|r| r.def_id.expect_local())
586+
.map(|r| r.def_id)
587587
.map(|id| self.infcx.tcx.return_type_impl_trait(id))
588588
.unwrap_or(None)
589589
{
@@ -592,7 +592,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
592592
//
593593
// eg. check for `impl Trait + 'static` instead of `impl Trait`.
594594
let has_static_predicate = {
595-
let predicates_of = self.infcx.tcx.predicates_of(*did);
595+
let predicates_of = self.infcx.tcx.predicates_of(did);
596596
let bounds = predicates_of.instantiate(self.infcx.tcx, substs);
597597

598598
let mut found = false;
@@ -625,7 +625,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
625625
diag.help(&format!("consider replacing `{}` with `{}`", fr_name, static_str));
626626
} else {
627627
// Otherwise, we should suggest adding a constraint on the return type.
628-
let span = self.infcx.tcx.def_span(*did);
628+
let span = self.infcx.tcx.def_span(did);
629629
if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
630630
let suggestable_fr_name = if fr_name.was_named() {
631631
fr_name.to_string()

src/librustc_mir/borrow_check/universal_regions.rs

+20-21
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ impl<'tcx> UniversalRegions<'tcx> {
232232
) -> Self {
233233
let tcx = infcx.tcx;
234234
let mir_hir_id = tcx.hir().as_local_hir_id(mir_def_id);
235-
UniversalRegionsBuilder { infcx, mir_def_id: mir_def_id.to_def_id(), mir_hir_id, param_env }
236-
.build()
235+
UniversalRegionsBuilder { infcx, mir_def_id, mir_hir_id, param_env }.build()
237236
}
238237

239238
/// Given a reference to a closure type, extracts all the values
@@ -389,7 +388,7 @@ impl<'tcx> UniversalRegions<'tcx> {
389388

390389
struct UniversalRegionsBuilder<'cx, 'tcx> {
391390
infcx: &'cx InferCtxt<'cx, 'tcx>,
392-
mir_def_id: DefId,
391+
mir_def_id: LocalDefId,
393392
mir_hir_id: HirId,
394393
param_env: ty::ParamEnv<'tcx>,
395394
}
@@ -418,15 +417,15 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
418417
let mut indices = self.compute_indices(fr_static, defining_ty);
419418
debug!("build: indices={:?}", indices);
420419

421-
let closure_base_def_id = self.infcx.tcx.closure_base_def_id(self.mir_def_id);
420+
let closure_base_def_id = self.infcx.tcx.closure_base_def_id(self.mir_def_id.to_def_id());
422421

423422
// If this is a closure or generator, then the late-bound regions from the enclosing
424423
// function are actually external regions to us. For example, here, 'a is not local
425424
// to the closure c (although it is local to the fn foo):
426425
// fn foo<'a>() {
427426
// let c = || { let x: &'a u32 = ...; }
428427
// }
429-
if self.mir_def_id != closure_base_def_id {
428+
if self.mir_def_id.to_def_id() != closure_base_def_id {
430429
self.infcx.replace_late_bound_regions_with_nll_infer_vars(self.mir_def_id, &mut indices)
431430
}
432431

@@ -443,7 +442,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
443442
);
444443
// Converse of above, if this is a function then the late-bound regions declared on its
445444
// signature are local to the fn.
446-
if self.mir_def_id == closure_base_def_id {
445+
if self.mir_def_id.to_def_id() == closure_base_def_id {
447446
self.infcx
448447
.replace_late_bound_regions_with_nll_infer_vars(self.mir_def_id, &mut indices);
449448
}
@@ -508,14 +507,14 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
508507
/// see `DefiningTy` for details.
509508
fn defining_ty(&self) -> DefiningTy<'tcx> {
510509
let tcx = self.infcx.tcx;
511-
let closure_base_def_id = tcx.closure_base_def_id(self.mir_def_id);
510+
let closure_base_def_id = tcx.closure_base_def_id(self.mir_def_id.to_def_id());
512511

513512
match tcx.hir().body_owner_kind(self.mir_hir_id) {
514513
BodyOwnerKind::Closure | BodyOwnerKind::Fn => {
515-
let defining_ty = if self.mir_def_id == closure_base_def_id {
514+
let defining_ty = if self.mir_def_id.to_def_id() == closure_base_def_id {
516515
tcx.type_of(closure_base_def_id)
517516
} else {
518-
let tables = tcx.typeck_tables_of(self.mir_def_id.expect_local());
517+
let tables = tcx.typeck_tables_of(self.mir_def_id);
519518
tables.node_type(self.mir_hir_id)
520519
};
521520

@@ -540,11 +539,11 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
540539
}
541540

542541
BodyOwnerKind::Const | BodyOwnerKind::Static(..) => {
543-
assert_eq!(closure_base_def_id, self.mir_def_id);
542+
assert_eq!(self.mir_def_id.to_def_id(), closure_base_def_id);
544543
let identity_substs = InternalSubsts::identity_for_item(tcx, closure_base_def_id);
545544
let substs =
546545
self.infcx.replace_free_regions_with_nll_infer_vars(FR, &identity_substs);
547-
DefiningTy::Const(self.mir_def_id, substs)
546+
DefiningTy::Const(self.mir_def_id.to_def_id(), substs)
548547
}
549548
}
550549
}
@@ -559,7 +558,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
559558
defining_ty: DefiningTy<'tcx>,
560559
) -> UniversalRegionIndices<'tcx> {
561560
let tcx = self.infcx.tcx;
562-
let closure_base_def_id = tcx.closure_base_def_id(self.mir_def_id);
561+
let closure_base_def_id = tcx.closure_base_def_id(self.mir_def_id.to_def_id());
563562
let identity_substs = InternalSubsts::identity_for_item(tcx, closure_base_def_id);
564563
let fr_substs = match defining_ty {
565564
DefiningTy::Closure(_, ref substs) | DefiningTy::Generator(_, ref substs, _) => {
@@ -593,7 +592,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
593592
let tcx = self.infcx.tcx;
594593
match defining_ty {
595594
DefiningTy::Closure(def_id, substs) => {
596-
assert_eq!(self.mir_def_id, def_id);
595+
assert_eq!(self.mir_def_id.to_def_id(), def_id);
597596
let closure_sig = substs.as_closure().sig();
598597
let inputs_and_output = closure_sig.inputs_and_output();
599598
let closure_ty = tcx.closure_env_ty(def_id, substs).unwrap();
@@ -617,7 +616,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
617616
}
618617

619618
DefiningTy::Generator(def_id, substs, movability) => {
620-
assert_eq!(self.mir_def_id, def_id);
619+
assert_eq!(self.mir_def_id.to_def_id(), def_id);
621620
let resume_ty = substs.as_generator().resume_ty();
622621
let output = substs.as_generator().return_ty();
623622
let generator_ty = tcx.mk_generator(def_id, substs, movability);
@@ -635,7 +634,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
635634
DefiningTy::Const(def_id, _) => {
636635
// For a constant body, there are no inputs, and one
637636
// "output" (the type of the constant).
638-
assert_eq!(self.mir_def_id, def_id);
637+
assert_eq!(self.mir_def_id.to_def_id(), def_id);
639638
let ty = tcx.type_of(def_id);
640639
let ty = indices.fold_to_region_vids(tcx, &ty);
641640
ty::Binder::dummy(tcx.intern_type_list(&[ty]))
@@ -656,7 +655,7 @@ trait InferCtxtExt<'tcx> {
656655
fn replace_bound_regions_with_nll_infer_vars<T>(
657656
&self,
658657
origin: NLLRegionVariableOrigin,
659-
all_outlive_scope: DefId,
658+
all_outlive_scope: LocalDefId,
660659
value: &ty::Binder<T>,
661660
indices: &mut UniversalRegionIndices<'tcx>,
662661
) -> T
@@ -665,7 +664,7 @@ trait InferCtxtExt<'tcx> {
665664

666665
fn replace_late_bound_regions_with_nll_infer_vars(
667666
&self,
668-
mir_def_id: DefId,
667+
mir_def_id: LocalDefId,
669668
indices: &mut UniversalRegionIndices<'tcx>,
670669
);
671670
}
@@ -685,7 +684,7 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> {
685684
fn replace_bound_regions_with_nll_infer_vars<T>(
686685
&self,
687686
origin: NLLRegionVariableOrigin,
688-
all_outlive_scope: DefId,
687+
all_outlive_scope: LocalDefId,
689688
value: &ty::Binder<T>,
690689
indices: &mut UniversalRegionIndices<'tcx>,
691690
) -> T
@@ -699,7 +698,7 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> {
699698
let (value, _map) = self.tcx.replace_late_bound_regions(value, |br| {
700699
debug!("replace_bound_regions_with_nll_infer_vars: br={:?}", br);
701700
let liberated_region = self.tcx.mk_region(ty::ReFree(ty::FreeRegion {
702-
scope: all_outlive_scope,
701+
scope: all_outlive_scope.to_def_id(),
703702
bound_region: br,
704703
}));
705704
let region_vid = self.next_nll_region_var(origin);
@@ -724,11 +723,11 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> {
724723
/// inputs vector.
725724
fn replace_late_bound_regions_with_nll_infer_vars(
726725
&self,
727-
mir_def_id: DefId,
726+
mir_def_id: LocalDefId,
728727
indices: &mut UniversalRegionIndices<'tcx>,
729728
) {
730729
debug!("replace_late_bound_regions_with_nll_infer_vars(mir_def_id={:?})", mir_def_id);
731-
let closure_base_def_id = self.tcx.closure_base_def_id(mir_def_id);
730+
let closure_base_def_id = self.tcx.closure_base_def_id(mir_def_id.to_def_id());
732731
for_each_late_bound_region_defined_on(self.tcx, closure_base_def_id, |r| {
733732
debug!("replace_late_bound_regions_with_nll_infer_vars: r={:?}", r);
734733
if !indices.indices.contains_key(&r) {

0 commit comments

Comments
 (0)