Skip to content

Commit 352a5b8

Browse files
committed
Auto merge of rust-lang#13212 - Veykril:no-std-config, r=Veykril
Add config to unconditionally prefer core imports over std Fixes rust-lang/rust-analyzer#12979
2 parents bc13142 + 7d19971 commit 352a5b8

33 files changed

+156
-43
lines changed

crates/hir-def/src/find_path.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,25 @@ use crate::{
1616

1717
/// Find a path that can be used to refer to a certain item. This can depend on
1818
/// *from where* you're referring to the item, hence the `from` parameter.
19-
pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
19+
pub fn find_path(
20+
db: &dyn DefDatabase,
21+
item: ItemInNs,
22+
from: ModuleId,
23+
prefer_core: bool,
24+
) -> Option<ModPath> {
2025
let _p = profile::span("find_path");
21-
find_path_inner(db, item, from, None)
26+
find_path_inner(db, item, from, None, prefer_core)
2227
}
2328

2429
pub fn find_path_prefixed(
2530
db: &dyn DefDatabase,
2631
item: ItemInNs,
2732
from: ModuleId,
2833
prefix_kind: PrefixKind,
34+
prefer_core: bool,
2935
) -> Option<ModPath> {
3036
let _p = profile::span("find_path_prefixed");
31-
find_path_inner(db, item, from, Some(prefix_kind))
37+
find_path_inner(db, item, from, Some(prefix_kind), prefer_core)
3238
}
3339

3440
const MAX_PATH_LEN: usize = 15;
@@ -100,12 +106,22 @@ fn find_path_inner(
100106
item: ItemInNs,
101107
from: ModuleId,
102108
prefixed: Option<PrefixKind>,
109+
prefer_core: bool,
103110
) -> Option<ModPath> {
104111
// FIXME: Do fast path for std/core libs?
105112

106113
let mut visited_modules = FxHashSet::default();
107114
let def_map = from.def_map(db);
108-
find_path_inner_(db, &def_map, from, item, MAX_PATH_LEN, prefixed, &mut visited_modules)
115+
find_path_inner_(
116+
db,
117+
&def_map,
118+
from,
119+
item,
120+
MAX_PATH_LEN,
121+
prefixed,
122+
&mut visited_modules,
123+
prefer_core,
124+
)
109125
}
110126

111127
fn find_path_inner_(
@@ -116,6 +132,7 @@ fn find_path_inner_(
116132
max_len: usize,
117133
mut prefixed: Option<PrefixKind>,
118134
visited_modules: &mut FxHashSet<ModuleId>,
135+
prefer_core: bool,
119136
) -> Option<ModPath> {
120137
if max_len == 0 {
121138
return None;
@@ -191,7 +208,9 @@ fn find_path_inner_(
191208
// Recursive case:
192209
// - if the item is an enum variant, refer to it via the enum
193210
if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() {
194-
if let Some(mut path) = find_path(db, ItemInNs::Types(variant.parent.into()), from) {
211+
if let Some(mut path) =
212+
find_path(db, ItemInNs::Types(variant.parent.into()), from, prefer_core)
213+
{
195214
let data = db.enum_data(variant.parent);
196215
path.push_segment(data.variants[variant.local_id].name.clone());
197216
return Some(path);
@@ -202,7 +221,7 @@ fn find_path_inner_(
202221
}
203222

204223
// - otherwise, look for modules containing (reexporting) it and import it from one of those
205-
let prefer_no_std = db.crate_supports_no_std(crate_root.krate);
224+
let prefer_no_std = prefer_core || db.crate_supports_no_std(crate_root.krate);
206225
let mut best_path = None;
207226
let mut best_path_len = max_len;
208227

@@ -223,6 +242,7 @@ fn find_path_inner_(
223242
best_path_len - 1,
224243
prefixed,
225244
visited_modules,
245+
prefer_core,
226246
) {
227247
path.push_segment(name);
228248

@@ -253,6 +273,7 @@ fn find_path_inner_(
253273
best_path_len - 1,
254274
prefixed,
255275
visited_modules,
276+
prefer_core,
256277
)?;
257278
cov_mark::hit!(partially_imported);
258279
path.push_segment(info.path.segments.last()?.clone());
@@ -428,7 +449,8 @@ mod tests {
428449
.take_types()
429450
.unwrap();
430451

431-
let found_path = find_path_inner(&db, ItemInNs::Types(resolved), module, prefix_kind);
452+
let found_path =
453+
find_path_inner(&db, ItemInNs::Types(resolved), module, prefix_kind, false);
432454
assert_eq!(found_path, Some(mod_path), "{:?}", prefix_kind);
433455
}
434456

crates/hir-ty/src/display.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ impl HirDisplay for Ty {
533533
f.db.upcast(),
534534
ItemInNs::Types((*def_id).into()),
535535
module_id,
536+
false,
536537
) {
537538
write!(f, "{}", path)?;
538539
} else {

crates/hir/src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,13 @@ impl Module {
582582

583583
/// Finds a path that can be used to refer to the given item from within
584584
/// this module, if possible.
585-
pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into<ItemInNs>) -> Option<ModPath> {
586-
hir_def::find_path::find_path(db, item.into().into(), self.into())
585+
pub fn find_use_path(
586+
self,
587+
db: &dyn DefDatabase,
588+
item: impl Into<ItemInNs>,
589+
prefer_core: bool,
590+
) -> Option<ModPath> {
591+
hir_def::find_path::find_path(db, item.into().into(), self.into(), prefer_core)
587592
}
588593

589594
/// Finds a path that can be used to refer to the given item from within
@@ -593,8 +598,15 @@ impl Module {
593598
db: &dyn DefDatabase,
594599
item: impl Into<ItemInNs>,
595600
prefix_kind: PrefixKind,
601+
prefer_core: bool,
596602
) -> Option<ModPath> {
597-
hir_def::find_path::find_path_prefixed(db, item.into().into(), self.into(), prefix_kind)
603+
hir_def::find_path::find_path_prefixed(
604+
db,
605+
item.into().into(),
606+
self.into(),
607+
prefix_kind,
608+
prefer_core,
609+
)
598610
}
599611
}
600612

crates/ide-assists/src/assist_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ pub struct AssistConfig {
1313
pub snippet_cap: Option<SnippetCap>,
1414
pub allowed: Option<Vec<AssistKind>>,
1515
pub insert_use: InsertUseConfig,
16+
pub prefer_core: bool,
1617
}

crates/ide-assists/src/handlers/add_missing_match_arms.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
8787
.into_iter()
8888
.filter_map(|variant| {
8989
Some((
90-
build_pat(ctx.db(), module, variant)?,
90+
build_pat(ctx.db(), module, variant, ctx.config.prefer_core)?,
9191
variant.should_be_hidden(ctx.db(), module.krate()),
9292
))
9393
})
@@ -132,8 +132,9 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
132132
let is_hidden = variants
133133
.iter()
134134
.any(|variant| variant.should_be_hidden(ctx.db(), module.krate()));
135-
let patterns =
136-
variants.into_iter().filter_map(|variant| build_pat(ctx.db(), module, variant));
135+
let patterns = variants.into_iter().filter_map(|variant| {
136+
build_pat(ctx.db(), module, variant, ctx.config.prefer_core)
137+
});
137138

138139
(ast::Pat::from(make::tuple_pat(patterns)), is_hidden)
139140
})
@@ -349,10 +350,16 @@ fn resolve_tuple_of_enum_def(
349350
.collect()
350351
}
351352

352-
fn build_pat(db: &RootDatabase, module: hir::Module, var: ExtendedVariant) -> Option<ast::Pat> {
353+
fn build_pat(
354+
db: &RootDatabase,
355+
module: hir::Module,
356+
var: ExtendedVariant,
357+
prefer_core: bool,
358+
) -> Option<ast::Pat> {
353359
match var {
354360
ExtendedVariant::Variant(var) => {
355-
let path = mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var))?);
361+
let path =
362+
mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var), prefer_core)?);
356363

357364
// FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though
358365
let pat: ast::Pat = match var.source(db)?.value.kind() {

crates/ide-assists/src/handlers/auto_import.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel};
8989
// ```
9090
pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
9191
let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;
92-
let mut proposed_imports =
93-
import_assets.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind);
92+
let mut proposed_imports = import_assets.search_for_imports(
93+
&ctx.sema,
94+
ctx.config.insert_use.prefix_kind,
95+
ctx.config.prefer_core,
96+
);
9497
if proposed_imports.is_empty() {
9598
return None;
9699
}

crates/ide-assists/src/handlers/convert_into_to_from.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext<'_>) -
5050
_ => return None,
5151
};
5252

53-
mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def)?)
53+
mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def, ctx.config.prefer_core)?)
5454
};
5555

