1
Fork 0

Auto merge of #91299 - cjgillot:expect-ldid, r=petrochenkov

Take a LocalDefId in expect_*item.

Items and item-likes are always HIR owners.
When trying to find such nodes, there is no ambiguity, the `LocalDefId` and the `HirId::owner` always match.
In such cases, `local_def_id_to_hir_id` does not carry any meaningful information, so we can just skip calling it altogether.
This commit is contained in:
bors 2021-11-29 15:02:01 +00:00
commit 6db0a0e9a4
28 changed files with 95 additions and 137 deletions

View file

@ -458,7 +458,7 @@ pub(super) fn check_opaque_for_inheriting_lifetimes(
def_id: LocalDefId,
span: Span,
) {
let item = tcx.hir().expect_item(tcx.hir().local_def_id_to_hir_id(def_id));
let item = tcx.hir().expect_item(def_id);
debug!(?item, ?span);
struct FoundParentLifetime;

View file

@ -1637,11 +1637,10 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(fcx, ty);
// Get the `impl Trait`'s `DefId`.
if let ty::Opaque(def_id, _) = ty.kind() {
let hir_id = fcx.tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
// Get the `impl Trait`'s `Item` so that we can get its trait bounds and
// get the `Trait`'s `DefId`.
if let hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, .. }) =
fcx.tcx.hir().expect_item(hir_id).kind
fcx.tcx.hir().expect_item(def_id.expect_local()).kind
{
// Are of this `impl Trait`'s traits object safe?
is_object_safe = bounds.iter().all(|bound| {

View file

@ -322,9 +322,7 @@ fn compare_predicate_entailment<'tcx>(
// When the `impl` receiver is an arbitrary self type, like `self: Box<Self>`, the
// span points only at the type `Box<Self`>, but we want to cover the whole
// argument pattern and type.
let impl_m_hir_id =
tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local());
let span = match tcx.hir().expect_impl_item(impl_m_hir_id).kind {
let span = match tcx.hir().expect_impl_item(impl_m.def_id.expect_local()).kind {
ImplItemKind::Fn(ref sig, body) => tcx
.hir()
.body_param_names(body)
@ -346,9 +344,7 @@ fn compare_predicate_entailment<'tcx>(
if trait_sig.inputs().len() == *i {
// Suggestion to change output type. We do not suggest in `async` functions
// to avoid complex logic or incorrect output.
let impl_m_hir_id =
tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local());
match tcx.hir().expect_impl_item(impl_m_hir_id).kind {
match tcx.hir().expect_impl_item(impl_m.def_id.expect_local()).kind {
ImplItemKind::Fn(ref sig, _)
if sig.header.asyncness == hir::IsAsync::NotAsync =>
{
@ -467,22 +463,19 @@ fn extract_spans_for_error_reporting<'a, 'tcx>(
trait_m: &ty::AssocItem,
) -> (Span, Option<Span>) {
let tcx = infcx.tcx;
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local());
let mut impl_args = match tcx.hir().expect_impl_item(impl_m_hir_id).kind {
let mut impl_args = match tcx.hir().expect_impl_item(impl_m.def_id.expect_local()).kind {
ImplItemKind::Fn(ref sig, _) => {
sig.decl.inputs.iter().map(|t| t.span).chain(iter::once(sig.decl.output.span()))
}
_ => bug!("{:?} is not a method", impl_m),
};
let trait_args = trait_m.def_id.as_local().map(|def_id| {
let trait_m_hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
match tcx.hir().expect_trait_item(trait_m_hir_id).kind {
let trait_args =
trait_m.def_id.as_local().map(|def_id| match tcx.hir().expect_trait_item(def_id).kind {
TraitItemKind::Fn(ref sig, _) => {
sig.decl.inputs.iter().map(|t| t.span).chain(iter::once(sig.decl.output.span()))
}
_ => bug!("{:?} is not a TraitItemKind::Fn", trait_m),
}
});
});
match *terr {
TypeError::ArgumentMutability(i) => {
@ -600,8 +593,7 @@ fn compare_number_of_generics<'tcx>(
err_occurred = true;
let (trait_spans, impl_trait_spans) = if let Some(def_id) = trait_.def_id.as_local() {
let trait_hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let trait_item = tcx.hir().expect_trait_item(trait_hir_id);
let trait_item = tcx.hir().expect_trait_item(def_id);
if trait_item.generics.params.is_empty() {
(Some(vec![trait_item.generics.span]), vec![])
} else {
@ -622,8 +614,7 @@ fn compare_number_of_generics<'tcx>(
(trait_span.map(|s| vec![s]), vec![])
};
let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_.def_id.expect_local());
let impl_item = tcx.hir().expect_impl_item(impl_hir_id);
let impl_item = tcx.hir().expect_impl_item(impl_.def_id.expect_local());
let impl_item_impl_trait_spans: Vec<Span> = impl_item
.generics
.params
@ -711,8 +702,7 @@ fn compare_number_of_method_arguments<'tcx>(
let impl_number_args = impl_m_fty.inputs().skip_binder().len();
if trait_number_args != impl_number_args {
let trait_span = if let Some(def_id) = trait_m.def_id.as_local() {
let trait_id = tcx.hir().local_def_id_to_hir_id(def_id);
match tcx.hir().expect_trait_item(trait_id).kind {
match tcx.hir().expect_trait_item(def_id).kind {
TraitItemKind::Fn(ref trait_m_sig, _) => {
let pos = if trait_number_args > 0 { trait_number_args - 1 } else { 0 };
if let Some(arg) = trait_m_sig.decl.inputs.get(pos) {
@ -730,8 +720,7 @@ fn compare_number_of_method_arguments<'tcx>(
} else {
trait_item_span
};
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local());
let impl_span = match tcx.hir().expect_impl_item(impl_m_hir_id).kind {
let impl_span = match tcx.hir().expect_impl_item(impl_m.def_id.expect_local()).kind {
ImplItemKind::Fn(ref impl_m_sig, _) => {
let pos = if impl_number_args > 0 { impl_number_args - 1 } else { 0 };
if let Some(arg) = impl_m_sig.decl.inputs.get(pos) {
@ -1055,7 +1044,7 @@ crate fn compare_const_impl<'tcx>(
);
// Locate the Span containing just the type of the offending impl
match tcx.hir().expect_impl_item(impl_c_hir_id).kind {
match tcx.hir().expect_impl_item(impl_c.def_id.expect_local()).kind {
ImplItemKind::Const(ref ty, _) => cause.make_mut().span = ty.span,
_ => bug!("{:?} is not a impl const", impl_c),
}
@ -1068,11 +1057,9 @@ crate fn compare_const_impl<'tcx>(
trait_c.ident
);
let trait_c_hir_id =
trait_c.def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id));
let trait_c_span = trait_c_hir_id.map(|trait_c_hir_id| {
let trait_c_span = trait_c.def_id.as_local().map(|trait_c_def_id| {
// Add a label to the Span containing just the type of the const
match tcx.hir().expect_trait_item(trait_c_hir_id).kind {
match tcx.hir().expect_trait_item(trait_c_def_id).kind {
TraitItemKind::Const(ref ty, _) => ty.span,
_ => bug!("{:?} is not a trait const", trait_c),
}

View file

@ -1097,12 +1097,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
(_, _) => return None,
};
let last_hir_id = self.tcx.hir().local_def_id_to_hir_id(last_local_id);
let exp_hir_id = self.tcx.hir().local_def_id_to_hir_id(exp_local_id);
match (
&self.tcx.hir().expect_item(last_hir_id).kind,
&self.tcx.hir().expect_item(exp_hir_id).kind,
&self.tcx.hir().expect_item(last_local_id).kind,
&self.tcx.hir().expect_item(exp_local_id).kind,
) {
(
hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds: last_bounds, .. }),

View file

@ -345,10 +345,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let import_items: Vec<_> = applicable_trait
.import_ids
.iter()
.map(|&import_id| {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(import_id);
self.tcx.hir().expect_item(hir_id)
})
.map(|&import_id| self.tcx.hir().expect_item(import_id))
.collect();
// Find an identifier with which this trait was imported (note that `_` doesn't count).

View file

@ -84,8 +84,7 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> {
/// the types first.
#[instrument(skip(tcx), level = "debug")]
pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let item = tcx.hir().expect_item(hir_id);
let item = tcx.hir().expect_item(def_id);
debug!(
?item.def_id,
@ -197,7 +196,7 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let trait_item = tcx.hir().expect_trait_item(hir_id);
let trait_item = tcx.hir().expect_trait_item(def_id);
let (method_sig, span) = match trait_item.kind {
hir::TraitItemKind::Fn(ref sig, _) => (Some(sig), trait_item.span),
@ -207,8 +206,8 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
check_object_unsafe_self_trait_by_name(tcx, trait_item);
check_associated_item(tcx, trait_item.def_id, span, method_sig);
let encl_trait_hir_id = tcx.hir().get_parent_item(hir_id);
let encl_trait = tcx.hir().expect_item(encl_trait_hir_id);
let encl_trait_def_id = tcx.hir().get_parent_did(hir_id);
let encl_trait = tcx.hir().expect_item(encl_trait_def_id);
let encl_trait_def_id = encl_trait.def_id.to_def_id();
let fn_lang_item_name = if Some(encl_trait_def_id) == tcx.lang_items().fn_trait() {
Some("fn")
@ -680,8 +679,7 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem
}
pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let impl_item = tcx.hir().expect_impl_item(hir_id);
let impl_item = tcx.hir().expect_impl_item(def_id);
let (method_sig, span) = match impl_item.kind {
hir::ImplItemKind::Fn(ref sig, _) => (Some(sig), impl_item.span),

View file

@ -119,13 +119,13 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
for extern_crate in &crates_to_lint {
let def_id = extern_crate.def_id.expect_local();
let id = tcx.hir().local_def_id_to_hir_id(def_id);
let item = tcx.hir().expect_item(id);
let item = tcx.hir().expect_item(def_id);
// If the crate is fully unused, we suggest removing it altogether.
// We do this in any edition.
if extern_crate.warn_if_unused {
if let Some(&span) = unused_extern_crates.get(&def_id) {
let id = tcx.hir().local_def_id_to_hir_id(def_id);
tcx.struct_span_lint_hir(lint, id, span, |lint| {
// Removal suggestion span needs to include attributes (Issue #54400)
let span_with_attrs = tcx
@ -173,6 +173,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
if !tcx.get_attrs(extern_crate.def_id).is_empty() {
continue;
}
let id = tcx.hir().local_def_id_to_hir_id(def_id);
tcx.struct_span_lint_hir(lint, id, extern_crate.span, |lint| {
// Otherwise, we can convert it into a `use` of some kind.
let base_replacement = match extern_crate.orig_name {

View file

@ -52,8 +52,7 @@ fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
return;
}
let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did);
let sp = match tcx.hir().expect_item(impl_hir_id).kind {
let sp = match tcx.hir().expect_item(impl_did).kind {
ItemKind::Impl(ref impl_) => impl_.self_ty.span,
_ => bug!("expected Drop impl item"),
};
@ -78,7 +77,7 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
match can_type_implement_copy(tcx, param_env, self_type) {
Ok(()) => {}
Err(CopyImplementationError::InfrigingFields(fields)) => {
let item = tcx.hir().expect_item(impl_hir_id);
let item = tcx.hir().expect_item(impl_did);
let span = if let ItemKind::Impl(hir::Impl { of_trait: Some(ref tr), .. }) = item.kind {
tr.path.span
} else {
@ -97,7 +96,7 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
err.emit()
}
Err(CopyImplementationError::NotAnAdt) => {
let item = tcx.hir().expect_item(impl_hir_id);
let item = tcx.hir().expect_item(impl_did);
let span =
if let ItemKind::Impl(ref impl_) = item.kind { impl_.self_ty.span } else { span };
@ -292,8 +291,8 @@ pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedI
debug!("compute_coerce_unsized_info(impl_did={:?})", impl_did);
// this provider should only get invoked for local def-ids
let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did.expect_local());
let span = tcx.hir().span(impl_hir_id);
let impl_did = impl_did.expect_local();
let span = tcx.def_span(impl_did);
let coerce_unsized_trait = tcx.require_lang_item(LangItem::CoerceUnsized, Some(span));
@ -315,6 +314,7 @@ pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedI
debug!("visit_implementation_of_coerce_unsized: {:?} -> {:?} (free)", source, target);
tcx.infer_ctxt().enter(|infcx| {
let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did);
let cause = ObligationCause::misc(span, impl_hir_id);
let check_mutbl = |mt_a: ty::TypeAndMut<'tcx>,
mt_b: ty::TypeAndMut<'tcx>,
@ -452,13 +452,13 @@ pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedI
.emit();
return err_info;
} else if diff_fields.len() > 1 {
let item = tcx.hir().expect_item(impl_hir_id);
let item = tcx.hir().expect_item(impl_did);
let span = if let ItemKind::Impl(hir::Impl { of_trait: Some(ref t), .. }) =
item.kind
{
t.path.span
} else {
tcx.hir().span(impl_hir_id)
tcx.def_span(impl_did)
};
struct_span_err!(
@ -530,7 +530,11 @@ pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedI
// Finally, resolve all regions.
let outlives_env = OutlivesEnvironment::new(param_env);
infcx.resolve_regions_and_report_errors(impl_did, &outlives_env, RegionckMode::default());
infcx.resolve_regions_and_report_errors(
impl_did.to_def_id(),
&outlives_env,
RegionckMode::default(),
);
CoerceUnsizedInfo { custom_kind: kind }
})

View file

@ -431,7 +431,7 @@ impl AstConv<'tcx> for ItemCtxt<'tcx> {
match self.node() {
hir::Node::Field(_) | hir::Node::Ctor(_) | hir::Node::Variant(_) => {
let item =
self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(self.hir_id()));
self.tcx.hir().expect_item(self.tcx.hir().get_parent_did(self.hir_id()));
match &item.kind {
hir::ItemKind::Enum(_, generics)
| hir::ItemKind::Struct(_, generics)
@ -1184,8 +1184,7 @@ fn super_predicates_that_define_assoc_type(
}
fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
let item = tcx.hir().expect_item(hir_id);
let item = tcx.hir().expect_item(def_id.expect_local());
let (is_auto, unsafety) = match item.kind {
hir::ItemKind::Trait(is_auto, unsafety, ..) => (is_auto == hir::IsAuto::Yes, unsafety),
@ -1880,9 +1879,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> {
let icx = ItemCtxt::new(tcx, def_id);
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
match tcx.hir().expect_item(hir_id).kind {
match tcx.hir().expect_item(def_id.expect_local()).kind {
hir::ItemKind::Impl(ref impl_) => impl_.of_trait.as_ref().map(|ast_trait_ref| {
let selfty = tcx.type_of(def_id);
<dyn AstConv<'_>>::instantiate_mono_trait_ref(&icx, ast_trait_ref, selfty)
@ -1892,9 +1889,8 @@ fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> {
}
fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
let item = tcx.hir().expect_item(hir_id);
let item = tcx.hir().expect_item(def_id.expect_local());
match &item.kind {
hir::ItemKind::Impl(hir::Impl {
polarity: hir::ImplPolarity::Negative(span),
@ -3225,7 +3221,7 @@ fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span:
let hir_id = tcx.hir().local_def_id_to_hir_id(id);
let node = tcx.hir().get(hir_id);
if let Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) = node {
let parent_id = tcx.hir().get_parent_item(hir_id);
let parent_id = tcx.hir().get_parent_did(hir_id);
let parent_item = tcx.hir().expect_item(parent_id);
if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = parent_item.kind {
tcx.sess