Unnamed repository; edit this file 'description' to name the repository.
fix: prefer `alloc` over `std` paths when `preferNoStd` is set
shulaoda 2 weeks ago
parent b5aa666 · commit 902eb9e
-rw-r--r--crates/hir-def/src/find_path.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/crates/hir-def/src/find_path.rs b/crates/hir-def/src/find_path.rs
index 2f502e4915..daece7fc5d 100644
--- a/crates/hir-def/src/find_path.rs
+++ b/crates/hir-def/src/find_path.rs
@@ -407,6 +407,10 @@ fn find_in_sysroot<'db>(
if matches!(best_choice, Some(Choice { stability: Stable, .. })) {
return;
}
+ search(LangCrateOrigin::Alloc, best_choice);
+ if matches!(best_choice, Some(Choice { stability: Stable, .. })) {
+ return;
+ }
search(LangCrateOrigin::Std, best_choice);
if matches!(best_choice, Some(Choice { stability: Stable, .. })) {
return;
@@ -420,6 +424,10 @@ fn find_in_sysroot<'db>(
if matches!(best_choice, Some(Choice { stability: Stable, .. })) {
return;
}
+ search(LangCrateOrigin::Alloc, best_choice);
+ if matches!(best_choice, Some(Choice { stability: Stable, .. })) {
+ return;
+ }
}
dependencies
.iter()
@@ -1587,6 +1595,43 @@ pub mod sync {
}
#[test]
+ fn prefer_alloc_paths_over_std_with_extern_crate_std() {
+ check_found_path(
+ r#"
+//- /main.rs crate:main deps:alloc,std
+#![no_std]
+
+extern crate alloc;
+
+extern crate std;
+
+$0
+
+//- /std.rs crate:std deps:alloc
+
+pub mod sync {
+ pub use alloc::sync::Arc;
+}
+
+//- /zzz.rs crate:alloc
+
+pub mod sync {
+ pub struct Arc;
+}
+ "#,
+ "alloc::sync::Arc",
+ expect![[r#"
+ Plain (imports ✔): alloc::sync::Arc
+ Plain (imports ✖): alloc::sync::Arc
+ ByCrate(imports ✔): alloc::sync::Arc
+ ByCrate(imports ✖): alloc::sync::Arc
+ BySelf (imports ✔): alloc::sync::Arc
+ BySelf (imports ✖): alloc::sync::Arc
+ "#]],
+ );
+ }
+
+ #[test]
fn prefer_shorter_paths_if_not_alloc() {
check_found_path(
r#"