Skip to content

fix: avoid prelude paths when imports.preferPrelude is false#22098

Merged
ChayimFriedman2 merged 2 commits into
rust-lang:masterfrom
Souradip121:2026-04-20-avoid-prelude-path-when-prefer-prelude-false
Apr 26, 2026
Merged

fix: avoid prelude paths when imports.preferPrelude is false#22098
ChayimFriedman2 merged 2 commits into
rust-lang:masterfrom
Souradip121:2026-04-20-avoid-prelude-path-when-prefer-prelude-false

Conversation

@Souradip121
Copy link
Copy Markdown
Contributor

@Souradip121 Souradip121 commented Apr 19, 2026

What changes

  • Rename Choice::prefer_due_to_preludehas_prelude_segment; track it
    unconditionally instead of gating on the config value
  • Make Choice::try_select bidirectional: deprioritize prelude paths when
    prefer_prelude: false, prefer them when true
  • Add container_max_len helper that relaxes the path-search limit by 1 when
    the current best is a prelude path and prefer_prelude: false, so a canonical
    path one segment longer is not silently cut off before it can be considered

Before / After

// Crate exposes two paths to the same item:
//   krate::prelude::Foo          (3 segments — re-export)
//   krate::module::sub::Foo      (4 segments — canonical)
// Before  (prefer_prelude: false, the default)
use krate::prelude::Foo;        // shorter path always won
// After   (prefer_prelude: false, the default)
use krate::module::sub::Foo;   // canonical path is preferred

// After   (prefer_prelude: true)
use krate::prelude::Foo;        // prelude path preferred, unchanged

Notes

Used AI for faster development, but properly reviewed the code myself after writing it.

When prefer_prelude=false (the default), the import path algorithm now
actively deprioritizes paths that pass through a module named `prelude`,
rather than ignoring the setting entirely.

Two changes in find_path.rs:
- Rename Choice::prefer_due_to_prelude to has_prelude_segment and track
  it unconditionally (not gated on the config). Make try_select compare
  bidirectionally: prefer prelude paths when prefer_prelude=true, avoid
  them when false.
- Add container_max_len helper that relaxes the search length limit by 1
  when the current best is a prelude path and prefer_prelude=false, so
  non-prelude alternatives one segment longer are not silently cut off.

Fixes the case where krate::prelude::Foo (3 segs) was always chosen over
krate::module::sub::Foo (4 segs) regardless of the preferPrelude setting.

Generated with AI assistance (Claude).
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 19, 2026
@ChayimFriedman2
Copy link
Copy Markdown
Contributor

Note that we require disclosure of AI tools, if you used them. See Use of AI tools in CONTRIBUTING.md.

@Souradip121
Copy link
Copy Markdown
Contributor Author

@ChayimFriedman2 Yep, used AI, already disclosed in the commit message.

@ChayimFriedman2
Copy link
Copy Markdown
Contributor

The current code passing the best path so far's length as max_len is wrong, see discussion on Zulip. Instead, we should pass the current max_len minus 1, and this will also solve the need for container_max_len. You can do that in this PR or in a separate PR.

@Souradip121
Copy link
Copy Markdown
Contributor Author

@ChayimFriedman2 doing in this PR only.

Souradip121 added a commit to Souradip121/rust-analyzer that referenced this pull request Apr 26, 2026
Per review on PR rust-lang#22098: the original code passing the current best path's
length as the container search ceiling was a flawed local optimization. The
correct ceiling is the recursive budget the caller passed down, minus the
one segment we'll push (the item name) on top.

Replacing both call sites with `max_len - 1` removes the broken pruning
(which also originally caused the prelude-avoidance bug) and makes the
container_max_len helper unnecessary. The bidirectional prelude comparison
in Choice::try_select still picks the better-ranked path among everything
the budget allows.

Generated with AI assistance (Claude).
@rustbot

This comment has been minimized.

Per review feedback: the previous code passing the current best path's
length as the container search ceiling was a flawed local optimization.
The correct ceiling is the recursive budget the caller passed down, minus
the one segment we'll push (the item name) on top.

Replacing both call sites with `max_len - 1` removes the broken pruning
(which also originally caused the prelude-avoidance bug) and makes the
container_max_len helper unnecessary. The bidirectional prelude comparison
in Choice::try_select still picks the better-ranked path among everything
the budget allows.

Generated with AI assistance (Claude).
@Souradip121 Souradip121 force-pushed the 2026-04-20-avoid-prelude-path-when-prefer-prelude-false branch from f38e78f to ef72f16 Compare April 26, 2026 16:24
@Souradip121
Copy link
Copy Markdown
Contributor Author

@ChayimFriedman2 check once!

Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Apr 26, 2026
Merged via the queue into rust-lang:master with commit 3aaa35b Apr 26, 2026
18 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 26, 2026
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.

Option for import path when re-exports exist — preferences/excludes

3 participants