Skip to content

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

Open
Souradip121 wants to merge 1 commit intorust-lang:masterfrom
Souradip121:2026-04-20-avoid-prelude-path-when-prefer-prelude-false
Open

fix: avoid prelude paths when imports.preferPrelude is false#22098
Souradip121 wants to merge 1 commit intorust-lang:masterfrom
Souradip121:2026-04-20-avoid-prelude-path-when-prefer-prelude-false

Conversation

@Souradip121
Copy link
Copy Markdown

@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
Author

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

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.

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

3 participants