internal: refactor prefer_no_std/prefer_prelude bools into a struct · rust-lang/rust@7a21dff (original) (raw)

`@@ -17,7 +17,7 @@ use crate::{

`

17

17

` nameres::DefMap,

`

18

18

` path::{ModPath, PathKind},

`

19

19

` visibility::{Visibility, VisibilityExplicitness},

`

20

``

`-

ModuleDefId, ModuleId,

`

``

20

`+

ImportPathConfig, ModuleDefId, ModuleId,

`

21

21

`};

`

22

22

``

23

23

`/// 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(

`

28

28

`from: ModuleId,

`

29

29

`prefix_kind: PrefixKind,

`

30

30

`ignore_local_imports: bool,

`

31

``

`-

prefer_no_std: bool,

`

32

``

`-

prefer_prelude: bool,

`

``

31

`+

cfg: ImportPathConfig,

`

33

32

`) -> Option {

`

34

33

`let _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)

`

46

35

`}

`

47

36

``

48

37

`#[derive(Copy, Clone, Debug)]

`

`@@ -88,8 +77,7 @@ impl PrefixKind {

`

88

77

`struct FindPathCtx<'db> {

`

89

78

`db: &'db dyn DefDatabase,

`

90

79

`prefix: PrefixKind,

`

91

``

`-

prefer_no_std: bool,

`

92

``

`-

prefer_prelude: bool,

`

``

80

`+

cfg: ImportPathConfig,

`

93

81

`ignore_local_imports: bool,

`

94

82

`}

`

95

83

``

`@@ -107,7 +95,11 @@ fn find_path_inner(ctx: FindPathCtx<'_>, item: ItemInNs, from: ModuleId) -> Opti

`

107

95

`let mut visited_modules = FxHashSet::default();

`

108

96

`return find_path_for_module(

`

109

97

`FindPathCtx {

`

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

`+

},

`

111

103

` ..ctx

`

112

104

`},

`

113

105

`&def_map,

`

`@@ -160,7 +152,11 @@ fn find_path_inner(ctx: FindPathCtx<'_>, item: ItemInNs, from: ModuleId) -> Opti

`

160

152

``

161

153

`calculate_best_path(

`

162

154

`FindPathCtx {

`

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

`+

},

`

164

160

` ..ctx

`

165

161

`},

`

166

162

`&def_map,

`

`@@ -381,9 +377,7 @@ fn calculate_best_path(

`

381

377

` path.0.push_segment(name);

`

382

378

``

383

379

`let 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),

`

387

381

`None => path,

`

388

382

`};

`

389

383

` best_path_len = new_path.0.len();

`

`@@ -425,12 +419,7 @@ fn calculate_best_path(

`

425

419

`);

`

426

420

``

427

421

`let 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),

`

434

423

`None => path_with_stab,

`

435

424

`};

`

436

425

`update_best_path(&mut best_path, new_path_with_stab);

`

`@@ -446,8 +435,7 @@ fn calculate_best_path(

`

446

435

`fn select_best_path(

`

447

436

` old_path @ (_, old_stability): (ModPath, Stability),

`

448

437

` new_path @ (_, new_stability): (ModPath, Stability),

`

449

``

`-

prefer_no_std: bool,

`

450

``

`-

prefer_prelude: bool,

`

``

438

`+

cfg: ImportPathConfig,

`

451

439

`) -> (ModPath, Stability) {

`

452

440

`match (old_stability, new_stability) {

`

453

441

`(Stable, Unstable) => return old_path,

`

`@@ -461,7 +449,7 @@ fn select_best_path(

`

461

449

`let (old_path, _) = &old;

`

462

450

`let new_has_prelude = new_path.segments().iter().any(|seg| seg == &known::prelude);

`

463

451

`let 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) {

`

465

453

`(true, false, true) | (false, true, false) => new,

`

466

454

`(true, false, false) | (false, true, true) => old,

`

467

455

`// no prelude difference in the paths, so pick the shorter one

`

`@@ -482,7 +470,7 @@ fn select_best_path(

`

482

470

``

483

471

`match (old_path.0.segments().first(), new_path.0.segments().first()) {

`

484

472

`(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 {

`

486

474

`false => |name: &Name| match name {

`

487

475

` name if name == &known::core => 0,

`

488

476

` name if name == &known::alloc => 1,

`

`@@ -647,10 +635,9 @@ mod tests {

`

647

635

`{

`

648

636

`let found_path = find_path_inner(

`

649

637

`FindPathCtx {

`

650

``

`-

prefer_no_std: false,

`

651

638

`db: &db,

`

652

639

` prefix,

`

653

``

`-

prefer_prelude,

`

``

640

`+

cfg: ImportPathConfig { prefer_no_std: false, prefer_prelude },

`

654

641

` ignore_local_imports,

`

655

642

`},

`

656

643

` resolved,

`