Skip to content

Commit

Permalink
simplify code of finding arg index
Browse files Browse the repository at this point in the history
  • Loading branch information
SparrowLii committed May 25, 2022
1 parent 07e7b43 commit 1b4714f
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions compiler/rustc_typeck/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,38 +161,23 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
// We've encountered an `AnonConst` in some path, so we need to
// figure out which generic parameter it corresponds to and return
// the relevant type.
let filtered = path.segments.iter().find_map(|seg| {
seg.args?
.args
let Some((arg_index, segment)) = path.segments.iter().find_map(|seg| {
let args = seg.args?;
args.args
.iter()
.filter(|arg| arg.is_ty_or_const())
.position(|arg| arg.id() == hir_id)
.map(|index| (index, seg)).or_else(|| args.bindings
.iter()
.filter(|arg| arg.is_ty_or_const())
.position(|arg| arg.id() == hir_id)
.map(|index| (index, seg))
});

// FIXME(associated_const_generics): can we blend this with iteration above?
let (arg_index, segment) = match filtered {
None => {
let binding_filtered = path.segments.iter().find_map(|seg| {
seg.args?
.bindings
.iter()
.filter_map(TypeBinding::opt_const)
.position(|ct| ct.hir_id == hir_id)
.map(|idx| (idx, seg))
});
match binding_filtered {
Some(inner) => inner,
None => {
tcx.sess.delay_span_bug(
tcx.def_span(def_id),
"no arg matching AnonConst in path",
);
return None;
}
}
}
Some(inner) => inner,
.filter_map(TypeBinding::opt_const)
.position(|ct| ct.hir_id == hir_id)
.map(|idx| (idx, seg)))
}) else {
tcx.sess.delay_span_bug(
tcx.def_span(def_id),
"no arg matching AnonConst in path",
);
return None;
};

// Try to use the segment resolution if it is valid, otherwise we
Expand Down

0 comments on commit 1b4714f

Please sign in to comment.