Skip to content

Commit 55bbb05

Browse files
committed
Invoke trait_def query only once
This may be a small performance boost as we have to hash less to lookup the value
1 parent 11a73f6 commit 55bbb05

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

compiler/rustc_hir_analysis/src/coherence/mod.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fn check_impl(
2323
tcx: TyCtxt<'_>,
2424
impl_def_id: LocalDefId,
2525
trait_ref: ty::TraitRef<'_>,
26+
trait_def: &ty::TraitDef,
2627
) -> Result<(), ErrorGuaranteed> {
2728
debug!(
2829
"(checking implementation) adding impl for trait '{:?}', item '{}'",
@@ -36,19 +37,20 @@ fn check_impl(
3637
return Ok(());
3738
}
3839

39-
enforce_trait_manually_implementable(tcx, impl_def_id, trait_ref.def_id)
40-
.and(enforce_empty_impls_for_marker_traits(tcx, impl_def_id, trait_ref.def_id))
40+
enforce_trait_manually_implementable(tcx, impl_def_id, trait_ref.def_id, trait_def)
41+
.and(enforce_empty_impls_for_marker_traits(tcx, impl_def_id, trait_ref.def_id, trait_def))
4142
}
4243

4344
fn enforce_trait_manually_implementable(
4445
tcx: TyCtxt<'_>,
4546
impl_def_id: LocalDefId,
4647
trait_def_id: DefId,
48+
trait_def: &ty::TraitDef,
4749
) -> Result<(), ErrorGuaranteed> {
4850
let impl_header_span = tcx.def_span(impl_def_id);
4951

5052
// Disallow *all* explicit impls of traits marked `#[rustc_deny_explicit_impl]`
51-
if tcx.trait_def(trait_def_id).deny_explicit_impl {
53+
if trait_def.deny_explicit_impl {
5254
let trait_name = tcx.item_name(trait_def_id);
5355
let mut err = struct_span_code_err!(
5456
tcx.dcx(),
@@ -67,8 +69,7 @@ fn enforce_trait_manually_implementable(
6769
return Err(err.emit());
6870
}
6971

70-
if let ty::trait_def::TraitSpecializationKind::AlwaysApplicable =
71-
tcx.trait_def(trait_def_id).specialization_kind
72+
if let ty::trait_def::TraitSpecializationKind::AlwaysApplicable = trait_def.specialization_kind
7273
{
7374
if !tcx.features().specialization
7475
&& !tcx.features().min_specialization
@@ -87,8 +88,9 @@ fn enforce_empty_impls_for_marker_traits(
8788
tcx: TyCtxt<'_>,
8889
impl_def_id: LocalDefId,
8990
trait_def_id: DefId,
91+
trait_def: &ty::TraitDef,
9092
) -> Result<(), ErrorGuaranteed> {
91-
if !tcx.trait_def(trait_def_id).is_marker {
93+
if !trait_def.is_marker {
9294
return Ok(());
9395
}
9496

@@ -133,11 +135,12 @@ fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) -> Result<(), ErrorGuaranteed>
133135

134136
for &impl_def_id in impls {
135137
let trait_header = tcx.impl_trait_header(impl_def_id).unwrap().instantiate_identity();
138+
let trait_def = tcx.trait_def(trait_header.trait_ref.def_id);
136139

137-
res = res.and(check_impl(tcx, impl_def_id, trait_header.trait_ref));
140+
res = res.and(check_impl(tcx, impl_def_id, trait_header.trait_ref, trait_def));
138141
res = res.and(check_object_overlap(tcx, impl_def_id, trait_header.trait_ref));
139142

140-
res = res.and(unsafety::check_item(tcx, impl_def_id, trait_header));
143+
res = res.and(unsafety::check_item(tcx, impl_def_id, trait_header, trait_def));
141144
res = res.and(tcx.ensure().orphan_check_impl(impl_def_id));
142145
res = res.and(builtin::check_trait(tcx, def_id, impl_def_id));
143146
}

compiler/rustc_hir_analysis/src/coherence/unsafety.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
44
use rustc_errors::{codes::*, struct_span_code_err};
55
use rustc_hir::Unsafety;
6-
use rustc_middle::ty::{ImplPolarity::*, ImplTraitHeader, TyCtxt};
6+
use rustc_middle::ty::{ImplPolarity::*, ImplTraitHeader, TraitDef, TyCtxt};
77
use rustc_span::def_id::LocalDefId;
88
use rustc_span::ErrorGuaranteed;
99

1010
pub(super) fn check_item(
1111
tcx: TyCtxt<'_>,
1212
def_id: LocalDefId,
1313
trait_header: ImplTraitHeader<'_>,
14+
trait_def: &TraitDef,
1415
) -> Result<(), ErrorGuaranteed> {
1516
let trait_ref = trait_header.trait_ref;
16-
let trait_def = tcx.trait_def(trait_ref.def_id);
1717
let unsafe_attr =
1818
tcx.generics_of(def_id).params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle");
1919
match (trait_def.unsafety, unsafe_attr, trait_header.unsafety, trait_header.polarity) {

0 commit comments

Comments
 (0)