Skip to content

Commit c435605

Browse files
committed
Introduce SearchPath and replace SearchPaths with Vec<SearchPath>.
It's more idiomatic, makes the code shorter, and will help with the next commit.
1 parent e63e7b4 commit c435605

File tree

7 files changed

+79
-94
lines changed

7 files changed

+79
-94
lines changed

src/librustc/session/config.rs

+26-25
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use std::str::FromStr;
1515

1616
use session::{early_error, early_warn, Session};
17-
use session::search_paths::SearchPaths;
17+
use session::search_paths::SearchPath;
1818

1919
use rustc_target::spec::{LinkerFlavor, PanicStrategy, RelroLevel};
2020
use rustc_target::spec::{Target, TargetTriple};
@@ -374,7 +374,7 @@ top_level_options!(
374374
lint_cap: Option<lint::Level> [TRACKED],
375375
describe_lints: bool [UNTRACKED],
376376
output_types: OutputTypes [TRACKED],
377-
search_paths: SearchPaths [UNTRACKED],
377+
search_paths: Vec<SearchPath> [UNTRACKED],
378378
libs: Vec<(String, Option<String>, Option<cstore::NativeLibraryKind>)> [TRACKED],
379379
maybe_sysroot: Option<PathBuf> [TRACKED],
380380

@@ -593,7 +593,7 @@ impl Default for Options {
593593
lint_cap: None,
594594
describe_lints: false,
595595
output_types: OutputTypes(BTreeMap::new()),
596-
search_paths: SearchPaths::new(),
596+
search_paths: vec![],
597597
maybe_sysroot: None,
598598
target_triple: TargetTriple::from_triple(host_triple()),
599599
test: false,
@@ -2115,9 +2115,9 @@ pub fn build_session_options_and_crate_config(
21152115
}
21162116
};
21172117

2118-
let mut search_paths = SearchPaths::new();
2118+
let mut search_paths = vec![];
21192119
for s in &matches.opt_strs("L") {
2120-
search_paths.add_path(&s[..], error_format);
2120+
search_paths.push(SearchPath::from_cli_opt(&s[..], error_format));
21212121
}
21222122

21232123
let libs = matches
@@ -2535,6 +2535,7 @@ mod tests {
25352535
use session::config::{build_configuration, build_session_options_and_crate_config};
25362536
use session::config::{LtoCli, CrossLangLto};
25372537
use session::build_session;
2538+
use session::search_paths::SearchPath;
25382539
use std::collections::{BTreeMap, BTreeSet};
25392540
use std::iter::FromIterator;
25402541
use std::path::PathBuf;
@@ -2790,48 +2791,48 @@ mod tests {
27902791

27912792
// Reference
27922793
v1.search_paths
2793-
.add_path("native=abc", super::ErrorOutputType::Json(false));
2794+
.push(SearchPath::from_cli_opt("native=abc", super::ErrorOutputType::Json(false)));
27942795
v1.search_paths
2795-
.add_path("crate=def", super::ErrorOutputType::Json(false));
2796+
.push(SearchPath::from_cli_opt("crate=def", super::ErrorOutputType::Json(false)));
27962797
v1.search_paths
2797-
.add_path("dependency=ghi", super::ErrorOutputType::Json(false));
2798+
.push(SearchPath::from_cli_opt("dependency=ghi", super::ErrorOutputType::Json(false)));
27982799
v1.search_paths
2799-
.add_path("framework=jkl", super::ErrorOutputType::Json(false));
2800+
.push(SearchPath::from_cli_opt("framework=jkl", super::ErrorOutputType::Json(false)));
28002801
v1.search_paths
2801-
.add_path("all=mno", super::ErrorOutputType::Json(false));
2802+
.push(SearchPath::from_cli_opt("all=mno", super::ErrorOutputType::Json(false)));
28022803

28032804
v2.search_paths
2804-
.add_path("native=abc", super::ErrorOutputType::Json(false));
2805+
.push(SearchPath::from_cli_opt("native=abc", super::ErrorOutputType::Json(false)));
28052806
v2.search_paths
2806-
.add_path("dependency=ghi", super::ErrorOutputType::Json(false));
2807+
.push(SearchPath::from_cli_opt("dependency=ghi", super::ErrorOutputType::Json(false)));
28072808
v2.search_paths
2808-
.add_path("crate=def", super::ErrorOutputType::Json(false));
2809+
.push(SearchPath::from_cli_opt("crate=def", super::ErrorOutputType::Json(false)));
28092810
v2.search_paths
2810-
.add_path("framework=jkl", super::ErrorOutputType::Json(false));
2811+
.push(SearchPath::from_cli_opt("framework=jkl", super::ErrorOutputType::Json(false)));
28112812
v2.search_paths
2812-
.add_path("all=mno", super::ErrorOutputType::Json(false));
2813+
.push(SearchPath::from_cli_opt("all=mno", super::ErrorOutputType::Json(false)));
28132814

28142815
v3.search_paths
2815-
.add_path("crate=def", super::ErrorOutputType::Json(false));
2816+
.push(SearchPath::from_cli_opt("crate=def", super::ErrorOutputType::Json(false)));
28162817
v3.search_paths
2817-
.add_path("framework=jkl", super::ErrorOutputType::Json(false));
2818+
.push(SearchPath::from_cli_opt("framework=jkl", super::ErrorOutputType::Json(false)));
28182819
v3.search_paths
2819-
.add_path("native=abc", super::ErrorOutputType::Json(false));
2820+
.push(SearchPath::from_cli_opt("native=abc", super::ErrorOutputType::Json(false)));
28202821
v3.search_paths
2821-
.add_path("dependency=ghi", super::ErrorOutputType::Json(false));
2822+
.push(SearchPath::from_cli_opt("dependency=ghi", super::ErrorOutputType::Json(false)));
28222823
v3.search_paths
2823-
.add_path("all=mno", super::ErrorOutputType::Json(false));
2824+
.push(SearchPath::from_cli_opt("all=mno", super::ErrorOutputType::Json(false)));
28242825

28252826
v4.search_paths
2826-
.add_path("all=mno", super::ErrorOutputType::Json(false));
2827+
.push(SearchPath::from_cli_opt("all=mno", super::ErrorOutputType::Json(false)));
28272828
v4.search_paths
2828-
.add_path("native=abc", super::ErrorOutputType::Json(false));
2829+
.push(SearchPath::from_cli_opt("native=abc", super::ErrorOutputType::Json(false)));
28292830
v4.search_paths
2830-
.add_path("crate=def", super::ErrorOutputType::Json(false));
2831+
.push(SearchPath::from_cli_opt("crate=def", super::ErrorOutputType::Json(false)));
28312832
v4.search_paths
2832-
.add_path("dependency=ghi", super::ErrorOutputType::Json(false));
2833+
.push(SearchPath::from_cli_opt("dependency=ghi", super::ErrorOutputType::Json(false)));
28332834
v4.search_paths
2834-
.add_path("framework=jkl", super::ErrorOutputType::Json(false));
2835+
.push(SearchPath::from_cli_opt("framework=jkl", super::ErrorOutputType::Json(false)));
28352836

28362837
assert!(v1.dep_tracking_hash() == v2.dep_tracking_hash());
28372838
assert!(v1.dep_tracking_hash() == v3.dep_tracking_hash());

src/librustc/session/filesearch.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::env;
1818
use std::fs;
1919
use std::path::{Path, PathBuf};
2020

21-
use session::search_paths::{SearchPaths, PathKind};
21+
use session::search_paths::{SearchPath, PathKind};
2222
use rustc_fs_util::fix_windows_verbatim_for_gcc;
2323

2424
#[derive(Copy, Clone)]
@@ -31,27 +31,28 @@ pub enum FileMatch {
3131

3232
pub struct FileSearch<'a> {
3333
pub sysroot: &'a Path,
34-
pub search_paths: &'a SearchPaths,
34+
pub search_paths: &'a [SearchPath],
3535
pub triple: &'a str,
3636
pub kind: PathKind,
3737
}
3838

3939
impl<'a> FileSearch<'a> {
4040
pub fn for_each_lib_search_path<F>(&self, mut f: F) where
41-
F: FnMut(&Path, PathKind)
41+
F: FnMut(&SearchPath)
4242
{
4343
let mut visited_dirs = FxHashSet::default();
44-
visited_dirs.reserve(self.search_paths.paths.len() + 1);
45-
for (path, kind) in self.search_paths.iter(self.kind) {
46-
f(path, kind);
47-
visited_dirs.insert(path.to_path_buf());
44+
visited_dirs.reserve(self.search_paths.len() + 1);
45+
let iter = self.search_paths.iter().filter(|sp| sp.kind.matches(self.kind));
46+
for search_path in iter {
47+
f(search_path);
48+
visited_dirs.insert(search_path.dir.to_path_buf());
4849
}
4950

5051
debug!("filesearch: searching lib path");
5152
let tlib_path = make_target_lib_path(self.sysroot,
5253
self.triple);
5354
if !visited_dirs.contains(&tlib_path) {
54-
f(&tlib_path, PathKind::All);
55+
f(&SearchPath { kind: PathKind::All, dir: tlib_path });
5556
}
5657
}
5758

@@ -62,9 +63,9 @@ impl<'a> FileSearch<'a> {
6263
pub fn search<F>(&self, mut pick: F)
6364
where F: FnMut(&Path, PathKind) -> FileMatch
6465
{
65-
self.for_each_lib_search_path(|lib_search_path, kind| {
66-
debug!("searching {}", lib_search_path.display());
67-
let files = match fs::read_dir(lib_search_path) {
66+
self.for_each_lib_search_path(|search_path| {
67+
debug!("searching {}", search_path.dir.display());
68+
let files = match fs::read_dir(&search_path.dir) {
6869
Ok(files) => files,
6970
Err(..) => return,
7071
};
@@ -81,7 +82,7 @@ impl<'a> FileSearch<'a> {
8182
let files2 = files.iter().filter(|p| !is_rlib(p));
8283
for path in files1.chain(files2) {
8384
debug!("testing {}", path.display());
84-
let maybe_picked = pick(path, kind);
85+
let maybe_picked = pick(path, search_path.kind);
8586
match maybe_picked {
8687
FileMatches => {
8788
debug!("picked {}", path.display());
@@ -96,7 +97,7 @@ impl<'a> FileSearch<'a> {
9697

9798
pub fn new(sysroot: &'a Path,
9899
triple: &'a str,
99-
search_paths: &'a SearchPaths,
100+
search_paths: &'a Vec<SearchPath>,
100101
kind: PathKind) -> FileSearch<'a> {
101102
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
102103
FileSearch {
@@ -110,8 +111,8 @@ impl<'a> FileSearch<'a> {
110111
// Returns a list of directories where target-specific dylibs might be located.
111112
pub fn get_dylib_search_paths(&self) -> Vec<PathBuf> {
112113
let mut paths = Vec::new();
113-
self.for_each_lib_search_path(|lib_search_path, _| {
114-
paths.push(lib_search_path.to_path_buf());
114+
self.for_each_lib_search_path(|search_path| {
115+
paths.push(search_path.dir.to_path_buf());
115116
});
116117
paths
117118
}
@@ -136,7 +137,7 @@ pub fn relative_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf
136137
p
137138
}
138139

139-
fn make_target_lib_path(sysroot: &Path,
140+
pub fn make_target_lib_path(sysroot: &Path,
140141
target_triple: &str) -> PathBuf {
141142
sysroot.join(&relative_target_lib_path(sysroot, target_triple))
142143
}

src/librustc/session/search_paths.rs

+20-36
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::slice;
1211
use std::path::{Path, PathBuf};
1312
use session::{early_error, config};
13+
use session::filesearch::make_target_lib_path;
1414

1515
#[derive(Clone, Debug)]
16-
pub struct SearchPaths {
17-
crate paths: Vec<(PathKind, PathBuf)>,
18-
}
19-
20-
pub struct Iter<'a> {
21-
kind: PathKind,
22-
iter: slice::Iter<'a, (PathKind, PathBuf)>,
16+
pub struct SearchPath {
17+
pub kind: PathKind,
18+
pub dir: PathBuf,
2319
}
2420

2521
#[derive(Eq, PartialEq, Clone, Copy, Debug, PartialOrd, Ord, Hash)]
@@ -32,12 +28,17 @@ pub enum PathKind {
3228
All,
3329
}
3430

35-
impl SearchPaths {
36-
pub fn new() -> SearchPaths {
37-
SearchPaths { paths: Vec::new() }
31+
impl PathKind {
32+
pub fn matches(&self, kind: PathKind) -> bool {
33+
match (self, kind) {
34+
(PathKind::All, _) | (_, PathKind::All) => true,
35+
_ => *self == kind,
36+
}
3837
}
38+
}
3939

40-
pub fn add_path(&mut self, path: &str, output: config::ErrorOutputType) {
40+
impl SearchPath {
41+
pub fn from_cli_opt(path: &str, output: config::ErrorOutputType) -> Self {
4142
let (kind, path) = if path.starts_with("native=") {
4243
(PathKind::Native, &path["native=".len()..])
4344
} else if path.starts_with("crate=") {
@@ -54,34 +55,17 @@ impl SearchPaths {
5455
if path.is_empty() {
5556
early_error(output, "empty search path given via `-L`");
5657
}
57-
self.paths.push((kind, PathBuf::from(path)));
58-
}
5958

60-
pub fn iter(&self, kind: PathKind) -> Iter<'_> {
61-
Iter { kind: kind, iter: self.paths.iter() }
59+
let dir = PathBuf::from(path);
60+
Self::new(kind, dir)
6261
}
63-
}
64-
65-
impl<'a> Iterator for Iter<'a> {
66-
type Item = (&'a Path, PathKind);
6762

68-
fn next(&mut self) -> Option<(&'a Path, PathKind)> {
69-
loop {
70-
match *self.iter.next()? {
71-
(kind, ref p) if self.kind == PathKind::All ||
72-
kind == PathKind::All ||
73-
kind == self.kind => {
74-
return Some((p, kind))
75-
}
76-
_ => {}
77-
}
78-
}
63+
pub fn from_sysroot_and_triple(sysroot: &Path, triple: &str) -> Self {
64+
Self::new(PathKind::All, make_target_lib_path(sysroot, triple))
7965
}
8066

81-
fn size_hint(&self) -> (usize, Option<usize>) {
82-
// This iterator will never return more elements than the base iterator;
83-
// but it can ignore all the remaining elements.
84-
let (_, upper) = self.iter.size_hint();
85-
(0, upper)
67+
fn new(kind: PathKind, dir: PathBuf) -> Self {
68+
SearchPath { kind, dir }
8669
}
8770
}
71+

src/librustc_codegen_llvm/back/link.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ fn link_binary_output(sess: &Session,
213213

214214
fn archive_search_paths(sess: &Session) -> Vec<PathBuf> {
215215
let mut search = Vec::new();
216-
sess.target_filesearch(PathKind::Native).for_each_lib_search_path(|path, _| {
217-
search.push(path.to_path_buf());
216+
sess.target_filesearch(PathKind::Native).for_each_lib_search_path(|search_path| {
217+
search.push(search_path.dir.to_path_buf());
218218
});
219219

220220
search
@@ -1067,10 +1067,10 @@ fn link_args(cmd: &mut dyn Linker,
10671067
fn add_local_native_libraries(cmd: &mut dyn Linker,
10681068
sess: &Session,
10691069
codegen_results: &CodegenResults) {
1070-
sess.target_filesearch(PathKind::All).for_each_lib_search_path(|path, k| {
1071-
match k {
1072-
PathKind::Framework => { cmd.framework_path(path); }
1073-
_ => { cmd.include_path(&fix_windows_verbatim_for_gcc(path)); }
1070+
sess.target_filesearch(PathKind::All).for_each_lib_search_path(|search_path| {
1071+
match search_path.kind {
1072+
PathKind::Framework => { cmd.framework_path(&search_path.dir); }
1073+
_ => { cmd.include_path(&fix_windows_verbatim_for_gcc(&search_path.dir)); }
10741074
}
10751075
});
10761076

src/librustdoc/config.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc::session::early_error;
2020
use rustc::session::config::{CodegenOptions, DebuggingOptions, ErrorOutputType, Externs};
2121
use rustc::session::config::{nightly_options, build_codegen_options, build_debugging_options,
2222
get_cmd_lint_options};
23-
use rustc::session::search_paths::SearchPaths;
23+
use rustc::session::search_paths::SearchPath;
2424
use rustc_driver;
2525
use rustc_target::spec::TargetTriple;
2626
use syntax::edition::Edition;
@@ -46,7 +46,7 @@ pub struct Options {
4646
/// How to format errors and warnings.
4747
pub error_format: ErrorOutputType,
4848
/// Library search paths to hand to the compiler.
49-
pub libs: SearchPaths,
49+
pub libs: Vec<SearchPath>,
5050
/// The list of external crates to link against.
5151
pub externs: Externs,
5252
/// List of `cfg` flags to hand to the compiler. Always includes `rustdoc`.
@@ -295,10 +295,9 @@ impl Options {
295295
}
296296
let input = PathBuf::from(&matches.free[0]);
297297

298-
let mut libs = SearchPaths::new();
299-
for s in &matches.opt_strs("L") {
300-
libs.add_path(s, error_format);
301-
}
298+
let libs = matches.opt_strs("L").iter()
299+
.map(|s| SearchPath::from_cli_opt(s, error_format))
300+
.collect();
302301
let externs = match parse_externs(&matches) {
303302
Ok(ex) => ex,
304303
Err(err) => {

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use html::render::RenderInfo;
5151
use passes;
5252

5353
pub use rustc::session::config::{Input, Options, CodegenOptions};
54-
pub use rustc::session::search_paths::SearchPaths;
54+
pub use rustc::session::search_paths::SearchPath;
5555

5656
pub type ExternalPaths = FxHashMap<DefId, (Vec<String>, clean::TypeKind)>;
5757

0 commit comments

Comments
 (0)