Skip to content

Commit ca50737

Browse files
committed
Remove some impl_trait_ref usages
1 parent c17b2dc commit ca50737

File tree

8 files changed

+30
-41
lines changed

8 files changed

+30
-41
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ pub(super) fn check_item<'tcx>(
245245
// won't be allowed unless there's an *explicit* implementation of `Send`
246246
// for `T`
247247
hir::ItemKind::Impl(ref impl_) => {
248-
crate::impl_wf_check::check_impl_wf(tcx, def_id)?;
248+
crate::impl_wf_check::check_impl_wf(tcx, def_id, impl_.of_trait.is_some())?;
249249
let mut res = Ok(());
250250
if let Some(of_trait) = impl_.of_trait {
251251
let header = tcx.impl_trait_header(def_id).unwrap();

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,12 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
350350
// before uses of `U`. This avoids false ambiguity errors
351351
// in trait checking. See `setup_constraining_predicates`
352352
// for details.
353-
if let Node::Item(&Item { kind: ItemKind::Impl { .. }, .. }) = node {
353+
if let Node::Item(&Item { kind: ItemKind::Impl(impl_), .. }) = node {
354354
let self_ty = tcx.type_of(def_id).instantiate_identity();
355-
let trait_ref = tcx.impl_trait_ref(def_id).map(ty::EarlyBinder::instantiate_identity);
355+
let trait_ref = impl_
356+
.of_trait
357+
.is_some()
358+
.then(|| tcx.impl_trait_ref(def_id).unwrap().instantiate_identity());
356359
cgp::setup_constraining_predicates(
357360
tcx,
358361
&mut predicates,
@@ -460,11 +463,12 @@ fn const_evaluatable_predicates_of<'tcx>(
460463
}
461464

462465
if let hir::Node::Item(item) = node
463-
&& let hir::ItemKind::Impl(_) = item.kind
466+
&& let hir::ItemKind::Impl(impl_) = item.kind
464467
{
465-
if let Some(of_trait) = tcx.impl_trait_ref(def_id) {
468+
if impl_.of_trait.is_some() {
466469
debug!("visit impl trait_ref");
467-
of_trait.instantiate_identity().visit_with(&mut collector);
470+
let trait_ref = tcx.impl_trait_ref(def_id).unwrap();
471+
trait_ref.instantiate_identity().visit_with(&mut collector);
468472
}
469473

470474
debug!("visit self_ty");

compiler/rustc_hir_analysis/src/impl_wf_check.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,17 @@ mod min_specialization;
5656
pub(crate) fn check_impl_wf(
5757
tcx: TyCtxt<'_>,
5858
impl_def_id: LocalDefId,
59+
of_trait: bool,
5960
) -> Result<(), ErrorGuaranteed> {
6061
debug_assert_matches!(tcx.def_kind(impl_def_id), DefKind::Impl { .. });
6162

6263
// Check that the args are constrained. We queryfied the check for ty/const params
6364
// since unconstrained type/const params cause ICEs in projection, so we want to
6465
// detect those specifically and project those to `TyKind::Error`.
6566
let mut res = tcx.ensure_ok().enforce_impl_non_lifetime_params_are_constrained(impl_def_id);
66-
res = res.and(enforce_impl_lifetime_params_are_constrained(tcx, impl_def_id));
67+
res = res.and(enforce_impl_lifetime_params_are_constrained(tcx, impl_def_id, of_trait));
6768

68-
if tcx.features().min_specialization() {
69+
if of_trait && tcx.features().min_specialization() {
6970
res = res.and(check_min_specialization(tcx, impl_def_id));
7071
}
7172
res
@@ -74,6 +75,7 @@ pub(crate) fn check_impl_wf(
7475
pub(crate) fn enforce_impl_lifetime_params_are_constrained(
7576
tcx: TyCtxt<'_>,
7677
impl_def_id: LocalDefId,
78+
of_trait: bool,
7779
) -> Result<(), ErrorGuaranteed> {
7880
let impl_self_ty = tcx.type_of(impl_def_id).instantiate_identity();
7981

@@ -83,7 +85,8 @@ pub(crate) fn enforce_impl_lifetime_params_are_constrained(
8385

8486
let impl_generics = tcx.generics_of(impl_def_id);
8587
let impl_predicates = tcx.predicates_of(impl_def_id);
86-
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).map(ty::EarlyBinder::instantiate_identity);
88+
let impl_trait_ref =
89+
of_trait.then(|| tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity());
8790

8891
impl_trait_ref.error_reported()?;
8992

compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub(super) fn check_min_specialization(
9393
}
9494

9595
fn parent_specialization_node(tcx: TyCtxt<'_>, impl1_def_id: LocalDefId) -> Option<Node> {
96-
let trait_ref = tcx.impl_trait_ref(impl1_def_id)?;
96+
let trait_ref = tcx.impl_trait_ref(impl1_def_id).unwrap();
9797
let trait_def = tcx.trait_def(trait_ref.skip_binder().def_id);
9898

9999
let impl2_node = trait_def.ancestors(tcx, impl1_def_id.to_def_id()).ok()?.nth(1)?;

compiler/rustc_hir_typeck/src/method/confirm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use rustc_middle::ty::adjustment::{
1818
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCoercion,
1919
};
2020
use rustc_middle::ty::{
21-
self, GenericArgs, GenericArgsRef, GenericParamDefKind, Ty, TyCtxt, TypeFoldable,
22-
TypeVisitableExt, UserArgs,
21+
self, AssocContainer, GenericArgs, GenericArgsRef, GenericParamDefKind, Ty, TyCtxt,
22+
TypeFoldable, TypeVisitableExt, UserArgs,
2323
};
2424
use rustc_middle::{bug, span_bug};
2525
use rustc_span::{DUMMY_SP, Span};
@@ -272,7 +272,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
272272
probe::InherentImplPick => {
273273
let impl_def_id = pick.item.container_id(self.tcx);
274274
assert!(
275-
self.tcx.impl_trait_ref(impl_def_id).is_none(),
275+
matches!(pick.item.container, AssocContainer::InherentImpl),
276276
"impl {impl_def_id:?} is not an inherent impl"
277277
);
278278
self.fresh_args_for_item(self.span, impl_def_id)

compiler/rustc_middle/src/ty/trait_def.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,7 @@ pub(super) fn traits_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> &[DefId] {
271271
pub(super) fn trait_impls_in_crate_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> &[DefId] {
272272
let mut trait_impls = Vec::new();
273273
for id in tcx.hir_free_items() {
274-
if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. })
275-
&& tcx.impl_trait_ref(id.owner_id).is_some()
276-
{
274+
if tcx.def_kind(id.owner_id) == (DefKind::Impl { of_trait: true }) {
277275
trait_impls.push(id.owner_id.to_def_id())
278276
}
279277
}

compiler/rustc_ty_utils/src/implied_bounds.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx [(Ty<'
4242
));
4343
tcx.arena.alloc_slice(&assumed_wf_types)
4444
}
45-
DefKind::Impl { .. } => {
45+
DefKind::Impl { of_trait } => {
4646
// Trait arguments and the self type for trait impls or only the self type for
4747
// inherent impls.
48-
let tys = match tcx.impl_trait_ref(def_id) {
49-
Some(trait_ref) => trait_ref.skip_binder().args.types().collect(),
50-
None => vec![tcx.type_of(def_id).instantiate_identity()],
48+
let tys = if of_trait {
49+
let trait_ref = tcx.impl_trait_ref(def_id).unwrap();
50+
trait_ref.skip_binder().args.types().collect()
51+
} else {
52+
vec![tcx.type_of(def_id).instantiate_identity()]
5153
};
5254

5355
let mut impl_spans = impl_spans(tcx, def_id);

compiler/rustc_ty_utils/src/opaque_types.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,6 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
6262
self.span = old;
6363
}
6464

65-
fn parent_impl_trait_ref(&self) -> Option<ty::TraitRef<'tcx>> {
66-
let parent = self.parent()?;
67-
if matches!(self.tcx.def_kind(parent), DefKind::Impl { .. }) {
68-
Some(self.tcx.impl_trait_ref(parent)?.instantiate_identity())
69-
} else {
70-
None
71-
}
72-
}
73-
74-
fn parent(&self) -> Option<LocalDefId> {
75-
match self.tcx.def_kind(self.item) {
76-
DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
77-
Some(self.tcx.local_parent(self.item))
78-
}
79-
_ => None,
80-
}
81-
}
82-
8365
#[instrument(level = "trace", skip(self))]
8466
fn collect_taits_declared_in_body(&mut self) {
8567
let body = self.tcx.hir_body_owned_by(self.item).value;
@@ -236,13 +218,13 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
236218
// This avoids having to do normalization of `Self::AssocTy` by only
237219
// supporting the case of a method defining opaque types from assoc types
238220
// in the same impl block.
239-
if let Some(impl_trait_ref) = self.parent_impl_trait_ref() {
221+
if let Some(parent) = self.tcx.trait_impl_of_assoc(self.item.to_def_id()) {
222+
let impl_trait_ref =
223+
self.tcx.impl_trait_ref(parent).unwrap().instantiate_identity();
240224
// If the trait ref of the associated item and the impl differs,
241225
// then we can't use the impl's identity args below, so
242226
// just skip.
243227
if alias_ty.trait_ref(self.tcx) == impl_trait_ref {
244-
let parent = self.parent().expect("we should have a parent here");
245-
246228
for &assoc in self.tcx.associated_items(parent).in_definition_order() {
247229
trace!(?assoc);
248230
if assoc.expect_trait_impl() != Ok(alias_ty.def_id) {

0 commit comments

Comments
 (0)