Skip to content

Commit 8e3df86

Browse files
committed
Improve error message, when found staticlib instead crate
1 parent 706be5b commit 8e3df86

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

src/librustc/metadata/creader.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -419,6 +419,7 @@ impl<'a> CrateReader<'a> {
419419
root: root,
420420
rejected_via_hash: vec!(),
421421
rejected_via_triple: vec!(),
422+
rejected_via_kind: vec!(),
422423
should_match_name: true,
423424
};
424425
let library = load_ctxt.load_library_crate();
@@ -483,6 +484,7 @@ impl<'a> CrateReader<'a> {
483484
root: &None,
484485
rejected_via_hash: vec!(),
485486
rejected_via_triple: vec!(),
487+
rejected_via_kind: vec!(),
486488
should_match_name: true,
487489
};
488490
let library = match load_ctxt.maybe_load_library_crate() {

src/librustc/metadata/loader.rs

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -257,6 +257,7 @@ pub struct Context<'a> {
257257
pub root: &'a Option<CratePaths>,
258258
pub rejected_via_hash: Vec<CrateMismatch>,
259259
pub rejected_via_triple: Vec<CrateMismatch>,
260+
pub rejected_via_kind: Vec<CrateMismatch>,
260261
pub should_match_name: bool,
261262
}
262263

@@ -311,6 +312,8 @@ impl<'a> Context<'a> {
311312
} else if self.rejected_via_triple.len() > 0 {
312313
format!("couldn't find crate `{}` with expected target triple {}",
313314
self.ident, self.triple)
315+
} else if self.rejected_via_kind.len() > 0 {
316+
format!("found staticlib `{}` instead of rlib or dylib", self.ident)
314317
} else {
315318
format!("can't find crate for `{}`", self.ident)
316319
};
@@ -335,8 +338,8 @@ impl<'a> Context<'a> {
335338
let mismatches = self.rejected_via_hash.iter();
336339
for (i, &CrateMismatch{ ref path, .. }) in mismatches.enumerate() {
337340
self.sess.fileline_note(self.span,
338-
&format!("crate `{}` path {}{}: {}",
339-
self.ident, "#", i+1, path.display())[]);
341+
&format!("crate `{}` path #{}: {}",
342+
self.ident, i+1, path.display())[]);
340343
}
341344
match self.root {
342345
&None => {}
@@ -349,6 +352,16 @@ impl<'a> Context<'a> {
349352
}
350353
}
351354
}
355+
if self.rejected_via_kind.len() > 0 {
356+
self.sess.span_help(self.span, "please recompile this crate using \
357+
--crate-type lib");
358+
let mismatches = self.rejected_via_kind.iter();
359+
for (i, &CrateMismatch { ref path, .. }) in mismatches.enumerate() {
360+
self.sess.fileline_note(self.span,
361+
&format!("crate `{}` path #{}: {}",
362+
self.ident, i+1, path.display())[]);
363+
}
364+
}
352365
self.sess.abort_if_errors();
353366
}
354367

@@ -369,8 +382,10 @@ impl<'a> Context<'a> {
369382
// want: crate_name.dir_part() + prefix + crate_name.file_part + "-"
370383
let dylib_prefix = format!("{}{}", dypair.0, self.crate_name);
371384
let rlib_prefix = format!("lib{}", self.crate_name);
385+
let staticlib_prefix = format!("lib{}", self.crate_name);
372386

373387
let mut candidates = HashMap::new();
388+
let mut staticlibs = vec!();
374389

375390
// First, find all possible candidate rlibs and dylibs purely based on
376391
// the name of the files themselves. We're trying to match against an
@@ -391,14 +406,21 @@ impl<'a> Context<'a> {
391406
Some(file) => file,
392407
};
393408
let (hash, rlib) = if file.starts_with(&rlib_prefix[]) &&
394-
file.ends_with(".rlib") {
409+
file.ends_with(".rlib") {
395410
(&file[(rlib_prefix.len()) .. (file.len() - ".rlib".len())],
396411
true)
397412
} else if file.starts_with(&dylib_prefix) &&
398413
file.ends_with(&dypair.1) {
399414
(&file[(dylib_prefix.len()) .. (file.len() - dypair.1.len())],
400415
false)
401416
} else {
417+
if file.starts_with(&staticlib_prefix[]) &&
418+
file.ends_with(".a") {
419+
staticlibs.push(CrateMismatch {
420+
path: path.clone(),
421+
got: "static".to_string()
422+
});
423+
}
402424
return FileDoesntMatch
403425
};
404426
info!("lib candidate: {}", path.display());
@@ -415,6 +437,7 @@ impl<'a> Context<'a> {
415437

416438
FileMatches
417439
});
440+
self.rejected_via_kind.extend(staticlibs.into_iter());
418441

419442
// We have now collected all known libraries into a set of candidates
420443
// keyed of the filename hash listed. For each filename, we also have a
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) foo.rs --crate-type staticlib
5+
$(RUSTC) bar.rs 2>&1 | grep "error: found staticlib"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2015 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+
extern crate foo;
12+
13+
fn main() {
14+
foo::foo();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2015 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+
pub fn foo() {}

0 commit comments

Comments
 (0)