Skip to content

Commit 4753fad

Browse files
committed
Add error explanation for E0057, E0059–E0061
These errors all relate to type checking, specifically the number of function arguments, and occur in librustc_typeck::check::check_argument_types.
1 parent 8025bc9 commit 4753fad

File tree

1 file changed

+91
-4
lines changed

1 file changed

+91
-4
lines changed

src/librustc_typeck/diagnostics.rs

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,97 @@ create an infinite recursion of dereferencing, in which case the only fix is to
358358
somehow break the recursion.
359359
"##,
360360

361+
E0057: r##"
362+
When invoking closures or other implementations of the function traits `Fn`,
363+
`FnMut` or `FnOnce` using call notation, the number of parameters passed to the
364+
function must match its definition.
365+
366+
An example using a closure:
367+
368+
```
369+
let f = |x| x * 3;
370+
let a = f(); // invalid, too few parameters
371+
let b = f(4); // this works!
372+
let c = f(2, 3); // invalid, too many parameters
373+
```
374+
375+
A generic function must be treated similarly:
376+
377+
```
378+
fn foo<F: Fn()>(f: F) {
379+
f(); // this is valid, but f(3) would not work
380+
}
381+
```
382+
"##,
383+
384+
E0059: r##"
385+
The built-in function traits are generic over a tuple of the function arguments.
386+
If one uses angle-bracket notation (`Fn<(T,), Output=U>`) instead of parentheses
387+
(`Fn(T) -> U`) to denote the function trait, the type parameter should be a
388+
tuple. Otherwise function call notation cannot be used and the trait will not be
389+
implemented by closures.
390+
391+
The most likely source of this error is using angle-bracket notation without
392+
wrapping the function argument type into a tuple, for example:
393+
394+
```
395+
fn foo<F: Fn<i32>>(f: F) -> F::Output { f(3) }
396+
```
397+
398+
It can be fixed by adjusting the trait bound like this:
399+
400+
```
401+
fn foo<F: Fn<(i32,)>>(f: F) -> F::Output { f(3) }
402+
```
403+
404+
Note that `(T,)` always denotes the type of a 1-tuple containing an element of
405+
type `T`. The comma is necessary for syntactic disambiguation.
406+
"##,
407+
408+
E0060: r##"
409+
External C functions are allowed to be variadic. However, a variadic function
410+
takes a minimum number of arguments. For example, consider C's variadic `printf`
411+
function:
412+
413+
```
414+
extern crate libc;
415+
use libc::{ c_char, c_int };
416+
417+
extern "C" {
418+
fn printf(_: *const c_char, ...) -> c_int;
419+
}
420+
```
421+
422+
Using this declaration, it must be called with at least one argument, so
423+
simply calling `printf()` is illegal. But the following uses are allowed:
424+
425+
```
426+
unsafe {
427+
use std::ffi::CString;
428+
429+
printf(CString::new("test\n").unwrap().as_ptr());
430+
printf(CString::new("number = %d\n").unwrap().as_ptr(), 3);
431+
printf(CString::new("%d, %d\n").unwrap().as_ptr(), 10, 5);
432+
}
433+
```
434+
"##,
435+
436+
E0061: r##"
437+
The number of arguments passed to a function must match the number of arguments
438+
specified in the function signature.
439+
440+
For example, a function like
441+
442+
```
443+
fn f(a: u16, b: &str) {}
444+
```
445+
446+
must always be called with exactly two arguments, e.g. `f(2, "test")`.
447+
448+
Note, that Rust does not have a notion of optional function arguments or
449+
variadic functions (except for its C-FFI).
450+
"##,
451+
361452
E0062: r##"
362453
This error indicates that during an attempt to build a struct or struct-like
363454
enum variant, one of the fields was specified more than once. Each field should
@@ -1210,10 +1301,6 @@ register_diagnostics! {
12101301
E0036, // incorrect number of type parameters given for this method
12111302
E0044, // foreign items may not have type parameters
12121303
E0045, // variadic function must have C calling convention
1213-
E0057, // method has an incompatible type for trait
1214-
E0059,
1215-
E0060,
1216-
E0061,
12171304
E0068,
12181305
E0071,
12191306
E0074,

0 commit comments

Comments
 (0)