Skip to content

Commit ef33748

Browse files
committed
Refactor structure of ExternEntry
1 parent da5f1bf commit ef33748

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
@@ -268,26 +268,24 @@ impl OutputTypes {
268268
// DO NOT switch BTreeMap or BTreeSet out for an unsorted container type! That
269269
// would break dependency tracking for command-line arguments.
270270
#[derive(Clone, Hash)]
271-
pub struct Externs(BTreeMap<String, BTreeSet<ExternEntry>>);
271+
pub struct Externs(BTreeMap<String, ExternEntry>);
272272

273273
#[derive(Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Debug)]
274274
pub struct ExternEntry {
275-
pub location: Option<String>,
276-
pub public: bool
275+
pub locations: BTreeSet<Option<String>>,
276+
pub is_private_dep: bool
277277
}
278278

279-
280-
281279
impl Externs {
282-
pub fn new(data: BTreeMap<String, BTreeSet<ExternEntry>>) -> Externs {
280+
pub fn new(data: BTreeMap<String, ExternEntry>) -> Externs {
283281
Externs(data)
284282
}
285283

286-
pub fn get(&self, key: &str) -> Option<&BTreeSet<ExternEntry>> {
284+
pub fn get(&self, key: &str) -> Option<&ExternEntry> {
287285
self.0.get(key)
288286
}
289287

290-
pub fn iter<'a>(&'a self) -> BTreeMapIter<'a, String, BTreeSet<ExternEntry>> {
288+
pub fn iter<'a>(&'a self) -> BTreeMapIter<'a, String, ExternEntry> {
291289
self.0.iter()
292290
}
293291
}
@@ -2296,9 +2294,9 @@ pub fn build_session_options_and_crate_config(
22962294
// and later convert it into a BTreeSet<(Option<String>, bool)>
22972295
// This allows to modify entries in-place to set their correct
22982296
// 'public' value
2299-
let mut externs: BTreeMap<_, BTreeMap<Option<String>, bool>> = BTreeMap::new();
2300-
for (arg, public) in matches.opt_strs("extern").into_iter().map(|v| (v, true))
2301-
.chain(matches.opt_strs("extern-private").into_iter().map(|v| (v, false))) {
2297+
let mut externs: BTreeMap<String, ExternEntry> = BTreeMap::new();
2298+
for (arg, private) in matches.opt_strs("extern").into_iter().map(|v| (v, false))
2299+
.chain(matches.opt_strs("extern-private").into_iter().map(|v| (v, true))) {
23022300

23032301
let mut parts = arg.splitn(2, '=');
23042302
let name = parts.next().unwrap_or_else(||
@@ -2313,36 +2311,26 @@ pub fn build_session_options_and_crate_config(
23132311
};
23142312

23152313

2316-
// Extern crates start out public,
2317-
// and become private if we later see
2318-
// an '--extern-private' key. They never
2319-
// go back to being public once we've seen
2320-
// '--extern-private', so we logical-AND
2321-
// their current and new 'public' value together
2322-
23232314
externs
23242315
.entry(name.to_owned())
2325-
.or_default()
2326-
.entry(location)
2327-
.and_modify(|e| *e &= public)
2328-
.or_insert(public);
2329-
}
2316+
.and_modify(|e| {
2317+
e.locations.insert(location.clone());
2318+
2319+
// Crates start out being not private,
2320+
// and go to being private if we see an '--extern-private'
2321+
// flag
2322+
e.is_private_dep |= private;
2323+
})
2324+
.or_insert_with(|| {
2325+
let mut locations = BTreeSet::new();
2326+
locations.insert(location);
23302327

2331-
// Now that we've determined the 'public' status of each extern,
2332-
// collect them into a set of ExternEntry
2333-
let externs: BTreeMap<String, BTreeSet<ExternEntry>> = externs.into_iter()
2334-
.map(|(k, v)| {
2335-
let values =v.into_iter().map(|(location, public)| {
23362328
ExternEntry {
2337-
location,
2338-
public
2329+
locations: locations,
2330+
is_private_dep: private
23392331
}
2340-
}).collect::<BTreeSet<ExternEntry>>();
2341-
(k, values)
2342-
})
2343-
.collect();
2344-
2345-
2332+
});
2333+
}
23462334

23472335
let crate_name = matches.opt_str("crate-name");
23482336

@@ -2671,9 +2659,11 @@ mod tests {
26712659

26722660
impl ExternEntry {
26732661
fn new_public(location: Option<String>) -> ExternEntry {
2662+
let mut locations = BTreeSet::new();
2663+
locations.insert(location);
26742664
ExternEntry {
2675-
location,
2676-
public: true
2665+
locations,
2666+
is_private_dep: false
26772667
}
26782668
}
26792669
}

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)