Skip to content

Commit fa17dac

Browse files
committed
Rollup merge of rust-lang#48123 - nikomatsakis:issue-47244-expected-num-args, r=estebank
Fixes rust-lang#47311. r? @nrc
2 parents 894707a + cc05561 commit fa17dac

File tree

5 files changed

+218
-51
lines changed

5 files changed

+218
-51
lines changed

src/librustc/traits/error_reporting.rs

+35-7
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
747747
ty::TyTuple(ref tys, _) => tys.iter()
748748
.map(|t| match t.sty {
749749
ty::TypeVariants::TyTuple(ref tys, _) => ArgKind::Tuple(
750-
span,
750+
Some(span),
751751
tys.iter()
752752
.map(|ty| ("_".to_owned(), format!("{}", ty.sty)))
753753
.collect::<Vec<_>>()
@@ -815,7 +815,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
815815
}
816816
}
817817

818-
fn get_fn_like_arguments(&self, node: hir::map::Node) -> (Span, Vec<ArgKind>) {
818+
/// Given some node representing a fn-like thing in the HIR map,
819+
/// returns a span and `ArgKind` information that describes the
820+
/// arguments it expects. This can be supplied to
821+
/// `report_arg_count_mismatch`.
822+
pub fn get_fn_like_arguments(&self, node: hir::map::Node) -> (Span, Vec<ArgKind>) {
819823
match node {
820824
hir::map::NodeExpr(&hir::Expr {
821825
node: hir::ExprClosure(_, ref _decl, id, span, _),
@@ -829,7 +833,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
829833
..
830834
} = arg.pat.clone().into_inner() {
831835
ArgKind::Tuple(
832-
span,
836+
Some(span),
833837
args.iter().map(|pat| {
834838
let snippet = self.tcx.sess.codemap()
835839
.span_to_snippet(pat.span).unwrap();
@@ -862,7 +866,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
862866
(self.tcx.sess.codemap().def_span(span), decl.inputs.iter()
863867
.map(|arg| match arg.clone().into_inner().node {
864868
hir::TyTup(ref tys) => ArgKind::Tuple(
865-
arg.span,
869+
Some(arg.span),
866870
tys.iter()
867871
.map(|_| ("_".to_owned(), "_".to_owned()))
868872
.collect::<Vec<_>>(),
@@ -874,7 +878,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
874878
}
875879
}
876880

877-
fn report_arg_count_mismatch(
881+
/// Reports an error when the number of arguments needed by a
882+
/// trait match doesn't match the number that the expression
883+
/// provides.
884+
pub fn report_arg_count_mismatch(
878885
&self,
879886
span: Span,
880887
found_span: Option<Span>,
@@ -1385,13 +1392,34 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
13851392
}
13861393
}
13871394

1388-
enum ArgKind {
1395+
/// Summarizes information
1396+
pub enum ArgKind {
1397+
/// An argument of non-tuple type. Parameters are (name, ty)
13891398
Arg(String, String),
1390-
Tuple(Span, Vec<(String, String)>),
1399+
1400+
/// An argument of tuple type. For a "found" argument, the span is
1401+
/// the locationo in the source of the pattern. For a "expected"
1402+
/// argument, it will be None. The vector is a list of (name, ty)
1403+
/// strings for the components of the tuple.
1404+
Tuple(Option<Span>, Vec<(String, String)>),
13911405
}
13921406

13931407
impl ArgKind {
13941408
fn empty() -> ArgKind {
13951409
ArgKind::Arg("_".to_owned(), "_".to_owned())
13961410
}
1411+
1412+
/// Creates an `ArgKind` from the expected type of an
1413+
/// argument. This has no name (`_`) and no source spans..
1414+
pub fn from_expected_ty(t: Ty<'_>) -> ArgKind {
1415+
match t.sty {
1416+
ty::TyTuple(ref tys, _) => ArgKind::Tuple(
1417+
None,
1418+
tys.iter()
1419+
.map(|ty| ("_".to_owned(), format!("{}", ty.sty)))
1420+
.collect::<Vec<_>>()
1421+
),
1422+
_ => ArgKind::Arg("_".to_owned(), format!("{}", t.sty)),
1423+
}
1424+
}
13971425
}

src/librustc/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub use self::util::SupertraitDefIds;
4949
pub use self::util::transitive_bounds;
5050

5151
mod coherence;
52-
mod error_reporting;
52+
pub mod error_reporting;
5353
mod fulfill;
5454
mod project;
5555
mod object_safety;

0 commit comments

Comments
 (0)