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 rust-lang/rust-analyzer#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

Lines changed: 219 additions & 141 deletions
Large diffs are not rendered by default.

crates/hir-def/src/item_scope.rs

Lines changed: 8 additions & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 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, 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

Lines changed: 1 addition & 1 deletion
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

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, 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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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();

0 commit comments

Comments
 (0)