5656
let dest_type = match &ast_trait {

crates/ide-assists/src/handlers/extract_function.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
152152
ctx.sema.db,
153153
ModuleDef::from(control_flow_enum),
154154
ctx.config.insert_use.prefix_kind,
155+
ctx.config.prefer_core,
155156
);
156157

157158
if let Some(mod_path) = mod_path {

crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ fn process_references(
409409
ctx.sema.db,
410410
*enum_module_def,
411411
ctx.config.insert_use.prefix_kind,
412+
ctx.config.prefer_core,
412413
);
413414
if let Some(mut mod_path) = mod_path {
414415
mod_path.pop_segment();

crates/ide-assists/src/handlers/generate_deref.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ fn generate_record_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
5858

5959
let module = ctx.sema.to_def(&strukt)?.module(ctx.db());
6060
let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?;
61-
let trait_path = module.find_use_path(ctx.db(), ModuleDef::Trait(trait_))?;
61+
let trait_path =
62+
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_core)?;
6263

6364
let field_type = field.ty()?;
6465
let field_name = field.name()?;
@@ -98,7 +99,8 @@ fn generate_tuple_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()
9899

99100
let module = ctx.sema.to_def(&strukt)?.module(ctx.db());
100101
let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?;
101-
let trait_path = module.find_use_path(ctx.db(), ModuleDef::Trait(trait_))?;
102+
let trait_path =
103+
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_core)?;
102104

