-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Spurious E0282 'type annotations needed' when other compile errors exist #88630
Comments
I've just learned from https://users.rust-lang.org/t/confusing-type-inference-error-with-json-macro/75522 that this same kind of problem, with the same message “type annotations needed”, can also appear with error code E0283. Mentioning that now for searchability. See also issues #75331, #68507 for other cases of erroneous “type annotations needed”, though in those cases it isn't action-at-a-distance. |
At the rustc source level, this is likely related to the use of either |
I spoke to @compiler-errors about this a little bit yesterday and we figured out what is up with this. Unfortunately its not a really cool type system issue :( First thing is that this doesn't reproduce locally if you just make a new crate. It's important to have As far as I know you don't get to use any impls from your dependencies if you havent actually used any of the crate's API anywhere. extern crate serde_json;
fn innocent_bystander() {
// Approximately what assert_eq! does, inlined for the sake of simplicity
if "x".as_bytes() != &[] {
panic!();
}
} in 2015 edition will unconditionally fail to compile with the inference error, wheras removing the What's happening is that for impl PartialEq<Foo> for u8 {
fn eq(&self, other: &Foo) -> bool {
todo!()
}
}
struct Foo;
fn innocent_bystander() {
"x".as_bytes() != &[];
}
fn main() {} The reason that the The minimal repro for this issue would be the following: // some library crate `foo`
// lib.rs
impl PartialEq<Foo> for u8 {
fn eq(&self, other: &Foo) -> bool {
todo!()
}
}
pub struct Foo;
// our binary crate with a `Cargo.toml` dep on `foo`
// main.rs
fn evil() {
does_not_exist(); // comment and uncomment this to trigger the inference error
}
fn innocent_bystander() {
"x".as_bytes() != &[];
}
fn main() {}
|
Given the following code:
The current output is:
However, if
does_not_exist();
is commented out, not only does its error E0425 disappear, the type inference error E0282 also does. This results in a frustrating situation where, whenever you have an error of the first sort, any==
s orassert_eq!()
s elsewhere in your crate that are fragile in this way also flood the error output. This makes it unnecessarily difficult to practice the workflow of “change something in a breaking way, then fix all the now-erroneous call sites”. It could also mislead beginners because the compiler is confidently stating a falsehood.Ideally, these “type annotations needed” errors would not appear unless the annotations were actually needed. An approximation of this would be to not show them when any other errors appear. An even better approximation, I speculate in ignorance of the compiler internals, would be to not show them if whatever inference pass would have resolved the concrete type is known to have been skipped due to previous errors.
Or, perhaps this code is inherently fraught, and it would be wise for the programmer to actually provide a type annotation. In that case, it would be nice if there was a warning about that when the code compiles successfully.
Tested on rustc 1.54.0 stable, also current nightly.
I previously mentioned this problem in #44345, but got no response there, and I also now think that this situation is significantly different. In particular, no trait implementations are being added, and the error is entirely spurious rather than being a newly-ambiguous trait implementation selection.
The text was updated successfully, but these errors were encountered: