Skip to content

Commit 6b2e677

Browse files
committed
fix: Only compute unstable paths on nightly toolchains for IDE features
1 parent b528d8a commit 6b2e677

36 files changed

+142
-99
lines changed

crates/hir-def/src/find_path.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use intern::sym;
1212
use rustc_hash::FxHashSet;
1313

1414
use crate::{
15-
ImportPathConfig, ModuleDefId, ModuleId,
15+
FindPathConfig, ModuleDefId, ModuleId,
1616
db::DefDatabase,
1717
item_scope::ItemInNs,
1818
nameres::DefMap,
@@ -27,7 +27,7 @@ pub fn find_path(
2727
from: ModuleId,
2828
mut prefix_kind: PrefixKind,
2929
ignore_local_imports: bool,
30-
mut cfg: ImportPathConfig,
30+
mut cfg: FindPathConfig,
3131
) -> Option<ModPath> {
3232
let _p = tracing::info_span!("find_path").entered();
3333

@@ -96,7 +96,7 @@ impl PrefixKind {
9696
struct FindPathCtx<'db> {
9797
db: &'db dyn DefDatabase,
9898
prefix: PrefixKind,
99-
cfg: ImportPathConfig,
99+
cfg: FindPathConfig,
100100
ignore_local_imports: bool,
101101
is_std_item: bool,
102102
from: ModuleId,
@@ -718,7 +718,7 @@ mod tests {
718718
module,
719719
prefix,
720720
ignore_local_imports,
721-
ImportPathConfig { prefer_no_std, prefer_prelude, prefer_absolute, allow_unstable },
721+
FindPathConfig { prefer_no_std, prefer_prelude, prefer_absolute, allow_unstable },
722722
);
723723
format_to!(
724724
res,

crates/hir-def/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ use crate::{
101101
type FxIndexMap<K, V> = indexmap::IndexMap<K, V, rustc_hash::FxBuildHasher>;
102102
/// A wrapper around three booleans
103103
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
104-
pub struct ImportPathConfig {
104+
pub struct FindPathConfig {
105105
/// If true, prefer to unconditionally use imports of the `core` and `alloc` crate
106106
/// over the std.
107107
pub prefer_no_std: bool,

crates/hir-ty/src/display.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use base_db::Crate;
1111
use chalk_ir::{BoundVar, Safety, TyKind};
1212
use either::Either;
1313
use hir_def::{
14-
GenericDefId, HasModule, ImportPathConfig, ItemContainerId, LocalFieldId, Lookup, ModuleDefId,
14+
FindPathConfig, GenericDefId, HasModule, ItemContainerId, LocalFieldId, Lookup, ModuleDefId,
1515
ModuleId, TraitId,
1616
db::DefDatabase,
1717
expr_store::{ExpressionStore, path::Path},
@@ -1299,7 +1299,7 @@ impl HirDisplay for Ty {
12991299
PrefixKind::Plain,
13001300
false,
13011301
// FIXME: no_std Cfg?
1302-
ImportPathConfig {
1302+
FindPathConfig {
13031303
prefer_no_std: false,
13041304
prefer_prelude: true,
13051305
prefer_absolute: false,

crates/hir/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub use {
117117
cfg::{CfgAtom, CfgExpr, CfgOptions},
118118
hir_def::{
119119
Complete,
120-
ImportPathConfig,
120+
FindPathConfig,
121121
attr::{AttrSourceMap, Attrs, AttrsWithOwner},
122122
find_path::PrefixKind,
123123
import_map,
@@ -949,7 +949,7 @@ impl Module {
949949
self,
950950
db: &dyn DefDatabase,
951951
item: impl Into<ItemInNs>,
952-
cfg: ImportPathConfig,
952+
cfg: FindPathConfig,
953953
) -> Option<ModPath> {
954954
hir_def::find_path::find_path(
955955
db,
@@ -968,7 +968,7 @@ impl Module {
968968
db: &dyn DefDatabase,
969969
item: impl Into<ItemInNs>,
970970
prefix_kind: PrefixKind,
971-
cfg: ImportPathConfig,
971+
cfg: FindPathConfig,
972972
) -> Option<ModPath> {
973973
hir_def::find_path::find_path(db, item.into().into(), self.into(), prefix_kind, true, cfg)
974974
}

crates/hir/src/semantics.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,13 @@ impl<DB: HirDatabase + ?Sized> Semantics<'_, DB> {
303303
self.imp.hir_file_to_module_defs(file.into())
304304
}
305305

306+
pub fn is_nightly(&self, krate: Crate) -> bool {
307+
let toolchain = self.db.toolchain_channel(krate.into());
308+
// `toolchain == None` means we're in some detached files. Since we have no information on
309+
// the toolchain being used, let's just allow unstable items to be listed.
310+
matches!(toolchain, Some(base_db::ReleaseChannel::Nightly) | None)
311+
}
312+
306313
pub fn to_adt_def(&self, a: &ast::Adt) -> Option<Adt> {
307314
self.imp.to_def(a)
308315
}

crates/hir/src/term_search/expr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Type tree for term search
22
3-
use hir_def::ImportPathConfig;
3+
use hir_def::FindPathConfig;
44
use hir_expand::mod_path::ModPath;
55
use hir_ty::{
66
db::HirDatabase,
@@ -18,7 +18,7 @@ use crate::{
1818
fn mod_item_path(
1919
sema_scope: &SemanticsScope<'_>,
2020
def: &ModuleDef,
21-
cfg: ImportPathConfig,
21+
cfg: FindPathConfig,
2222
) -> Option<ModPath> {
2323
let db = sema_scope.db;
2424
let m = sema_scope.module();
@@ -29,7 +29,7 @@ fn mod_item_path(
2929
fn mod_item_path_str(
3030
sema_scope: &SemanticsScope<'_>,
3131
def: &ModuleDef,
32-
cfg: ImportPathConfig,
32+
cfg: FindPathConfig,
3333
edition: Edition,
3434
) -> Result<String, DisplaySourceCodeError> {
3535
let path = mod_item_path(sema_scope, def, cfg);
@@ -103,7 +103,7 @@ impl<'db> Expr<'db> {
103103
&self,
104104
sema_scope: &SemanticsScope<'db>,
105105
many_formatter: &mut dyn FnMut(&Type<'db>) -> String,
106-
cfg: ImportPathConfig,
106+
cfg: FindPathConfig,
107107
display_target: DisplayTarget,
108108
) -> Result<String, DisplaySourceCodeError> {
109109
let db = sema_scope.db;
@@ -380,7 +380,7 @@ impl<'db> Expr<'db> {
380380
fn container_name(
381381
container: AssocItemContainer,
382382
sema_scope: &SemanticsScope<'_>,
383-
cfg: ImportPathConfig,
383+
cfg: FindPathConfig,
384384
edition: Edition,
385385
display_target: DisplayTarget,
386386
) -> Result<String, DisplaySourceCodeError> {

crates/ide-assists/src/assist_config.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
//! module, and we use to statically check that we only produce snippet
55
//! assists if we are allowed to.
66
7-
use hir::ImportPathConfig;
8-
use ide_db::{SnippetCap, assists::ExprFillDefaultMode, imports::insert_use::InsertUseConfig};
7+
use hir::FindPathConfig;
8+
use ide_db::{
9+
SnippetCap,
10+
assists::ExprFillDefaultMode,
11+
imports::{import_assets::ImportPathConfig, insert_use::InsertUseConfig},
12+
};
913

1014
use crate::AssistKind;
1115

@@ -31,7 +35,15 @@ impl AssistConfig {
3135
prefer_no_std: self.prefer_no_std,
3236
prefer_prelude: self.prefer_prelude,
3337
prefer_absolute: self.prefer_absolute,
34-
allow_unstable: true,
38+
}
39+
}
40+
41+
pub fn find_path_confg(&self, allow_unstable: bool) -> FindPathConfig {
42+
FindPathConfig {
43+
prefer_no_std: self.prefer_no_std,
44+
prefer_prelude: self.prefer_prelude,
45+
prefer_absolute: self.prefer_absolute,
46+
allow_unstable,
3547
}
3648
}
3749
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::iter::{self, Peekable};
22

33
use either::Either;
4-
use hir::{Adt, AsAssocItem, Crate, HasAttrs, ImportPathConfig, ModuleDef, Semantics, sym};
4+
use hir::{Adt, AsAssocItem, Crate, FindPathConfig, HasAttrs, ModuleDef, Semantics, sym};
55
use ide_db::RootDatabase;
66
use ide_db::assists::ExprFillDefaultMode;
77
use ide_db::syntax_helpers::suggest_name;
@@ -76,12 +76,11 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
7676
.filter(|pat| !matches!(pat, Pat::WildcardPat(_)))
7777
.collect();
7878

79-
let cfg = ctx.config.import_path_config();
80-
8179
let make = SyntaxFactory::with_mappings();
8280

8381
let scope = ctx.sema.scope(expr.syntax())?;
8482
let module = scope.module();
83+
let cfg = ctx.config.find_path_confg(ctx.sema.is_nightly(scope.krate()));
8584
let self_ty = if ctx.config.prefer_self_ty {
8685
scope
8786
.containing_function()
@@ -498,7 +497,7 @@ fn build_pat(
498497
make: &SyntaxFactory,
499498
module: hir::Module,
500499
var: ExtendedVariant,
501-
cfg: ImportPathConfig,
500+
cfg: FindPathConfig,
502501
) -> Option<ast::Pat> {
503502
let db = ctx.db();
504503
match var {

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,6 @@ fn augment_references_with_imports(
329329
) -> Vec<FileReferenceWithImport> {
330330
let mut visited_modules = FxHashSet::default();
331331

332-
let cfg = ctx.config.import_path_config();
333-
334332
let edition = target_module.krate().edition(ctx.db());
335333
references
336334
.into_iter()
@@ -345,22 +343,27 @@ fn augment_references_with_imports(
345343
{
346344
visited_modules.insert(ref_module);
347345

348-
let import_scope = ImportScope::find_insert_use_container(name.syntax(), &ctx.sema);
349-
let path = ref_module
350-
.find_use_path(
351-
ctx.sema.db,
352-
ModuleDef::Module(*target_module),
353-
ctx.config.insert_use.prefix_kind,
354-
cfg,
355-
)
356-
.map(|mod_path| {
357-
make::path_concat(
358-
mod_path_to_ast(&mod_path, edition),
359-
make::path_from_text("Bool"),
360-
)
361-
});
346+
ImportScope::find_insert_use_container(name.syntax(), &ctx.sema).and_then(
347+
|import_scope| {
348+
let cfg =
349+
ctx.config.find_path_confg(ctx.sema.is_nightly(target_module.krate()));
350+
let path = ref_module
351+
.find_use_path(
352+
ctx.sema.db,
353+
ModuleDef::Module(*target_module),
354+
ctx.config.insert_use.prefix_kind,
355+
cfg,
356+
)
357+
.map(|mod_path| {
358+
make::path_concat(
359+
mod_path_to_ast(&mod_path, edition),
360+
make::path_from_text("Bool"),
361+
)
362+
})?;
362363

363-
import_scope.zip(path)
364+
Some((import_scope, path))
365+
},
366+
)
364367
} else {
365368
None
366369
};

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

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

46-
let cfg = ctx.config.import_path_config();
46+
let cfg = ctx.config.find_path_confg(ctx.sema.is_nightly(module.krate()));
4747

4848
let src_type_path = {
4949
let src_type_path = src_type.syntax().descendants().find_map(ast::Path::cast)?;

0 commit comments

Comments
 (0)