Skip to content

Commit 08a7249

Browse files
committed
get_fn_like_arguments: avoid .unwrap
1 parent 9c08269 commit 08a7249

File tree

2 files changed

+33
-37
lines changed
  • src
    • librustc_trait_selection/traits/error_reporting
    • librustc_typeck/check

2 files changed

+33
-37
lines changed

src/librustc_trait_selection/traits/error_reporting/mod.rs

+24-33
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub trait InferCtxtExt<'tcx> {
6565
/// returns a span and `ArgKind` information that describes the
6666
/// arguments it expects. This can be supplied to
6767
/// `report_arg_count_mismatch`.
68-
fn get_fn_like_arguments(&self, node: Node<'_>) -> (Span, Vec<ArgKind>);
68+
fn get_fn_like_arguments(&self, node: Node<'_>) -> Option<(Span, Vec<ArgKind>)>;
6969

7070
/// Reports an error when the number of arguments needed by a
7171
/// trait match doesn't match the number that the expression
@@ -611,10 +611,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
611611
)
612612
} else {
613613
let (closure_span, found) = found_did
614-
.and_then(|did| self.tcx.hir().get_if_local(did))
615-
.map(|node| {
616-
let (found_span, found) = self.get_fn_like_arguments(node);
617-
(Some(found_span), found)
614+
.and_then(|did| {
615+
let node = self.tcx.hir().get_if_local(did)?;
616+
let (found_span, found) = self.get_fn_like_arguments(node)?;
617+
Some((Some(found_span), found))
618618
})
619619
.unwrap_or((found_span, found));
620620

@@ -672,43 +672,38 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
672672
/// returns a span and `ArgKind` information that describes the
673673
/// arguments it expects. This can be supplied to
674674
/// `report_arg_count_mismatch`.
675-
fn get_fn_like_arguments(&self, node: Node<'_>) -> (Span, Vec<ArgKind>) {
676-
match node {
675+
fn get_fn_like_arguments(&self, node: Node<'_>) -> Option<(Span, Vec<ArgKind>)> {
676+
let sm = self.tcx.sess.source_map();
677+
let hir = self.tcx.hir();
678+
Some(match node {
677679
Node::Expr(&hir::Expr {
678680
kind: hir::ExprKind::Closure(_, ref _decl, id, span, _),
679681
..
680682
}) => (
681-
self.tcx.sess.source_map().guess_head_span(span),
682-
self.tcx
683-
.hir()
684-
.body(id)
683+
sm.guess_head_span(span),
684+
hir.body(id)
685685
.params
686686
.iter()
687687
.map(|arg| {
688688
if let hir::Pat { kind: hir::PatKind::Tuple(ref args, _), span, .. } =
689689
*arg.pat
690690
{
691-
ArgKind::Tuple(
691+
Some(ArgKind::Tuple(
692692
Some(span),
693693
args.iter()
694694
.map(|pat| {
695-
let snippet = self
696-
.tcx
697-
.sess
698-
.source_map()
699-
.span_to_snippet(pat.span)
700-
.unwrap();
701-
(snippet, "_".to_owned())
695+
sm.span_to_snippet(pat.span)
696+
.ok()
697+
.map(|snippet| (snippet, "_".to_owned()))
702698
})
703-
.collect::<Vec<_>>(),
704-
)
699+
.collect::<Option<Vec<_>>>()?,
700+
))
705701
} else {
706-
let name =
707-
self.tcx.sess.source_map().span_to_snippet(arg.pat.span).unwrap();
708-
ArgKind::Arg(name, "_".to_owned())
702+
let name = sm.span_to_snippet(arg.pat.span).ok()?;
703+
Some(ArgKind::Arg(name, "_".to_owned()))
709704
}
710705
})
711-
.collect::<Vec<ArgKind>>(),
706+
.collect::<Option<Vec<ArgKind>>>()?,
712707
),
713708
Node::Item(&hir::Item { span, kind: hir::ItemKind::Fn(ref sig, ..), .. })
714709
| Node::ImplItem(&hir::ImplItem {
@@ -721,7 +716,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
721716
kind: hir::TraitItemKind::Fn(ref sig, _),
722717
..
723718
}) => (
724-
self.tcx.sess.source_map().guess_head_span(span),
719+
sm.guess_head_span(span),
725720
sig.decl
726721
.inputs
727722
.iter()
@@ -735,16 +730,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
735730
.collect::<Vec<ArgKind>>(),
736731
),
737732
Node::Ctor(ref variant_data) => {
738-
let span = variant_data
739-
.ctor_hir_id()
740-
.map(|hir_id| self.tcx.hir().span(hir_id))
741-
.unwrap_or(DUMMY_SP);
742-
let span = self.tcx.sess.source_map().guess_head_span(span);
743-
733+
let span = variant_data.ctor_hir_id().map(|id| hir.span(id)).unwrap_or(DUMMY_SP);
734+
let span = sm.guess_head_span(span);
744735
(span, vec![ArgKind::empty(); variant_data.fields().len()])
745736
}
746737
_ => panic!("non-FnLike node found: {:?}", node),
747-
}
738+
})
748739
}
749740

750741
/// Reports an error when the number of arguments needed by a

src/librustc_typeck/check/closure.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -432,18 +432,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
432432
body: &hir::Body<'_>,
433433
expected_sig: ExpectedSig<'tcx>,
434434
) -> ClosureSignatures<'tcx> {
435-
let expr_map_node = self.tcx.hir().get_if_local(expr_def_id).unwrap();
435+
let hir = self.tcx.hir();
436+
let expr_map_node = hir.get_if_local(expr_def_id).unwrap();
436437
let expected_args: Vec<_> = expected_sig
437438
.sig
438439
.inputs()
439440
.iter()
440441
.map(|ty| ArgKind::from_expected_ty(ty, None))
441442
.collect();
442-
let (closure_span, found_args) = self.get_fn_like_arguments(expr_map_node);
443-
let expected_span = expected_sig.cause_span.unwrap_or(closure_span);
443+
let (closure_span, found_args) = match self.get_fn_like_arguments(expr_map_node) {
444+
Some((sp, args)) => (Some(sp), args),
445+
None => (None, Vec::new()),
446+
};
447+
let expected_span =
448+
expected_sig.cause_span.unwrap_or_else(|| hir.span_if_local(expr_def_id).unwrap());
444449
self.report_arg_count_mismatch(
445450
expected_span,
446-
Some(closure_span),
451+
closure_span,
447452
expected_args,
448453
found_args,
449454
true,

0 commit comments

Comments
 (0)