Skip to content

Commit 2170b4d

Browse files
committed
Auto merge of #144607 - camsteffen:impl-trait-header-option, r=lcnr
Limit impl_trait_header query to only trait impls Changes `impl_trait_header` to panic on inherent impls intstead of returning None. A few downstream functions are split into option and non-option returning functions. This gets rid of a lot of unwraps where we know we have a trait impl, while there are still some cases where the Option is helpful. Summary of changes to tcx methods: * `impl_is_of_trait` (new) * `impl_trait_header` -> `impl_trait_header`/`impl_opt_trait_header` * `impl_trait_ref` -> `impl_trait_ref`/`impl_opt_trait_ref` * `trait_id_of_impl` -> `impl_trait_id`/`impl_opt_trait_id`
2 parents 28fad95 + d9a5389 commit 2170b4d

File tree

82 files changed

+294
-335
lines changed

Some content is hidden

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

82 files changed

+294
-335
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
708708
return (false, false, None);
709709
}
710710
let my_def = self.body.source.def_id();
711-
let Some(td) =
712-
tcx.trait_impl_of_assoc(my_def).and_then(|id| self.infcx.tcx.trait_id_of_impl(id))
711+
let Some(td) = tcx.trait_impl_of_assoc(my_def).map(|id| self.infcx.tcx.impl_trait_id(id))
713712
else {
714713
return (false, false, None);
715714
};

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::ty::TyCtxt;
77
fn parent_impl_or_trait_constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
88
let parent_id = tcx.local_parent(def_id);
99
match tcx.def_kind(parent_id) {
10-
DefKind::Impl { of_trait: true } => tcx.impl_trait_header(parent_id).unwrap().constness,
10+
DefKind::Impl { of_trait: true } => tcx.impl_trait_header(parent_id).constness,
1111
DefKind::Trait => {
1212
if tcx.is_const_trait(parent_id.into()) {
1313
hir::Constness::Const

compiler/rustc_hir_analysis/src/check/always_applicable.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ fn ensure_impl_params_and_item_params_correspond<'tcx>(
148148
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
149149
ty::ImplPolarity::Negative => "!",
150150
};
151-
let trait_name = tcx
152-
.item_name(tcx.trait_id_of_impl(impl_def_id.to_def_id()).expect("expected impl of trait"));
151+
let trait_name = tcx.item_name(tcx.impl_trait_id(impl_def_id.to_def_id()));
153152
let mut err = struct_span_code_err!(
154153
tcx.dcx(),
155154
impl_span,
@@ -187,8 +186,7 @@ fn ensure_impl_predicates_are_implied_by_item_defn<'tcx>(
187186
let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
188187

189188
let impl_span = tcx.def_span(impl_def_id.to_def_id());
190-
let trait_name = tcx
191-
.item_name(tcx.trait_id_of_impl(impl_def_id.to_def_id()).expect("expected impl of trait"));
189+
let trait_name = tcx.item_name(tcx.impl_trait_id(impl_def_id.to_def_id()));
192190
let polarity = match tcx.impl_polarity(impl_def_id) {
193191
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
194192
ty::ImplPolarity::Negative => "!",
@@ -212,8 +210,7 @@ fn ensure_impl_predicates_are_implied_by_item_defn<'tcx>(
212210
ty::EarlyBinder::bind(tcx.param_env(adt_def_id)).instantiate(tcx, adt_to_impl_args);
213211

214212
let fresh_impl_args = infcx.fresh_args_for_item(impl_span, impl_def_id.to_def_id());
215-
let fresh_adt_ty =
216-
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();
217214

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

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -806,10 +806,10 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
806806
DefKind::Impl { of_trait } => {
807807
tcx.ensure_ok().generics_of(def_id);
808808
tcx.ensure_ok().type_of(def_id);
809-
tcx.ensure_ok().impl_trait_header(def_id);
810809
tcx.ensure_ok().predicates_of(def_id);
811810
tcx.ensure_ok().associated_items(def_id);
812-
if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) {
811+
if of_trait {
812+
let impl_trait_header = tcx.impl_trait_header(def_id);
813813
res = res.and(
814814
tcx.ensure_ok()
815815
.coherent_trait(impl_trait_header.trait_ref.instantiate_identity().def_id),
@@ -1191,9 +1191,7 @@ fn check_impl_items_against_trait<'tcx>(
11911191
tcx,
11921192
ty_impl_item,
11931193
ty_trait_item,
1194-
tcx.impl_trait_ref(ty_impl_item.container_id(tcx))
1195-
.unwrap()
1196-
.instantiate_identity(),
1194+
tcx.impl_trait_ref(ty_impl_item.container_id(tcx)).instantiate_identity(),
11971195
);
11981196
}
11991197
ty::AssocKind::Const { .. } => {}

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 2 additions & 3 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.expect_trait_impl()?);
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 {
@@ -443,7 +442,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
443442
let impl_m = tcx.associated_item(impl_m_def_id.to_def_id());
444443
let trait_m = tcx.associated_item(impl_m.expect_trait_impl()?);
445444
let impl_trait_ref =
446-
tcx.impl_trait_ref(tcx.parent(impl_m_def_id.to_def_id())).unwrap().instantiate_identity();
445+
tcx.impl_trait_ref(tcx.parent(impl_m_def_id.to_def_id())).instantiate_identity();
447446
// First, check a few of the same things as `compare_impl_method`,
448447
// just so we don't ICE during instantiation later.
449448
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
@@ -244,7 +244,7 @@ fn missing_items_err(
244244
let snippet = with_types_for_signature!(suggestion_signature(
245245
tcx,
246246
trait_item,
247-
tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity(),
247+
tcx.impl_trait_ref(impl_def_id).instantiate_identity(),
248248
));
249249
let code = format!("{padding}{snippet}\n{padding}");
250250
if let Some(span) = tcx.hir_span_if_local(trait_item.def_id) {

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ 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 {
251-
let header = tcx.impl_trait_header(def_id).unwrap();
251+
let header = tcx.impl_trait_header(def_id);
252252
let is_auto = tcx.trait_is_auto(header.trait_ref.skip_binder().def_id);
253253
if let (hir::Defaultness::Default { .. }, true) = (of_trait.defaultness, is_auto) {
254254
let sp = of_trait.trait_ref.path.span;
@@ -1258,7 +1258,7 @@ fn check_impl<'tcx>(
12581258
// `#[rustc_reservation_impl]` impls are not real impls and
12591259
// therefore don't need to be WF (the trait's `Self: Trait` predicate
12601260
// won't hold).
1261-
let trait_ref = tcx.impl_trait_ref(item.owner_id).unwrap().instantiate_identity();
1261+
let trait_ref = tcx.impl_trait_ref(item.owner_id).instantiate_identity();
12621262
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
12631263
// other `Foo` impls are incoherent.
12641264
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
@@ -377,7 +377,7 @@ pub(crate) fn coerce_unsized_info<'tcx>(
377377
let unsize_trait = tcx.require_lang_item(LangItem::Unsize, span);
378378

379379
let source = tcx.type_of(impl_did).instantiate_identity();
380-
let trait_ref = tcx.impl_trait_ref(impl_did).unwrap().instantiate_identity();
380+
let trait_ref = tcx.impl_trait_ref(impl_did).instantiate_identity();
381381

382382
assert_eq!(trait_ref.def_id, coerce_unsized_trait);
383383
let target = trait_ref.args.type_at(1);
@@ -707,7 +707,7 @@ fn visit_implementation_of_coerce_pointee_validity(
707707
checker: &Checker<'_>,
708708
) -> Result<(), ErrorGuaranteed> {
709709
let tcx = checker.tcx;
710-
let self_ty = tcx.impl_trait_ref(checker.impl_def_id).unwrap().instantiate_identity().self_ty();
710+
let self_ty = tcx.impl_trait_ref(checker.impl_def_id).instantiate_identity().self_ty();
711711
let span = tcx.def_span(checker.impl_def_id);
712712
if !tcx.is_builtin_derived(checker.impl_def_id.into()) {
713713
return Err(tcx.dcx().emit_err(errors::CoercePointeeNoUserValidityAssertion { span }));

compiler/rustc_hir_analysis/src/coherence/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) -> Result<(), ErrorGuaranteed>
163163
let mut res = tcx.ensure_ok().specialization_graph_of(def_id);
164164

165165
for &impl_def_id in impls {
166-
let impl_header = tcx.impl_trait_header(impl_def_id).unwrap();
166+
let impl_header = tcx.impl_trait_header(impl_def_id);
167167
let trait_ref = impl_header.trait_ref.instantiate_identity();
168168
let trait_def = tcx.trait_def(trait_ref.def_id);
169169

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) {
@@ -294,7 +294,7 @@ fn orphan_check<'tcx>(
294294
) -> Result<(), OrphanCheckErr<TyCtxt<'tcx>, FxIndexSet<DefId>>> {
295295
// We only accept this routine to be invoked on implementations
296296
// of a trait, not inherent implementations.
297-
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
297+
let trait_ref = tcx.impl_trait_ref(impl_def_id);
298298
debug!(trait_ref = ?trait_ref.skip_binder());
299299

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

0 commit comments

Comments
 (0)