Skip to content

Commit 125d43c

Browse files
committed
Auto merge of #13227 - Veykril:core-pref, r=Veykril
Restructure `find_path` into a separate functions for modules and non-module items Follow up to #13212 Also renames `prefer_core` imports config to `prefer_no_std` and changes the behavior of no_std path searching by preferring `core` paths `over` alloc This PR turned into a slight rewrite, so it unfortunately does a few more things that I initially planned to (including a bug fix for enum variant paths)
2 parents 482a992 + a8ecaa1 commit 125d43c

31 files changed

+291
-200
lines changed

crates/hir-def/src/find_path.rs

+219-141
Large diffs are not rendered by default.

crates/hir-def/src/item_scope.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,15 @@ impl ItemInNs {
457457
/// Returns the crate defining this item (or `None` if `self` is built-in).
458458
pub fn krate(&self, db: &dyn DefDatabase) -> Option<CrateId> {
459459
match self {
460-
ItemInNs::Types(did) | ItemInNs::Values(did) => did.module(db).map(|m| m.krate),
460+
ItemInNs::Types(id) | ItemInNs::Values(id) => id.module(db).map(|m| m.krate),
461461
ItemInNs::Macros(id) => Some(id.module(db).krate),
462462
}
463463
}
464+
465+
pub fn module(&self, db: &dyn DefDatabase) -> Option<ModuleId> {
466+
match self {
467+
ItemInNs::Types(id) | ItemInNs::Values(id) => id.module(db),
468+
ItemInNs::Macros(id) => Some(id.module(db)),
469+
}
470+
}
464471
}

crates/hir/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,9 @@ impl Module {
585585
self,
586586
db: &dyn DefDatabase,
587587
item: impl Into<ItemInNs>,
588-
prefer_core: bool,
588+
prefer_no_std: bool,
589589
) -> Option<ModPath> {
590-
hir_def::find_path::find_path(db, item.into().into(), self.into(), prefer_core)
590+
hir_def::find_path::find_path(db, item.into().into(), self.into(), prefer_no_std)
591591
}
592592

593593
/// Finds a path that can be used to refer to the given item from within
@@ -597,14 +597,14 @@ impl Module {
597597
db: &dyn DefDatabase,
598598
item: impl Into<ItemInNs>,
599599
prefix_kind: PrefixKind,
600-
prefer_core: bool,
600+
prefer_no_std: bool,
601601
) -> Option<ModPath> {
602602
hir_def::find_path::find_path_prefixed(
603603
db,
604604
item.into().into(),
605605
self.into(),
606606
prefix_kind,
607-
prefer_core,
607+
prefer_no_std,
608608
)
609609
}
610610
}

crates/ide-assists/src/assist_config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +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,
16+
pub prefer_no_std: bool,
1717
}

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

+4-4
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, ctx.config.prefer_core)?,
90+
build_pat(ctx.db(), module, variant, ctx.config.prefer_no_std)?,
9191
variant.should_be_hidden(ctx.db(), module.krate()),
9292
))
9393
})
@@ -133,7 +133,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
133133
.iter()
134134
.any(|variant| variant.should_be_hidden(ctx.db(), module.krate()));
135135
let patterns = variants.into_iter().filter_map(|variant| {
136-
build_pat(ctx.db(), module, variant, ctx.config.prefer_core)
136+
build_pat(ctx.db(), module, variant, ctx.config.prefer_no_std)
137137
});
138138

139139
(ast::Pat::from(make::tuple_pat(patterns)), is_hidden)
@@ -354,12 +354,12 @@ fn build_pat(
354354
db: &RootDatabase,
355355
module: hir::Module,
356356
var: ExtendedVariant,
357-
prefer_core: bool,
357+
prefer_no_std: bool,
358358
) -> Option<ast::Pat> {
359359
match var {
360360
ExtendedVariant::Variant(var) => {
361361
let path =
362-
mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var), prefer_core)?);
362+
mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var), prefer_no_std)?);
363363

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
9292
let mut proposed_imports = import_assets.search_for_imports(
9393
&ctx.sema,
9494
ctx.config.insert_use.prefix_kind,
95-
ctx.config.prefer_core,
95+
ctx.config.prefer_no_std,
9696
);
9797
if proposed_imports.is_empty() {
9898
return None;

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

+1-1
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, ctx.config.prefer_core)?)
53+
mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def, ctx.config.prefer_no_std)?)
5454
};
5555

5656
let dest_type = match &ast_trait {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +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,
155+
ctx.config.prefer_no_std,
156156
);
157157

158158
if let Some(mod_path) = mod_path {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +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,
412+
ctx.config.prefer_no_std,
413413
);
414414
if let Some(mut mod_path) = mod_path {
415415
mod_path.pop_segment();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn generate_record_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
5959
let module = ctx.sema.to_def(&strukt)?.module(ctx.db());
6060
let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?;
6161
let trait_path =
62-
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_core)?;
62+
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_no_std)?;
6363

