Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7a21dff

Browse files
davidbarskyVeykril
authored andcommitted
internal: refactorprefer_no_std/prefer_prelude bools into a struct
1 parentad810a5 commit7a21dff

File tree

37 files changed

+304
-351
lines changed

37 files changed

+304
-351
lines changed

‎src/tools/rust-analyzer/crates/hir-def/src/find_path.rs‎

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};
2222

2323
/// 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(
2828
from:ModuleId,
2929
prefix_kind:PrefixKind,
3030
ignore_local_imports:bool,
31-
prefer_no_std:bool,
32-
prefer_prelude:bool,
31+
cfg:ImportPathConfig,
3332
) ->Option<ModPath>{
3433
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)
4635
}
4736

4837
#[derive(Copy,Clone,Debug)]
@@ -88,8 +77,7 @@ impl PrefixKind {
8877
structFindPathCtx<'db>{
8978
db:&'dbdynDefDatabase,
9079
prefix:PrefixKind,
91-
prefer_no_std:bool,
92-
prefer_prelude:bool,
80+
cfg:ImportPathConfig,
9381
ignore_local_imports:bool,
9482
}
9583

@@ -107,7 +95,11 @@ fn find_path_inner(ctx: FindPathCtx<'_>, item: ItemInNs, from: ModuleId) -> Opti
10795
letmut visited_modules =FxHashSet::default();
10896
returnfind_path_for_module(
10997
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+
},
111103
..ctx
112104
},
113105
&def_map,
@@ -160,7 +152,11 @@ fn find_path_inner(ctx: FindPathCtx<'_>, item: ItemInNs, from: ModuleId) -> Opti
160152

161153
calculate_best_path(
162154
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+
},
164160
..ctx
165161
},
166162
&def_map,
@@ -381,9 +377,7 @@ fn calculate_best_path(
381377
path.0.push_segment(name);
382378

383379
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),
387381
None => path,
388382
};
389383
best_path_len = new_path.0.len();
@@ -425,12 +419,7 @@ fn calculate_best_path(
425419
);
426420

427421
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),
434423
None => path_with_stab,
435424
};
436425
update_best_path(&mut best_path, new_path_with_stab);
@@ -446,8 +435,7 @@ fn calculate_best_path(
446435
fnselect_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){
452440
match(old_stability, new_stability){
453441
(Stable,Unstable) =>return old_path,
@@ -461,7 +449,7 @@ fn select_best_path(
461449
let(old_path, _) =&old;
462450
let new_has_prelude = new_path.segments().iter().any(|seg| seg ==&known::prelude);
463451
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){
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(
482470

483471
match(old_path.0.segments().first(), new_path.0.segments().first()){
484472
(Some(old),Some(new))ifSTD_CRATES.contains(old) &&STD_CRATES.contains(new) =>{
485-
let rank =match prefer_no_std{
473+
let rank =matchcfg.prefer_no_std{
486474
false => |name:&Name|match name{
487475
nameif name ==&known::core =>0,
488476
nameif name ==&known::alloc =>1,
@@ -647,10 +635,9 @@ mod tests {
647635
{
648636
let found_path =find_path_inner(
649637
FindPathCtx{
650-
prefer_no_std:false,
651638
db:&db,
652639
prefix,
653-
prefer_prelude,
640+
cfg:ImportPathConfig{prefer_no_std:false,prefer_prelude},
654641
ignore_local_imports,
655642
},
656643
resolved,

‎src/tools/rust-analyzer/crates/hir-def/src/lib.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ use crate::{
108108

109109
typeFxIndexMap<K,V> =
110110
indexmap::IndexMap<K,V, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
111+
/// A wrapper around two booleans, [`ImportPathConfig::prefer_no_std`] and [`ImportPathConfig::prefer_prelude`].
112+
#[derive(Debug,Clone,PartialEq,Eq,Hash,Copy)]
113+
pubstructImportPathConfig{
114+
/// If true, prefer to unconditionally use imports of the `core` and `alloc` crate
115+
/// over the std.
116+
pubprefer_no_std:bool,
117+
/// If true, prefer import paths containing a prelude module.
118+
pubprefer_prelude:bool,
119+
}
111120

112121
#[derive(Debug)]
113122
pubstructItemLoc<N:ItemTreeNode>{

‎src/tools/rust-analyzer/crates/hir-ty/src/display.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use hir_def::{
2121
path::{Path,PathKind},
2222
type_ref::{TraitBoundModifier,TypeBound,TypeRef},
2323
visibility::Visibility,
24-
HasModule,ItemContainerId,LocalFieldId,Lookup,ModuleDefId,ModuleId,TraitId,
24+
HasModule,ImportPathConfig,ItemContainerId,LocalFieldId,Lookup,ModuleDefId,ModuleId,
25+
TraitId,
2526
};
2627
use hir_expand::name::Name;
2728
use intern::{Internable,Interned};
@@ -1000,9 +1001,8 @@ impl HirDisplay for Ty {
10001001
ItemInNs::Types((*def_id).into()),
10011002
module_id,
10021003
PrefixKind::Plain,
1003-
false,
1004-
false,
10051004
true,
1005+
ImportPathConfig{prefer_no_std:false,prefer_prelude:true},
10061006
){
10071007
write!(f,"{}", path.display(f.db.upcast()))?;
10081008
}else{

‎src/tools/rust-analyzer/crates/hir/src/lib.rs‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub use {
122122
per_ns::Namespace,
123123
type_ref::{Mutability,TypeRef},
124124
visibility::Visibility,
125+
ImportPathConfig,
125126
// FIXME: This is here since some queries take it as input that are used
126127
// outside of hir.
127128
{AdtId,MacroId,ModuleDefId},
@@ -792,17 +793,15 @@ impl Module {
792793
self,
793794
db:&dynDefDatabase,
794795
item:implInto<ItemInNs>,
795-
prefer_no_std:bool,
796-
prefer_prelude:bool,
796+
cfg:ImportPathConfig,
797797
) ->Option<ModPath>{
798798
hir_def::find_path::find_path(
799799
db,
800800
item.into().into(),
801801
self.into(),
802802
PrefixKind::Plain,
803803
false,
804-
prefer_no_std,
805-
prefer_prelude,
804+
cfg,
806805
)
807806
}
808807

@@ -813,18 +812,9 @@ impl Module {
813812
db:&dynDefDatabase,
814813
item:implInto<ItemInNs>,
815814
prefix_kind:PrefixKind,
816-
prefer_no_std:bool,
817-
prefer_prelude:bool,
815+
cfg:ImportPathConfig,
818816
) ->Option<ModPath>{
819-
hir_def::find_path::find_path(
820-
db,
821-
item.into().into(),
822-
self.into(),
823-
prefix_kind,
824-
true,
825-
prefer_no_std,
826-
prefer_prelude,
827-
)
817+
hir_def::find_path::find_path(db, item.into().into(),self.into(), prefix_kind,true, cfg)
828818
}
829819
}
830820

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp