Skip to content

fix: Exclude impls with errors from impl enumeration#22619

Open
Wilfred wants to merge 1 commit into
rust-lang:masterfrom
Wilfred:error_type_receivers
Open

fix: Exclude impls with errors from impl enumeration#22619
Wilfred wants to merge 1 commit into
rust-lang:masterfrom
Wilfred:error_type_receivers

Conversation

@Wilfred

@Wilfred Wilfred commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

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.

use std::io::Read;
fn f<T>(mut o: Option<T>) {
    o.take().unwrap();
}

rust-analyzer was incorrectly resolving the .take() to Read::take() instead of Option::take().

Inside tokio, it uses mockall:mock! to define 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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 19, 2026
@Veykril

Veykril commented Jun 19, 2026

Copy link
Copy Markdown
Member

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

@Wilfred

Wilfred commented Jun 22, 2026

Copy link
Copy Markdown
Contributor Author

Yeah, this is the curse of a monorepo: we don't really know which libraries you're actively working on, so I defensively set cfg(test) essentially everywhere so you can always go-to-def on test helpers. I noticed this when using rust-project as a discover command.

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 cfg(test) is genuinely wrong there.

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.

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.

    use std::io::Read;
    fn f<T>(mut o: Option<T>) {
        o.take().unwrap();
    }

rust-analyzer was incorrectly resolving the `.take()` to `Read::take()`
instead of `Option::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
@Wilfred Wilfred force-pushed the error_type_receivers branch from 46ed697 to 4b20283 Compare June 22, 2026 16:35
@rustbot

rustbot commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

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.

meta-codesync Bot pushed a commit to facebook/buck2 that referenced this pull request Jun 23, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants