You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's quite difficult to clearly explain what's wrong, so here's a MRE:
#![allow(dead_code, unused_variables)]use std::borrow::Borrow;fnmain(){}structFoo{}fnaccepts_ref_foo(ref_foo:&Foo){// ...}/// This is the MRE.fnthis_errors(foos:implBorrow<Vec<Foo>>){let vec_of_ref_foo = foos.borrow().iter().collect::<Vec<_>>();// 1. observe that RA fails to infer the type of `vec_of_ref_foo` (should be `Vec<&Foo>`)// 2. in the following for loop, observe that RA thinks `ref_foo` is `Foo` (should be `&Foo`)// 3. run `cargo check` and `cargo build`; verify that the compiler sees no problems herefor ref_foo in vec_of_ref_foo {// this destructing statement is critical// without it, RA sees no issuesletFoo{ .. } = ref_foo;// RA thinks there's an error here where there is noneaccepts_ref_foo(ref_foo);}}/// Only `Borrow<Vec<Foo>>` is affected, not `Vec<Borrow<Foo>>`.fnthis_is_fine_0(foos:Vec<implBorrow<Foo>>){let vec_of_ref_foo = foos.iter().map(|foo| foo.borrow()).collect::<Vec<_>>();for ref_foo in vec_of_ref_foo {letFoo{ .. } = ref_foo;accepts_ref_foo(ref_foo);}}
The text was updated successfully, but these errors were encountered:
cyqsimon
changed the title
Type inference failure with std::borrow::Borrow leads to false positive errors
Type inference failure with Borrow<Vec<Foo>> leads to false positive type mismatch errors
Dec 12, 2022
I think this is effectively a duplicate of #5514. We cannot resolve vec_of_ref_foo's type correctly as you pointed out because of that.
rust-analyzer doesn't show type mismatch errors that come from unresolved types (i.e. {unknown}), but when it sees that let Foo { .. } = ref_foo statement there, it coerces the unresolved type to Foo because that's the only information it has.
I think I'll leave this issue open for now, because it showcases a situation where this limitation leads to an actual false error rather than just an annoyance.
It's quite difficult to clearly explain what's wrong, so here's a MRE:
rust-analyzer version:
0.3.1309-standalone
rustc version:
rustc 1.65.0 (897e37553 2022-11-02)
The text was updated successfully, but these errors were encountered: