Skip to content

Commit 35dbd1a

Browse files
committed
Split impl_(opt_)trait_ref
1 parent 74f65e6 commit 35dbd1a

File tree

44 files changed

+104
-116
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+104
-116
lines changed

compiler/rustc_hir_analysis/src/check/always_applicable.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ fn ensure_impl_predicates_are_implied_by_item_defn<'tcx>(
210210
ty::EarlyBinder::bind(tcx.param_env(adt_def_id)).instantiate(tcx, adt_to_impl_args);
211211

212212
let fresh_impl_args = infcx.fresh_args_for_item(impl_span, impl_def_id.to_def_id());
213-
let fresh_adt_ty =
214-
tcx.impl_trait_ref(impl_def_id).unwrap().instantiate(tcx, fresh_impl_args).self_ty();
213+
let fresh_adt_ty = tcx.impl_trait_ref(impl_def_id).instantiate(tcx, fresh_impl_args).self_ty();
215214

216215
ocx.eq(&ObligationCause::dummy_with_span(impl_span), adt_env, fresh_adt_ty, impl_adt_ty)
217216
.expect("equating fully generic trait ref should never fail");

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,9 +1194,7 @@ fn check_impl_items_against_trait<'tcx>(
11941194
tcx,
11951195
ty_impl_item,
11961196
ty_trait_item,
1197-
tcx.impl_trait_ref(ty_impl_item.container_id(tcx))
1198-
.unwrap()
1199-
.instantiate_identity(),
1197+
tcx.impl_trait_ref(ty_impl_item.container_id(tcx)).instantiate_identity(),
12001198
);
12011199
}
12021200
ty::AssocKind::Const { .. } => {}

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ pub(super) fn compare_impl_item(
3838
) -> Result<(), ErrorGuaranteed> {
3939
let impl_item = tcx.associated_item(impl_item_def_id);
4040
let trait_item = tcx.associated_item(impl_item.trait_item_def_id.unwrap());
41-
let impl_trait_ref =
42-
tcx.impl_trait_ref(impl_item.container_id(tcx)).unwrap().instantiate_identity();
41+
let impl_trait_ref = tcx.impl_trait_ref(impl_item.container_id(tcx)).instantiate_identity();
4342
debug!(?impl_trait_ref);
4443

4544
match impl_item.kind {
@@ -445,10 +444,9 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
445444
tcx: TyCtxt<'tcx>,
446445
impl_m_def_id: LocalDefId,
447446
) -> Result<&'tcx DefIdMap<ty::EarlyBinder<'tcx, Ty<'tcx>>>, ErrorGuaranteed> {
448-
let impl_m = tcx.opt_associated_item(impl_m_def_id.to_def_id()).unwrap();
449-
let trait_m = tcx.opt_associated_item(impl_m.trait_item_def_id.unwrap()).unwrap();
450-
let impl_trait_ref =
451-
tcx.impl_trait_ref(impl_m.impl_container(tcx).unwrap()).unwrap().instantiate_identity();
447+
let impl_m = tcx.associated_item(impl_m_def_id.to_def_id());
448+
let trait_m = tcx.associated_item(impl_m.trait_item_def_id.unwrap());
449+
let impl_trait_ref = tcx.impl_trait_ref(tcx.parent(impl_m.def_id)).instantiate_identity();
452450
// First, check a few of the same things as `compare_impl_method`,
453451
// just so we don't ICE during instantiation later.
454452
check_method_is_structurally_compatible(tcx, impl_m, trait_m, impl_trait_ref, true)?;

compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ fn missing_items_err(
241241
let snippet = with_types_for_signature!(suggestion_signature(
242242
tcx,
243243
trait_item,
244-
tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity(),
244+
tcx.impl_trait_ref(impl_def_id).instantiate_identity(),
245245
));
246246
let code = format!("{padding}{snippet}\n{padding}");
247247
if let Some(span) = tcx.hir_span_if_local(trait_item.def_id) {

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 2 additions & 2 deletions
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(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 impl_.of_trait.is_some() {
251251
let header = tcx.impl_trait_header(def_id);
@@ -1273,7 +1273,7 @@ fn check_impl<'tcx>(
12731273
// `#[rustc_reservation_impl]` impls are not real impls and
12741274
// therefore don't need to be WF (the trait's `Self: Trait` predicate
12751275
// won't hold).
1276-
let trait_ref = tcx.impl_trait_ref(item.owner_id).unwrap().instantiate_identity();
1276+
let trait_ref = tcx.impl_trait_ref(item.owner_id).instantiate_identity();
12771277
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
12781278
// other `Foo` impls are incoherent.
12791279
tcx.ensure_ok().coherent_trait(trait_ref.def_id)?;

compiler/rustc_hir_analysis/src/coherence/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ pub(crate) fn coerce_unsized_info<'tcx>(
386386
let unsize_trait = tcx.require_lang_item(LangItem::Unsize, span);
387387

388388
let source = tcx.type_of(impl_did).instantiate_identity();
389-
let trait_ref = tcx.impl_trait_ref(impl_did).unwrap().instantiate_identity();
389+
let trait_ref = tcx.impl_trait_ref(impl_did).instantiate_identity();
390390

391391
assert_eq!(trait_ref.def_id, coerce_unsized_trait);
392392
let target = trait_ref.args.type_at(1);
@@ -714,7 +714,7 @@ fn visit_implementation_of_coerce_pointee_validity(
714714
checker: &Checker<'_>,
715715
) -> Result<(), ErrorGuaranteed> {
716716
let tcx = checker.tcx;
717-
let self_ty = tcx.impl_trait_ref(checker.impl_def_id).unwrap().instantiate_identity().self_ty();
717+
let self_ty = tcx.impl_trait_ref(checker.impl_def_id).instantiate_identity().self_ty();
718718
let span = tcx.def_span(checker.impl_def_id);
719719
if !tcx.is_builtin_derived(checker.impl_def_id.into()) {
720720
return Err(tcx.dcx().emit_err(errors::CoercePointeeNoUserValidityAssertion { span }));

compiler/rustc_hir_analysis/src/coherence/orphan.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) fn orphan_check_impl(
2222
tcx: TyCtxt<'_>,
2323
impl_def_id: LocalDefId,
2424
) -> Result<(), ErrorGuaranteed> {
25-
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity();
25+
let trait_ref = tcx.impl_trait_ref(impl_def_id).instantiate_identity();
2626
trait_ref.error_reported()?;
2727

2828
match orphan_check(tcx, impl_def_id, OrphanCheckMode::Proper) {
@@ -292,7 +292,7 @@ fn orphan_check<'tcx>(
292292
) -> Result<(), OrphanCheckErr<TyCtxt<'tcx>, FxIndexSet<DefId>>> {
293293
// We only accept this routine to be invoked on implementations
294294
// of a trait, not inherent implementations.
295-
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
295+
let trait_ref = tcx.impl_trait_ref(impl_def_id);
296296
debug!(trait_ref = ?trait_ref.skip_binder());
297297

298298
// If the *trait* is local to the crate, ok.

compiler/rustc_hir_analysis/src/collect/dump.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
108108

109109
let vtable_entries = match tcx.hir_item(id).kind {
110110
hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) => {
111-
let trait_ref = tcx.impl_trait_ref(def_id).unwrap().instantiate_identity();
111+
let trait_ref = tcx.impl_trait_ref(def_id).instantiate_identity();
112112
if trait_ref.has_non_region_param() {
113113
tcx.dcx().span_err(
114114
attr.span(),

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
116116

117117
let impl_assoc_identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
118118
let impl_def_id = tcx.parent(fn_def_id);
119-
let impl_trait_ref_args =
120-
tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity().args;
119+
let impl_trait_ref_args = tcx.impl_trait_ref(impl_def_id).instantiate_identity().args;
121120

122121
let impl_assoc_args =
123122
impl_assoc_identity_args.rebase_onto(tcx, impl_def_id, impl_trait_ref_args);
@@ -158,9 +157,8 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
158157
match item.kind {
159158
ItemKind::Impl(impl_) => {
160159
if impl_.defaultness.is_default() {
161-
is_default_impl_trait = tcx
162-
.impl_trait_ref(def_id)
163-
.map(|t| ty::Binder::dummy(t.instantiate_identity()));
160+
is_default_impl_trait =
161+
Some(ty::Binder::dummy(tcx.impl_trait_ref(def_id).instantiate_identity()));
164162
}
165163
}
166164
ItemKind::Trait(_, _, _, _, _, self_bounds, ..)
@@ -350,9 +348,10 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
350348
// before uses of `U`. This avoids false ambiguity errors
351349
// in trait checking. See `setup_constraining_predicates`
352350
// for details.
353-
if let Node::Item(&Item { kind: ItemKind::Impl { .. }, .. }) = node {
351+
if let Node::Item(&Item { kind: ItemKind::Impl(impl_), .. }) = node {
354352
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);
353+
let trait_ref =
354+
impl_.of_trait.is_some().then(|| tcx.impl_trait_ref(def_id).instantiate_identity());
356355
cgp::setup_constraining_predicates(
357356
tcx,
358357
&mut predicates,
@@ -460,11 +459,12 @@ fn const_evaluatable_predicates_of<'tcx>(
460459
}
461460

462461
if let hir::Node::Item(item) = node
463-
&& let hir::ItemKind::Impl(_) = item.kind
462+
&& let hir::ItemKind::Impl(impl_) = item.kind
464463
{
465-
if let Some(of_trait) = tcx.impl_trait_ref(def_id) {
464+
if impl_.of_trait.is_some() {
466465
debug!("visit impl trait_ref");
467-
of_trait.instantiate_identity().visit_with(&mut collector);
466+
let trait_ref = tcx.impl_trait_ref(def_id);
467+
trait_ref.instantiate_identity().visit_with(&mut collector);
468468
}
469469

470470
debug!("visit self_ty");

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,10 +1375,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
13751375
(_, Res::SelfTyAlias { alias_to: impl_def_id, is_trait_impl: true, .. }) => {
13761376
// `Self` in an impl of a trait -- we have a concrete self type and a
13771377
// trait reference.
1378-
let Some(trait_ref) = tcx.impl_trait_ref(impl_def_id) else {
1379-
// A cycle error occurred, most likely.
1380-
self.dcx().span_bug(span, "expected cycle error");
1381-
};
1378+
let trait_ref = tcx.impl_trait_ref(impl_def_id);
13821379

13831380
self.probe_single_bound_for_assoc_item(
13841381
|| {

0 commit comments

Comments
 (0)