Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace more DefIds with LocalDefId #73796

Merged
merged 3 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,27 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
br: &ty::BoundRegion,
) -> Option<(&hir::Ty<'tcx>, &hir::FnDecl<'tcx>)> {
if let Some(anon_reg) = self.tcx().is_suitable_region(region) {
let def_id = anon_reg.def_id;
if let Some(def_id) = def_id.as_local() {
let hir_id = self.tcx().hir().as_local_hir_id(def_id);
let fndecl = match self.tcx().hir().get(hir_id) {
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. })
| Node::TraitItem(&hir::TraitItem {
kind: hir::TraitItemKind::Fn(ref m, ..),
..
})
| Node::ImplItem(&hir::ImplItem {
kind: hir::ImplItemKind::Fn(ref m, ..),
..
}) => &m.decl,
_ => return None,
};
let hir_id = self.tcx().hir().as_local_hir_id(anon_reg.def_id);
let fndecl = match self.tcx().hir().get(hir_id) {
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. })
| Node::TraitItem(&hir::TraitItem {
kind: hir::TraitItemKind::Fn(ref m, ..),
..
})
| Node::ImplItem(&hir::ImplItem {
kind: hir::ImplItemKind::Fn(ref m, ..), ..
}) => &m.decl,
_ => return None,
};

return fndecl
.inputs
.iter()
.find_map(|arg| self.find_component_for_bound_region(arg, br))
.map(|ty| (ty, &**fndecl));
}
fndecl
.inputs
.iter()
.find_map(|arg| self.find_component_for_bound_region(arg, br))
.map(|ty| (ty, &**fndecl))
} else {
None
}
None
}

// This method creates a FindNestedTypeVisitor which returns the type corresponding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
}

if let Some((_, fndecl)) = self.find_anon_type(anon, &br) {
let is_self_anon = self.is_self_anon(is_first, scope_def_id);
if is_self_anon {
if self.is_self_anon(is_first, scope_def_id) {
return None;
}

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::LocalDefId;
use rustc_middle::ty::{self, DefIdTree, Region, Ty};
use rustc_span::Span;

Expand Down Expand Up @@ -92,7 +92,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
// FIXME(#42703) - Need to handle certain cases here.
pub(super) fn is_return_type_anon(
&self,
scope_def_id: DefId,
scope_def_id: LocalDefId,
br: ty::BoundRegion,
decl: &hir::FnDecl<'_>,
) -> Option<Span> {
Expand All @@ -112,9 +112,12 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
// corresponds to self and if yes, we display E0312.
// FIXME(#42700) - Need to format self properly to
// enable E0621 for it.
pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: DefId) -> bool {
pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: LocalDefId) -> bool {
is_first
&& self.tcx().opt_associated_item(scope_def_id).map(|i| i.fn_has_self_parameter)
&& self
.tcx()
.opt_associated_item(scope_def_id.to_def_id())
.map(|i| i.fn_has_self_parameter)
== Some(true)
}
}
4 changes: 2 additions & 2 deletions src/librustc_middle/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,11 @@ impl<'hir> Map<'hir> {
}
}

