fix: Exclude impls on the error type from impl enumeration#22619
Conversation
|
Not opposed to this, it sounds like the right thing to do here, but why is cfg(test) set for you in tokio which I presume is a dependency of your workspace? That asks for weird issues |
|
Yeah, this is the curse of a monorepo: we don't really know which libraries you're actively working on, so I defensively set It looks like I should modify rust-project to exclude cfg(test) on imported libraries like tokio, since our monorepo (using reindeer) doesn't actually have dev-dependencies for imported libraries so The bug was a spooky action-at-a-distance issue: adding tokio to the transitive set of dependencies causes spurious red squiggles and it took me a while to get to the bottom of it. This fix does feel appropriately defensive. I see CI is red, I'll fix that too. |
46ed697 to
4b20283
Compare
This comment has been minimized.
This comment has been minimized.
Summary: We should only set `cfg(test)` when we actually have the test dependencies, and reindeer-imported crates don't have their test-only dev-dependencies. If we set `cfg(test)` without dependencies, rust-analyzer tries to run type inference without having all the types, which can lead to issues like [this upstream issue](rust-lang/rust-analyzer#22619). Instead, only set `cfg(test)` on first party code. Reviewed By: michel-slm Differential Revision: D109321780 fbshipit-source-id: 9399e597061539d9006e8c70c4a57f0fbc4a0041
There was a problem hiding this comment.
Generally that's something that came up multiple times in the past. In each of them we ended up finding the problematic impl and resolving its issue, but I agree this is something we want in general as the solver is quite sensitive to such impls.
However I think references_non_lt_error() is too strong condition, checking for error type itself should be enough IMO.
4b20283 to
f1d3f38
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@ChayimFriedman2 how about this, using I did play with |
f1d3f38 to
6f4c10f
Compare
|
I'm a bit on the edge about this. On one hand, const errors are indeed more commonly used by bugs in r-a than type errors. On the other hand... removing the impl due to any type error feels a bit like going too far. Maybe, and given that this is the most problematic case, only filter it if it has an error constructor? That is, peeling references, raw pointers(?), arrays(?), slices(?), pattern types, and unsafe binders. But I'm not sure myself. |
6f4c10f to
ab02708
Compare
|
That makes sense to me: the only really problematic case is instances of |
ab02708 to
54bd333
Compare
|
Something like this? I've added a |
Previously, rust-analyzer would include impl methods from `impl Foo
for NoSuchType`, which would lead to rust-analyzer reporting
nonexistent type errors.
This is noticeable when depending on tokio with feature="fs" and cfg(test)
enabled. The following code would produce a type error, because
Read::take() requires an argument and rust-analyzer was including Read
on Option via an error type.
use std::io::Read;
fn f<T>(mut o: Option<T>) {
o.take();
}
Inside tokio, it uses mockall:mock! to create a MockFile, and then
does `impl Read for &'_ MockFile`. However, since `mockall` is a dev
dependency of tokio and we don't include dev dependencies of
dependencies, rust-analyzer just sees `impl Read for &'_ NoSuchType`.
Instead, exclude traits whose self type is error.
AI disclosure: Partially written by Codex and GPT-5.5
54bd333 to
8667ca5
Compare
|
OK, moved the logic, and done another pass on making the test small and more self-documenting. |
Previously, rust-analyzer would include impl methods from
impl Foo for NoSuchType, which would lead to rust-analyzer reporting nonexistent type errors.This is noticeable when depending on tokio with feature="fs" and cfg(test) enabled. The following code would produce an invalid type error.
rust-analyzer was incorrectly resolving the
.take()toRead::take()instead ofOption::take().Inside tokio, it uses
mockall:mock!to define aMockFile, and then doesimpl Read for &'_ MockFile. However, sincemockallis a dev dependency of tokio and we don't include dev dependencies of dependencies, rust-analyzer just seesimpl Read for &'_ NoSuchType.Instead, exclude traits whose self type is error.
AI disclosure: Partially written by Codex and GPT-5.5