Skip to content

Commit

Permalink
Handle fully qualified rustc
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Jul 12, 2022
1 parent c785679 commit 9345f26
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
29 changes: 16 additions & 13 deletions driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern crate rustc_lint;
extern crate rustc_session;

use anyhow::{bail, ensure, Result};
use dylint_internal::{env, parse_path_filename};
use dylint_internal::{env, parse_path_filename, rustup::is_rustc};
use std::{
collections::BTreeSet,
ffi::{CString, OsStr},
Expand Down Expand Up @@ -202,20 +202,26 @@ pub fn dylint_driver<T: AsRef<OsStr>>(args: &[T]) -> Result<()> {
return Ok(());
}

// smoelius: By the above check, there are at least two arguments.

if args[1].as_ref() == "rustc" {
run(&args[2..])
} else {
run(&args[1..])
}
run(&args[1..])
}

pub fn run<T: AsRef<OsStr>>(args: &[T]) -> Result<()> {
let rustflags = rustflags();
let paths = paths();

let mut rustc_args = vec!["rustc".to_owned()];
let mut rustc_args = Vec::new();

let mut args = args.iter();

if let Some(arg) = args.next() {
if !is_rustc(arg) {
rustc_args.push("rustc".to_owned());
}
rustc_args.push(arg.as_ref().to_string_lossy().to_string());
} else {
rustc_args.push("rustc".to_owned());
}

if let Ok(sysroot) = sysroot() {
rustc_args.extend(vec![
"--sysroot".to_owned(),
Expand All @@ -229,10 +235,7 @@ pub fn run<T: AsRef<OsStr>>(args: &[T]) -> Result<()> {
bail!("could not parse `{}`", path.to_string_lossy());
}
}
rustc_args.extend(
args.iter()
.map(|s| s.as_ref().to_string_lossy().to_string()),
);
rustc_args.extend(args.map(|s| s.as_ref().to_string_lossy().to_string()));
rustc_args.extend(rustflags);

let mut callbacks = Callbacks::new(paths);
Expand Down
14 changes: 13 additions & 1 deletion internal/src/rustup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{env, Command};
use anyhow::{anyhow, Result};
use std::path::{Path, PathBuf};
use std::{
ffi::OsStr,
path::{Path, PathBuf},
};

pub trait SanitizeEnvironment {
fn sanitize_environment(&mut self) -> &mut Self;
Expand Down Expand Up @@ -44,3 +47,12 @@ pub fn toolchain_path(path: &Path) -> Result<PathBuf> {
.map(Into::into)
.ok_or_else(|| anyhow!("Could not get ancestor"))
}

pub fn is_rustc<T: AsRef<OsStr> + ?Sized>(arg: &T) -> bool {
Path::new(arg).file_name() == Some(OsStr::new("rustc"))
}

#[test]
fn rustc_is_rustc() {
assert!(is_rustc("rustc"));
}
4 changes: 2 additions & 2 deletions utils/testing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{anyhow, ensure, Context, Result};
use cargo_metadata::{Metadata, Package, Target};
use compiletest_rs as compiletest;
use dylint_internal::{env, library_filename};
use dylint_internal::{env, library_filename, rustup::is_rustc};
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use regex::Regex;
Expand Down Expand Up @@ -205,7 +205,7 @@ fn rustc_flags(metadata: &Metadata, package: &Package, target: &Target) -> Resul
.split(' ')
.map(ToOwned::to_owned)
.collect::<Vec<_>>();
if args.first().map(AsRef::as_ref) == Some("rustc")
if args.first().map_or(false, is_rustc)
&& args
.as_slice()
.windows(2)
Expand Down

0 comments on commit 9345f26

Please sign in to comment.