Skip to content

Commit

Permalink
Auto merge of #13212 - Veykril:no-std-config, r=Veykril
Browse files Browse the repository at this point in the history
Add config to unconditionally prefer core imports over std

Fixes #12979
  • Loading branch information
bors committed Sep 12, 2022
2 parents bc13142 + 7d19971 commit 352a5b8
Show file tree
Hide file tree
Showing 33 changed files with 156 additions and 43 deletions.
36 changes: 29 additions & 7 deletions crates/hir-def/src/find_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ use crate::{

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

pub fn find_path_prefixed(
db: &dyn DefDatabase,
item: ItemInNs,
from: ModuleId,
prefix_kind: PrefixKind,
prefer_core: bool,
) -> Option<ModPath> {
let _p = profile::span("find_path_prefixed");
find_path_inner(db, item, from, Some(prefix_kind))
find_path_inner(db, item, from, Some(prefix_kind), prefer_core)
}

const MAX_PATH_LEN: usize = 15;
Expand Down Expand Up @@ -100,12 +106,22 @@ fn find_path_inner(
item: ItemInNs,
from: ModuleId,
prefixed: Option<PrefixKind>,
prefer_core: bool,
) -> Option<ModPath> {
// FIXME: Do fast path for std/core libs?

let mut visited_modules = FxHashSet::default();
let def_map = from.def_map(db);
find_path_inner_(db, &def_map, from, item, MAX_PATH_LEN, prefixed, &mut visited_modules)
find_path_inner_(
db,
&def_map,
from,
item,
MAX_PATH_LEN,
prefixed,
&mut visited_modules,
prefer_core,
)
}

fn find_path_inner_(
Expand All @@ -116,6 +132,7 @@ fn find_path_inner_(
max_len: usize,
mut prefixed: Option<PrefixKind>,
visited_modules: &mut FxHashSet<ModuleId>,
prefer_core: bool,
) -> Option<ModPath> {
if max_len == 0 {
return None;
Expand Down Expand Up @@ -191,7 +208,9 @@ fn find_path_inner_(
// Recursive case:
// - if the item is an enum variant, refer to it via the enum
if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() {
if let Some(mut path) = find_path(db, ItemInNs::Types(variant.parent.into()), from) {
if let Some(mut path) =
find_path(db, ItemInNs::Types(variant.parent.into()), from, prefer_core)
{
let data = db.enum_data(variant.parent);
path.push_segment(data.variants[variant.local_id].name.clone());
return Some(path);
Expand All @@ -202,7 +221,7 @@ fn find_path_inner_(
}

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

Expand All @@ -223,6 +242,7 @@ fn find_path_inner_(
best_path_len - 1,
prefixed,
visited_modules,
prefer_core,
) {
path.push_segment(name);

Expand Down Expand Up @@ -253,6 +273,7 @@ fn find_path_inner_(
best_path_len - 1,
prefixed,
visited_modules,
prefer_core,
)?;
cov_mark::hit!(partially_imported);
path.push_segment(info.path.segments.last()?.clone());
Expand Down Expand Up @@ -428,7 +449,8 @@ mod tests {
.take_types()
.unwrap();

let found_path = find_path_inner(&db, ItemInNs::Types(resolved), module, prefix_kind);
let found_path =
find_path_inner(&db, ItemInNs::Types(resolved), module, prefix_kind, false);
assert_eq!(found_path, Some(mod_path), "{:?}", prefix_kind);
}

Expand Down
1 change: 1 addition & 0 deletions crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ impl HirDisplay for Ty {
f.db.upcast(),
ItemInNs::Types((*def_id).into()),
module_id,
false,
) {
write!(f, "{}", path)?;
} else {
Expand Down
18 changes: 15 additions & 3 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,13 @@ impl Module {

/// Finds a path that can be used to refer to the given item from within
/// this module, if possible.
pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into<ItemInNs>) -> Option<ModPath> {
hir_def::find_path::find_path(db, item.into().into(), self.into())
pub fn find_use_path(
self,
db: &dyn DefDatabase,
item: impl Into<ItemInNs>,
prefer_core: bool,
) -> Option<ModPath> {
hir_def::find_path::find_path(db, item.into().into(), self.into(), prefer_core)
}

/// Finds a path that can be used to refer to the given item from within
Expand All @@ -593,8 +598,15 @@ impl Module {
db: &dyn DefDatabase,
item: impl Into<ItemInNs>,
prefix_kind: PrefixKind,
prefer_core: bool,
) -> Option<ModPath> {
hir_def::find_path::find_path_prefixed(db, item.into().into(), self.into(), prefix_kind)
hir_def::find_path::find_path_prefixed(
db,
item.into().into(),
self.into(),
prefix_kind,
prefer_core,
)
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/assist_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ pub struct AssistConfig {
pub snippet_cap: Option<SnippetCap>,
pub allowed: Option<Vec<AssistKind>>,
pub insert_use: InsertUseConfig,
pub prefer_core: bool,
}
17 changes: 12 additions & 5 deletions crates/ide-assists/src/handlers/add_missing_match_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
.into_iter()
.filter_map(|variant| {
Some((
build_pat(ctx.db(), module, variant)?,
build_pat(ctx.db(), module, variant, ctx.config.prefer_core)?,
variant.should_be_hidden(ctx.db(), module.krate()),
))
})
Expand Down Expand Up @@ -132,8 +132,9 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
let is_hidden = variants
.iter()
.any(|variant| variant.should_be_hidden(ctx.db(), module.krate()));
let patterns =
variants.into_iter().filter_map(|variant| build_pat(ctx.db(), module, variant));
let patterns = variants.into_iter().filter_map(|variant| {
build_pat(ctx.db(), module, variant, ctx.config.prefer_core)
});

(ast::Pat::from(make::tuple_pat(patterns)), is_hidden)
})
Expand Down Expand Up @@ -349,10 +350,16 @@ fn resolve_tuple_of_enum_def(
.collect()
}

fn build_pat(db: &RootDatabase, module: hir::Module, var: ExtendedVariant) -> Option<ast::Pat> {
fn build_pat(
db: &RootDatabase,
module: hir::Module,
var: ExtendedVariant,
prefer_core: bool,
) -> Option<ast::Pat> {
match var {
ExtendedVariant::Variant(var) => {
let path = mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var))?);
let path =
mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var), prefer_core)?);

// FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though
let pat: ast::Pat = match var.source(db)?.value.kind() {
Expand Down
7 changes: 5 additions & 2 deletions crates/ide-assists/src/handlers/auto_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel};
// ```
pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;
let mut proposed_imports =
import_assets.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind);
let mut proposed_imports = import_assets.search_for_imports(
&ctx.sema,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
);
if proposed_imports.is_empty() {
return None;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/convert_into_to_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext<'_>) -
_ => return None,
};

mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def)?)
mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def, ctx.config.prefer_core)?)
};

let dest_type = match &ast_trait {
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/extract_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
ctx.sema.db,
ModuleDef::from(control_flow_enum),
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
);

if let Some(mod_path) = mod_path {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ fn process_references(
ctx.sema.db,
*enum_module_def,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
);
if let Some(mut mod_path) = mod_path {
mod_path.pop_segment();
Expand Down
6 changes: 4 additions & 2 deletions crates/ide-assists/src/handlers/generate_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ fn generate_record_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(

let module = ctx.sema.to_def(&strukt)?.module(ctx.db());
let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?;
let trait_path = module.find_use_path(ctx.db(), ModuleDef::Trait(trait_))?;
let trait_path =
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_core)?;

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

let module = ctx.sema.to_def(&strukt)?.module(ctx.db());
let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?;
let trait_path = module.find_use_path(ctx.db(), ModuleDef::Trait(trait_))?;
let trait_path =
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_core)?;

let field_type = field.ty()?;
let target = field.syntax().text_range();
Expand Down
7 changes: 5 additions & 2 deletions crates/ide-assists/src/handlers/generate_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option

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

let type_path = current_module
.find_use_path(ctx.sema.db, item_for_path_search(ctx.sema.db, item_in_ns)?)?;
let type_path = current_module.find_use_path(
ctx.sema.db,
item_for_path_search(ctx.sema.db, item_in_ns)?,
ctx.config.prefer_core,
)?;

let expr = use_trivial_constructor(
&ctx.sema.db,
Expand Down
7 changes: 5 additions & 2 deletions crates/ide-assists/src/handlers/qualify_method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext<'_>) ->
let current_module = ctx.sema.scope(call.syntax())?.module();
let target_module_def = ModuleDef::from(resolved_call);
let item_in_ns = ItemInNs::from(target_module_def);
let receiver_path = current_module
.find_use_path(ctx.sema.db, item_for_path_search(ctx.sema.db, item_in_ns)?)?;
let receiver_path = current_module.find_use_path(
ctx.sema.db,
item_for_path_search(ctx.sema.db, item_in_ns)?,
ctx.config.prefer_core,
)?;

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

Expand Down
3 changes: 2 additions & 1 deletion crates/ide-assists/src/handlers/qualify_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ use crate::{
// ```
pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;
let mut proposed_imports = import_assets.search_for_relative_paths(&ctx.sema);
let mut proposed_imports =
import_assets.search_for_relative_paths(&ctx.sema, ctx.config.prefer_core);
if proposed_imports.is_empty() {
return None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub(crate) fn replace_derive_with_manual_impl(
})
.flat_map(|trait_| {
current_module
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_), ctx.config.prefer_core)
.as_ref()
.map(mod_path_to_ast)
.zip(Some(trait_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub(crate) fn replace_qualified_name_with_use(
ctx.sema.db,
module,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
)
})
.flatten();
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
group: true,
skip_glob_imports: true,
},
prefer_core: false,
};

pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
Expand Down
4 changes: 3 additions & 1 deletion crates/ide-completion/src/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,9 @@ fn enum_variants_with_paths(
}

for variant in variants {
if let Some(path) = ctx.module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) {
if let Some(path) =
ctx.module.find_use_path(ctx.db, hir::ModuleDef::from(variant), ctx.config.prefer_core)
{
// Variants with trivial paths are already added by the existing completion logic,
// so we should avoid adding these twice
if path.segments().len() > 1 {
Expand Down
8 changes: 6 additions & 2 deletions crates/ide-completion/src/completions/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ pub(crate) fn complete_expr_path(
hir::Adt::Struct(strukt) => {
let path = ctx
.module
.find_use_path(ctx.db, hir::ModuleDef::from(strukt))
.find_use_path(
ctx.db,
hir::ModuleDef::from(strukt),
ctx.config.prefer_core,
)
.filter(|it| it.len() > 1);

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

acc.add_union_literal(ctx, un, path, None);
Expand Down
14 changes: 11 additions & 3 deletions crates/ide-completion/src/completions/flyimport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ fn import_on_the_fly(

acc.add_all(
import_assets
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
.search_for_imports(
&ctx.sema,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
)
.into_iter()
.filter(ns_filter)
.filter(|import| {
Expand Down Expand Up @@ -306,7 +310,11 @@ fn import_on_the_fly_pat_(

acc.add_all(
import_assets
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
.search_for_imports(
&ctx.sema,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
)
.into_iter()
.filter(ns_filter)
.filter(|import| {
Expand Down Expand Up @@ -344,7 +352,7 @@ fn import_on_the_fly_method(
let user_input_lowercased = potential_import_name.to_lowercase();

import_assets
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_core)
.into_iter()
.filter(|import| {
!ctx.is_item_hidden(&import.item_to_import)
Expand Down
1 change: 1 addition & 0 deletions crates/ide-completion/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct CompletionConfig {
pub callable: Option<CallableSnippets>,
pub snippet_cap: Option<SnippetCap>,
pub insert_use: InsertUseConfig,
pub prefer_core: bool,
pub snippets: Vec<Snippet>,
}

Expand Down
Loading

0 comments on commit 352a5b8

Please sign in to comment.