103105
let field_type = field.ty()?;
104106
let target = field.syntax().text_range();

crates/ide-assists/src/handlers/generate_new.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
6060

6161
let item_in_ns = hir::ItemInNs::from(hir::ModuleDef::from(ty.as_adt()?));
6262

63-
let type_path = current_module
64-
.find_use_path(ctx.sema.db, item_for_path_search(ctx.sema.db, item_in_ns)?)?;
63+
let type_path = current_module.find_use_path(
64+
ctx.sema.db,
65+
item_for_path_search(ctx.sema.db, item_in_ns)?,
66+
ctx.config.prefer_core,
67+
)?;
6568

6669
let expr = use_trivial_constructor(
6770
&ctx.sema.db,

crates/ide-assists/src/handlers/qualify_method_call.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext<'_>) ->
4444
let current_module = ctx.sema.scope(call.syntax())?.module();
4545
let target_module_def = ModuleDef::from(resolved_call);
4646
let item_in_ns = ItemInNs::from(target_module_def);
47-
let receiver_path = current_module
48-
.find_use_path(ctx.sema.db, item_for_path_search(ctx.sema.db, item_in_ns)?)?;
47+
let receiver_path = current_module.find_use_path(
48+
ctx.sema.db,
49+
item_for_path_search(ctx.sema.db, item_in_ns)?,
50+
ctx.config.prefer_core,
51+
)?;
4952

5053
let qualify_candidate = QualifyCandidate::ImplMethod(ctx.sema.db, call, resolved_call);
5154

crates/ide-assists/src/handlers/qualify_path.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ use crate::{
3737
// ```
3838
pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
3939
let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;
40-
let mut proposed_imports = import_assets.search_for_relative_paths(&ctx.sema);
40+
let mut proposed_imports =
41+
import_assets.search_for_relative_paths(&ctx.sema, ctx.config.prefer_core);
4142
if proposed_imports.is_empty() {
4243
return None;
4344
}

crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub(crate) fn replace_derive_with_manual_impl(
8585
})
8686
.flat_map(|trait_| {
8787
current_module
88-
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
88+
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_), ctx.config.prefer_core)
8989
.as_ref()
9090
.map(mod_path_to_ast)
9191
.zip(Some(trait_))

crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub(crate) fn replace_qualified_name_with_use(
6767
ctx.sema.db,
6868
module,
6969
ctx.config.insert_use.prefix_kind,
70+
ctx.config.prefer_core,
7071
)
7172
})
7273
.flatten();