6464
let field_type = field.ty()?;
6565
let field_name = field.name()?;
@@ -100,7 +100,7 @@ fn generate_tuple_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()
100100
let module = ctx.sema.to_def(&strukt)?.module(ctx.db());
101101
let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?;
102102
let trait_path =
103-
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_core)?;
103+
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_no_std)?;
104104

105105
let field_type = field.ty()?;
106106
let target = field.syntax().text_range();

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
6363
let type_path = current_module.find_use_path(
6464
ctx.sema.db,
6565
item_for_path_search(ctx.sema.db, item_in_ns)?,
66-
ctx.config.prefer_core,
66+
ctx.config.prefer_no_std,
6767
)?;
6868

6969
let expr = use_trivial_constructor(

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext<'_>) ->
4747
let receiver_path = current_module.find_use_path(
4848
ctx.sema.db,
4949
item_for_path_search(ctx.sema.db, item_in_ns)?,
50-
ctx.config.prefer_core,
50+
ctx.config.prefer_no_std,
5151
)?;
5252

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::{
3838
pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
3939
let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;
4040
let mut proposed_imports =
41-
import_assets.search_for_relative_paths(&ctx.sema, ctx.config.prefer_core);
41+
import_assets.search_for_relative_paths(&ctx.sema, ctx.config.prefer_no_std);
4242
if proposed_imports.is_empty() {
4343
return None;
4444
}

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

+1-1
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_), ctx.config.prefer_core)
88+
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_), ctx.config.prefer_no_std)
8989
.as_ref()
9090
.map(mod_path_to_ast)
9191
.zip(Some(trait_))

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +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,
70+
ctx.config.prefer_no_std,
7171
)
7272
})
7373
.flatten();

crates/ide-assists/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
2929
group: true,
3030
skip_glob_imports: true,
3131
},
32-
prefer_core: false,
32+
prefer_no_std: false,
3333
};
3434

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

