Skip to content

Commit 482b77a

Browse files
committed
Refactor structure of ExternEntry
1 parent b23a883 commit 482b77a

File tree

5 files changed

+40
-59
lines changed

5 files changed

+40
-59
lines changed

src/librustc/session/config.rs

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -283,26 +283,24 @@ impl OutputTypes {
283283
// DO NOT switch BTreeMap or BTreeSet out for an unsorted container type! That
284284
// would break dependency tracking for command-line arguments.
285285
#[derive(Clone, Hash)]
286-
pub struct Externs(BTreeMap<String, BTreeSet<ExternEntry>>);
286+
pub struct Externs(BTreeMap<String, ExternEntry>);
287287

288288
#[derive(Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Debug)]
289289
pub struct ExternEntry {
290-
pub location: Option<String>,
291-
pub public: bool
290+
pub locations: BTreeSet<Option<String>>,
291+
pub is_private_dep: bool
292292
}
293293

294-
295-
296294
impl Externs {
297-
pub fn new(data: BTreeMap<String, BTreeSet<ExternEntry>>) -> Externs {
295+
pub fn new(data: BTreeMap<String, ExternEntry>) -> Externs {
298296
Externs(data)
299297
}
300298

301-
pub fn get(&self, key: &str) -> Option<&BTreeSet<ExternEntry>> {
299+
pub fn get(&self, key: &str) -> Option<&ExternEntry> {
302300
self.0.get(key)
303301
}
304302

305-
pub fn iter<'a>(&'a self) -> BTreeMapIter<'a, String, BTreeSet<ExternEntry>> {
303+
pub fn iter<'a>(&'a self) -> BTreeMapIter<'a, String, ExternEntry> {
306304
self.0.iter()
307305
}
308306
}
@@ -2323,9 +2321,9 @@ pub fn build_session_options_and_crate_config(
23232321
// and later convert it into a BTreeSet<(Option<String>, bool)>
23242322
// This allows to modify entries in-place to set their correct
23252323
// 'public' value
2326-
let mut externs: BTreeMap<_, BTreeMap<Option<String>, bool>> = BTreeMap::new();
2327-
for (arg, public) in matches.opt_strs("extern").into_iter().map(|v| (v, true))
2328-
.chain(matches.opt_strs("extern-private").into_iter().map(|v| (v, false))) {
2324+
let mut externs: BTreeMap<String, ExternEntry> = BTreeMap::new();
2325+
for (arg, private) in matches.opt_strs("extern").into_iter().map(|v| (v, false))
2326+
.chain(matches.opt_strs("extern-private").into_iter().map(|v| (v, true))) {
23292327

23302328
let mut parts = arg.splitn(2, '=');
23312329
let name = parts.next().unwrap_or_else(||
@@ -2340,36 +2338,26 @@ pub fn build_session_options_and_crate_config(
23402338
};
23412339

23422340

2343-
// Extern crates start out public,
2344-
// and become private if we later see
2345-
// an '--extern-private' key. They never
2346-
// go back to being public once we've seen
2347-
// '--extern-private', so we logical-AND
2348-
// their current and new 'public' value together
2349-
23502341
externs
23512342
.entry(name.to_owned())
2352-
.or_default()
2353-
.entry(location)
2354-
.and_modify(|e| *e &= public)
2355-
.or_insert(public);
2356-
}
2343+
.and_modify(|e| {
2344+
e.locations.insert(location.clone());
2345+
2346+
// Crates start out being not private,
2347+
// and go to being private if we see an '--extern-private'
2348+
// flag
2349+
e.is_private_dep |= private;
2350+
})
2351+
.or_insert_with(|| {
2352+
let mut locations = BTreeSet::new();
2353+
locations.insert(location);
23572354

2358-
// Now that we've determined the 'public' status of each extern,
2359-
// collect them into a set of ExternEntry
2360-
let externs: BTreeMap<String, BTreeSet<ExternEntry>> = externs.into_iter()
2361-
.map(|(k, v)| {
2362-
let values =v.into_iter().map(|(location, public)| {
23632355
ExternEntry {
2364-
location,
2365-
public
2356+
locations: locations,
2357+
is_private_dep: private
23662358
}
2367-
}).collect::<BTreeSet<ExternEntry>>();
2368-
(k, values)
2369-
})
2370-
.collect();
2371-
2372-
2359+
});
2360+
}
23732361

23742362
let crate_name = matches.opt_str("crate-name");
23752363

@@ -2699,9 +2687,11 @@ mod tests {
26992687

27002688
impl ExternEntry {
27012689
fn new_public(location: Option<String>) -> ExternEntry {
2690+
let mut locations = BTreeSet::new();
2691+
locations.insert(location);
27022692
ExternEntry {
2703-
location,
2704-
public: true
2693+
locations,
2694+
is_private_dep: false
27052695
}
27062696
}
27072697
}

src/librustc_metadata/creader.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ impl<'a> CrateLoader<'a> {
131131
// `source` stores paths which are normalized which may be different
132132
// from the strings on the command line.
133133
let source = &self.cstore.get_crate_data(cnum).source;
134-
if let Some(locs) = self.sess.opts.externs.get(&*name.as_str()) {
134+
if let Some(entry) = self.sess.opts.externs.get(&*name.as_str()) {
135135
// Only use `--extern crate_name=path` here, not `--extern crate_name`.
136-
let found = locs.iter().filter_map(|l| l.location.as_ref()).any(|l| {
136+
let found = entry.locations.iter().filter_map(|l| l.as_ref()).any(|l| {
137137
let l = fs::canonicalize(l).ok();
138138
source.dylib.as_ref().map(|p| &p.0) == l.as_ref() ||
139139
source.rlib.as_ref().map(|p| &p.0) == l.as_ref()
@@ -201,19 +201,9 @@ impl<'a> CrateLoader<'a> {
201201
let crate_root = lib.metadata.get_root();
202202
self.verify_no_symbol_conflicts(span, &crate_root);
203203

204-
let mut private_dep = false;
205-
if let Some(s) = self.sess.opts.externs.get(&name.as_str()) {
206-
for entry in s {
207-
let p = entry.location.as_ref().map(|s| s.as_str());
208-
if p == lib.dylib.as_ref().and_then(|r| r.0.to_str()) ||
209-
p == lib.rlib.as_ref().and_then(|r| r.0.to_str()) {
210-
211-
private_dep = !entry.public;
212-
break;
213-
}
214-
}
215-
}
216-
204+
let private_dep = self.sess.opts.externs.get(&name.as_str())
205+
.map(|e| e.is_private_dep)
206+
.unwrap_or(false);
217207

218208
info!("register crate `extern crate {} as {}` (private_dep = {})",
219209
crate_root.name, ident, private_dep);

src/librustc_metadata/locator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,11 @@ impl<'a> Context<'a> {
442442
// must be loaded via -L plus some filtering.
443443
if self.hash.is_none() {
444444
self.should_match_name = false;
445-
if let Some(s) = self.sess.opts.externs.get(&self.crate_name.as_str()) {
445+
if let Some(entry) = self.sess.opts.externs.get(&self.crate_name.as_str()) {
446446
// Only use `--extern crate_name=path` here, not `--extern crate_name`.
447-
if s.iter().any(|l| l.location.is_some()) {
447+
if entry.locations.iter().any(|l| l.is_some()) {
448448
return self.find_commandline_library(
449-
s.iter().filter_map(|l| l.location.as_ref()),
449+
entry.locations.iter().filter_map(|l| l.as_ref()),
450450
);
451451
}
452452
}

src/librustc_typeck/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn require_same_types<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
176176
})
177177
}
178178

179-
fn check_main_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, main_def_id: DefId) {
179+
pub fn check_main_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, main_def_id: DefId) {
180180
let main_id = tcx.hir().as_local_hir_id(main_def_id).unwrap();
181181
let main_span = tcx.def_span(main_def_id);
182182
let main_t = tcx.type_of(main_def_id);
@@ -241,7 +241,7 @@ fn check_main_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, main_def_id: DefId) {
241241
}
242242
}
243243

244-
fn check_start_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, start_def_id: DefId) {
244+
pub fn check_start_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, start_def_id: DefId) {
245245
let start_id = tcx.hir().as_local_hir_id(start_def_id).unwrap();
246246
let start_span = tcx.def_span(start_def_id);
247247
let start_t = tcx.type_of(start_def_id);
@@ -298,7 +298,7 @@ fn check_start_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, start_def_id: DefId)
298298
}
299299
}
300300

301-
fn check_for_entry_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
301+
pub fn check_for_entry_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
302302
match tcx.entry_fn(LOCAL_CRATE) {
303303
Some((def_id, EntryFnType::Main)) => check_main_fn_ty(tcx, def_id),
304304
Some((def_id, EntryFnType::Start)) => check_start_fn_ty(tcx, def_id),

src/tools/compiletest/src/header.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ pub struct TestProps {
288288
pub aux_builds: Vec<String>,
289289
// A list of crates to pass '--extern-private name:PATH' flags for
290290
// This should be a subset of 'aux_build'
291+
// FIXME: Replace this with a better solution: https://github.com/rust-lang/rust/pull/54020
291292
pub extern_private: Vec<String>,
292293
// Environment settings to use for compiling
293294
pub rustc_env: Vec<(String, String)>,

0 commit comments

Comments
 (0)