Skip to content

Deprecate std::path::Path's aliases of std::fs functions. #80741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_llvm/src/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_session::config::{self, CrateType, Lto};
use tracing::{debug, info};

use std::ffi::{CStr, CString};
use std::fs::File;
use std::fs::{self, File};
use std::io;
use std::path::Path;
use std::ptr;
Expand Down Expand Up @@ -497,7 +497,7 @@ fn thin_lto(
// reading the file, then we'll just use `None` as the
// prev_key_map, which will force the code to be recompiled.
let prev =
if path.exists() { ThinLTOKeysMap::load_from_file(&path).ok() } else { None };
if fs::exists(&path) { ThinLTOKeysMap::load_from_file(&path).ok() } else { None };
let curr = ThinLTOKeysMap::from_thin_lto_modules(&data, &thin_modules, &module_names);
(Some(path), prev, curr)
} else {
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_codegen_ssa/src/back/archive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_session::Session;
use rustc_span::symbol::Symbol;

use std::fs;
use std::io;
use std::path::{Path, PathBuf};

Expand All @@ -14,12 +15,12 @@ pub fn find_library(name: Symbol, search_paths: &[PathBuf], sess: &Session) -> P
for path in search_paths {
debug!("looking for {} inside {:?}", name, path);
let test = path.join(&oslibname);
if test.exists() {
if fs::exists(&test) {
return test;
}
if oslibname != unixlibname {
let test = path.join(&unixlibname);
if test.exists() {
if fs::exists(&test) {
return test;
}
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,19 +1110,19 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
fn get_object_file_path(sess: &Session, name: &str, self_contained: bool) -> PathBuf {
let fs = sess.target_filesearch(PathKind::Native);
let file_path = fs.get_lib_path().join(name);
if file_path.exists() {
if fs::exists(&file_path) {
return file_path;
}
// Special directory with objects used only in self-contained linkage mode
if self_contained {
let file_path = fs.get_self_contained_lib_path().join(name);
if file_path.exists() {
if fs::exists(&file_path) {
return file_path;
}
}
for search_path in fs.search_paths() {
let file_path = search_path.dir.join(name);
if file_path.exists() {
if fs::exists(&file_path) {
return file_path;
}
}
Expand Down Expand Up @@ -1312,7 +1312,7 @@ fn detect_self_contained_mingw(sess: &Session) -> bool {
for dir in env::split_paths(&env::var_os("PATH").unwrap_or_default()) {
let full_path = dir.join(&linker_with_extension);
// If linker comes from sysroot assume self-contained mode
if full_path.is_file() && !full_path.starts_with(&sess.sysroot) {
if fs::is_file(&full_path) && !full_path.starts_with(&sess.sysroot) {
return false;
}
}
Expand Down Expand Up @@ -2228,7 +2228,7 @@ fn get_apple_sdk_root(sdk_name: &str) -> Result<String, String> {
if sdkroot.contains("iPhoneOS.platform")
|| sdkroot.contains("iPhoneSimulator.platform") => {}
// Ignore `SDKROOT` if it's not a valid path.
_ if !p.is_absolute() || p == Path::new("/") || !p.exists() => {}
_ if !p.is_absolute() || p == Path::new("/") || !fs::exists(p) => {}
_ => return Ok(sdkroot),
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ impl<'a> Linker for MsvcLinker<'a> {
// check to see if the file is there and just omit linking to it if it's
// not present.
let name = format!("{}.dll.lib", lib);
if fs::metadata(&path.join(&name)).is_ok() {
if fs::exists(&path.join(&name)) {
self.cmd.arg(name);
}
}
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_fs_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ pub enum LinkOrCopy {
pub fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<LinkOrCopy> {
let p = p.as_ref();
let q = q.as_ref();
if q.exists() {
fs::remove_file(&q)?;
match fs::remove_file(&q) {
Ok(()) => (),
Err(err) if err.kind() == io::ErrorKind::NotFound => (),
Err(err) => return Err(err),
}

match fs::hard_link(p, q) {
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_incremental/src/persist/file_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ pub fn read_file(
path: &Path,
nightly_build: bool,
) -> io::Result<Option<(Vec<u8>, usize)>> {
if !path.exists() {
return Ok(None);
}

let data = fs::read(path)?;
let data = match fs::read(path) {
Ok(data) => data,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(err) => return Err(err),
};

let mut file = io::Cursor::new(data);

Expand Down
41 changes: 21 additions & 20 deletions compiler/rustc_incremental/src/persist/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ pub fn prepare_session_directory(
// because, on windows, long paths can cause problems;
// canonicalization inserts this weird prefix that makes windows
// tolerate long paths.
let crate_dir = match crate_dir.canonicalize() {
let crate_dir = match std_fs::canonicalize(&crate_dir) {
Ok(v) => v,
Err(err) => {
sess.err(&format!(
Expand Down Expand Up @@ -387,7 +387,7 @@ pub fn finalize_session_directory(sess: &Session, svh: Svh) {
}

pub fn delete_all_session_dir_contents(sess: &Session) -> io::Result<()> {
let sess_dir_iterator = sess.incr_comp_session_dir().read_dir()?;
let sess_dir_iterator = std_fs::read_dir(&*sess.incr_comp_session_dir())?;
for entry in sess_dir_iterator {
let entry = entry?;
safe_remove_file(&entry.path())?
Expand All @@ -412,7 +412,7 @@ fn copy_files(sess: &Session, target_dir: &Path, source_dir: &Path) -> Result<bo
return Err(());
};

let source_dir_iterator = match source_dir.read_dir() {
let source_dir_iterator = match std_fs::read_dir(source_dir) {
Ok(it) => it,
Err(_) => return Err(()),
};
Expand Down Expand Up @@ -534,8 +534,7 @@ fn find_source_directory(
crate_dir: &Path,
source_directories_already_tried: &FxHashSet<PathBuf>,
) -> Option<PathBuf> {
let iter = crate_dir
.read_dir()
let iter = std_fs::read_dir(crate_dir)
.unwrap() // FIXME
.filter_map(|e| e.ok().map(|e| e.path()));

Expand Down Expand Up @@ -670,7 +669,7 @@ pub fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> {
let mut session_directories = FxHashSet::default();
let mut lock_files = FxHashSet::default();

for dir_entry in crate_directory.read_dir()? {
for dir_entry in std_fs::read_dir(crate_directory)? {
let dir_entry = match dir_entry {
Ok(dir_entry) => dir_entry,
_ => {
Expand Down Expand Up @@ -922,22 +921,24 @@ fn all_except_most_recent(
/// before passing it to std::fs::remove_dir_all(). This will convert the path
/// into the '\\?\' format, which supports much longer paths.
fn safe_remove_dir_all(p: &Path) -> io::Result<()> {
if p.exists() {
let canonicalized = p.canonicalize()?;
std_fs::remove_dir_all(canonicalized)
} else {
Ok(())
}
let canonicalized = match std_fs::canonicalize(p) {
Ok(canonicalized) => canonicalized,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
Err(err) => return Err(err),
};

std_fs::remove_dir_all(canonicalized)
}

fn safe_remove_file(p: &Path) -> io::Result<()> {
if p.exists() {
let canonicalized = p.canonicalize()?;
match std_fs::remove_file(canonicalized) {
Err(ref err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
result => result,
}
} else {
Ok(())
let canonicalized = match std_fs::canonicalize(p) {
Ok(canonicalized) => canonicalized,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
Err(err) => return Err(err),
};

match std_fs::remove_file(canonicalized) {
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
result => result,
}
}
3 changes: 2 additions & 1 deletion compiler/rustc_incremental/src/persist/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rustc_middle::ty::TyCtxt;
use rustc_serialize::opaque::Decoder;
use rustc_serialize::Decodable as RustcDecodable;
use rustc_session::Session;
use std::fs;
use std::path::Path;

use super::data::*;
Expand Down Expand Up @@ -142,7 +143,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
let mut all_files_exist = true;
if let Some(ref file_name) = swp.work_product.saved_file {
let path = in_incr_comp_dir_sess(sess, file_name);
if !path.exists() {
if !fs::exists(&path) {
all_files_exist = false;

if sess.opts.debugging_opts.incremental_info {
Expand Down
37 changes: 17 additions & 20 deletions compiler/rustc_incremental/src/persist/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use rustc_middle::ty::TyCtxt;
use rustc_serialize::opaque::Encoder;
use rustc_serialize::Encodable as RustcEncodable;
use rustc_session::Session;
use std::fs;
use std::path::PathBuf;
use std::{fs, io};

use super::data::*;
use super::dirty_clean;
Expand Down Expand Up @@ -73,11 +73,9 @@ pub fn save_work_product_index(
for (id, wp) in previous_work_products.iter() {
if !new_work_products.contains_key(id) {
work_product::delete_workproduct_files(sess, wp);
debug_assert!(
wp.saved_file.as_ref().map_or(true, |file_name| {
!in_incr_comp_dir_sess(sess, &file_name).exists()
})
);
debug_assert!(wp.saved_file.as_ref().map_or(true, |file_name| {
!fs::exists(in_incr_comp_dir_sess(sess, &file_name))
}));
}
}

Expand All @@ -87,7 +85,7 @@ pub fn save_work_product_index(
.iter()
.flat_map(|(_, wp)| wp.saved_file.iter())
.map(|name| in_incr_comp_dir_sess(sess, name))
.all(|path| path.exists())
.all(|path| fs::exists(path))
});
}

Expand All @@ -101,19 +99,18 @@ where
// Note: It's important that we actually delete the old file and not just
// truncate and overwrite it, since it might be a shared hard-link, the
// underlying data of which we don't want to modify
if path_buf.exists() {
match fs::remove_file(&path_buf) {
Ok(()) => {
debug!("save: remove old file");
}
Err(err) => {
sess.err(&format!(
"unable to delete old dep-graph at `{}`: {}",
path_buf.display(),
err
));
return;
}
match fs::remove_file(&path_buf) {
Ok(()) => {
debug!("save: remove old file");
}
Err(err) if err.kind() == io::ErrorKind::NotFound => (),
Err(err) => {
sess.err(&format!(
"unable to delete old dep-graph at `{}`: {}",
path_buf.display(),
err
));
return;
}
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,18 +506,18 @@ where
}

fn output_contains_path(output_paths: &[PathBuf], input_path: &PathBuf) -> bool {
let input_path = input_path.canonicalize().ok();
let input_path = fs::canonicalize(input_path).ok();
if input_path.is_none() {
return false;
}
let check = |output_path: &PathBuf| {
if output_path.canonicalize().ok() == input_path { Some(()) } else { None }
if fs::canonicalize(output_path).ok() == input_path { Some(()) } else { None }
};
check_output(output_paths, check).is_some()
}

fn output_conflicts_with_dir(output_paths: &[PathBuf]) -> Option<PathBuf> {
let check = |output_path: &PathBuf| output_path.is_dir().then(|| output_path.clone());
let check = |output_path: &PathBuf| fs::is_dir(output_path).then(|| output_path.clone());
check_output(output_paths, check)
}

Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use rustc_span::symbol::{sym, Symbol};
use smallvec::SmallVec;
use std::env;
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
use std::fs;
use std::io;
use std::lazy::SyncOnceCell;
use std::mem;
Expand Down Expand Up @@ -269,14 +270,14 @@ fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
} else {
"rustc"
});
candidate.exists().then_some(candidate)
fs::exists(&candidate).then_some(candidate)
})
}

fn sysroot_candidates() -> Vec<PathBuf> {
let target = session::config::host_triple();
let mut sysroot_candidates = vec![filesearch::get_or_default_sysroot()];
let path = current_dll_path().and_then(|s| s.canonicalize().ok());
let path = current_dll_path().and_then(|s| fs::canonicalize(s).ok());
if let Some(dll) = path {
// use `parent` twice to chop off the file name and then also the
// directory containing the dll which should be either `lib` or `bin`.
Expand Down Expand Up @@ -397,7 +398,7 @@ pub fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend
})
.find(|f| {
info!("codegen backend candidate: {}", f.display());
f.exists()
fs::exists(f)
});
let sysroot = sysroot.unwrap_or_else(|| {
let candidates = sysroot_candidates
Expand All @@ -414,7 +415,7 @@ pub fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend
});
info!("probing {} for a codegen backend", sysroot.display());

let d = sysroot.read_dir().unwrap_or_else(|e| {
let d = fs::read_dir(&sysroot).unwrap_or_else(|e| {
let err = format!(
"failed to load default codegen backend, couldn't \
read `{}`: {}",
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_llvm/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

Expand Down Expand Up @@ -205,7 +206,7 @@ fn main() {
stripped
} else if let Some(stripped) = lib.strip_prefix('-') {
stripped
} else if Path::new(lib).exists() {
} else if fs::exists(Path::new(lib)) {
// On MSVC llvm-config will print the full name to libraries, but
// we're only interested in the name part
let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ impl<'a> CrateLocator<'a> {
// as well.
if let Some((prev, _)) = &ret {
let sysroot = &self.sess.sysroot;
let sysroot = sysroot.canonicalize().unwrap_or_else(|_| sysroot.to_path_buf());
let sysroot = fs::canonicalize(sysroot).unwrap_or_else(|_| sysroot.to_path_buf());
if prev.starts_with(&sysroot) {
continue;
}
Expand Down Expand Up @@ -664,7 +664,7 @@ impl<'a> CrateLocator<'a> {
let mut rmetas = FxHashMap::default();
let mut dylibs = FxHashMap::default();
for loc in &self.exact_paths {
if !loc.exists() {
if !fs::exists(loc) {
return Err(CrateError::ExternLocationNotExist(self.crate_name, loc.clone()));
}
let file = match loc.file_name().and_then(|s| s.to_str()) {
Expand Down Expand Up @@ -738,7 +738,7 @@ fn get_metadata_section(
filename: &Path,
loader: &dyn MetadataLoader,
) -> Result<MetadataBlob, String> {
if !filename.exists() {
if !fs::exists(filename) {
return Err(format!("no such file: '{}'", filename.display()));
}
let raw_bytes: MetadataRef = match flavor {
Expand Down
Loading