Skip to content

Improve E0277 error message in a generic context #32641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,47 @@ fn main() {
some_func(5i32); // ok!
}
```

Or in a generic context, an erroneous code example would look like:
```compile_fail
fn some_func<T>(foo: T) {
println!("{:?}", foo); // error: the trait `core::fmt::Debug` is not
// implemented for the type `T`
}

fn main() {
// We now call the method with the i32 type,
// which *does* implement the Debug trait.
some_func(5i32);
}
```

Note that the error here is in the definition of the generic function: Although
we only call it with a parameter that does implement `Debug`, the compiler
still rejects the function: It must work with all possible input types. In
order to make this example compile, we need to restrict the generic type we're
accepting:
```
use std::fmt;

// Restrict the input type to types that implement Debug.
fn some_func<T: fmt::Debug>(foo: T) {
println!("{:?}", foo);
}

fn main() {
// Calling the method is still fine, as i32 implements Debug.
some_func(5i32);

// This would fail to compile now:
// struct WithoutDebug;
// some_func(WithoutDebug);
}

Rust only looks at the signature of the called function, as such it must
already specify all requirements that will be used for every type parameter.
```

"##,

E0281: r##"
Expand Down