pub fn visit_item_likes_in_module<V>(&self, module: DefId, visitor: &mut V)
pub fn visit_item_likes_in_module<V>(&self, module: LocalDefId, visitor: &mut V)
where
V: ItemLikeVisitor<'hir>,
{
let module = self.tcx.hir_module_items(module.expect_local());
let module = self.tcx.hir_module_items(module);

for id in &module.items {
visitor.visit_item(self.expect_item(*id));
Expand Down
26 changes: 13 additions & 13 deletions src/librustc_middle/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use rustc_query_system::query::QueryDescription;
use rustc_span::symbol::Symbol;
use std::borrow::Cow;

fn describe_as_module(def_id: DefId, tcx: TyCtxt<'_>) -> String {
fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String {
if def_id.is_top_level_module() {
"top-level module".to_string()
} else {
format!("module `{}`", tcx.def_path_str(def_id))
format!("module `{}`", tcx.def_path_str(def_id.to_def_id()))
}
}

Expand Down Expand Up @@ -473,49 +473,49 @@ rustc_queries! {

Other {
query lint_mod(key: LocalDefId) -> () {
desc { |tcx| "linting {}", describe_as_module(key.to_def_id(), tcx) }
desc { |tcx| "linting {}", describe_as_module(key, tcx) }
}

/// Checks the attributes in the module.
query check_mod_attrs(key: DefId) -> () {
query check_mod_attrs(key: LocalDefId) -> () {
desc { |tcx| "checking attributes in {}", describe_as_module(key, tcx) }
}

query check_mod_unstable_api_usage(key: DefId) -> () {
query check_mod_unstable_api_usage(key: LocalDefId) -> () {
desc { |tcx| "checking for unstable API usage in {}", describe_as_module(key, tcx) }
}

/// Checks the const bodies in the module for illegal operations (e.g. `if` or `loop`).
query check_mod_const_bodies(key: DefId) -> () {
query check_mod_const_bodies(key: LocalDefId) -> () {
desc { |tcx| "checking consts in {}", describe_as_module(key, tcx) }
}

/// Checks the loops in the module.
query check_mod_loops(key: DefId) -> () {
query check_mod_loops(key: LocalDefId) -> () {
desc { |tcx| "checking loops in {}", describe_as_module(key, tcx) }
}

query check_mod_item_types(key: DefId) -> () {
query check_mod_item_types(key: LocalDefId) -> () {
desc { |tcx| "checking item types in {}", describe_as_module(key, tcx) }
}

query check_mod_privacy(key: LocalDefId) -> () {
desc { |tcx| "checking privacy in {}", describe_as_module(key.to_def_id(), tcx) }
desc { |tcx| "checking privacy in {}", describe_as_module(key, tcx) }
}

query check_mod_intrinsics(key: DefId) -> () {
query check_mod_intrinsics(key: LocalDefId) -> () {
desc { |tcx| "checking intrinsics in {}", describe_as_module(key, tcx) }
}

query check_mod_liveness(key: DefId) -> () {
query check_mod_liveness(key: LocalDefId) -> () {
desc { |tcx| "checking liveness of variables in {}", describe_as_module(key, tcx) }
}

query check_mod_impl_wf(key: DefId) -> () {
query check_mod_impl_wf(key: LocalDefId) -> () {
desc { |tcx| "checking that impls are well-formed in {}", describe_as_module(key, tcx) }
}

query collect_mod_item_types(key: DefId) -> () {
query collect_mod_item_types(key: LocalDefId) -> () {
desc { |tcx| "collecting item types in {}", describe_as_module(key, tcx) }
}

Expand Down
31 changes: 19 additions & 12 deletions src/librustc_middle/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,8 @@ impl<'tcx> CommonConsts<'tcx> {
// conflict.
#[derive(Debug)]
pub struct FreeRegionInfo {
// def id corresponding to FreeRegion
pub def_id: DefId,
// `LocalDefId` corresponding to FreeRegion
pub def_id: LocalDefId,
// the bound region corresponding to FreeRegion
pub boundregion: ty::BoundRegion,
// checks if bound region is in Impl Item
Expand Down Expand Up @@ -1412,14 +1412,17 @@ impl<'tcx> TyCtxt<'tcx> {
// Returns the `DefId` and the `BoundRegion` corresponding to the given region.
pub fn is_suitable_region(&self, region: Region<'tcx>) -> Option<FreeRegionInfo> {
let (suitable_region_binding_scope, bound_region) = match *region {
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
ty::ReEarlyBound(ref ebr) => {
(self.parent(ebr.def_id).unwrap(), ty::BoundRegion::BrNamed(ebr.def_id, ebr.name))
ty::ReFree(ref free_region) => {
(free_region.scope.expect_local(), free_region.bound_region)
}
ty::ReEarlyBound(ref ebr) => (
self.parent(ebr.def_id).unwrap().expect_local(),
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
),
_ => return None, // not a free region
};

let hir_id = self.hir().as_local_hir_id(suitable_region_binding_scope.expect_local());
let hir_id = self.hir().as_local_hir_id(suitable_region_binding_scope);
let is_impl_item = match self.hir().find(hir_id) {
Some(Node::Item(..) | Node::TraitItem(..)) => false,
Some(Node::ImplItem(..)) => {
Expand All @@ -1436,8 +1439,11 @@ impl<'tcx> TyCtxt<'tcx> {
}

/// Given a `DefId` for an `fn`, return all the `dyn` and `impl` traits in its return type.
pub fn return_type_impl_or_dyn_traits(&self, scope_def_id: DefId) -> Vec<&'tcx hir::Ty<'tcx>> {
let hir_id = self.hir().as_local_hir_id(scope_def_id.expect_local());
pub fn return_type_impl_or_dyn_traits(
&self,
scope_def_id: LocalDefId,
) -> Vec<&'tcx hir::Ty<'tcx>> {
let hir_id = self.hir().as_local_hir_id(scope_def_id);
let hir_output = match self.hir().get(hir_id) {
Node::Item(hir::Item {
kind:
Expand Down Expand Up @@ -1480,9 +1486,9 @@ impl<'tcx> TyCtxt<'tcx> {
v.0
}

pub fn return_type_impl_trait(&self, scope_def_id: DefId) -> Option<(Ty<'tcx>, Span)> {
pub fn return_type_impl_trait(&self, scope_def_id: LocalDefId) -> Option<(Ty<'tcx>, Span)> {
// HACK: `type_of_def_id()` will fail on these (#55796), so return `None`.
let hir_id = self.hir().as_local_hir_id(scope_def_id.expect_local());
let hir_id = self.hir().as_local_hir_id(scope_def_id);
match self.hir().get(hir_id) {
Node::Item(item) => {
match item.kind {
Expand Down Expand Up @@ -1512,8 +1518,9 @@ impl<'tcx> TyCtxt<'tcx> {
}

// Checks if the bound region is in Impl Item.
pub fn is_bound_region_in_impl_item(&self, suitable_region_binding_scope: DefId) -> bool {
let container_id = self.associated_item(suitable_region_binding_scope).container.id();
pub fn is_bound_region_in_impl_item(&self, suitable_region_binding_scope: LocalDefId) -> bool {
let container_id =
self.associated_item(suitable_region_binding_scope.to_def_id()).container.id();
if self.impl_trait_ref(container_id).is_some() {
// For now, we do not try to target impls of traits. This is
// because this message is going to suggest that the user
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/borrow_check/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
if let (Some(f), Some(ty::RegionKind::ReStatic)) =
(self.to_error_region(fr), self.to_error_region(outlived_fr))
{
if let Some((ty::TyS { kind: ty::Opaque(did, substs), .. }, _)) = self
if let Some((&ty::TyS { kind: ty::Opaque(did, substs), .. }, _)) = self
.infcx
.tcx
.is_suitable_region(f)
Expand All @@ -592,7 +592,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
//
// eg. check for `impl Trait + 'static` instead of `impl Trait`.
let has_static_predicate = {
let predicates_of = self.infcx.tcx.predicates_of(*did);
let predicates_of = self.infcx.tcx.predicates_of(did);
let bounds = predicates_of.instantiate(self.infcx.tcx, substs);

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