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();

0 commit comments

Comments
 (0)