Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2d3c7c2

Browse files
committedDec 2, 2021
Custom code for joining paths no longer needed
Since rust 1.57.0, path.push() works correctly on verbatim paths on Windows. See rust-lang/rust#89270 Signed-off-by: Sean Young <sean@mess.org>
1 parent 4c2733b commit 2d3c7c2

File tree

2 files changed

+8
-101
lines changed

2 files changed

+8
-101
lines changed
 

‎.github/workflows/test.yml

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ jobs:
6161
run: unzip c:\llvm.zip -d c:/
6262
- name: Add LLVM to Path
6363
run: echo "c:\llvm13.0\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8
64+
- name: Rust stable
65+
run: rustup default 1.57.0
6466
# We run clippy on Linux in the lint job above, but this does not check #[cfg(windows)] items
6567
- name: Run cargo clippy
6668
run: cargo clippy --tests --bins -- -D warnings -D clippy::inconsistent-struct-constructor

‎src/file_resolver.rs

+6-101
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::ffi::OsString;
55
use std::fs::File;
66
use std::io;
77
use std::io::{prelude::*, Error, ErrorKind};
8-
use std::path::{Component, Path, PathBuf};
8+
use std::path::{Path, PathBuf};
99
use std::sync::Arc;
1010

1111
pub struct FileResolver {
@@ -138,7 +138,7 @@ impl FileResolver {
138138
if let (Some(mapping), import_path) = import {
139139
if first_part == mapping {
140140
// match!
141-
if let Ok(full_path) = join_fold(import_path, &relpath).canonicalize() {
141+
if let Ok(full_path) = import_path.join(&relpath).canonicalize() {
142142
self.load_file(&full_path)?;
143143
let base = full_path
144144
.parent()
@@ -165,7 +165,7 @@ impl FileResolver {
165165
{
166166
if self.import_paths.is_empty() {
167167
// we have no import paths, resolve by what's in the cache
168-
let full_path = join_fold(base, &path);
168+
let full_path = base.join(&path);
169169
let base = (&full_path.parent())
170170
.expect("path should include filename")
171171
.to_path_buf();
@@ -178,9 +178,9 @@ impl FileResolver {
178178
}
179179

180180
if let (None, import_path) = &self.import_paths[*import_no] {
181-
let import_path = join_fold(import_path, base);
181+
let import_path = import_path.join(base);
182182

183-
if let Ok(full_path) = join_fold(&import_path, &path).canonicalize() {
183+
if let Ok(full_path) = import_path.join(&path).canonicalize() {
184184
self.load_file(&full_path)?;
185185
let base = full_path
186186
.parent()
@@ -218,7 +218,7 @@ impl FileResolver {
218218
let import_no = (i + start_import_no) % self.import_paths.len();
219219

220220
if let (None, import_path) = &self.import_paths[import_no] {
221-
if let Ok(full_path) = join_fold(import_path, &path).canonicalize() {
221+
if let Ok(full_path) = import_path.join(&path).canonicalize() {
222222
let base = full_path
223223
.parent()
224224
.expect("path should include filename")
@@ -275,98 +275,3 @@ impl FileResolver {
275275
(full_line, begin_line, begin_column, size)
276276
}
277277
}
278-
279-
// see https://github.com/rust-lang/rust/pull/89270
280-
fn join_fold(left: &Path, right: &Path) -> PathBuf {
281-
let mut buf = Vec::new();
282-
let mut has_prefix = false;
283-
284-
for c in left.components() {
285-
match c {
286-
Component::Prefix(_) => {
287-
has_prefix = true;
288-
buf.push(c);
289-
}
290-
Component::Normal(_) | Component::RootDir => {
291-
buf.push(c);
292-
}
293-
Component::CurDir => (),
294-
Component::ParentDir => {
295-
if let Some(Component::Normal(_)) = buf.last() {
296-
buf.pop();
297-
} else {
298-
buf.push(c);
299-
}
300-
}
301-
}
302-
}
303-
304-
for c in right.components() {
305-
match c {
306-
Component::Prefix(_) => {
307-
buf = vec![c];
308-
has_prefix = true;
309-
}
310-
Component::RootDir => {
311-
if has_prefix {
312-
buf.push(c);
313-
} else {
314-
buf = vec![c];
315-
}
316-
}
317-
Component::CurDir => (),
318-
Component::ParentDir => match buf.last() {
319-
Some(Component::RootDir) => (),
320-
Some(Component::Prefix(_) | Component::ParentDir) | None => buf.push(c),
321-
_ => {
322-
let _ = buf.pop();
323-
}
324-
},
325-
Component::Normal(_) => {
326-
buf.push(c);
327-
}
328-
}
329-
}
330-
331-
buf.iter().collect()
332-
}
333-
334-
#[test]
335-
#[cfg(not(windows))]
336-
fn test_join() {
337-
let x = join_fold(&PathBuf::from("/foo//"), &PathBuf::from("bar"));
338-
339-
assert_eq!(x.to_string_lossy(), r"/foo/bar");
340-
341-
let x = join_fold(&PathBuf::from("/foo//"), &PathBuf::from("/../bar"));
342-
343-
assert_eq!(x.to_string_lossy(), r"/bar");
344-
}
345-
346-
#[test]
347-
#[cfg(windows)]
348-
fn test_win_join() {
349-
let x = join_fold(&PathBuf::from("/foo//"), &PathBuf::from("bar"));
350-
351-
assert_eq!(x.to_string_lossy(), r"\foo\bar");
352-
353-
let x = join_fold(&PathBuf::from("/foo//"), &PathBuf::from("/../bar"));
354-
355-
assert_eq!(x.to_string_lossy(), r"\bar");
356-
357-
let x = join_fold(&PathBuf::from("C:/foo//"), &PathBuf::from("bar"));
358-
359-
assert_eq!(x.to_string_lossy(), r"C:\foo\bar");
360-
361-
let x = join_fold(&PathBuf::from("C:"), &PathBuf::from("bar/../foo"));
362-
363-
assert_eq!(x.to_string_lossy(), r"C:foo");
364-
365-
let x = join_fold(&PathBuf::from("C:"), &PathBuf::from("/bar/../foo"));
366-
367-
assert_eq!(x.to_string_lossy(), r"C:\foo");
368-
369-
let x = join_fold(&PathBuf::from("C:"), &PathBuf::from("../foo"));
370-
371-
assert_eq!(x.to_string_lossy(), r"C:..\foo");
372-
}

0 commit comments

Comments
 (0)
Failed to load comments.