Repository: rust-analyzer/rust-analyzer
Pull request #1770 (2019-09-05 18:48:34)

rename test file to match impl file


Patch:
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index f69179bf68..dc2e2172b5 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -50,6 +50,7 @@
 mod per_ns;
 mod raw;
 mod collector;
+mod mod_resolution;
 #[cfg(test)]
 mod tests;
 
diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs
index 5d1c429265..d6c7c083dc 100644
--- a/crates/ra_hir/src/nameres/collector.rs
+++ b/crates/ra_hir/src/nameres/collector.rs
@@ -1,9 +1,5 @@
-use std::borrow::Cow;
-use std::sync::Arc;
-
-use ra_db::{FileId, SourceRoot};
-use ra_syntax::{ast, SmolStr};
-use relative_path::RelativePathBuf;
+use ra_db::FileId;
+use ra_syntax::ast;
 use rustc_hash::FxHashMap;
 use test_utils::tested_by;
 
@@ -12,8 +8,10 @@ use crate::{
     ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind},
     name::MACRO_RULES,
     nameres::{
-        diagnostics::DefDiagnostic, raw, CrateDefMap, CrateModuleId, ItemOrMacro, ModuleData,
-        ModuleDef, PerNs, ReachedFixedPoint, Resolution, ResolveMode,
+        diagnostics::DefDiagnostic,
+        mod_resolution::{resolve_submodule, ParentModule},
+        raw, CrateDefMap, CrateModuleId, ItemOrMacro, ModuleData, ModuleDef, PerNs,
+        ReachedFixedPoint, Resolution, ResolveMode,
     },
     AstId, Const, DefDatabase, Enum, Function, HirFileId, MacroDef, Module, Name, Path, Static,
     Struct, Trait, TypeAlias, Union,
@@ -568,7 +566,7 @@ where
                     name,
                     is_root,
                     attr_path.as_ref(),
-                    self.parent_module.as_ref(),
+                    self.parent_module,
                 ) {
                     Ok(file_id) => {
                         let module_id = self.push_child_module(name.clone(), ast_id, Some(file_id));
@@ -679,180 +677,6 @@ fn is_macro_rules(path: &Path) -> bool {
     path.as_ident() == Some(&MACRO_RULES)
 }
 
-fn resolve_submodule(
-    db: &impl DefDatabase,
-    file_id: HirFileId,
-    name: &Name,
-    is_root: bool,
-    attr_path: Option<&SmolStr>,
-    parent_module: Option<&ParentModule>,
-) -> Result<FileId, RelativePathBuf> {
-    let file_id = file_id.original_file(db);
-    let source_root_id = db.file_source_root(file_id);
-    let path = db.file_relative_path(file_id);
-    let root = RelativePathBuf::default();
-    let dir_path = path.parent().unwrap_or(&root);
-    let mod_name = path.file_stem().unwrap_or("unknown");
-
-    let resolve_mode = match (attr_path.filter(|p| !p.is_empty()), parent_module) {
-        (Some(file_path), Some(parent_module)) => {
-            let file_path = normalize_attribute_path(file_path);
-            match parent_module.attribute_path() {
-                Some(parent_module_attr_path) => {
-                    let path = dir_path
-                        .join(format!(
-                            "{}/{}",
-                            normalize_attribute_path(parent_module_attr_path),
-                            file_path
-                        ))
-                        .normalize();
-                    ResolutionMode::InlineModuleWithAttributePath(
-                        InsideInlineModuleMode::WithAttributePath(path),
-                    )
-                }
-                None => {
-                    let path =
-                        dir_path.join(format!("{}/{}", parent_module.name, file_path)).normalize();
-                    ResolutionMode::InsideInlineModule(InsideInlineModuleMode::WithAttributePath(
-                        path,
-                    ))
-                }
-            }
-        }
-        (None, Some(parent_module)) => match parent_module.attribute_path() {
-            Some(parent_module_attr_path) => {
-                let path = dir_path.join(format!(
-                    "{}/{}.rs",
-                    normalize_attribute_path(parent_module_attr_path),
-                    name
-                ));
-                ResolutionMode::InlineModuleWithAttributePath(InsideInlineModuleMode::File(path))
-            }
-            None => {
-                let path = dir_path.join(format!("{}/{}.rs", parent_module.name, name));
-                ResolutionMode::InsideInlineModule(InsideInlineModuleMode::File(path))
-            }
-        },
-        (Some(file_path), None) => {
-            let file_path = normalize_attribute_path(file_path);
-            let path = dir_path.join(file_path.as_ref()).normalize();
-            ResolutionMode::OutOfLine(OutOfLineMode::WithAttributePath(path))
-        }
-        _ => {
-            let is_dir_owner = is_root || mod_name == "mod";
-            if is_dir_owner {
-                let file_mod = dir_path.join(format!("{}.rs", name));
-                let dir_mod = dir_path.join(format!("{}/mod.rs", name));
-                ResolutionMode::OutOfLine(OutOfLineMode::RootOrModRs {
-                    file: file_mod,
-                    directory: dir_mod,
-                })
-            } else {
-                let path = dir_path.join(format!("{}/{}.rs", mod_name, name));
-                ResolutionMode::OutOfLine(OutOfLineMode::FileInDirectory(path))
-            }
-        }
-    };
-
-    resolve_mode.resolve(db.source_root(source_root_id))
-}
-
-fn normalize_attribute_path(file_path: &SmolStr) -> Cow<str> {
-    let current_dir = "./";
-    let windows_path_separator = r#"\"#;
-    let current_dir_normalize = if file_path.starts_with(current_dir) {
-        &file_path[current_dir.len()..]
-    } else {
-        file_path.as_str()
-    };
-    if current_dir_normalize.contains(windows_path_separator) {
-        Cow::Owned(current_dir_normalize.replace(windows_path_separator, "/"))
-    } else {
-        Cow::Borrowed(current_dir_normalize)
-    }
-}
-
-enum OutOfLineMode {
-    RootOrModRs { file: RelativePathBuf, directory: RelativePathBuf },
-    FileInDirectory(RelativePathBuf),
-    WithAttributePath(RelativePathBuf),
-}
-
-impl OutOfLineMode {
-    pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> {
-        match self {
-            OutOfLineMode::RootOrModRs { file, directory } => match source_root.files.get(file) {
-                None => resolve_simple_path(source_root, directory).map_err(|_| file.clone()),
-                file_id => resolve_find_result(file_id, file),
-            },
-            OutOfLineMode::FileInDirectory(path) => resolve_simple_path(source_root, path),
-            OutOfLineMode::WithAttributePath(path) => resolve_simple_path(source_root, path),
-        }
-    }
-}
-
-enum InsideInlineModuleMode {
-    File(RelativePathBuf),
-    WithAttributePath(RelativePathBuf),
-}
-
-impl InsideInlineModuleMode {
-    pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> {
-        match self {
-            InsideInlineModuleMode::File(path) => resolve_simple_path(source_root, path),
-            InsideInlineModuleMode::WithAttributePath(path) => {
-                resolve_simple_path(source_root, path)
-            }
-        }
-    }
-}
-
-enum ResolutionMode {
-    OutOfLine(OutOfLineMode),
-    InsideInlineModule(InsideInlineModuleMode),
-    InlineModuleWithAttributePath(InsideInlineModuleMode),
-}
-
-impl ResolutionMode {
-    pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> {
-        use self::ResolutionMode::*;
-
-        match self {
-            OutOfLine(mode) => mode.resolve(source_root),
-            InsideInlineModule(mode) => mode.resolve(source_root),
-            InlineModuleWithAttributePath(mode) => mode.resolve(source_root),
-        }
-    }
-}
-
-fn resolve_simple_path(
-    source_root: Arc<SourceRoot>,
-    path: &RelativePathBuf,
-) -> Result<FileId, RelativePathBuf> {
-    resolve_find_result(source_root.files.get(path), path)
-}
-
-fn resolve_find_result(
-    file_id: Option<&FileId>,
-    path: &RelativePathBuf,
-) -> Result<FileId, RelativePathBuf> {
-    match file_id {
-        Some(file_id) => Ok(file_id.clone()),
-        None => Err(path.clone()),
-    }
-}
-
-struct ParentModule<'a> {
-    name: &'a Name,
-    attr_path: Option<&'a SmolStr>,
-}
-
-impl<'a> ParentModule<'a> {
-    pub fn attribute_path(&self) -> Option<&SmolStr> {
-        self.attr_path.filter(|p| !p.is_empty())
-    }
-}
-
 #[cfg(test)]
 mod tests {
     use ra_db::SourceDatabase;
diff --git a/crates/ra_hir/src/nameres/mod_resolution.rs b/crates/ra_hir/src/nameres/mod_resolution.rs
new file mode 100644
index 0000000000..94c9946ffd
--- /dev/null
+++ b/crates/ra_hir/src/nameres/mod_resolution.rs
@@ -0,0 +1,182 @@
+use std::{borrow::Cow, sync::Arc};
+
+use ra_db::{FileId, SourceRoot};
+use ra_syntax::SmolStr;
+use relative_path::RelativePathBuf;
+
+use crate::{DefDatabase, HirFileId, Name};
+
+#[derive(Clone, Copy)]
+pub(super) struct ParentModule<'a> {
+    pub(super) name: &'a Name,
+    pub(super) attr_path: Option<&'a SmolStr>,
+}
+
+impl<'a> ParentModule<'a> {
+    fn attribute_path(&self) -> Option<&SmolStr> {
+        self.attr_path.filter(|p| !p.is_empty())
+    }
+}
+
+pub(super) fn resolve_submodule(
+    db: &impl DefDatabase,
+    file_id: HirFileId,
+    name: &Name,
+    is_root: bool,
+    attr_path: Option<&SmolStr>,
+    parent_module: Option<ParentModule<'_>>,
+) -> Result<FileId, RelativePathBuf> {
+    let file_id = file_id.original_file(db);
+    let source_root_id = db.file_source_root(file_id);
+    let path = db.file_relative_path(file_id);
+    let root = RelativePathBuf::default();
+    let dir_path = path.parent().unwrap_or(&root);
+    let mod_name = path.file_stem().unwrap_or("unknown");
+
+    let resolve_mode = match (attr_path.filter(|p| !p.is_empty()), parent_module) {
+        (Some(file_path), Some(parent_module)) => {
+            let file_path = normalize_attribute_path(file_path);
+            match parent_module.attribute_path() {
+                Some(parent_module_attr_path) => {
+                    let path = dir_path
+                        .join(format!(
+                            "{}/{}",
+                            normalize_attribute_path(parent_module_attr_path),
+                            file_path
+                        ))
+                        .normalize();
+                    ResolutionMode::InlineModuleWithAttributePath(
+                        InsideInlineModuleMode::WithAttributePath(path),
+                    )
+                }
+                None => {
+                    let path =
+                        dir_path.join(format!("{}/{}", parent_module.name, file_path)).normalize();
+                    ResolutionMode::InsideInlineModule(InsideInlineModuleMode::WithAttributePath(
+                        path,
+                    ))
+                }
+            }
+        }
+        (None, Some(parent_module)) => match parent_module.attribute_path() {
+            Some(parent_module_attr_path) => {
+                let path = dir_path.join(format!(
+                    "{}/{}.rs",
+                    normalize_attribute_path(parent_module_attr_path),
+                    name
+                ));
+                ResolutionMode::InlineModuleWithAttributePath(InsideInlineModuleMode::File(path))
+            }
+            None => {
+                let path = dir_path.join(format!("{}/{}.rs", parent_module.name, name));
+                ResolutionMode::InsideInlineModule(InsideInlineModuleMode::File(path))
+            }
+        },
+        (Some(file_path), None) => {
+            let file_path = normalize_attribute_path(file_path);
+            let path = dir_path.join(file_path.as_ref()).normalize();
+            ResolutionMode::OutOfLine(OutOfLineMode::WithAttributePath(path))
+        }
+        _ => {
+            let is_dir_owner = is_root || mod_name == "mod";
+            if is_dir_owner {
+                let file_mod = dir_path.join(format!("{}.rs", name));
+                let dir_mod = dir_path.join(format!("{}/mod.rs", name));
+                ResolutionMode::OutOfLine(OutOfLineMode::RootOrModRs {
+                    file: file_mod,
+                    directory: dir_mod,
+                })
+            } else {
+                let path = dir_path.join(format!("{}/{}.rs", mod_name, name));
+                ResolutionMode::OutOfLine(OutOfLineMode::FileInDirectory(path))
+            }
+        }
+    };
+
+    resolve_mode.resolve(db.source_root(source_root_id))
+}
+
+fn normalize_attribute_path(file_path: &SmolStr) -> Cow<str> {
+    let current_dir = "./";
+    let windows_path_separator = r#"\"#;
+    let current_dir_normalize = if file_path.starts_with(current_dir) {
+        &file_path[current_dir.len()..]
+    } else {
+        file_path.as_str()
+    };
+    if current_dir_normalize.contains(windows_path_separator) {
+        Cow::Owned(current_dir_normalize.replace(windows_path_separator, "/"))
+    } else {
+        Cow::Borrowed(current_dir_normalize)
+    }
+}
+
+enum OutOfLineMode {
+    RootOrModRs { file: RelativePathBuf, directory: RelativePathBuf },
+    FileInDirectory(RelativePathBuf),
+    WithAttributePath(RelativePathBuf),
+}
+
+impl OutOfLineMode {
+    pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> {
+        match self {
+            OutOfLineMode::RootOrModRs { file, directory } => match source_root.files.get(file) {
+                None => resolve_simple_path(source_root, directory).map_err(|_| file.clone()),
+                file_id => resolve_find_result(file_id, file),
+            },
+            OutOfLineMode::FileInDirectory(path) => resolve_simple_path(source_root, path),
+            OutOfLineMode::WithAttributePath(path) => resolve_simple_path(source_root, path),
+        }
+    }
+}
+
+enum InsideInlineModuleMode {
+    File(RelativePathBuf),
+    WithAttributePath(RelativePathBuf),
+}
+
+impl InsideInlineModuleMode {
+    pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> {
+        match self {
+            InsideInlineModuleMode::File(path) => resolve_simple_path(source_root, path),
+            InsideInlineModuleMode::WithAttributePath(path) => {
+                resolve_simple_path(source_root, path)
+            }
+        }
+    }
+}
+
+enum ResolutionMode {
+    OutOfLine(OutOfLineMode),
+    InsideInlineModule(InsideInlineModuleMode),
+    InlineModuleWithAttributePath(InsideInlineModuleMode),
+}
+
+impl ResolutionMode {
+    pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> {
+        use self::ResolutionMode::*;
+
+        match self {
+            OutOfLine(mode) => mode.resolve(source_root),
+            InsideInlineModule(mode) => mode.resolve(source_root),
+            InlineModuleWithAttributePath(mode) => mode.resolve(source_root),
+        }
+    }
+}
+
+fn resolve_simple_path(
+    source_root: Arc<SourceRoot>,
+    path: &RelativePathBuf,
+) -> Result<FileId, RelativePathBuf> {
+    resolve_find_result(source_root.files.get(path), path)
+}
+
+fn resolve_find_result(
+    file_id: Option<&FileId>,
+    path: &RelativePathBuf,
+) -> Result<FileId, RelativePathBuf> {
+    match file_id {
+        Some(file_id) => Ok(file_id.clone()),
+        None => Err(path.clone()),
+    }
+}
diff --git a/crates/ra_hir/src/nameres/tests/mods.rs b/crates/ra_hir/src/nameres/tests/mod_resolution.rs
similarity index 100%
rename from crates/ra_hir/src/nameres/tests/mods.rs
rename to crates/ra_hir/src/nameres/tests/mod_resolution.rs
diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs
index b4327b78f4..6d426206e9 100644
--- a/crates/ra_parser/src/grammar/items.rs
+++ b/crates/ra_parser/src/grammar/items.rs
@@ -31,7 +31,7 @@ pub(super) enum ItemFlavor {
 
 pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![
     FN_KW, STRUCT_KW, ENUM_KW, IMPL_KW, TRAIT_KW, CONST_KW, STATIC_KW, LET_KW, MOD_KW, PUB_KW,
-    CRATE_KW, USE_KW
+    CRATE_KW
 ];
 
 pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) {
diff --git a/crates/ra_parser/src/grammar/items/use_item.rs b/crates/ra_parser/src/grammar/items/use_item.rs
index 83a65e2265..c0c7d0ec6f 100644
--- a/crates/ra_parser/src/grammar/items/use_item.rs
+++ b/crates/ra_parser/src/grammar/items/use_item.rs
@@ -101,10 +101,7 @@ fn use_tree(p: &mut Parser) {
         }
         _ => {
             m.abandon(p);
-            p.err_recover(
-                "expected one of `*`, `::`, `{`, `self`, `super` or an identifier",
-                ITEM_RECOVERY_SET,
-            );
+            p.err_and_bump("expected one of `*`, `::`, `{`, `self`, `super` or an indentifier");
             return;
         }
     }


Test patch:
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs
index c1dbad283a..4ff897ca5e 100644
--- a/crates/ra_hir/src/nameres/tests.rs
+++ b/crates/ra_hir/src/nameres/tests.rs
@@ -2,7 +2,7 @@ mod macros;
 mod globs;
 mod incremental;
 mod primitives;
-mod mods;
+mod mod_resolution;
 
 use std::sync::Arc;
 
diff --git a/crates/ra_syntax/test_data/parser/err/0002_duplicate_shebang.txt b/crates/ra_syntax/test_data/parser/err/0002_duplicate_shebang.txt
index bdb5fa6c5a..84867026f4 100644
--- a/crates/ra_syntax/test_data/parser/err/0002_duplicate_shebang.txt
+++ b/crates/ra_syntax/test_data/parser/err/0002_duplicate_shebang.txt
@@ -28,7 +28,7 @@ SOURCE_FILE@[0; 42)
   WHITESPACE@[41; 42) "\n"
 error 23: expected `[`
 error 23: expected an item
-error 27: expected one of `*`, `::`, `{`, `self`, `super` or an identifier
+error 27: expected one of `*`, `::`, `{`, `self`, `super` or an indentifier
 error 28: expected SEMI
 error 31: expected EXCL
 error 31: expected `{`, `[`, `(`
diff --git a/crates/ra_syntax/test_data/parser/err/0035_use_recover.rs b/crates/ra_syntax/test_data/parser/err/0035_use_recover.rs
deleted file mode 100644
index 4a26681262..0000000000
--- a/crates/ra_syntax/test_data/parser/err/0035_use_recover.rs
+++ /dev/null
@@ -1,5 +0,0 @@
-use foo::bar;
-use
-use crate::baz;
-use
-fn f() {}
diff --git a/crates/ra_syntax/test_data/parser/err/0035_use_recover.txt b/crates/ra_syntax/test_data/parser/err/0035_use_recover.txt
deleted file mode 100644
index 6368408288..0000000000
--- a/crates/ra_syntax/test_data/parser/err/0035_use_recover.txt
+++ /dev/null
@@ -1,54 +0,0 @@
-SOURCE_FILE@[0; 48)
-  USE_ITEM@[0; 13)
-    USE_KW@[0; 3) "use"
-    WHITESPACE@[3; 4) " "
-    USE_TREE@[4; 12)
-      PATH@[4; 12)
-        PATH@[4; 7)
-          PATH_SEGMENT@[4; 7)
-            NAME_REF@[4; 7)
-              IDENT@[4; 7) "foo"
-        COLONCOLON@[7; 9) "::"
-        PATH_SEGMENT@[9; 12)
-          NAME_REF@[9; 12)
-            IDENT@[9; 12) "bar"
-    SEMI@[12; 13) ";"
-  WHITESPACE@[13; 14) "\n"
-  USE_ITEM@[14; 17)
-    USE_KW@[14; 17) "use"
-  WHITESPACE@[17; 18) "\n"
-  USE_ITEM@[18; 33)
-    USE_KW@[18; 21) "use"
-    WHITESPACE@[21; 22) " "
-    USE_TREE@[22; 32)
-      PATH@[22; 32)
-        PATH@[22; 27)
-          PATH_SEGMENT@[22; 27)
-            CRATE_KW@[22; 27) "crate"
-        COLONCOLON@[27; 29) "::"
-        PATH_SEGMENT@[29; 32)
-          NAME_REF@[29; 32)
-            IDENT@[29; 32) "baz"
-    SEMI@[32; 33) ";"
-  WHITESPACE@[33; 34) "\n"
-  USE_ITEM@[34; 37)
-    USE_KW@[34; 37) "use"
-  WHITESPACE@[37; 38) "\n"
-  FN_DEF@[38; 47)
-    FN_KW@[38; 40) "fn"
-    WHITESPACE@[40; 41) " "
-    NAME@[41; 42)
-      IDENT@[41; 42) "f"
-    PARAM_LIST@[42; 44)
-      L_PAREN@[42; 43) "("
-      R_PAREN@[43; 44) ")"
-    WHITESPACE@[44; 45) " "
-    BLOCK_EXPR@[45; 47)
-      BLOCK@[45; 47)
-        L_CURLY@[45; 46) "{"
-        R_CURLY@[46; 47) "}"
-  WHITESPACE@[47; 48) "\n"
-error 17: expected one of `*`, `::`, `{`, `self`, `super` or an identifier
-error 17: expected SEMI
-error 37: expected one of `*`, `::`, `{`, `self`, `super` or an identifier
-error 37: expected SEMI