Skip to content

Commit d97b39d

Browse files
authored
Rollup merge of rust-lang#110461 - WaffleLapkin:expect_, r=Nilstrieb
Use `Item::expect_*` and `ImplItem::expect_*` more r? ``@Nilstrieb``
2 parents d646891 + 880da9f commit d97b39d

File tree

6 files changed

+11
-24
lines changed

6 files changed

+11
-24
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/src/hir.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3146,7 +3146,6 @@ impl<'hir> Item<'hir> {
31463146
(ty, gen)
31473147
}
31483148

3149-
/// An opaque `impl Trait` type alias, e.g., `type Foo = impl Bar;`.
31503149
/// Expect an [`ItemKind::OpaqueTy`] or panic.
31513150
#[track_caller]
31523151
pub fn expect_opaque_ty(&self) -> &OpaqueTy<'hir> {
@@ -3168,7 +3167,6 @@ impl<'hir> Item<'hir> {
31683167
(data, gen)
31693168
}
31703169

3171-
/// A union definition, e.g., `union Foo<A, B> {x: A, y: B}`.
31723170
/// Expect an [`ItemKind::Union`] or panic.
31733171
#[track_caller]
31743172
pub fn expect_union(&self) -> (&VariantData<'hir>, &'hir Generics<'hir>) {

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)