internal: refactor `prefer_no_std`/`prefer_prelude` bools into a struct · rust-lang/rust@7a21dff
@@ -17,7 +17,7 @@ use crate::{
1717 nameres::DefMap,
1818 path::{ModPath, PathKind},
1919 visibility::{Visibility, VisibilityExplicitness},
20-ModuleDefId, ModuleId,
20+ImportPathConfig, ModuleDefId, ModuleId,
2121};
22222323/// Find a path that can be used to refer to a certain item. This can depend on
@@ -28,21 +28,10 @@ pub fn find_path(
2828from: ModuleId,
2929prefix_kind: PrefixKind,
3030ignore_local_imports: bool,
31-prefer_no_std: bool,
32-prefer_prelude: bool,
31+cfg: ImportPathConfig,
3332) -> Option<ModPath> {
3433let _p = tracing::span!(tracing::Level::INFO, "find_path").entered();
35-find_path_inner(
36-FindPathCtx {
37- db,
38-prefix: prefix_kind,
39- prefer_no_std,
40- prefer_prelude,
41- ignore_local_imports,
42-},
43- item,
44- from,
45-)
34+find_path_inner(FindPathCtx { db, prefix: prefix_kind, cfg, ignore_local_imports }, item, from)
4635}
47364837#[derive(Copy, Clone, Debug)]
@@ -88,8 +77,7 @@ impl PrefixKind {
8877struct FindPathCtx<'db> {
8978db: &'db dyn DefDatabase,
9079prefix: PrefixKind,
91-prefer_no_std: bool,
92-prefer_prelude: bool,
80+cfg: ImportPathConfig,
9381ignore_local_imports: bool,
9482}
9583@@ -107,7 +95,11 @@ fn find_path_inner(ctx: FindPathCtx<'_>, item: ItemInNs, from: ModuleId) -> Opti
10795let mut visited_modules = FxHashSet::default();
10896return find_path_for_module(
10997FindPathCtx {
110-prefer_no_std: ctx.prefer_no_std || ctx.db.crate_supports_no_std(crate_root.krate),
98+cfg: ImportPathConfig {
99+prefer_no_std: ctx.cfg.prefer_no_std
100+ || ctx.db.crate_supports_no_std(crate_root.krate),
101+ ..ctx.cfg
102+},
111103 ..ctx
112104},
113105&def_map,
@@ -160,7 +152,11 @@ fn find_path_inner(ctx: FindPathCtx<'_>, item: ItemInNs, from: ModuleId) -> Opti
160152161153calculate_best_path(
162154FindPathCtx {
163-prefer_no_std: ctx.prefer_no_std || ctx.db.crate_supports_no_std(crate_root.krate),
155+cfg: ImportPathConfig {
156+prefer_no_std: ctx.cfg.prefer_no_std
157+ || ctx.db.crate_supports_no_std(crate_root.krate),
158+ ..ctx.cfg
159+},
164160 ..ctx
165161},
166162&def_map,
@@ -381,9 +377,7 @@ fn calculate_best_path(
381377 path.0.push_segment(name);
382378383379let new_path = match best_path.take() {
384-Some(best_path) => {
385-select_best_path(best_path, path, ctx.prefer_no_std, ctx.prefer_prelude)
386-}
380+Some(best_path) => select_best_path(best_path, path, ctx.cfg),
387381None => path,
388382};
389383 best_path_len = new_path.0.len();
@@ -425,12 +419,7 @@ fn calculate_best_path(
425419);
426420427421let new_path_with_stab = match best_path.take() {
428-Some(best_path) => select_best_path(
429- best_path,
430- path_with_stab,
431- ctx.prefer_no_std,
432- ctx.prefer_prelude,
433-),
422+Some(best_path) => select_best_path(best_path, path_with_stab, ctx.cfg),
434423None => path_with_stab,
435424};
436425update_best_path(&mut best_path, new_path_with_stab);
@@ -446,8 +435,7 @@ fn calculate_best_path(
446435fn select_best_path(
447436 old_path @ (_, old_stability): (ModPath, Stability),
448437 new_path @ (_, new_stability): (ModPath, Stability),
449-prefer_no_std: bool,
450-prefer_prelude: bool,
438+cfg: ImportPathConfig,
451439) -> (ModPath, Stability) {
452440match (old_stability, new_stability) {
453441(Stable, Unstable) => return old_path,
@@ -461,7 +449,7 @@ fn select_best_path(
461449let (old_path, _) = &old;
462450let new_has_prelude = new_path.segments().iter().any(|seg| seg == &known::prelude);
463451let old_has_prelude = old_path.segments().iter().any(|seg| seg == &known::prelude);
464-match (new_has_prelude, old_has_prelude, prefer_prelude) {
452+match (new_has_prelude, old_has_prelude, cfg.prefer_prelude) {
465453(true, false, true) | (false, true, false) => new,
466454(true, false, false) | (false, true, true) => old,
467455// no prelude difference in the paths, so pick the shorter one
@@ -482,7 +470,7 @@ fn select_best_path(
482470483471match (old_path.0.segments().first(), new_path.0.segments().first()) {
484472(Some(old), Some(new)) if STD_CRATES.contains(old) && STD_CRATES.contains(new) => {
485-let rank = match prefer_no_std {
473+let rank = match cfg.prefer_no_std {
486474false => |name: &Name| match name {
487475 name if name == &known::core => 0,
488476 name if name == &known::alloc => 1,
@@ -647,10 +635,9 @@ mod tests {
647635{
648636let found_path = find_path_inner(
649637FindPathCtx {
650-prefer_no_std: false,
651638db: &db,
652639 prefix,
653- prefer_prelude,
640+cfg: ImportPathConfig { prefer_no_std: false, prefer_prelude },
654641 ignore_local_imports,
655642},
656643 resolved,