Skip to content

Commit 58401ab

Browse files
authored
Merge pull request #19028 from Veykril/push-vuytpkvqzwzs
fix: Fix flyimport not filtering via stability of import path
2 parents f45b8ad + 0db8d05 commit 58401ab

File tree

21 files changed

+89
-20
lines changed

21 files changed

+89
-20
lines changed

src/tools/rust-analyzer/crates/hir-def/src/find_path.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ fn find_in_dep(
445445
};
446446
cov_mark::hit!(partially_imported);
447447
if info.is_unstable {
448+
if !ctx.cfg.allow_unstable {
449+
// the item is unstable and we are not allowed to use unstable items
450+
continue;
451+
}
448452
choice.stability = Unstable;
449453
}
450454

@@ -670,6 +674,7 @@ mod tests {
670674
prefer_prelude: bool,
671675
prefer_absolute: bool,
672676
prefer_no_std: bool,
677+
allow_unstable: bool,
673678
expect: Expect,
674679
) {
675680
let (db, pos) = TestDB::with_position(ra_fixture);
@@ -711,7 +716,7 @@ mod tests {
711716
module,
712717
prefix,
713718
ignore_local_imports,
714-
ImportPathConfig { prefer_no_std, prefer_prelude, prefer_absolute },
719+
ImportPathConfig { prefer_no_std, prefer_prelude, prefer_absolute, allow_unstable },
715720
);
716721
format_to!(
717722
res,
@@ -732,31 +737,39 @@ mod tests {
732737
path: &str,
733738
expect: Expect,
734739
) {
735-
check_found_path_(ra_fixture, path, false, false, false, expect);
740+
check_found_path_(ra_fixture, path, false, false, false, false, expect);
736741
}
737742

738743
fn check_found_path_prelude(
739744
#[rust_analyzer::rust_fixture] ra_fixture: &str,
740745
path: &str,
741746
expect: Expect,
742747
) {
743-
check_found_path_(ra_fixture, path, true, false, false, expect);
748+
check_found_path_(ra_fixture, path, true, false, false, false, expect);
744749
}
745750

746751
fn check_found_path_absolute(
747752
#[rust_analyzer::rust_fixture] ra_fixture: &str,
748753
path: &str,
749754
expect: Expect,
750755
) {
751-
check_found_path_(ra_fixture, path, false, true, false, expect);
756+
check_found_path_(ra_fixture, path, false, true, false, false, expect);
752757
}
753758

754759
fn check_found_path_prefer_no_std(
755760
#[rust_analyzer::rust_fixture] ra_fixture: &str,
756761
path: &str,
757762
expect: Expect,
758763
) {
759-
check_found_path_(ra_fixture, path, false, false, true, expect);
764+
check_found_path_(ra_fixture, path, false, false, true, false, expect);
765+
}
766+
767+
fn check_found_path_prefer_no_std_allow_unstable(
768+
#[rust_analyzer::rust_fixture] ra_fixture: &str,
769+
path: &str,
770+
expect: Expect,
771+
) {
772+
check_found_path_(ra_fixture, path, false, false, true, true, expect);
760773
}
761774

762775
#[test]
@@ -1951,7 +1964,7 @@ pub mod ops {
19511964

19521965
#[test]
19531966
fn respect_unstable_modules() {
1954-
check_found_path_prefer_no_std(
1967+
check_found_path_prefer_no_std_allow_unstable(
19551968
r#"
19561969
//- /main.rs crate:main deps:std,core
19571970
extern crate std;

src/tools/rust-analyzer/crates/hir-def/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ pub struct ImportPathConfig {
114114
pub prefer_prelude: bool,
115115
/// If true, prefer abs path (starting with `::`) where it is available.
116116
pub prefer_absolute: bool,
117+
/// If true, paths containing `#[unstable]` segments may be returned, but only if if there is no
118+
/// stable path. This does not check, whether the item itself that is being imported is `#[unstable]`.
119+
pub allow_unstable: bool,
117120
}
118121

119122
#[derive(Debug)]

src/tools/rust-analyzer/crates/hir-ty/src/display.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ impl HirDisplay for Ty {
11591159
prefer_no_std: false,
11601160
prefer_prelude: true,
11611161
prefer_absolute: false,
1162+
allow_unstable: true,
11621163
},
11631164
) {
11641165
write!(f, "{}", path.display(f.db.upcast(), f.edition()))?;

src/tools/rust-analyzer/crates/ide-assists/src/assist_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ impl AssistConfig {
2828
prefer_no_std: self.prefer_no_std,
2929
prefer_prelude: self.prefer_prelude,
3030
prefer_absolute: self.prefer_absolute,
31+
allow_unstable: true,
3132
}
3233
}
3334
}

src/tools/rust-analyzer/crates/ide-completion/src/completions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ fn enum_variants_with_paths(
660660
if let Some(path) = ctx.module.find_path(
661661
ctx.db,
662662
hir::ModuleDef::from(variant),
663-
ctx.config.import_path_config(),
663+
ctx.config.import_path_config(ctx.is_nightly),
664664
) {
665665
// Variants with trivial paths are already added by the existing completion logic,
666666
// so we should avoid adding these twice

src/tools/rust-analyzer/crates/ide-completion/src/completions/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub(crate) fn complete_expr_path(
247247
.find_path(
248248
ctx.db,
249249
hir::ModuleDef::from(strukt),
250-
ctx.config.import_path_config(),
250+
ctx.config.import_path_config(ctx.is_nightly),
251251
)
252252
.filter(|it| it.len() > 1);
253253

@@ -269,7 +269,7 @@ pub(crate) fn complete_expr_path(
269269
.find_path(
270270
ctx.db,
271271
hir::ModuleDef::from(un),
272-
ctx.config.import_path_config(),
272+
ctx.config.import_path_config(ctx.is_nightly),
273273
)
274274
.filter(|it| it.len() > 1);
275275

src/tools/rust-analyzer/crates/ide-completion/src/completions/flyimport.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ fn import_on_the_fly(
257257
};
258258
let user_input_lowercased = potential_import_name.to_lowercase();
259259

260-
let import_cfg = ctx.config.import_path_config();
260+
let import_cfg = ctx.config.import_path_config(ctx.is_nightly);
261261

262262
import_assets
263263
.search_for_imports(&ctx.sema, import_cfg, ctx.config.insert_use.prefix_kind)
@@ -316,7 +316,7 @@ fn import_on_the_fly_pat_(
316316
ItemInNs::Values(def) => matches!(def, hir::ModuleDef::Const(_)),
317317
};
318318
let user_input_lowercased = potential_import_name.to_lowercase();
319-
let cfg = ctx.config.import_path_config();
319+
let cfg = ctx.config.import_path_config(ctx.is_nightly);
320320

321321
import_assets
322322
.search_for_imports(&ctx.sema, cfg, ctx.config.insert_use.prefix_kind)
@@ -358,7 +358,7 @@ fn import_on_the_fly_method(
358358

359359
let user_input_lowercased = potential_import_name.to_lowercase();
360360

361-
let cfg = ctx.config.import_path_config();
361+
let cfg = ctx.config.import_path_config(ctx.is_nightly);
362362

363363
import_assets
364364
.search_for_imports(&ctx.sema, cfg, ctx.config.insert_use.prefix_kind)

src/tools/rust-analyzer/crates/ide-completion/src/completions/postfix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(crate) fn complete_postfix(
6060
None => return,
6161
};
6262

63-
let cfg = ctx.config.import_path_config();
63+
let cfg = ctx.config.import_path_config(ctx.is_nightly);
6464

6565
if let Some(drop_trait) = ctx.famous_defs().core_ops_Drop() {
6666
if receiver_ty.impls_trait(ctx.db, drop_trait, &[]) {

src/tools/rust-analyzer/crates/ide-completion/src/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ impl CompletionConfig<'_> {
5959
.flat_map(|snip| snip.prefix_triggers.iter().map(move |trigger| (&**trigger, snip)))
6060
}
6161

62-
pub fn import_path_config(&self) -> ImportPathConfig {
62+
pub fn import_path_config(&self, allow_unstable: bool) -> ImportPathConfig {
6363
ImportPathConfig {
6464
prefer_no_std: self.prefer_no_std,
6565
prefer_prelude: self.prefer_prelude,
6666
prefer_absolute: self.prefer_absolute,
67+
allow_unstable,
6768
}
6869
}
6970
}

src/tools/rust-analyzer/crates/ide-completion/src/context.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,9 @@ pub(crate) struct CompletionContext<'a> {
443443
/// The module of the `scope`.
444444
pub(crate) module: hir::Module,
445445
/// Whether nightly toolchain is used. Cached since this is looked up a lot.
446-
is_nightly: bool,
446+
pub(crate) is_nightly: bool,
447+
/// The edition of the current crate
448+
// FIXME: This should probably be the crate of the current token?
447449
pub(crate) edition: Edition,
448450

449451
/// The expected name of what we are completing.

0 commit comments

Comments
 (0)