Repository: rust-analyzer/rust-analyzer
Pull request #3573 (2020-03-13 09:57:34)

Check all crates of the workspace
Previously, if the root of the was was a real crate, only this crate
was checked.

Ideally, we might want some kind of config here (which might be just
overriding the whole command), but `--workspace` is def a nicer
default.

r? @kiljacken 

matklad: I also have a got feeling that we might have a bunch of issues about this....

Patch:
diff --git a/Cargo.lock b/Cargo.lock
index f3707f1162..330bdd1cba 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -940,7 +940,6 @@ dependencies = [
 name = "ra_hir"
 version = "0.1.0"
 dependencies = [
- "arrayvec",
  "either",
  "itertools",
  "log",
diff --git a/crates/ra_cargo_watch/src/lib.rs b/crates/ra_cargo_watch/src/lib.rs
index 94b9c03d0c..1a6926db3b 100644
--- a/crates/ra_cargo_watch/src/lib.rs
+++ b/crates/ra_cargo_watch/src/lib.rs
@@ -254,6 +254,7 @@ impl WatchThread {
     fn new(options: &CheckOptions, workspace_root: &PathBuf) -> WatchThread {
         let mut args: Vec<String> = vec![
             options.command.clone(),
+            "--workspace".to_string(),
             "--message-format=json".to_string(),
             "--manifest-path".to_string(),
             format!("{}/Cargo.toml", workspace_root.to_string_lossy()),
diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml
index 42193b4929..266c4cff38 100644
--- a/crates/ra_hir/Cargo.toml
+++ b/crates/ra_hir/Cargo.toml
@@ -11,7 +11,6 @@ doctest = false
 log = "0.4.8"
 rustc-hash = "1.1.0"
 either = "1.5.3"
-arrayvec = "0.5.1"
 
 itertools = "0.8.2"
 
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 9e2fa03f88..41d4e2ed3e 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -1,7 +1,6 @@
 //! FIXME: write short doc here
 use std::sync::Arc;
 
-use arrayvec::ArrayVec;
 use either::Either;
 use hir_def::{
     adt::StructKind,
@@ -227,11 +226,7 @@ impl Module {
                     Some((name, def))
                 }
             })
-            .flat_map(|(name, def)|
-                ScopeDef::all_items(def)
-                    .into_iter()
-                    .map(move |item| (name.clone(), item))
-            )
+            .map(|(name, def)| (name.clone(), def.into()))
             .collect()
     }
 
@@ -1293,38 +1288,15 @@ pub enum ScopeDef {
     Unknown,
 }
 
-impl ScopeDef {
-    pub fn all_items(def: PerNs) -> ArrayVec<[Self; 3]> {
-        let mut items = ArrayVec::new();
-
-        match (def.take_types(), def.take_values()) {
-            (Some(m1), None) => 
-                items.push(ScopeDef::ModuleDef(m1.into())),
-            (None, Some(m2)) =>
-                items.push(ScopeDef::ModuleDef(m2.into())),
-            (Some(m1), Some(m2)) => {
-                // Some items, like unit structs and enum variants, are
-                // returned as both a type and a value. Here we want
-                // to de-duplicate them.
-                if m1 != m2 {
-                    items.push(ScopeDef::ModuleDef(m1.into()));
-                    items.push(ScopeDef::ModuleDef(m2.into()));
-                } else {
-                    items.push(ScopeDef::ModuleDef(m1.into()));
-                }
-            },
-            (None, None) => {},
-        };
-
-        if let Some(macro_def_id) = def.take_macros() {
-            items.push(ScopeDef::MacroDef(macro_def_id.into()));
-        }
-
-        if items.is_empty() {
-            items.push(ScopeDef::Unknown);
-        }
-
-        items
+impl From<PerNs> for ScopeDef {
+    fn from(def: PerNs) -> Self {
+        def.take_types()
+            .or_else(|| def.take_values())
+            .map(|module_def_id| ScopeDef::ModuleDef(module_def_id.into()))
+            .or_else(|| {
+                def.take_macros().map(|macro_def_id| ScopeDef::MacroDef(macro_def_id.into()))
+            })
+            .unwrap_or(ScopeDef::Unknown)
     }
 }
 
diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs
index e83eb1fdc2..3782a9984b 100644
--- a/crates/ra_hir/src/semantics.rs
+++ b/crates/ra_hir/src/semantics.rs
@@ -344,13 +344,7 @@ impl<'a, DB: HirDatabase> SemanticsScope<'a, DB> {
 
         resolver.process_all_names(self.db, &mut |name, def| {
             let def = match def {
-                resolver::ScopeDef::PerNs(it) => {
-                    let items = ScopeDef::all_items(it);
-                    for item in items {
-                        f(name.clone(), item);
-                    }
-                    return
-                },
+                resolver::ScopeDef::PerNs(it) => it.into(),
                 resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
                 resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
                 resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(TypeParam { id }),
diff --git a/crates/ra_ide/src/completion/complete_path.rs b/crates/ra_ide/src/completion/complete_path.rs
index 648c1d39b2..d588ee3641 100644
--- a/crates/ra_ide/src/completion/complete_path.rs
+++ b/crates/ra_ide/src/completion/complete_path.rs
@@ -967,43 +967,4 @@ mod tests {
         ]
         "###);
     }
-
-    #[test]
-    fn function_mod_share_name() {
-        assert_debug_snapshot!(
-        do_reference_completion(
-                r"
-                fn foo() {
-                    self::m::<|>
-                }
-
-                mod m {
-                    pub mod z {}
-                    pub fn z() {}
-                }
-                ",
-        ),
-            @r###"
-        [
-            CompletionItem {
-                label: "z",
-                source_range: [57; 57),
-                delete: [57; 57),
-                insert: "z",
-                kind: Module,
-            },
-            CompletionItem {
-                label: "z()",
-                source_range: [57; 57),
-                delete: [57; 57),
-                insert: "z()$0",
-                kind: Function,
-                lookup: "z",
-                detail: "pub fn z()",
-            },
-        ]
-        "###
-        );
-    }
-
 }


Test patch:
diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs
index 3f33a75b9a..dda5ed699c 100644
--- a/crates/ra_hir_def/src/nameres/tests.rs
+++ b/crates/ra_hir_def/src/nameres/tests.rs
@@ -101,28 +101,6 @@ fn crate_def_map_super_super() {
     "###)
 }
 
-#[test]
-fn crate_def_map_fn_mod_same_name() {
-    let map = def_map(
-        "
-        //- /lib.rs
-        mod m {
-            pub mod z {}
-            pub fn z() {}
-        }
-        ",
-    );
-    assert_snapshot!(map, @r###"
-        ⋮crate
-        ⋮m: t
-        ⋮
-        ⋮crate::m
-        ⋮z: t v
-        ⋮
-        ⋮crate::m::z
-    "###)
-}
-
 #[test]
 fn bogus_paths() {
     covers!(bogus_paths);