5076: Make VFS join methods fallible r=matklad a=jonas-schievink



Co-authored-by: Jonas Schievink <jonas.schievink@ferrous-systems.com>
This commit is contained in:
bors[bot]
2020-06-26 14:37:15 +00:00
committed by GitHub
4 changed files with 27 additions and 9 deletions
@@ -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(
+1 -1
View File
@@ -209,7 +209,7 @@ pub(crate) fn file_line_endings(&self, id: FileId) -> LineEndings {
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)
}
+1 -1
View File
@@ -18,7 +18,7 @@ impl FileSet {
pub fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
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
}
+9 -7
View File
@@ -22,15 +22,15 @@ pub fn as_path(&self) -> Option<&AbsPath> {
VfsPathRepr::VirtualPath(_) => None,
}
}
pub fn join(&self, path: &str) -> VfsPath {
pub fn join(&self, path: &str) -> Option<VfsPath> {
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 @@ fn pop(&mut self) -> bool {
self.0 = self.0[..pos].to_string();
true
}
fn join(&self, mut path: &str) -> VirtualPath {
fn join(&self, mut path: &str) -> Option<VirtualPath> {
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)
}
}