fix: Do not use PostAnalysis TypingMode for IDE method resolution#21750
Conversation
As explained in the comments, PostAnalysis is good for most IDE things but not method resolution. This fixes a bug which should not be impacted by this at all - return position impl trait in trait. It is currently lowered to an opaque, while it should be lowered to an anonymous associated type. But today when it is lowered as an opaque, this opaque of course has no definition and will normalize to an error, preventing method resolution on it from succeeding in some cases.
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in IDE method resolution related to Return Position impl Trait in Traits (RPITIT). Previously, with_method_resolution always built an InferCtxt using TypingMode::PostAnalysis, which would attempt to reveal opaque types. However, RPITIT opaques currently lack a proper definition (they're lowered as opaque rather than as anonymous associated types), causing them to normalize to errors and preventing valid method suggestions.
Changes:
- In
with_method_resolution, replace the hardcodedTypingMode::PostAnalysiswith a context-appropriate typing mode:TypingMode::analysis_in_bodywhen inside a function body, andTypingMode::non_body_analysis()otherwise. - Add a regression test
rpitit_with_referenceverifying that method completion works correctly on&impl Fooreturned from a trait method.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
crates/hir/src/lib.rs |
Replace TypingMode::PostAnalysis with a body-aware typing mode selection in with_method_resolution |
crates/ide-completion/src/tests/expression.rs |
Add regression test for RPITIT method completions via reference |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Sorry for the Copilot review, I was curious to see how much it will understand from the jargon. Looks like it understand nothing and just repeats what I said, as expected 😂 |
As explained in the comments, PostAnalysis is good for most IDE things but not method resolution.
This fixes a bug which should not be impacted by this at all - return position impl trait in trait. It is currently lowered to an opaque, while it should be lowered to an anonymous associated type. But today when it is lowered as an opaque, this opaque of course has no definition and will normalize to an error, preventing method resolution on it from succeeding in some cases.
Fixes #21749.