Skip to content

Commit b323f56

Browse files
committed
Remove Option from impl_trait_header
1 parent e60e9f0 commit b323f56

File tree

24 files changed

+63
-67
lines changed

24 files changed

+63
-67
lines changed

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/check.rs

Lines changed: 2 additions & 2 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),

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ pub(super) fn check_item<'tcx>(
248248
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;

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/collect.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,28 +1291,26 @@ pub fn suggest_impl_trait<'tcx>(
12911291
None
12921292
}
12931293

1294-
fn impl_trait_header(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::ImplTraitHeader<'_>> {
1294+
fn impl_trait_header(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::ImplTraitHeader<'_> {
12951295
let icx = ItemCtxt::new(tcx, def_id);
12961296
let item = tcx.hir_expect_item(def_id);
12971297
let impl_ = item.expect_impl();
1298+
let of_trait = impl_
1299+
.of_trait
1300+
.unwrap_or_else(|| panic!("expected impl trait, found inherent impl on {def_id:?}"));
1301+
let selfty = tcx.type_of(def_id).instantiate_identity();
12981302
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
1299-
if is_rustc_reservation && impl_.of_trait.is_none() {
1300-
tcx.dcx().span_err(item.span, "reservation impls can't be inherent");
1301-
}
1302-
impl_.of_trait.map(|of_trait| {
1303-
let selfty = tcx.type_of(def_id).instantiate_identity();
13041303

1305-
check_impl_constness(tcx, of_trait.constness, &of_trait.trait_ref);
1304+
check_impl_constness(tcx, of_trait.constness, &of_trait.trait_ref);
13061305

1307-
let trait_ref = icx.lowerer().lower_impl_trait_ref(&of_trait.trait_ref, selfty);
1306+
let trait_ref = icx.lowerer().lower_impl_trait_ref(&of_trait.trait_ref, selfty);
13081307

1309-
ty::ImplTraitHeader {
1310-
trait_ref: ty::EarlyBinder::bind(trait_ref),
1311-
safety: of_trait.safety,
1312-
polarity: polarity_of_impl(tcx, of_trait, is_rustc_reservation),
1313-
constness: of_trait.constness,
1314-
}
1315-
})
1308+
ty::ImplTraitHeader {
1309+
trait_ref: ty::EarlyBinder::bind(trait_ref),
1310+
safety: of_trait.safety,
1311+
polarity: polarity_of_impl(tcx, of_trait, is_rustc_reservation),
1312+
constness: of_trait.constness,
1313+
}
13161314
}
13171315

13181316
fn check_impl_constness(

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -514,17 +514,15 @@ pub(super) fn impl_super_outlives(
514514
tcx: TyCtxt<'_>,
515515
def_id: DefId,
516516
) -> ty::EarlyBinder<'_, ty::Clauses<'_>> {
517-
tcx.impl_trait_header(def_id).expect("expected an impl of trait").trait_ref.map_bound(
518-
|trait_ref| {
519-
let clause: ty::Clause<'_> = trait_ref.upcast(tcx);
520-
tcx.mk_clauses_from_iter(util::elaborate(tcx, [clause]).filter(|clause| {
521-
matches!(
522-
clause.kind().skip_binder(),
523-
ty::ClauseKind::TypeOutlives(_) | ty::ClauseKind::RegionOutlives(_)
524-
)
525-
}))
526-
},
527-
)
517+
tcx.impl_trait_header(def_id).trait_ref.map_bound(|trait_ref| {
518+
let clause: ty::Clause<'_> = trait_ref.upcast(tcx);
519+
tcx.mk_clauses_from_iter(util::elaborate(tcx, [clause]).filter(|clause| {
520+
matches!(
521+
clause.kind().skip_binder(),
522+
ty::ClauseKind::TypeOutlives(_) | ty::ClauseKind::RegionOutlives(_)
523+
)
524+
}))
525+
})
528526
}
529527

530528
struct AssocTyToOpaque<'tcx> {

compiler/rustc_hir_analysis/src/delegation.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,7 @@ fn create_generic_args<'tcx>(
272272
(FnKind::AssocTraitImpl, FnKind::AssocTrait) => {
273273
let callee_generics = tcx.generics_of(sig_id);
274274
let parent = tcx.parent(def_id.into());
275-
let parent_args =
276-
tcx.impl_trait_header(parent).unwrap().trait_ref.instantiate_identity().args;
275+
let parent_args = tcx.impl_trait_header(parent).trait_ref.instantiate_identity().args;
277276

278277
let trait_args = ty::GenericArgs::identity_for_item(tcx, sig_id);
279278
let method_args = tcx.mk_args(&trait_args[callee_generics.parent_count..]);

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
473473
} else {
474474
// Find all the types that have an `impl` for the trait.
475475
tcx.all_impls(trait_def_id)
476-
.filter_map(|impl_def_id| tcx.impl_trait_header(impl_def_id))
476+
.map(|impl_def_id| tcx.impl_trait_header(impl_def_id))
477477
.filter(|header| {
478478
// Consider only accessible traits
479479
tcx.visibility(trait_def_id).is_accessible_from(self.item_def_id(), tcx)

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
16151615
.is_accessible_from(self.item_def_id(), tcx)
16161616
&& tcx.all_impls(*trait_def_id)
16171617
.any(|impl_def_id| {
1618-
let header = tcx.impl_trait_header(impl_def_id).unwrap();
1618+
let header = tcx.impl_trait_header(impl_def_id);
16191619
let trait_ref = header.trait_ref.instantiate(
16201620
tcx,
16211621
infcx.fresh_args_for_item(DUMMY_SP, impl_def_id),

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3980,11 +3980,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
39803980
if self
39813981
.tcx
39823982
.all_impls(candidate.def_id)
3983-
.map(|imp_did| {
3984-
self.tcx.impl_trait_header(imp_did).expect(
3985-
"inherent impls can't be candidates, only trait impls can be",
3986-
)
3987-
})
3983+
.map(|imp_did| self.tcx.impl_trait_header(imp_did))
39883984
.filter(|header| header.polarity != ty::ImplPolarity::Positive)
39893985
.any(|header| {
39903986
let imp = header.trait_ref.instantiate_identity();

0 commit comments

Comments
 (0)