Skip to content

Commit 6f82dea

Browse files
committed
Auto merge of rust-lang#44059 - oli-obk:ok_suggestion, r=nikomatsakis
Suggest `Ok(())` when encountering `Result::<(), E>::Ok()`
2 parents d2d5069 + 0b72497 commit 6f82dea

File tree

5 files changed

+101
-18
lines changed

5 files changed

+101
-18
lines changed

src/librustc_data_structures/unify/mod.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ impl<K: UnifyKey> VarValue<K> {
119119
}
120120
}
121121

122-
// We can't use V:LatticeValue, much as I would like to,
123-
// because frequently the pattern is that V=Option<U> for some
124-
// other type parameter U, and we have no way to say
125-
// Option<U>:LatticeValue.
122+
/// We can't use V:LatticeValue, much as I would like to,
123+
/// because frequently the pattern is that V=Option<U> for some
124+
/// other type parameter U, and we have no way to say
125+
/// Option<U>:LatticeValue.
126126
127127
impl<K: UnifyKey> UnificationTable<K> {
128128
pub fn new() -> UnificationTable<K> {
@@ -249,7 +249,7 @@ impl<K: UnifyKey> sv::SnapshotVecDelegate for Delegate<K> {
249249
fn reverse(_: &mut Vec<VarValue<K>>, _: ()) {}
250250
}
251251

252-
// # Base union-find algorithm, where we are just making sets
252+
/// # Base union-find algorithm, where we are just making sets
253253
254254
impl<'tcx, K: UnifyKey> UnificationTable<K>
255255
where K::Value: Combine
@@ -281,11 +281,11 @@ impl<'tcx, K: UnifyKey> UnificationTable<K>
281281
}
282282
}
283283

284-
// # Non-subtyping unification
285-
//
286-
// Code to handle keys which carry a value, like ints,
287-
// floats---anything that doesn't have a subtyping relationship we
288-
// need to worry about.
284+
/// # Non-subtyping unification
285+
///
286+
/// Code to handle keys which carry a value, like ints,
287+
/// floats---anything that doesn't have a subtyping relationship we
288+
/// need to worry about.
289289
290290
impl<'tcx, K, V> UnificationTable<K>
291291
where K: UnifyKey<Value = Option<V>>,

src/librustc_typeck/check/callee.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub fn check_legal_trait_for_method_call(tcx: TyCtxt, span: Span, trait_id: DefI
3838
enum CallStep<'tcx> {
3939
Builtin(Ty<'tcx>),
4040
DeferredClosure(ty::FnSig<'tcx>),
41+
/// e.g. enum variant constructors
4142
Overloaded(MethodCallee<'tcx>),
4243
}
4344

src/librustc_typeck/check/mod.rs

+26-8
Original file line numberDiff line numberDiff line change
@@ -2472,7 +2472,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
24722472

24732473
fn parameter_count_error<'tcx>(sess: &Session, sp: Span, expected_count: usize,
24742474
arg_count: usize, error_code: &str, variadic: bool,
2475-
def_span: Option<Span>) {
2475+
def_span: Option<Span>, sugg_unit: bool) {
24762476
let mut err = sess.struct_span_err_with_code(sp,
24772477
&format!("this function takes {}{} parameter{} but {} parameter{} supplied",
24782478
if variadic {"at least "} else {""},
@@ -2482,13 +2482,23 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
24822482
if arg_count == 1 {" was"} else {"s were"}),
24832483
error_code);
24842484

2485-
err.span_label(sp, format!("expected {}{} parameter{}",
2486-
if variadic {"at least "} else {""},
2487-
expected_count,
2488-
if expected_count == 1 {""} else {"s"}));
24892485
if let Some(def_s) = def_span {
24902486
err.span_label(def_s, "defined here");
24912487
}
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+
}
24922502
err.emit();
24932503
}
24942504

@@ -2497,7 +2507,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
24972507
match tuple_type.sty {
24982508
ty::TyTuple(arg_types, _) if arg_types.len() != args.len() => {
24992509
parameter_count_error(tcx.sess, sp_args, arg_types.len(), args.len(),
2500-
"E0057", false, def_span);
2510+
"E0057", false, def_span, false);
25012511
expected_arg_tys = &[];
25022512
self.err_args(args.len())
25032513
}
@@ -2526,13 +2536,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
25262536
fn_inputs.to_vec()
25272537
} else {
25282538
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);
25302540
expected_arg_tys = &[];
25312541
self.err_args(supplied_arg_count)
25322542
}
25332543
} 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+
};
25342552
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);
25362554
expected_arg_tys = &[];
25372555
self.err_args(supplied_arg_count)
25382556
};
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn foo(():(), ():()) {}
12+
fn bar(():()) {}
13+
14+
fn main() {
15+
let _: Result<(), String> = Ok();
16+
foo();
17+
foo(());
18+
bar();
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error[E0061]: this function takes 1 parameter but 0 parameters were supplied
2+
--> $DIR/missing-unit-argument.rs:15:33
3+
|
4+
15 | let _: Result<(), String> = Ok();
5+
| ^^^^
6+
|
7+
help: expected the unit value `()`. You can create one with a pair of parenthesis
8+
|
9+
15 | let _: Result<(), String> = Ok(());
10+
| ^^
11+
12+
error[E0061]: this function takes 2 parameters but 0 parameters were supplied
13+
--> $DIR/missing-unit-argument.rs:16:5
14+
|
15+
11 | fn foo(():(), ():()) {}
16+
| ----------------------- defined here
17+
...
18+
16 | foo();
19+
| ^^^^^ expected 2 parameters
20+
21+
error[E0061]: this function takes 2 parameters but 1 parameter was supplied
22+
--> $DIR/missing-unit-argument.rs:17:9
23+
|
24+
11 | fn foo(():(), ():()) {}
25+
| ----------------------- defined here
26+
...
27+
17 | foo(());
28+
| ^^ expected 2 parameters
29+
30+
error[E0061]: this function takes 1 parameter but 0 parameters were supplied
31+
--> $DIR/missing-unit-argument.rs:18:5
32+
|
33+
12 | fn bar(():()) {}
34+
| ---------------- defined here
35+
...
36+
18 | bar();
37+
| ^^^^^
38+
|
39+
help: expected the unit value `()`. You can create one with a pair of parenthesis
40+
|
41+
18 | bar(());
42+
| ^^
43+
44+
error: aborting due to 4 previous errors
45+

0 commit comments

Comments
 (0)