Skip to content

Commit 58401ab

Browse files
authored
Merge pull request rust-lang#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.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ pub fn resolve_completion_edits(
289289
let new_ast = scope.clone_for_update();
290290
let mut import_insert = TextEdit::builder();
291291

292-
let cfg = config.import_path_config();
292+
let cfg = config.import_path_config(true);
293293

294294
imports.into_iter().for_each(|(full_import_path, imported_name)| {
295295
let items_with_name = items_locator::items_with_name(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ pub(crate) fn render_expr(
301301
.unwrap_or_else(|| String::from("..."))
302302
};
303303

304-
let cfg = ctx.config.import_path_config();
304+
let cfg = ctx.config.import_path_config(ctx.is_nightly);
305305

306306
let label = expr.gen_source_code(&ctx.scope, &mut label_formatter, cfg, ctx.edition).ok()?;
307307

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl Snippet {
164164
}
165165

166166
fn import_edits(ctx: &CompletionContext<'_>, requires: &[ModPath]) -> Option<Vec<LocatedImport>> {
167-
let import_cfg = ctx.config.import_path_config();
167+
let import_cfg = ctx.config.import_path_config(ctx.is_nightly);
168168

169169
let resolve = |import| {
170170
let item = ctx.scope.resolve_mod_path(import).next()?;

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,41 @@ pub struct FooStruct {}
13901390
);
13911391
}
13921392

1393+
#[test]
1394+
fn flyimport_pattern_unstable_path() {
1395+
check(
1396+
r#"
1397+
//- /main.rs crate:main deps:std
1398+
fn function() {
1399+
let foo$0
1400+
}
1401+
//- /std.rs crate:std
1402+
#[unstable]
1403+
pub mod unstable {
1404+
pub struct FooStruct {}
1405+
}
1406+
"#,
1407+
expect![""],
1408+
);
1409+
check(
1410+
r#"
1411+
//- toolchain:nightly
1412+
//- /main.rs crate:main deps:std
1413+
fn function() {
1414+
let foo$0
1415+
}
1416+
//- /std.rs crate:std
1417+
#[unstable]
1418+
pub mod unstable {
1419+
pub struct FooStruct {}
1420+
}
1421+
"#,
1422+
expect![[r#"
1423+
st FooStruct (use std::unstable::FooStruct)
1424+
"#]],
1425+
);
1426+
}
1427+
13931428
#[test]
13941429
fn flyimport_pattern_unstable_item_on_nightly() {
13951430
check(

src/tools/rust-analyzer/crates/ide-db/src/path_transform.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ impl Ctx<'_> {
319319
prefer_no_std: false,
320320
prefer_prelude: true,
321321
prefer_absolute: false,
322+
allow_unstable: true,
322323
};
323324
let found_path = self.target_module.find_path(
324325
self.source_scope.db.upcast(),
@@ -378,6 +379,7 @@ impl Ctx<'_> {
378379
prefer_no_std: false,
379380
prefer_prelude: true,
380381
prefer_absolute: false,
382+
allow_unstable: true,
381383
};
382384
let found_path =
383385
self.target_module.find_path(self.source_scope.db.upcast(), def, cfg)?;
@@ -417,6 +419,7 @@ impl Ctx<'_> {
417419
prefer_no_std: false,
418420
prefer_prelude: true,
419421
prefer_absolute: false,
422+
allow_unstable: true,
420423
};
421424
let found_path = self.target_module.find_path(
422425
self.source_scope.db.upcast(),

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/json_is_not_rust.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ pub(crate) fn json_in_items(
147147
prefer_no_std: config.prefer_no_std,
148148
prefer_prelude: config.prefer_prelude,
149149
prefer_absolute: config.prefer_absolute,
150+
allow_unstable: true,
150151
};
151152

152153
if !scope_has("Serialize") {

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_fields.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
128128
prefer_no_std: ctx.config.prefer_no_std,
129129
prefer_prelude: ctx.config.prefer_prelude,
130130
prefer_absolute: ctx.config.prefer_absolute,
131+
allow_unstable: ctx.is_nightly,
131132
},
132133
)?;
133134

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/typed_hole.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::TypedHole) -> Option<Vec<Assist>
7070
prefer_no_std: ctx.config.prefer_no_std,
7171
prefer_prelude: ctx.config.prefer_prelude,
7272
prefer_absolute: ctx.config.prefer_absolute,
73+
allow_unstable: ctx.is_nightly,
7374
},
7475
ctx.edition,
7576
)

src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ use either::Either;
8383
use hir::{db::ExpandDatabase, diagnostics::AnyDiagnostic, Crate, HirFileId, InFile, Semantics};
8484
use ide_db::{
8585
assists::{Assist, AssistId, AssistKind, AssistResolveStrategy},
86-
base_db::SourceDatabase,
86+
base_db::{ReleaseChannel, SourceDatabase},
8787
generated::lints::{Lint, LintGroup, CLIPPY_LINT_GROUPS, DEFAULT_LINTS, DEFAULT_LINT_GROUPS},
8888
imports::insert_use::InsertUseConfig,
8989
label::Label,
@@ -276,6 +276,7 @@ struct DiagnosticsContext<'a> {
276276
sema: Semantics<'a, RootDatabase>,
277277
resolve: &'a AssistResolveStrategy,
278278
edition: Edition,
279+
is_nightly: bool,
279280
}
280281

281282
impl DiagnosticsContext<'_> {
@@ -368,7 +369,11 @@ pub fn semantic_diagnostics(
368369

369370
let module = sema.file_to_module_def(file_id);
370371

371-
let ctx = DiagnosticsContext { config, sema, resolve, edition: file_id.edition() };
372+
let is_nightly = matches!(
373+
module.and_then(|m| db.toolchain_channel(m.krate().into())),
374+
Some(ReleaseChannel::Nightly) | None
375+
);
376+
let ctx = DiagnosticsContext { config, sema, resolve, edition: file_id.edition(), is_nightly };
372377

373378
let mut diags = Vec::new();
374379
match module {

src/tools/rust-analyzer/crates/ide-ssr/src/matching.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ impl Match {
673673
prefer_no_std: false,
674674
prefer_prelude: true,
675675
prefer_absolute: false,
676+
allow_unstable: true,
676677
};
677678
let mod_path = module.find_path(sema.db, module_def, cfg).ok_or_else(|| {
678679
match_error!("Failed to render template path `{}` at match location")

src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ impl flags::AnalysisStats {
465465
prefer_no_std: false,
466466
prefer_prelude: true,
467467
prefer_absolute: false,
468+
allow_unstable: true,
468469
},
469470
Edition::LATEST,
470471
)

0 commit comments

Comments
 (0)