crates/ide-completion/src/completions.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,11 @@ fn enum_variants_with_paths(
551551
}
552552

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

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ pub(crate) fn complete_expr_path(
168168
.find_use_path(
169169
ctx.db,
170170
hir::ModuleDef::from(strukt),
171-
ctx.config.prefer_core,
171+
ctx.config.prefer_no_std,
172172
)
173173
.filter(|it| it.len() > 1);
174174

@@ -187,7 +187,11 @@ pub(crate) fn complete_expr_path(
187187
hir::Adt::Union(un) => {
188188
let path = ctx
189189
.module
190-
.find_use_path(ctx.db, hir::ModuleDef::from(un), ctx.config.prefer_core)
190+
.find_use_path(
191+
ctx.db,
192+
hir::ModuleDef::from(un),
193+
ctx.config.prefer_no_std,
194+
)
191195
.filter(|it| it.len() > 1);
192196

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

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ fn import_on_the_fly(
265265
.search_for_imports(
266266
&ctx.sema,
267267
ctx.config.insert_use.prefix_kind,
268-
ctx.config.prefer_core,
268+
ctx.config.prefer_no_std,
269269
)
270270
.into_iter()
271271
.filter(ns_filter)
@@ -313,7 +313,7 @@ fn import_on_the_fly_pat_(
313313
.search_for_imports(
314314
&ctx.sema,
315315
ctx.config.insert_use.prefix_kind,
316-
ctx.config.prefer_core,
316+
ctx.config.prefer_no_std,
317317
)
318318
.into_iter()
319319
.filter(ns_filter)
@@ -352,7 +352,7 @@ fn import_on_the_fly_method(
352352
let user_input_lowercased = potential_import_name.to_lowercase();
353353

354354
import_assets
355-
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_core)
355+
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_no_std)
356356
.into_iter()
357357
.filter(|import| {
358358
!ctx.is_item_hidden(&import.item_to_import)

crates/ide-completion/src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +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,
20+
pub prefer_no_std: bool,
2121
pub snippets: Vec<Snippet>,
2222
}
2323

crates/ide-completion/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ pub fn resolve_completion_edits(
238238
db,
239239
candidate,
240240
config.insert_use.prefix_kind,
241-
config.prefer_core,
241+
config.prefer_no_std,
242242
)
243243
})
244244
.find(|mod_path| mod_path.to_string() == full_import_path);

crates/ide-completion/src/snippet.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn import_edits(ctx: &CompletionContext<'_>, requires: &[GreenNode]) -> Option<V
178178
ctx.db,
179179
item,
180180
ctx.config.insert_use.prefix_kind,
181-
ctx.config.prefer_core,
181+
ctx.config.prefer_no_std,
182182
)?;
183183
Some((path.len() > 1).then(|| LocatedImport::new(path.clone(), item, item, None)))
184184
};

crates/ide-completion/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
6666
enable_private_editable: false,
6767
callable: Some(CallableSnippets::FillArguments),
6868
snippet_cap: SnippetCap::new(true),
69-
prefer_core: false,
69+
prefer_no_std: false,
7070
insert_use: InsertUseConfig {
7171
granularity: ImportGranularity::Crate,
7272
prefix_kind: PrefixKind::Plain,

crates/ide-db/src/imports/import_assets.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,20 @@ impl ImportAssets {
212212
&self,
213213
sema: &Semantics<'_, RootDatabase>,
214214
prefix_kind: PrefixKind,
215-
prefer_core: bool,
215+
prefer_no_std: bool,
216216
) -> Vec<LocatedImport> {
217217
let _p = profile::span("import_assets::search_for_imports");
218-
self.search_for(sema, Some(prefix_kind), prefer_core)
218+
self.search_for(sema, Some(prefix_kind), prefer_no_std)
219219
}
220220

221221
/// This may return non-absolute paths if a part of the returned path is already imported into scope.
222222
pub fn search_for_relative_paths(
223223
&self,
224224
sema: &Semantics<'_, RootDatabase>,
225-
prefer_core: bool,
225+
prefer_no_std: bool,
226226
) -> Vec<LocatedImport> {
227227
let _p = profile::span("import_assets::search_for_relative_paths");
228-
self.search_for(sema, None, prefer_core)
228+
self.search_for(sema, None, prefer_no_std)
229229
}
230230

231231
pub fn path_fuzzy_name_to_exact(&mut self, case_sensitive: bool) {
@@ -244,7 +244,7 @@ impl ImportAssets {
244244
&self,
245245
sema: &Semantics<'_, RootDatabase>,
246246
prefixed: Option<PrefixKind>,
247-
prefer_core: bool,
247+
prefer_no_std: bool,
248248
) -> Vec<LocatedImport> {
249249
let _p = profile::span("import_assets::search_for");
250250

@@ -255,7 +255,7 @@ impl ImportAssets {
255255
item_for_path_search(sema.db, item)?,
256256
&self.module_with_candidate,
257257
prefixed,
258-
prefer_core,
258+
prefer_no_std,
259259
)
260260
};
261261

@@ -568,12 +568,12 @@ fn get_mod_path(
568568
item_to_search: ItemInNs,
569569
module_with_candidate: &Module,
570570
prefixed: Option<PrefixKind>,
571-
prefer_core: bool,
571+
prefer_no_std: bool,
572572
) -> Option<ModPath> {
573573
if let Some(prefix_kind) = prefixed {
574-
module_with_candidate.find_use_path_prefixed(db, item_to_search, prefix_kind, prefer_core)
574+
module_with_candidate.find_use_path_prefixed(db, item_to_search, prefix_kind, prefer_no_std)
575575
} else {
576-
module_with_candidate.find_use_path(db, item_to_search, prefer_core)
576+
module_with_candidate.find_use_path(db, item_to_search, prefer_no_std)
577577
}
578578
}
579579

crates/ide-diagnostics/src/handlers/json_is_not_rust.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub(crate) fn json_in_items(
137137
sema.db,
138138
it,
139139
config.insert_use.prefix_kind,
140-
config.prefer_core,
140+
config.prefer_no_std,
141141
) {
142142
insert_use(
143143
&scope,
@@ -153,7 +153,7 @@ pub(crate) fn json_in_items(
153153
sema.db,
154154
it,
155155
config.insert_use.prefix_kind,
156-
config.prefer_core,
156+
config.prefer_no_std,
157157
) {
158158
insert_use(
159159
&scope,

crates/ide-diagnostics/src/handlers/missing_fields.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
124124
let type_path = current_module?.find_use_path(
125125
ctx.sema.db,
126126
item_for_path_search(ctx.sema.db, item_in_ns)?,
127-
ctx.config.prefer_core,
127+
ctx.config.prefer_no_std,
128128
)?;
129129

130130
use_trivial_constructor(

crates/ide-diagnostics/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub struct DiagnosticsConfig {
150150
pub expr_fill_default: ExprFillDefaultMode,
151151
// FIXME: We may want to include a whole `AssistConfig` here
152152
pub insert_use: InsertUseConfig,
153-
pub prefer_core: bool,
153+
pub prefer_no_std: bool,
154154
}
155155

156156
impl DiagnosticsConfig {
@@ -171,7 +171,7 @@ impl DiagnosticsConfig {
171171
group: false,
172172
skip_glob_imports: false,
173173
},
174-
prefer_core: false,
174+
prefer_no_std: false,
175175
}
176176
}
177177
}

0 commit comments

Comments
 (0)