Skip to content

Commit

Permalink
Custom code for joining paths no longer needed
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
seanyoung committed Dec 2, 2021
1 parent 4c2733b commit 3920e70
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 105 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ jobs:
run: unzip c:\llvm.zip -d c:/
- name: Add LLVM to Path
run: echo "c:\llvm13.0\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8
- name: Stable with rustfmt and clippy
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: clippy
# We run clippy on Linux in the lint job above, but this does not check #[cfg(windows)] items
- name: Run cargo clippy
run: cargo clippy --tests --bins -- -D warnings -D clippy::inconsistent-struct-constructor
Expand Down Expand Up @@ -122,17 +128,17 @@ jobs:
name: solang-mac-intel
path: ./target/debug/solang

container:
name: Container
image:
name: Container Image
runs-on: ubuntu-20.04
steps:
- name: Checkout sources
uses: actions/checkout@v2
with:
# Make sure "git describe --tags" works for solang --version
fetch-depth: 0
- run: |
docker build . -t ghcr.io/${GITHUB_REPOSITORY}:latest \
- run:
docker build . -t ghcr.io/${GITHUB_REPOSITORY}:latest
--label org.opencontainers.image.description="Solidity Compiler for Solana, Substrate, and ewasm version $(git describe --tags)"
- name: Push to github container registry
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
Expand Down
107 changes: 6 additions & 101 deletions src/file_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::ffi::OsString;
use std::fs::File;
use std::io;
use std::io::{prelude::*, Error, ErrorKind};
use std::path::{Component, Path, PathBuf};
use std::path::{Path, PathBuf};
use std::sync::Arc;

pub struct FileResolver {
Expand Down Expand Up @@ -138,7 +138,7 @@ impl FileResolver {
if let (Some(mapping), import_path) = import {
if first_part == mapping {
// match!
if let Ok(full_path) = join_fold(import_path, &relpath).canonicalize() {
if let Ok(full_path) = import_path.join(&relpath).canonicalize() {
self.load_file(&full_path)?;
let base = full_path
.parent()
Expand All @@ -165,7 +165,7 @@ impl FileResolver {
{
if self.import_paths.is_empty() {
// we have no import paths, resolve by what's in the cache
let full_path = join_fold(base, &path);
let full_path = base.join(&path);
let base = (&full_path.parent())
.expect("path should include filename")
.to_path_buf();
Expand All @@ -178,9 +178,9 @@ impl FileResolver {
}

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

if let Ok(full_path) = join_fold(&import_path, &path).canonicalize() {
if let Ok(full_path) = import_path.join(&path).canonicalize() {
self.load_file(&full_path)?;
let base = full_path
.parent()
Expand Down Expand Up @@ -218,7 +218,7 @@ impl FileResolver {
let import_no = (i + start_import_no) % self.import_paths.len();

if let (None, import_path) = &self.import_paths[import_no] {
if let Ok(full_path) = join_fold(import_path, &path).canonicalize() {
if let Ok(full_path) = import_path.join(&path).canonicalize() {
let base = full_path
.parent()
.expect("path should include filename")
Expand Down Expand Up @@ -275,98 +275,3 @@ impl FileResolver {
(full_line, begin_line, begin_column, size)
}
}

// see https://github.com/rust-lang/rust/pull/89270
fn join_fold(left: &Path, right: &Path) -> PathBuf {
let mut buf = Vec::new();
let mut has_prefix = false;

for c in left.components() {
match c {
Component::Prefix(_) => {
has_prefix = true;
buf.push(c);
}
Component::Normal(_) | Component::RootDir => {
buf.push(c);
}
Component::CurDir => (),
Component::ParentDir => {
if let Some(Component::Normal(_)) = buf.last() {
buf.pop();
} else {
buf.push(c);
}
}
}
}

for c in right.components() {
match c {
Component::Prefix(_) => {
buf = vec![c];
has_prefix = true;
}
Component::RootDir => {
if has_prefix {
buf.push(c);
} else {
buf = vec![c];
}
}
Component::CurDir => (),
Component::ParentDir => match buf.last() {
Some(Component::RootDir) => (),
Some(Component::Prefix(_) | Component::ParentDir) | None => buf.push(c),
_ => {
let _ = buf.pop();
}
},
Component::Normal(_) => {
buf.push(c);
}
}
}

buf.iter().collect()
}

#[test]
#[cfg(not(windows))]
fn test_join() {
let x = join_fold(&PathBuf::from("/foo//"), &PathBuf::from("bar"));

assert_eq!(x.to_string_lossy(), r"/foo/bar");

let x = join_fold(&PathBuf::from("/foo//"), &PathBuf::from("/../bar"));

assert_eq!(x.to_string_lossy(), r"/bar");
}

#[test]
#[cfg(windows)]
fn test_win_join() {
let x = join_fold(&PathBuf::from("/foo//"), &PathBuf::from("bar"));

assert_eq!(x.to_string_lossy(), r"\foo\bar");

let x = join_fold(&PathBuf::from("/foo//"), &PathBuf::from("/../bar"));

assert_eq!(x.to_string_lossy(), r"\bar");

let x = join_fold(&PathBuf::from("C:/foo//"), &PathBuf::from("bar"));

assert_eq!(x.to_string_lossy(), r"C:\foo\bar");

let x = join_fold(&PathBuf::from("C:"), &PathBuf::from("bar/../foo"));

assert_eq!(x.to_string_lossy(), r"C:foo");

let x = join_fold(&PathBuf::from("C:"), &PathBuf::from("/bar/../foo"));

assert_eq!(x.to_string_lossy(), r"C:\foo");

let x = join_fold(&PathBuf::from("C:"), &PathBuf::from("../foo"));

assert_eq!(x.to_string_lossy(), r"C:..\foo");
}

0 comments on commit 3920e70

Please sign in to comment.