Skip to content

Commit af9dcf7

Browse files
committed
Remove one layer of nested matching on the same thing
1 parent a3ca139 commit af9dcf7

File tree

1 file changed

+39
-55
lines changed

1 file changed

+39
-55
lines changed

compiler/rustc_ty_utils/src/opaque_types.rs

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -239,73 +239,56 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
239239
fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [LocalDefId] {
240240
let kind = tcx.def_kind(item);
241241
trace!(?kind);
242+
let mut collector = OpaqueTypeCollector::new(tcx, item);
242243
match kind {
243244
// We're also doing this for `AssocTy` for the wf checks in `check_opaque_meets_bounds`
244245
DefKind::Static(_)
245246
| DefKind::Const
246-
| DefKind::TyAlias
247-
| DefKind::Fn
248-
| DefKind::OpaqueTy
247+
| DefKind::AssocConst
248+
| DefKind::AssocFn
249249
| DefKind::AnonConst
250250
| DefKind::InlineConst
251-
| DefKind::AssocFn
252-
| DefKind::AssocTy
253-
| DefKind::AssocConst => {
254-
let mut collector = OpaqueTypeCollector::new(tcx, item);
251+
| DefKind::Fn => {
255252
match kind {
253+
// Walk over the signature of the function-like to find the opaques.
254+
DefKind::AssocFn | DefKind::Fn => {
255+
let ty_sig = tcx.fn_sig(item).subst_identity();
256+
let hir_sig = tcx.hir().get_by_def_id(item).fn_sig().unwrap();
257+
// Walk over the inputs and outputs manually in order to get good spans for them.
258+
collector.visit_spanned(hir_sig.decl.output.span(), ty_sig.output());
259+
for (hir, ty) in hir_sig.decl.inputs.iter().zip(ty_sig.inputs().iter()) {
260+
collector.visit_spanned(hir.span, ty.map_bound(|x| *x));
261+
}
262+
}
263+
// Walk over the type of the item to find opaques.
256264
DefKind::Static(_)
257265
| DefKind::Const
258266
| DefKind::AssocConst
259-
| DefKind::AssocFn
260267
| DefKind::AnonConst
261-
| DefKind::InlineConst
262-
| DefKind::Fn => {
263-
match kind {
264-
// Walk over the signature of the function-like to find the opaques.
265-
DefKind::AssocFn | DefKind::Fn => {
266-
let ty_sig = tcx.fn_sig(item).subst_identity();
267-
let hir_sig = tcx.hir().get_by_def_id(item).fn_sig().unwrap();
268-
// Walk over the inputs and outputs manually in order to get good spans for them.
269-
collector.visit_spanned(hir_sig.decl.output.span(), ty_sig.output());
270-
for (hir, ty) in hir_sig.decl.inputs.iter().zip(ty_sig.inputs().iter())
271-
{
272-
collector.visit_spanned(hir.span, ty.map_bound(|x| *x));
273-
}
274-
}
275-
// Walk over the type of the item to find opaques.
276-
DefKind::Static(_)
277-
| DefKind::Const
278-
| DefKind::AssocConst
279-
| DefKind::AnonConst
280-
| DefKind::InlineConst => {
281-
let span = match tcx.hir().get_by_def_id(item).ty() {
282-
Some(ty) => ty.span,
283-
_ => tcx.def_span(item),
284-
};
285-
collector.visit_spanned(span, tcx.type_of(item).subst_identity());
286-
}
287-
_ => unreachable!(),
288-
}
289-
// Look at all where bounds.
290-
tcx.predicates_of(item).instantiate_identity(tcx).visit_with(&mut collector);
291-
// An item is allowed to constrain opaques declared within its own body (but not nested within
292-
// nested functions).
293-
for id in find_taits_declared_in_body(tcx, item) {
294-
collector.opaques.extend(tcx.opaque_types_defined_by(id))
295-
}
296-
}
297-
DefKind::TyAlias | DefKind::AssocTy => {
298-
tcx.type_of(item).subst_identity().visit_with(&mut collector);
299-
}
300-
DefKind::OpaqueTy => {
301-
for (pred, span) in tcx.explicit_item_bounds(item).subst_identity_iter_copied()
302-
{
303-
collector.visit_spanned(span, pred);
304-
}
268+
| DefKind::InlineConst => {
269+
let span = match tcx.hir().get_by_def_id(item).ty() {
270+
Some(ty) => ty.span,
271+
_ => tcx.def_span(item),
272+
};
273+
collector.visit_spanned(span, tcx.type_of(item).subst_identity());
305274
}
306275
_ => unreachable!(),
307276
}
308-
tcx.arena.alloc_from_iter(collector.opaques)
277+
// Look at all where bounds.
278+
tcx.predicates_of(item).instantiate_identity(tcx).visit_with(&mut collector);
279+
// An item is allowed to constrain opaques declared within its own body (but not nested within
280+
// nested functions).
281+
for id in find_taits_declared_in_body(tcx, item) {
282+
collector.opaques.extend(tcx.opaque_types_defined_by(id))
283+
}
284+
}
285+
DefKind::TyAlias | DefKind::AssocTy => {
286+
tcx.type_of(item).subst_identity().visit_with(&mut collector);
287+
}
288+
DefKind::OpaqueTy => {
289+
for (pred, span) in tcx.explicit_item_bounds(item).subst_identity_iter_copied() {
290+
collector.visit_spanned(span, pred);
291+
}
309292
}
310293
DefKind::Mod
311294
| DefKind::Struct
@@ -326,11 +309,12 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [
326309
| DefKind::Field
327310
| DefKind::LifetimeParam
328311
| DefKind::GlobalAsm
329-
| DefKind::Impl { .. } => &[],
312+
| DefKind::Impl { .. } => {}
330313
DefKind::Closure | DefKind::Generator => {
331-
tcx.opaque_types_defined_by(tcx.local_parent(item))
314+
return tcx.opaque_types_defined_by(tcx.local_parent(item));
332315
}
333316
}
317+
tcx.arena.alloc_from_iter(collector.opaques)
334318
}
335319

336320
#[instrument(level = "trace", skip(tcx), ret)]

0 commit comments

Comments
 (0)