crates/ide-assists/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
2929
group: true,
3030
skip_glob_imports: true,
3131
},
32+
prefer_core: false,
3233
};
3334

3435
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {

crates/ide-completion/src/completions.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,9 @@ fn enum_variants_with_paths(
551551
}
552552

553553
for variant in variants {
554-
if let Some(path) = ctx.module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) {
554+
if let Some(path) =
555+
ctx.module.find_use_path(ctx.db, hir::ModuleDef::from(variant), ctx.config.prefer_core)
556+
{
555557
// Variants with trivial paths are already added by the existing completion logic,
556558
// so we should avoid adding these twice
557559
if path.segments().len() > 1 {

crates/ide-completion/src/completions/expr.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ pub(crate) fn complete_expr_path(
165165
hir::Adt::Struct(strukt) => {
166166
let path = ctx
167167
.module
168-
.find_use_path(ctx.db, hir::ModuleDef::from(strukt))
168+
.find_use_path(
169+
ctx.db,
170+
hir::ModuleDef::from(strukt),
171+
ctx.config.prefer_core,
172+
)
169173
.filter(|it| it.len() > 1);
170174

171175
acc.add_struct_literal(ctx, path_ctx, strukt, path, None);
@@ -183,7 +187,7 @@ pub(crate) fn complete_expr_path(
183187
hir::Adt::Union(un) => {
184188
let path = ctx
185189
.module
186-
.find_use_path(ctx.db, hir::ModuleDef::from(un))
190+
.find_use_path(ctx.db, hir::ModuleDef::from(un), ctx.config.prefer_core)
187191
.filter(|it| it.len() > 1);
188192

189193
acc.add_union_literal(ctx, un, path, None);

crates/ide-completion/src/completions/flyimport.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,11 @@ fn import_on_the_fly(
262262

263263
acc.add_all(
264264
import_assets
265-
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
265+
.search_for_imports(
266+
&ctx.sema,
267+
ctx.config.insert_use.prefix_kind,
268+
ctx.config.prefer_core,
269+
)
266270
.into_iter()
267271
.filter(ns_filter)
268272
.filter(|import| {
@@ -306,7 +310,11 @@ fn import_on_the_fly_pat_(
306310

307311
acc.add_all(
308312
import_assets
309-
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
313+
.search_for_imports(
314+
&ctx.sema,
315+
ctx.config.insert_use.prefix_kind,
316+
ctx.config.prefer_core,
317+
)
310318
.into_iter()
311319
.filter(ns_filter)
312320
.filter(|import| {
@@ -344,7 +352,7 @@ fn import_on_the_fly_method(
344352
let user_input_lowercased = potential_import_name.to_lowercase();
345353

346354
import_assets
347-
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
355+
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_core)
348356
.into_iter()
349357
.filter(|import| {
350358
!ctx.is_item_hidden(&import.item_to_import)

crates/ide-completion/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct CompletionConfig {
1717
pub callable: Option<CallableSnippets>,
1818
pub snippet_cap: Option<SnippetCap>,
1919
pub insert_use: InsertUseConfig,
20+
pub prefer_core: bool,
2021
pub snippets: Vec<Snippet>,
2122
}
2223

0 commit comments

Comments
 (0)