Skip to content

Commit ab7bb51

Browse files
authored
Rollup merge of rust-lang#49253 - chmanchester:probing_fix, r=alexcrichton
Take the original extra-filename passed to a crate into account when resolving it as a dependency resolving it as a dependency. Fixes rust-lang#46816
2 parents 0afb655 + bd81547 commit ab7bb51

File tree

13 files changed

+107
-14
lines changed

13 files changed

+107
-14
lines changed

src/librustc/dep_graph/dep_node.rs

+1
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ define_dep_nodes!( <'tcx>
589589
[input] CrateDisambiguator(CrateNum),
590590
[input] CrateHash(CrateNum),
591591
[input] OriginalCrateName(CrateNum),
592+
[input] ExtraFileName(CrateNum),
592593

593594
[] ImplementationsOfTrait { krate: CrateNum, trait_id: DefId },
594595
[] AllTraitImplementations(CrateNum),

src/librustc/ty/maps/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,12 @@ impl<'tcx> QueryDescription<'tcx> for queries::original_crate_name<'tcx> {
466466
}
467467
}
468468

469+
impl<'tcx> QueryDescription<'tcx> for queries::extra_filename<'tcx> {
470+
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
471+
format!("looking up the extra filename for a crate")
472+
}
473+
}
474+
469475
impl<'tcx> QueryDescription<'tcx> for queries::implementations_of_trait<'tcx> {
470476
fn describe(_tcx: TyCtxt, _: (CrateNum, DefId)) -> String {
471477
format!("looking up implementations of a trait in a crate")

src/librustc/ty/maps/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ define_maps! { <'tcx>
328328
[] fn crate_disambiguator: CrateDisambiguator(CrateNum) -> CrateDisambiguator,
329329
[] fn crate_hash: CrateHash(CrateNum) -> Svh,
330330
[] fn original_crate_name: OriginalCrateName(CrateNum) -> Symbol,
331+
[] fn extra_filename: ExtraFileName(CrateNum) -> String,
331332

332333
[] fn implementations_of_trait: implementations_of_trait_node((CrateNum, DefId))
333334
-> Lrc<Vec<DefId>>,

src/librustc/ty/maps/plumbing.rs

+1
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
881881
DepKind::CrateDisambiguator => { force!(crate_disambiguator, krate!()); }
882882
DepKind::CrateHash => { force!(crate_hash, krate!()); }
883883
DepKind::OriginalCrateName => { force!(original_crate_name, krate!()); }
884+
DepKind::ExtraFileName => { force!(extra_filename, krate!()); }
884885

885886
DepKind::AllTraitImplementations => {
886887
force!(all_trait_implementations, krate!());

src/librustc_metadata/creader.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ impl<'a> CrateLoader<'a> {
262262
ident: Symbol,
263263
name: Symbol,
264264
hash: Option<&Svh>,
265+
extra_filename: Option<&str>,
265266
span: Span,
266267
path_kind: PathKind,
267268
mut dep_kind: DepKind)
@@ -277,6 +278,7 @@ impl<'a> CrateLoader<'a> {
277278
ident,
278279
crate_name: name,
279280
hash: hash.map(|a| &*a),
281+
extra_filename: extra_filename,
280282
filesearch: self.sess.target_filesearch(path_kind),
281283
target: &self.sess.target.target,
282284
triple: &self.sess.opts.target_triple,
@@ -409,7 +411,8 @@ impl<'a> CrateLoader<'a> {
409411
::std::iter::once(krate).chain(crate_root.crate_deps
410412
.decode(metadata)
411413
.map(|dep| {
412-
debug!("resolving dep crate {} hash: `{}`", dep.name, dep.hash);
414+
info!("resolving dep crate {} hash: `{}` extra filename: `{}`", dep.name, dep.hash,
415+
dep.extra_filename);
413416
if dep.kind == DepKind::UnexportedMacrosOnly {
414417
return krate;
415418
}
@@ -418,7 +421,8 @@ impl<'a> CrateLoader<'a> {
418421
_ => dep.kind,
419422
};
420423
let (local_cnum, ..) = self.resolve_crate(
421-
root, dep.name, dep.name, Some(&dep.hash), span, PathKind::Dependency, dep_kind,
424+
root, dep.name, dep.name, Some(&dep.hash), Some(&dep.extra_filename), span,
425+
PathKind::Dependency, dep_kind,
422426
);
423427
local_cnum
424428
})).collect()
@@ -437,6 +441,7 @@ impl<'a> CrateLoader<'a> {
437441
ident: orig_name,
438442
crate_name: rename,
439443
hash: None,
444+
extra_filename: None,
440445
filesearch: self.sess.host_filesearch(PathKind::Crate),
441446
target: &self.sess.host,
442447
triple: &host_triple,
@@ -664,7 +669,7 @@ impl<'a> CrateLoader<'a> {
664669

665670
let dep_kind = DepKind::Implicit;
666671
let (cnum, data) =
667-
self.resolve_crate(&None, name, name, None, DUMMY_SP, PathKind::Crate, dep_kind);
672+
self.resolve_crate(&None, name, name, None, None, DUMMY_SP, PathKind::Crate, dep_kind);
668673

669674
// Sanity check the loaded crate to ensure it is indeed a panic runtime
670675
// and the panic strategy is indeed what we thought it was.
@@ -771,7 +776,7 @@ impl<'a> CrateLoader<'a> {
771776
let symbol = Symbol::intern(name);
772777
let dep_kind = DepKind::Explicit;
773778
let (_, data) =
774-
self.resolve_crate(&None, symbol, symbol, None, DUMMY_SP,
779+
self.resolve_crate(&None, symbol, symbol, None, None, DUMMY_SP,
775780
PathKind::Crate, dep_kind);
776781

777782
// Sanity check the loaded crate to ensure it is indeed a sanitizer runtime
@@ -794,7 +799,7 @@ impl<'a> CrateLoader<'a> {
794799
let symbol = Symbol::intern("profiler_builtins");
795800
let dep_kind = DepKind::Implicit;
796801
let (_, data) =
797-
self.resolve_crate(&None, symbol, symbol, None, DUMMY_SP,
802+
self.resolve_crate(&None, symbol, symbol, None, None, DUMMY_SP,
798803
PathKind::Crate, dep_kind);
799804

800805
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
@@ -909,6 +914,7 @@ impl<'a> CrateLoader<'a> {
909914
name,
910915
name,
911916
None,
917+
None,
912918
DUMMY_SP,
913919
PathKind::Crate,
914920
DepKind::Implicit);
@@ -1059,7 +1065,8 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10591065
};
10601066

10611067
let (cnum, ..) = self.resolve_crate(
1062-
&None, item.ident.name, orig_name, None, item.span, PathKind::Crate, dep_kind,
1068+
&None, item.ident.name, orig_name, None, None,
1069+
item.span, PathKind::Crate, dep_kind,
10631070
);
10641071

10651072
let def_id = definitions.opt_local_def_id(item.id).unwrap();
@@ -1074,6 +1081,7 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10741081
}
10751082

10761083
fn resolve_crate_from_path(&mut self, name: Symbol, span: Span) -> CrateNum {
1077-
self.resolve_crate(&None, name, name, None, span, PathKind::Crate, DepKind::Explicit).0
1084+
self.resolve_crate(&None, name, name, None, None, span, PathKind::Crate,
1085+
DepKind::Explicit).0
10781086
}
10791087
}

src/librustc_metadata/cstore_impl.rs

+3
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ provide! { <'tcx> tcx, def_id, other, cdata,
213213
crate_hash => { cdata.hash() }
214214
original_crate_name => { cdata.name() }
215215

216+
extra_filename => { cdata.root.extra_filename.clone() }
217+
218+
216219
implementations_of_trait => {
217220
let mut result = vec![];
218221
let filter = Some(other);

src/librustc_metadata/encoder.rs

+2
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
462462
let has_global_allocator = tcx.sess.has_global_allocator.get();
463463
let root = self.lazy(&CrateRoot {
464464
name: tcx.crate_name(LOCAL_CRATE),
465+
extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
465466
triple: tcx.sess.opts.target_triple.clone(),
466467
hash: link_meta.crate_hash,
467468
disambiguator: tcx.sess.local_crate_disambiguator(),
@@ -1357,6 +1358,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
13571358
name: self.tcx.original_crate_name(cnum),
13581359
hash: self.tcx.crate_hash(cnum),
13591360
kind: self.tcx.dep_kind(cnum),
1361+
extra_filename: self.tcx.extra_filename(cnum),
13601362
};
13611363
(cnum, dep)
13621364
})

src/librustc_metadata/locator.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@
8383
//! 1. Does the filename match an rlib/dylib pattern? That is to say, does the
8484
//! filename have the right prefix/suffix?
8585
//! 2. Does the filename have the right prefix for the crate name being queried?
86-
//! This is filtering for files like `libfoo*.rlib` and such.
86+
//! This is filtering for files like `libfoo*.rlib` and such. If the crate
87+
//! we're looking for was originally compiled with -C extra-filename, the
88+
//! extra filename will be included in this prefix to reduce reading
89+
//! metadata from crates that would otherwise share our prefix.
8790
//! 3. Is the file an actual rust library? This is done by loading the metadata
8891
//! from the library and making sure it's actually there.
8992
//! 4. Does the name in the metadata agree with the name of the library?
@@ -236,6 +239,7 @@ use syntax_pos::Span;
236239
use rustc_back::target::{Target, TargetTriple};
237240

238241
use std::cmp;
242+
use std::collections::HashSet;
239243
use std::fmt;
240244
use std::fs;
241245
use std::io::{self, Read};
@@ -256,6 +260,7 @@ pub struct Context<'a> {
256260
pub ident: Symbol,
257261
pub crate_name: Symbol,
258262
pub hash: Option<&'a Svh>,
263+
pub extra_filename: Option<&'a str>,
259264
// points to either self.sess.target.target or self.sess.host, must match triple
260265
pub target: &'a Target,
261266
pub triple: &'a TargetTriple,
@@ -303,7 +308,12 @@ impl CratePaths {
303308

304309
impl<'a> Context<'a> {
305310
pub fn maybe_load_library_crate(&mut self) -> Option<Library> {
306-
self.find_library_crate()
311+
let mut seen_paths = HashSet::new();
312+
match self.extra_filename {
313+
Some(s) => self.find_library_crate(s, &mut seen_paths)
314+
.or_else(|| self.find_library_crate("", &mut seen_paths)),
315+
None => self.find_library_crate("", &mut seen_paths)
316+
}
307317
}
308318

309319
pub fn report_errs(&mut self) -> ! {
@@ -419,7 +429,10 @@ impl<'a> Context<'a> {
419429
unreachable!();
420430
}
421431

422-
fn find_library_crate(&mut self) -> Option<Library> {
432+
fn find_library_crate(&mut self,
433+
extra_prefix: &str,
434+
seen_paths: &mut HashSet<PathBuf>)
435+
-> Option<Library> {
423436
// If an SVH is specified, then this is a transitive dependency that
424437
// must be loaded via -L plus some filtering.
425438
if self.hash.is_none() {
@@ -434,9 +447,9 @@ impl<'a> Context<'a> {
434447
let staticpair = self.staticlibname();
435448

436449
// want: crate_name.dir_part() + prefix + crate_name.file_part + "-"
437-
let dylib_prefix = format!("{}{}", dypair.0, self.crate_name);
438-
let rlib_prefix = format!("lib{}", self.crate_name);
439-
let staticlib_prefix = format!("{}{}", staticpair.0, self.crate_name);
450+
let dylib_prefix = format!("{}{}{}", dypair.0, self.crate_name, extra_prefix);
451+
let rlib_prefix = format!("lib{}{}", self.crate_name, extra_prefix);
452+
let staticlib_prefix = format!("{}{}{}", staticpair.0, self.crate_name, extra_prefix);
440453

441454
let mut candidates = FxHashMap();
442455
let mut staticlibs = vec![];
@@ -476,6 +489,7 @@ impl<'a> Context<'a> {
476489
}
477490
return FileDoesntMatch;
478491
};
492+
479493
info!("lib candidate: {}", path.display());
480494

481495
let hash_str = hash.to_string();
@@ -484,6 +498,10 @@ impl<'a> Context<'a> {
484498
let (ref mut rlibs, ref mut rmetas, ref mut dylibs) = *slot;
485499
fs::canonicalize(path)
486500
.map(|p| {
501+
if seen_paths.contains(&p) {
502+
return FileDoesntMatch
503+
};
504+
seen_paths.insert(p.clone());
487505
match found_kind {
488506
CrateFlavor::Rlib => { rlibs.insert(p, kind); }
489507
CrateFlavor::Rmeta => { rmetas.insert(p, kind); }

src/librustc_metadata/schema.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ pub enum LazyState {
188188
pub struct CrateRoot {
189189
pub name: Symbol,
190190
pub triple: TargetTriple,
191+
pub extra_filename: String,
191192
pub hash: hir::svh::Svh,
192193
pub disambiguator: CrateDisambiguator,
193194
pub panic_strategy: PanicStrategy,
@@ -216,12 +217,14 @@ pub struct CrateDep {
216217
pub name: ast::Name,
217218
pub hash: hir::svh::Svh,
218219
pub kind: DepKind,
220+
pub extra_filename: String,
219221
}
220222

221223
impl_stable_hash_for!(struct CrateDep {
222224
name,
223225
hash,
224-
kind
226+
kind,
227+
extra_filename
225228
});
226229

227230
#[derive(RustcEncodable, RustcDecodable)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) -C extra-filename=-hash foo.rs
5+
$(RUSTC) bar.rs
6+
mv $(TMPDIR)/libfoo-hash.rlib $(TMPDIR)/libfoo-another-hash.rlib
7+
$(RUSTC) baz.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
12+
13+
extern crate foo;
14+
15+
pub fn bar() { foo::foo() }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
12+
13+
extern crate bar;
14+
15+
pub fn baz() { bar::bar() }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
12+
13+
pub fn foo() {}

0 commit comments

Comments
 (0)