From 55bbb054c937ea53de4e8bdc01b8c5eab82b9635 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 13 Feb 2024 09:29:31 +0000 Subject: [PATCH] Invoke `trait_def` query only once This may be a small performance boost as we have to hash less to lookup the value --- .../rustc_hir_analysis/src/coherence/mod.rs | 19 +++++++++++-------- .../src/coherence/unsafety.rs | 4 ++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/coherence/mod.rs b/compiler/rustc_hir_analysis/src/coherence/mod.rs index f1978a0fc355..793458c4d8df 100644 --- a/compiler/rustc_hir_analysis/src/coherence/mod.rs +++ b/compiler/rustc_hir_analysis/src/coherence/mod.rs @@ -23,6 +23,7 @@ fn check_impl( tcx: TyCtxt<'_>, impl_def_id: LocalDefId, trait_ref: ty::TraitRef<'_>, + trait_def: &ty::TraitDef, ) -> Result<(), ErrorGuaranteed> { debug!( "(checking implementation) adding impl for trait '{:?}', item '{}'", @@ -36,19 +37,20 @@ fn check_impl( return Ok(()); } - enforce_trait_manually_implementable(tcx, impl_def_id, trait_ref.def_id) - .and(enforce_empty_impls_for_marker_traits(tcx, impl_def_id, trait_ref.def_id)) + enforce_trait_manually_implementable(tcx, impl_def_id, trait_ref.def_id, trait_def) + .and(enforce_empty_impls_for_marker_traits(tcx, impl_def_id, trait_ref.def_id, trait_def)) } fn enforce_trait_manually_implementable( tcx: TyCtxt<'_>, impl_def_id: LocalDefId, trait_def_id: DefId, + trait_def: &ty::TraitDef, ) -> Result<(), ErrorGuaranteed> { let impl_header_span = tcx.def_span(impl_def_id); // Disallow *all* explicit impls of traits marked `#[rustc_deny_explicit_impl]` - if tcx.trait_def(trait_def_id).deny_explicit_impl { + if trait_def.deny_explicit_impl { let trait_name = tcx.item_name(trait_def_id); let mut err = struct_span_code_err!( tcx.dcx(), @@ -67,8 +69,7 @@ fn enforce_trait_manually_implementable( return Err(err.emit()); } - if let ty::trait_def::TraitSpecializationKind::AlwaysApplicable = - tcx.trait_def(trait_def_id).specialization_kind + if let ty::trait_def::TraitSpecializationKind::AlwaysApplicable = trait_def.specialization_kind { if !tcx.features().specialization && !tcx.features().min_specialization @@ -87,8 +88,9 @@ fn enforce_empty_impls_for_marker_traits( tcx: TyCtxt<'_>, impl_def_id: LocalDefId, trait_def_id: DefId, + trait_def: &ty::TraitDef, ) -> Result<(), ErrorGuaranteed> { - if !tcx.trait_def(trait_def_id).is_marker { + if !trait_def.is_marker { return Ok(()); } @@ -133,11 +135,12 @@ fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) -> Result<(), ErrorGuaranteed> for &impl_def_id in impls { let trait_header = tcx.impl_trait_header(impl_def_id).unwrap().instantiate_identity(); + let trait_def = tcx.trait_def(trait_header.trait_ref.def_id); - res = res.and(check_impl(tcx, impl_def_id, trait_header.trait_ref)); + res = res.and(check_impl(tcx, impl_def_id, trait_header.trait_ref, trait_def)); res = res.and(check_object_overlap(tcx, impl_def_id, trait_header.trait_ref)); - res = res.and(unsafety::check_item(tcx, impl_def_id, trait_header)); + res = res.and(unsafety::check_item(tcx, impl_def_id, trait_header, trait_def)); res = res.and(tcx.ensure().orphan_check_impl(impl_def_id)); res = res.and(builtin::check_trait(tcx, def_id, impl_def_id)); } diff --git a/compiler/rustc_hir_analysis/src/coherence/unsafety.rs b/compiler/rustc_hir_analysis/src/coherence/unsafety.rs index 158d010b05d3..688760a39126 100644 --- a/compiler/rustc_hir_analysis/src/coherence/unsafety.rs +++ b/compiler/rustc_hir_analysis/src/coherence/unsafety.rs @@ -3,7 +3,7 @@ use rustc_errors::{codes::*, struct_span_code_err}; use rustc_hir::Unsafety; -use rustc_middle::ty::{ImplPolarity::*, ImplTraitHeader, TyCtxt}; +use rustc_middle::ty::{ImplPolarity::*, ImplTraitHeader, TraitDef, TyCtxt}; use rustc_span::def_id::LocalDefId; use rustc_span::ErrorGuaranteed; @@ -11,9 +11,9 @@ pub(super) fn check_item( tcx: TyCtxt<'_>, def_id: LocalDefId, trait_header: ImplTraitHeader<'_>, + trait_def: &TraitDef, ) -> Result<(), ErrorGuaranteed> { let trait_ref = trait_header.trait_ref; - let trait_def = tcx.trait_def(trait_ref.def_id); let unsafe_attr = tcx.generics_of(def_id).params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle"); match (trait_def.unsafety, unsafe_attr, trait_header.unsafety, trait_header.polarity) {