-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
"Type annotations needed" error is shown in a misleading wrong location #72690
Comments
It is actually that the first statement has type error, but somehow the error is reported to the second statement. The first statement alone reports the error correctly fn main() {
vec!["hello"]
.iter_mut()
.map(|x| String::from(x.as_ref()))
.collect::<Vec<String>>();
//vec![String::from("First"), String::from("Second")]
// .iter()
// .find(|s| *s == "Second");
}
and by fixing the error, the following compiles fine fn main() {
vec!["hello"]
.iter_mut()
.map(|x| String::from(*x))
.collect::<Vec<String>>();
vec![String::from("First"), String::from("Second")]
.iter()
.find(|s| *s == "Second");
} |
This happens on Nightly and on Stable. |
Labeling as this was a regression introduced by 1.41.0. |
Assigning |
Problem seems to be that we always assume the last closure was the source of the inference failure rust/src/librustc_infer/infer/error_reporting/need_type_info.rs Lines 109 to 129 in 56daaf6
|
Just sticking a return after
|
Definitely not the case - looking into some alternative fixes. @rustbot claim |
Ok this is even more subtle than I first thought. There's just far too much ambiguity here for this diagnostic code to produce a sensible suggestion. Any time a sub-expression/local pattern/argument pattern/closure's type contains the inference target it immediately becomes a candidate for suggesting on. In this case though, the inference target is |
fn main() {
vec!["hello"]
.iter_mut()
.map(|x| String::from(x.as_ref()))
.collect::<Vec<String>>();
format!("Hello");
} Compiling the above results in this - clearly the
|
I have a similar message to:
But what I don't understand is: why does it complain it can't infer the type if it clearly knows the type? |
This compiles just fine:
But this gives a compiler error:
It seems really weird that the second statement is affected like this?
Meta
Empty project (created with
cargo new
), unmodified Cargo.toml (so no extra dependencies):This issue has been assigned to @doctorn via this comment.
The text was updated successfully, but these errors were encountered: