Skip to content

Commit 17c368e

Browse files
committed
compiletest: consistently use {Utf8Path,Utf8PathBuf}
Since compiletest already assumes UTF-8 paths and does not try to be robust against non-UTF-8 paths.
1 parent 7c5de64 commit 17c368e

20 files changed

+402
-432
lines changed

Diff for: src/tools/compiletest/src/common.rs

+41-30
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
use std::collections::{BTreeSet, HashMap, HashSet};
2-
use std::ffi::OsString;
3-
use std::path::{Path, PathBuf};
42
use std::process::Command;
53
use std::str::FromStr;
64
use std::sync::OnceLock;
75
use std::{fmt, iter};
86

97
use build_helper::git::GitConfig;
8+
use camino::{Utf8Path, Utf8PathBuf};
109
use semver::Version;
1110
use serde::de::{Deserialize, Deserializer, Error as _};
1211

1312
pub use self::Mode::*;
1413
use crate::executor::{ColorConfig, OutputFormat};
15-
use crate::util::{PathBufExt, add_dylib_path};
14+
use crate::util::{Utf8PathBufExt, add_dylib_path};
1615

1716
macro_rules! string_enum {
1817
($(#[$meta:meta])* $vis:vis enum $name:ident { $($variant:ident => $repr:expr,)* }) => {
@@ -183,25 +182,25 @@ pub struct Config {
183182
pub fail_fast: bool,
184183

185184
/// The library paths required for running the compiler.
186-
pub compile_lib_path: PathBuf,
185+
pub compile_lib_path: Utf8PathBuf,
187186

188187
/// The library paths required for running compiled programs.
189-
pub run_lib_path: PathBuf,
188+
pub run_lib_path: Utf8PathBuf,
190189

191190
/// The rustc executable.
192-
pub rustc_path: PathBuf,
191+
pub rustc_path: Utf8PathBuf,
193192

194193
/// The cargo executable.
195-
pub cargo_path: Option<PathBuf>,
194+
pub cargo_path: Option<Utf8PathBuf>,
196195

197196
/// Rustc executable used to compile run-make recipes.
198-
pub stage0_rustc_path: Option<PathBuf>,
197+
pub stage0_rustc_path: Option<Utf8PathBuf>,
199198

200199
/// The rustdoc executable.
201-
pub rustdoc_path: Option<PathBuf>,
200+
pub rustdoc_path: Option<Utf8PathBuf>,
202201

203202
/// The coverage-dump executable.
204-
pub coverage_dump_path: Option<PathBuf>,
203+
pub coverage_dump_path: Option<Utf8PathBuf>,
205204

206205
/// The Python executable to use for LLDB and htmldocck.
207206
pub python: String,
@@ -213,27 +212,27 @@ pub struct Config {
213212
pub jsondoclint_path: Option<String>,
214213

215214
/// The LLVM `FileCheck` binary path.
216-
pub llvm_filecheck: Option<PathBuf>,
215+
pub llvm_filecheck: Option<Utf8PathBuf>,
217216

218217
/// Path to LLVM's bin directory.
219-
pub llvm_bin_dir: Option<PathBuf>,
218+
pub llvm_bin_dir: Option<Utf8PathBuf>,
220219

221220
/// The path to the Clang executable to run Clang-based tests with. If
222221
/// `None` then these tests will be ignored.
223222
pub run_clang_based_tests_with: Option<String>,
224223

225224
/// The directory containing the sources.
226-
pub src_root: PathBuf,
225+
pub src_root: Utf8PathBuf,
227226
/// The directory containing the test suite sources. Must be a subdirectory of `src_root`.
228-
pub src_test_suite_root: PathBuf,
227+
pub src_test_suite_root: Utf8PathBuf,
229228

230229
/// Root build directory (e.g. `build/`).
231-
pub build_root: PathBuf,
230+
pub build_root: Utf8PathBuf,
232231
/// Test suite specific build directory (e.g. `build/host/test/ui/`).
233-
pub build_test_suite_root: PathBuf,
232+
pub build_test_suite_root: Utf8PathBuf,
234233

235234
/// The directory containing the compiler sysroot
236-
pub sysroot_base: PathBuf,
235+
pub sysroot_base: Utf8PathBuf,
237236

238237
/// The number of the stage under test.
239238
pub stage: u32,
@@ -301,7 +300,7 @@ pub struct Config {
301300
pub host: String,
302301

303302
/// Path to / name of the Microsoft Console Debugger (CDB) executable
304-
pub cdb: Option<OsString>,
303+
pub cdb: Option<Utf8PathBuf>,
305304

306305
/// Version of CDB
307306
pub cdb_version: Option<[u16; 4]>,
@@ -322,7 +321,7 @@ pub struct Config {
322321
pub system_llvm: bool,
323322

324323
/// Path to the android tools
325-
pub android_cross_path: PathBuf,
324+
pub android_cross_path: Utf8PathBuf,
326325

327326
/// Extra parameter to run adb on arm-linux-androideabi
328327
pub adb_path: String,
@@ -346,7 +345,7 @@ pub struct Config {
346345
pub color: ColorConfig,
347346

348347
/// where to find the remote test client process, if we're using it
349-
pub remote_test_client: Option<PathBuf>,
348+
pub remote_test_client: Option<Utf8PathBuf>,
350349

351350
/// mode describing what file the actual ui output will be compared to
352351
pub compare_mode: Option<CompareMode>,
@@ -414,7 +413,7 @@ pub struct Config {
414413
/// Path to minicore aux library, used for `no_core` tests that need `core` stubs in
415414
/// cross-compilation scenarios that do not otherwise want/need to `-Zbuild-std`. Used in e.g.
416415
/// ABI tests.
417-
pub minicore_path: PathBuf,
416+
pub minicore_path: Utf8PathBuf,
418417
}
419418

420419
impl Config {
@@ -804,8 +803,8 @@ fn serde_parse_u32<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u32, D:
804803

805804
#[derive(Debug, Clone)]
806805
pub struct TestPaths {
807-
pub file: PathBuf, // e.g., compile-test/foo/bar/baz.rs
808-
pub relative_dir: PathBuf, // e.g., foo/bar
806+
pub file: Utf8PathBuf, // e.g., compile-test/foo/bar/baz.rs
807+
pub relative_dir: Utf8PathBuf, // e.g., foo/bar
809808
}
810809

811810
/// Used by `ui` tests to generate things like `foo.stderr` from `foo.rs`.
@@ -814,7 +813,7 @@ pub fn expected_output_path(
814813
revision: Option<&str>,
815814
compare_mode: &Option<CompareMode>,
816815
kind: &str,
817-
) -> PathBuf {
816+
) -> Utf8PathBuf {
818817
assert!(UI_EXTENSIONS.contains(&kind));
819818
let mut parts = Vec::new();
820819

@@ -865,7 +864,7 @@ pub const UI_COVERAGE_MAP: &str = "cov-map";
865864
/// ```
866865
///
867866
/// This is created early when tests are collected to avoid race conditions.
868-
pub fn output_relative_path(config: &Config, relative_dir: &Path) -> PathBuf {
867+
pub fn output_relative_path(config: &Config, relative_dir: &Utf8Path) -> Utf8PathBuf {
869868
config.build_test_suite_root.join(relative_dir)
870869
}
871870

@@ -874,10 +873,10 @@ pub fn output_testname_unique(
874873
config: &Config,
875874
testpaths: &TestPaths,
876875
revision: Option<&str>,
877-
) -> PathBuf {
876+
) -> Utf8PathBuf {
878877
let mode = config.compare_mode.as_ref().map_or("", |m| m.to_str());
879878
let debugger = config.debugger.as_ref().map_or("", |m| m.to_str());
880-
PathBuf::from(&testpaths.file.file_stem().unwrap())
879+
Utf8PathBuf::from(&testpaths.file.file_stem().unwrap())
881880
.with_extra_extension(config.mode.output_dir_disambiguator())
882881
.with_extra_extension(revision.unwrap_or(""))
883882
.with_extra_extension(mode)
@@ -887,20 +886,32 @@ pub fn output_testname_unique(
887886
/// Absolute path to the directory where all output for the given
888887
/// test/revision should reside. Example:
889888
/// /path/to/build/host-tuple/test/ui/relative/testname.revision.mode/
890-
pub fn output_base_dir(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
889+
pub fn output_base_dir(
890+
config: &Config,
891+
testpaths: &TestPaths,
892+
revision: Option<&str>,
893+
) -> Utf8PathBuf {
891894
output_relative_path(config, &testpaths.relative_dir)
892895
.join(output_testname_unique(config, testpaths, revision))
893896
}
894897

895898
/// Absolute path to the base filename used as output for the given
896899
/// test/revision. Example:
897900
/// /path/to/build/host-tuple/test/ui/relative/testname.revision.mode/testname
898-
pub fn output_base_name(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
901+
pub fn output_base_name(
902+
config: &Config,
903+
testpaths: &TestPaths,
904+
revision: Option<&str>,
905+
) -> Utf8PathBuf {
899906
output_base_dir(config, testpaths, revision).join(testpaths.file.file_stem().unwrap())
900907
}
901908

902909
/// Absolute path to the directory to use for incremental compilation. Example:
903910
/// /path/to/build/host-tuple/test/ui/relative/testname.mode/testname.inc
904-
pub fn incremental_dir(config: &Config, testpaths: &TestPaths, revision: Option<&str>) -> PathBuf {
911+
pub fn incremental_dir(
912+
config: &Config,
913+
testpaths: &TestPaths,
914+
revision: Option<&str>,
915+
) -> Utf8PathBuf {
905916
output_base_name(config, testpaths, revision).with_extension("inc")
906917
}

Diff for: src/tools/compiletest/src/compute_diff.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::VecDeque;
22
use std::fs::{File, FileType};
3-
use std::path::Path;
3+
4+
use camino::Utf8Path;
45

56
#[derive(Debug, PartialEq)]
67
pub enum DiffLine {
@@ -112,8 +113,8 @@ pub(crate) fn write_diff(expected: &str, actual: &str, context_size: usize) -> S
112113
/// Returns whether any data was actually written.
113114
pub(crate) fn write_filtered_diff<Filter>(
114115
diff_filename: &str,
115-
out_dir: &Path,
116-
compare_dir: &Path,
116+
out_dir: &Utf8Path,
117+
compare_dir: &Utf8Path,
117118
verbose: bool,
118119
filter: Filter,
119120
) -> bool
@@ -123,19 +124,21 @@ where
123124
use std::io::{Read, Write};
124125
let mut diff_output = File::create(diff_filename).unwrap();
125126
let mut wrote_data = false;
126-
for entry in walkdir::WalkDir::new(out_dir) {
127+
for entry in walkdir::WalkDir::new(out_dir.as_std_path()) {
127128
let entry = entry.expect("failed to read file");
128129
let extension = entry.path().extension().and_then(|p| p.to_str());
129130
if filter(entry.file_type(), extension) {
130-
let expected_path = compare_dir.join(entry.path().strip_prefix(&out_dir).unwrap());
131+
let expected_path = compare_dir
132+
.as_std_path()
133+
.join(entry.path().strip_prefix(&out_dir.as_std_path()).unwrap());
131134
let expected = if let Ok(s) = std::fs::read(&expected_path) { s } else { continue };
132135
let actual_path = entry.path();
133136
let actual = std::fs::read(&actual_path).unwrap();
134137
let diff = unified_diff::diff(
135138
&expected,
136-
&expected_path.to_string_lossy(),
139+
&expected_path.to_str().unwrap(),
137140
&actual,
138-
&actual_path.to_string_lossy(),
141+
&actual_path.to_str().unwrap(),
139142
3,
140143
);
141144
wrote_data |= !diff.is_empty();

Diff for: src/tools/compiletest/src/debuggers.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::env;
2-
use std::ffi::OsString;
3-
use std::path::{Path, PathBuf};
42
use std::process::Command;
53
use std::sync::Arc;
64

5+
use camino::{Utf8Path, Utf8PathBuf};
6+
77
use crate::common::{Config, Debugger};
88

99
pub(crate) fn configure_cdb(config: &Config) -> Option<Arc<Config>> {
@@ -78,12 +78,15 @@ fn is_pc_windows_msvc_target(target: &str) -> bool {
7878
target.ends_with("-pc-windows-msvc")
7979
}
8080

81-
fn find_cdb(target: &str) -> Option<OsString> {
81+
fn find_cdb(target: &str) -> Option<Utf8PathBuf> {
8282
if !(cfg!(windows) && is_pc_windows_msvc_target(target)) {
8383
return None;
8484
}
8585

86-
let pf86 = env::var_os("ProgramFiles(x86)").or_else(|| env::var_os("ProgramFiles"))?;
86+
let pf86 = Utf8PathBuf::from_path_buf(
87+
env::var_os("ProgramFiles(x86)").or_else(|| env::var_os("ProgramFiles"))?.into(),
88+
)
89+
.unwrap();
8790
let cdb_arch = if cfg!(target_arch = "x86") {
8891
"x86"
8992
} else if cfg!(target_arch = "x86_64") {
@@ -96,8 +99,7 @@ fn find_cdb(target: &str) -> Option<OsString> {
9699
return None; // No compatible CDB.exe in the Windows 10 SDK
97100
};
98101

99-
let mut path = PathBuf::new();
100-
path.push(pf86);
102+
let mut path = pf86;
101103
path.push(r"Windows Kits\10\Debuggers"); // We could check 8.1 etc. too?
102104
path.push(cdb_arch);
103105
path.push(r"cdb.exe");
@@ -106,15 +108,15 @@ fn find_cdb(target: &str) -> Option<OsString> {
106108
return None;
107109
}
108110

109-
Some(path.into_os_string())
111+
Some(path)
110112
}
111113

112114
/// Returns Path to CDB
113115
pub(crate) fn analyze_cdb(
114116
cdb: Option<String>,
115117
target: &str,
116-
) -> (Option<OsString>, Option<[u16; 4]>) {
117-
let cdb = cdb.map(OsString::from).or_else(|| find_cdb(target));
118+
) -> (Option<Utf8PathBuf>, Option<[u16; 4]>) {
119+
let cdb = cdb.map(Utf8PathBuf::from).or_else(|| find_cdb(target));
118120

119121
let mut version = None;
120122
if let Some(cdb) = cdb.as_ref() {
@@ -143,7 +145,7 @@ pub(crate) fn extract_cdb_version(full_version_line: &str) -> Option<[u16; 4]> {
143145
pub(crate) fn analyze_gdb(
144146
gdb: Option<String>,
145147
target: &str,
146-
android_cross_path: &Path,
148+
android_cross_path: &Utf8Path,
147149
) -> (Option<String>, Option<u32>) {
148150
#[cfg(not(windows))]
149151
const GDB_FALLBACK: &str = "gdb";
@@ -152,10 +154,7 @@ pub(crate) fn analyze_gdb(
152154

153155
let fallback_gdb = || {
154156
if is_android_gdb_target(target) {
155-
let mut gdb_path = match android_cross_path.to_str() {
156-
Some(x) => x.to_owned(),
157-
None => panic!("cannot find android cross path"),
158-
};
157+
let mut gdb_path = android_cross_path.to_string();
159158
gdb_path.push_str("/bin/gdb");
160159
gdb_path
161160
} else {

Diff for: src/tools/compiletest/src/errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use std::fmt;
22
use std::fs::File;
33
use std::io::BufReader;
44
use std::io::prelude::*;
5-
use std::path::Path;
65
use std::sync::OnceLock;
76

7+
use camino::Utf8Path;
88
use regex::Regex;
99
use tracing::*;
1010

@@ -102,8 +102,8 @@ impl Error {
102102
///
103103
/// If revision is not None, then we look
104104
/// for `//[X]~` instead, where `X` is the current revision.
105-
pub fn load_errors(testfile: &Path, revision: Option<&str>) -> Vec<Error> {
106-
let rdr = BufReader::new(File::open(testfile).unwrap());
105+
pub fn load_errors(testfile: &Utf8Path, revision: Option<&str>) -> Vec<Error> {
106+
let rdr = BufReader::new(File::open(testfile.as_std_path()).unwrap());
107107

108108
// `last_nonfollow_error` tracks the most recently seen
109109
// line with an error template that did not use the

0 commit comments

Comments
 (0)