Skip to content

fix: Exclude impls on the error type from impl enumeration#22619

Merged
ChayimFriedman2 merged 1 commit into
rust-lang:masterfrom
Wilfred:error_type_receivers
Jun 30, 2026
Merged

fix: Exclude impls on the error type from impl enumeration#22619
ChayimFriedman2 merged 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.

@Wilfred Wilfred force-pushed the error_type_receivers branch from 46ed697 to 4b20283 Compare June 22, 2026 16:35
@rustbot

This comment has been minimized.

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

@ChayimFriedman2 ChayimFriedman2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

View changes since this review

@Wilfred Wilfred force-pushed the error_type_receivers branch from 4b20283 to f1d3f38 Compare June 29, 2026 10:29
@rustbot

rustbot commented Jun 29, 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.

@Wilfred

Wilfred commented Jun 29, 2026

Copy link
Copy Markdown
Contributor Author

@ChayimFriedman2 how about this, using references_only_ty_error instead? That seems to be a narrower condition.

I did play with if matches!(trait_ref.skip_binder().self_ty().kind(), TyKind::Error(_)) but that doesn't work with my original repro, since the impl is on &NoSuchType.

@Wilfred Wilfred force-pushed the error_type_receivers branch from f1d3f38 to 6f4c10f Compare June 29, 2026 11:31
@ChayimFriedman2

Copy link
Copy Markdown
Contributor

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.

@Wilfred Wilfred force-pushed the error_type_receivers branch from 6f4c10f to ab02708 Compare June 29, 2026 14:41
@Wilfred

Wilfred commented Jun 29, 2026

Copy link
Copy Markdown
Contributor Author

That makes sense to me: the only really problematic case is instances of impl Foo for SomeMissingType leaking everywhere. I've updated the PR and things like impl Foo for Option<SomeMissingType> seem to work correctly.

@Wilfred Wilfred changed the title fix: Exclude impls with errors from impl enumeration fix: Exclude impls on the error type from impl enumeration Jun 29, 2026
Comment thread crates/hir-ty/src/next_solver/interner.rs Outdated
@Wilfred Wilfred force-pushed the error_type_receivers branch from ab02708 to 54bd333 Compare June 30, 2026 11:22
@Wilfred

Wilfred commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

Something like this? I've added a references_non_lt_error() call and also tweaked the helper to be trait specific, I think it reads better.

Comment thread crates/hir-ty/src/next_solver/interner.rs Outdated
Comment thread crates/test-utils/src/minicore.rs Outdated
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
@Wilfred Wilfred force-pushed the error_type_receivers branch from 54bd333 to 8667ca5 Compare June 30, 2026 13:00
@Wilfred

Wilfred commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

OK, moved the logic, and done another pass on making the test small and more self-documenting.

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Jun 30, 2026
Merged via the queue into rust-lang:master with commit 5300ee2 Jun 30, 2026
18 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 30, 2026
@Wilfred Wilfred deleted the error_type_receivers branch July 3, 2026 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants