From 72fe70f2f8aee9556166ba0f984a29d19a485e61 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 26 Jun 2020 16:25:08 +0200 Subject: Make VFS join methods fallible --- crates/ra_hir_def/src/nameres/tests/mod_resolution.rs | 16 ++++++++++++++++ crates/rust-analyzer/src/global_state.rs | 2 +- crates/vfs/src/file_set.rs | 2 +- crates/vfs/src/vfs_path.rs | 16 +++++++++------- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs index e9a5e4cba..753684201 100644 --- a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs @@ -334,6 +334,22 @@ fn module_resolution_relative_path_2() { "###); } +#[test] +fn module_resolution_relative_path_outside_root() { + let map = def_map( + r###" + //- /main.rs + + #[path="../../../../../outside.rs"] + mod foo; + "###, + ); + + assert_snapshot!(map, @r###" + â‹®crate + "###); +} + #[test] fn module_resolution_explicit_path_mod_rs_2() { let map = def_map( diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 7533bb319..28daec5a3 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -213,7 +213,7 @@ impl GlobalStateSnapshot { pub(crate) fn anchored_path(&self, file_id: FileId, path: &str) -> Url { let mut base = self.vfs.read().0.file_path(file_id); base.pop(); - let path = base.join(path); + let path = base.join(path).unwrap(); let path = path.as_path().unwrap(); url_from_abs_path(&path) } diff --git a/crates/vfs/src/file_set.rs b/crates/vfs/src/file_set.rs index 0173f7464..d0ddeafe7 100644 --- a/crates/vfs/src/file_set.rs +++ b/crates/vfs/src/file_set.rs @@ -18,7 +18,7 @@ impl FileSet { pub fn resolve_path(&self, anchor: FileId, path: &str) -> Option { let mut base = self.paths[&anchor].clone(); base.pop(); - let path = base.join(path); + let path = base.join(path)?; let res = self.files.get(&path).copied(); res } diff --git a/crates/vfs/src/vfs_path.rs b/crates/vfs/src/vfs_path.rs index 940f91d0e..dc3031ada 100644 --- a/crates/vfs/src/vfs_path.rs +++ b/crates/vfs/src/vfs_path.rs @@ -22,15 +22,15 @@ impl VfsPath { VfsPathRepr::VirtualPath(_) => None, } } - pub fn join(&self, path: &str) -> VfsPath { + pub fn join(&self, path: &str) -> Option { match &self.0 { VfsPathRepr::PathBuf(it) => { let res = it.join(path).normalize(); - VfsPath(VfsPathRepr::PathBuf(res)) + Some(VfsPath(VfsPathRepr::PathBuf(res))) } VfsPathRepr::VirtualPath(it) => { - let res = it.join(path); - VfsPath(VfsPathRepr::VirtualPath(res)) + let res = it.join(path)?; + Some(VfsPath(VfsPathRepr::VirtualPath(res))) } } } @@ -101,13 +101,15 @@ impl VirtualPath { self.0 = self.0[..pos].to_string(); true } - fn join(&self, mut path: &str) -> VirtualPath { + fn join(&self, mut path: &str) -> Option { let mut res = self.clone(); while path.starts_with("../") { - assert!(res.pop()); + if !res.pop() { + return None; + } path = &path["../".len()..] } res.0 = format!("{}/{}", res.0, path); - res + Some(res) } } -- cgit v1.2.3