@@ -2472,7 +2472,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2472
2472
2473
2473
fn parameter_count_error < ' tcx > ( sess : & Session , sp : Span , expected_count : usize ,
2474
2474
arg_count : usize , error_code : & str , variadic : bool ,
2475
- def_span : Option < Span > ) {
2475
+ def_span : Option < Span > , sugg_unit : bool ) {
2476
2476
let mut err = sess. struct_span_err_with_code ( sp,
2477
2477
& format ! ( "this function takes {}{} parameter{} but {} parameter{} supplied" ,
2478
2478
if variadic { "at least " } else { "" } ,
@@ -2482,13 +2482,23 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2482
2482
if arg_count == 1 { " was" } else { "s were" } ) ,
2483
2483
error_code) ;
2484
2484
2485
- err. span_label ( sp, format ! ( "expected {}{} parameter{}" ,
2486
- if variadic { "at least " } else { "" } ,
2487
- expected_count,
2488
- if expected_count == 1 { "" } else { "s" } ) ) ;
2489
2485
if let Some ( def_s) = def_span {
2490
2486
err. span_label ( def_s, "defined here" ) ;
2491
2487
}
2488
+ if sugg_unit {
2489
+ let mut sugg_span = sp. end_point ( ) ;
2490
+ // remove closing `)` from the span
2491
+ sugg_span. hi = sugg_span. lo ;
2492
+ err. span_suggestion (
2493
+ sugg_span,
2494
+ "expected the unit value `()`. You can create one with a pair of parenthesis" ,
2495
+ String :: from ( "()" ) ) ;
2496
+ } else {
2497
+ err. span_label ( sp, format ! ( "expected {}{} parameter{}" ,
2498
+ if variadic { "at least " } else { "" } ,
2499
+ expected_count,
2500
+ if expected_count == 1 { "" } else { "s" } ) ) ;
2501
+ }
2492
2502
err. emit ( ) ;
2493
2503
}
2494
2504
@@ -2497,7 +2507,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2497
2507
match tuple_type. sty {
2498
2508
ty:: TyTuple ( arg_types, _) if arg_types. len ( ) != args. len ( ) => {
2499
2509
parameter_count_error ( tcx. sess , sp_args, arg_types. len ( ) , args. len ( ) ,
2500
- "E0057" , false , def_span) ;
2510
+ "E0057" , false , def_span, false ) ;
2501
2511
expected_arg_tys = & [ ] ;
2502
2512
self . err_args ( args. len ( ) )
2503
2513
}
@@ -2526,13 +2536,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2526
2536
fn_inputs. to_vec ( )
2527
2537
} else {
2528
2538
parameter_count_error ( tcx. sess , sp_args, expected_arg_count,
2529
- supplied_arg_count, "E0060" , true , def_span) ;
2539
+ supplied_arg_count, "E0060" , true , def_span, false ) ;
2530
2540
expected_arg_tys = & [ ] ;
2531
2541
self . err_args ( supplied_arg_count)
2532
2542
}
2533
2543
} else {
2544
+ // is the missing argument of type `()`?
2545
+ let sugg_unit = if expected_arg_tys. len ( ) == 1 && supplied_arg_count == 0 {
2546
+ self . resolve_type_vars_if_possible ( & expected_arg_tys[ 0 ] ) . is_nil ( )
2547
+ } else if fn_inputs. len ( ) == 1 && supplied_arg_count == 0 {
2548
+ self . resolve_type_vars_if_possible ( & fn_inputs[ 0 ] ) . is_nil ( )
2549
+ } else {
2550
+ false
2551
+ } ;
2534
2552
parameter_count_error ( tcx. sess , sp_args, expected_arg_count,
2535
- supplied_arg_count, "E0061" , false , def_span) ;
2553
+ supplied_arg_count, "E0061" , false , def_span, sugg_unit ) ;
2536
2554
expected_arg_tys = & [ ] ;
2537
2555
self . err_args ( supplied_arg_count)
2538
2556
} ;
0 commit comments