Skip to content

Commit

Permalink
Rollup merge of rust-lang#97399 - SparrowLii:arg_index, r=oli-obk
Browse files Browse the repository at this point in the history
simplify code of finding arg index in `opt_const_param_of`

From the FIXME in the impl of `opt_const_param_of`. Part of the code is simplified by blending two iterator statements and using `let...else` statement.
  • Loading branch information
Dylan-DPC authored Jun 1, 2022
2 parents 9f84345 + 1b4714f commit 59b802d
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 59b802d

Please sign in to comment.