Skip to content

Commit 9534541

Browse files
committed
Use Item::expect_* and ImplItem::expect_* more
1 parent e49122f commit 9534541

File tree

5 files changed

+11
-22
lines changed

5 files changed

+11
-22
lines changed

compiler/rustc_ast_lowering/src/item.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,10 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
138138
// Evaluate with the lifetimes in `params` in-scope.
139139
// This is used to track which lifetimes have already been defined,
140140
// and which need to be replicated when lowering an async fn.
141-
match parent_hir.node().expect_item().kind {
142-
hir::ItemKind::Impl(hir::Impl { of_trait, .. }) => {
143-
lctx.is_in_trait_impl = of_trait.is_some();
144-
}
145-
_ => {}
146-
};
141+
142+
if let hir::ItemKind::Impl(impl_) = parent_hir.node().expect_item().kind {
143+
lctx.is_in_trait_impl = impl_.of_trait.is_some();
144+
}
147145

148146
match ctxt {
149147
AssocCtxt::Trait => hir::OwnerNode::TraitItem(lctx.lower_trait_item(item)),

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
7474

7575
debug!("visit_implementation_of_copy: self_type={:?} (free)", self_type);
7676

77-
let span = match tcx.hir().expect_item(impl_did).kind {
78-
ItemKind::Impl(hir::Impl { polarity: hir::ImplPolarity::Negative(_), .. }) => return,
79-
ItemKind::Impl(impl_) => impl_.self_ty.span,
80-
_ => bug!("expected Copy impl item"),
77+
let span = match tcx.hir().expect_item(impl_did).expect_impl() {
78+
hir::Impl { polarity: hir::ImplPolarity::Negative(_), .. } => return,
79+
hir::Impl { self_ty, .. } => self_ty.span,
8180
};
8281

8382
let cause = traits::ObligationCause::misc(span, impl_did);

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,7 @@ fn foo(&self) -> Self::T { String::new() }
462462
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = *proj_ty.self_ty().kind() {
463463
let opaque_local_def_id = def_id.as_local();
464464
let opaque_hir_ty = if let Some(opaque_local_def_id) = opaque_local_def_id {
465-
match &tcx.hir().expect_item(opaque_local_def_id).kind {
466-
hir::ItemKind::OpaqueTy(opaque_hir_ty) => opaque_hir_ty,
467-
_ => bug!("The HirId comes from a `ty::Opaque`"),
468-
}
465+
tcx.hir().expect_item(opaque_local_def_id).expect_opaque_ty()
469466
} else {
470467
return false;
471468
};

compiler/rustc_infer/src/infer/opaque_types.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,7 @@ impl<'tcx> InferCtxt<'tcx> {
392392
/// defining scope.
393393
#[instrument(skip(self), level = "trace", ret)]
394394
fn opaque_type_origin_unchecked(&self, def_id: LocalDefId) -> OpaqueTyOrigin {
395-
match self.tcx.hir().expect_item(def_id).kind {
396-
hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => origin,
397-
ref itemkind => {
398-
bug!("weird opaque type: {:?}, {:#?}", def_id, itemkind)
399-
}
400-
}
395+
self.tcx.hir().expect_item(def_id).expect_opaque_ty().origin
401396
}
402397
}
403398

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1469,8 +1469,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14691469

14701470
match impl_item.kind {
14711471
ty::AssocKind::Fn => {
1472-
let ast_item = self.tcx.hir().expect_impl_item(def_id.expect_local());
1473-
let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind else { bug!() };
1472+
let (sig, body) =
1473+
self.tcx.hir().expect_impl_item(def_id.expect_local()).expect_fn();
14741474
self.tables.asyncness.set_some(def_id.index, sig.header.asyncness);
14751475
record_array!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));
14761476
// Can be inside `impl const Trait`, so using sig.header.constness is not reliable

0 commit comments

Comments
 (0)