Skip to content

Commit 0ba47f3

Browse files
committed
Make SearchPathFile::file_name_str non-optional.
Currently, it can be `None` if the conversion from `OsString` fails, in which case all searches will skip over the `SearchPathFile`. The commit changes things so that the `SearchPathFile` just doesn't get created in the first place. Same behaviour, but slightly simpler code.
1 parent 89b61ea commit 0ba47f3

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

compiler/rustc_metadata/src/locator.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,7 @@ impl<'a> CrateLocator<'a> {
399399
for spf in search_path.files.iter() {
400400
debug!("testing {}", spf.path.display());
401401

402-
let file = match &spf.file_name_str {
403-
None => continue,
404-
Some(file) => file,
405-
};
402+
let file = &spf.file_name_str;
406403
let (hash, found_kind) = if file.starts_with(&rlib_prefix)
407404
&& file.ends_with(".rlib")
408405
{

compiler/rustc_session/src/search_paths.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,15 @@ pub struct SearchPath {
1515
/// doable, but very slow, because it involves calls to `file_name` and
1616
/// `extension` that are themselves slow.
1717
///
18-
/// This type augments the `PathBuf` with an `Option<String>` containing the
18+
/// This type augments the `PathBuf` with an `String` containing the
1919
/// `PathBuf`'s filename. The prefix and suffix checking is much faster on the
20-
/// `Option<String>` than the `PathBuf`. (It's an `Option` because
21-
/// `Path::file_name` can fail; if that happens then all subsequent checking
22-
/// will also fail, which is fine.)
20+
/// `String` than the `PathBuf`. (The filename must be valid UTF-8. If it's
21+
/// not, the entry should be skipped, because all Rust output files are valid
22+
/// UTF-8, and so a non-UTF-8 filename couldn't be one we're looking for.)
2323
#[derive(Clone, Debug)]
2424
pub struct SearchPathFile {
2525
pub path: PathBuf,
26-
pub file_name_str: Option<String>,
27-
}
28-
29-
impl SearchPathFile {
30-
fn new(path: PathBuf) -> SearchPathFile {
31-
let file_name_str = path.file_name().and_then(|f| f.to_str()).map(|s| s.to_string());
32-
SearchPathFile { path, file_name_str }
33-
}
26+
pub file_name_str: String,
3427
}
3528

3629
#[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, Encodable, Decodable)]
@@ -85,7 +78,14 @@ impl SearchPath {
8578
// Get the files within the directory.
8679
let files = match std::fs::read_dir(&dir) {
8780
Ok(files) => files
88-
.filter_map(|e| e.ok().map(|e| SearchPathFile::new(e.path())))
81+
.filter_map(|e| {
82+
e.ok().and_then(|e| {
83+
e.file_name().to_str().map(|s| SearchPathFile {
84+
path: e.path(),
85+
file_name_str: s.to_string(),
86+
})
87+
})
88+
})
8989
.collect::<Vec<_>>(),
9090
Err(..) => vec![],
9191
};

0 commit comments

Comments
 (0)