aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-06-26 15:37:15 +0100
committerGitHub <[email protected]>2020-06-26 15:37:15 +0100
commit4f60b4f2a32190830215defe6cf81836f34d99c9 (patch)
tree008a5cf106fd62a65b1931cf340cf058a838be5d /crates
parentce06f8d0416d5851264769eb9583ce43d66f0474 (diff)
parent72fe70f2f8aee9556166ba0f984a29d19a485e61 (diff)
Merge #5076
5076: Make VFS join methods fallible r=matklad a=jonas-schievink Co-authored-by: Jonas Schievink <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_def/src/nameres/tests/mod_resolution.rs16
-rw-r--r--crates/rust-analyzer/src/global_state.rs2
-rw-r--r--crates/vfs/src/file_set.rs2
-rw-r--r--crates/vfs/src/vfs_path.rs16
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
@@ -335,6 +335,22 @@ fn module_resolution_relative_path_2() {
335} 335}
336 336
337#[test] 337#[test]
338fn module_resolution_relative_path_outside_root() {
339 let map = def_map(
340 r###"
341 //- /main.rs
342
343 #[path="../../../../../outside.rs"]
344 mod foo;
345 "###,
346 );
347
348 assert_snapshot!(map, @r###"
349 â‹®crate
350 "###);
351}
352
353#[test]
338fn module_resolution_explicit_path_mod_rs_2() { 354fn module_resolution_explicit_path_mod_rs_2() {
339 let map = def_map( 355 let map = def_map(
340 r###" 356 r###"
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs
index 4da094083..f224c69e7 100644
--- a/crates/rust-analyzer/src/global_state.rs
+++ b/crates/rust-analyzer/src/global_state.rs
@@ -209,7 +209,7 @@ impl GlobalStateSnapshot {
209 pub(crate) fn anchored_path(&self, file_id: FileId, path: &str) -> Url { 209 pub(crate) fn anchored_path(&self, file_id: FileId, path: &str) -> Url {
210 let mut base = self.vfs.read().0.file_path(file_id); 210 let mut base = self.vfs.read().0.file_path(file_id);
211 base.pop(); 211 base.pop();
212 let path = base.join(path); 212 let path = base.join(path).unwrap();
213 let path = path.as_path().unwrap(); 213 let path = path.as_path().unwrap();
214 url_from_abs_path(&path) 214 url_from_abs_path(&path)
215 } 215 }
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 {
18 pub fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> { 18 pub fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
19 let mut base = self.paths[&anchor].clone(); 19 let mut base = self.paths[&anchor].clone();
20 base.pop(); 20 base.pop();
21 let path = base.join(path); 21 let path = base.join(path)?;
22 let res = self.files.get(&path).copied(); 22 let res = self.files.get(&path).copied();
23 res 23 res
24 } 24 }
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 {
22 VfsPathRepr::VirtualPath(_) => None, 22 VfsPathRepr::VirtualPath(_) => None,
23 } 23 }
24 } 24 }
25 pub fn join(&self, path: &str) -> VfsPath { 25 pub fn join(&self, path: &str) -> Option<VfsPath> {
26 match &self.0 { 26 match &self.0 {
27 VfsPathRepr::PathBuf(it) => { 27 VfsPathRepr::PathBuf(it) => {
28 let res = it.join(path).normalize(); 28 let res = it.join(path).normalize();
29 VfsPath(VfsPathRepr::PathBuf(res)) 29 Some(VfsPath(VfsPathRepr::PathBuf(res)))
30 } 30 }
31 VfsPathRepr::VirtualPath(it) => { 31 VfsPathRepr::VirtualPath(it) => {
32 let res = it.join(path); 32 let res = it.join(path)?;
33 VfsPath(VfsPathRepr::VirtualPath(res)) 33 Some(VfsPath(VfsPathRepr::VirtualPath(res)))
34 } 34 }
35 } 35 }
36 } 36 }
@@ -101,13 +101,15 @@ impl VirtualPath {
101 self.0 = self.0[..pos].to_string(); 101 self.0 = self.0[..pos].to_string();
102 true 102 true
103 } 103 }
104 fn join(&self, mut path: &str) -> VirtualPath { 104 fn join(&self, mut path: &str) -> Option<VirtualPath> {
105 let mut res = self.clone(); 105 let mut res = self.clone();
106 while path.starts_with("../") { 106 while path.starts_with("../") {
107 assert!(res.pop()); 107 if !res.pop() {
108 return None;
109 }
108 path = &path["../".len()..] 110 path = &path["../".len()..]
109 } 111 }
110 res.0 = format!("{}/{}", res.0, path); 112 res.0 = format!("{}/{}", res.0, path);
111 res 113 Some(res)
112 } 114 }
113} 115}