@@ -2423,7 +2423,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2423
2423
2424
2424
fn parameter_count_error < ' tcx > ( sess : & Session , sp : Span , expected_count : usize ,
2425
2425
arg_count : usize , error_code : & str , variadic : bool ,
2426
- def_span : Option < Span > ) {
2426
+ def_span : Option < Span > , sugg_unit : bool ) {
2427
2427
let mut err = sess. struct_span_err_with_code ( sp,
2428
2428
& format ! ( "this function takes {}{} parameter{} but {} parameter{} supplied" ,
2429
2429
if variadic { "at least " } else { "" } ,
@@ -2433,13 +2433,23 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2433
2433
if arg_count == 1 { " was" } else { "s were" } ) ,
2434
2434
error_code) ;
2435
2435
2436
- err. span_label ( sp, format ! ( "expected {}{} parameter{}" ,
2437
- if variadic { "at least " } else { "" } ,
2438
- expected_count,
2439
- if expected_count == 1 { "" } else { "s" } ) ) ;
2440
2436
if let Some ( def_s) = def_span {
2441
2437
err. span_label ( def_s, "defined here" ) ;
2442
2438
}
2439
+ if sugg_unit {
2440
+ let mut sugg_span = sp. end_point ( ) ;
2441
+ // remove closing `)` from the span
2442
+ sugg_span. hi = sugg_span. lo ;
2443
+ err. span_suggestion (
2444
+ sugg_span,
2445
+ "expected the unit value `()`. You can create one with a pair of parenthesis" ,
2446
+ String :: from ( "()" ) ) ;
2447
+ } else {
2448
+ err. span_label ( sp, format ! ( "expected {}{} parameter{}" ,
2449
+ if variadic { "at least " } else { "" } ,
2450
+ expected_count,
2451
+ if expected_count == 1 { "" } else { "s" } ) ) ;
2452
+ }
2443
2453
err. emit ( ) ;
2444
2454
}
2445
2455
@@ -2448,7 +2458,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2448
2458
match tuple_type. sty {
2449
2459
ty:: TyTuple ( arg_types, _) if arg_types. len ( ) != args. len ( ) => {
2450
2460
parameter_count_error ( tcx. sess , sp_args, arg_types. len ( ) , args. len ( ) ,
2451
- "E0057" , false , def_span) ;
2461
+ "E0057" , false , def_span, false ) ;
2452
2462
expected_arg_tys = & [ ] ;
2453
2463
self . err_args ( args. len ( ) )
2454
2464
}
@@ -2477,13 +2487,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2477
2487
fn_inputs. to_vec ( )
2478
2488
} else {
2479
2489
parameter_count_error ( tcx. sess , sp_args, expected_arg_count,
2480
- supplied_arg_count, "E0060" , true , def_span) ;
2490
+ supplied_arg_count, "E0060" , true , def_span, false ) ;
2481
2491
expected_arg_tys = & [ ] ;
2482
2492
self . err_args ( supplied_arg_count)
2483
2493
}
2484
2494
} else {
2495
+ // is the missing argument of type `()`?
2496
+ let sugg_unit = if expected_arg_tys. len ( ) == 1 && supplied_arg_count == 0 {
2497
+ self . resolve_type_vars_if_possible ( & expected_arg_tys[ 0 ] ) . is_nil ( )
2498
+ } else if fn_inputs. len ( ) == 1 && supplied_arg_count == 0 {
2499
+ self . resolve_type_vars_if_possible ( & fn_inputs[ 0 ] ) . is_nil ( )
2500
+ } else {
2501
+ false
2502
+ } ;
2485
2503
parameter_count_error ( tcx. sess , sp_args, expected_arg_count,
2486
- supplied_arg_count, "E0061" , false , def_span) ;
2504
+ supplied_arg_count, "E0061" , false , def_span, sugg_unit ) ;
2487
2505
expected_arg_tys = & [ ] ;
2488
2506
self . err_args ( supplied_arg_count)
2489
2507
} ;
0 commit comments