Skip to content

Commit a23c470

Browse files
committed
Tighten up error checking of library renames.
1 parent a9a6f8c commit a23c470

File tree

5 files changed

+85
-22
lines changed

5 files changed

+85
-22
lines changed

src/librustc_metadata/creader.rs

+37-5
Original file line numberDiff line numberDiff line change
@@ -946,19 +946,51 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
946946
}
947947

948948
// Process libs passed on the command line
949+
// First, check for errors
950+
let mut renames = FxHashSet();
951+
for &(ref name, ref new_name, _) in &self.sess.opts.libs {
952+
if let &Some(ref new_name) = new_name {
953+
if new_name.is_empty() {
954+
self.sess.err(
955+
&format!("an empty renaming target was specified for library `{}`",name));
956+
} else if !self.cstore.get_used_libraries().borrow().iter()
957+
.any(|lib| lib.name == name as &str) {
958+
self.sess.err(&format!("renaming of the library `{}` was specified, \
959+
however this crate contains no #[link(...)] \
960+
attributes referencing this library.", name));
961+
} else if renames.contains(name) {
962+
self.sess.err(&format!("multiple renamings were specified for library `{}` .",
963+
name));
964+
} else {
965+
renames.insert(name);
966+
}
967+
}
968+
}
969+
// Update kind and, optionally, the name of all native libaries
970+
// (there may be more than one) with the specified name.
949971
for &(ref name, ref new_name, kind) in &self.sess.opts.libs {
950-
// First, try to update existing lib(s) added via #[link(...)]
951-
let new_name = new_name.as_ref().map(|s| &**s); // &Option<String> -> Option<&str>
952-
if !self.cstore.update_used_library(name, new_name, kind) {
972+
let mut found = false;
973+
for lib in self.cstore.get_used_libraries().borrow_mut().iter_mut() {
974+
if lib.name == name as &str {
975+
lib.kind = kind;
976+
if let &Some(ref new_name) = new_name {
977+
lib.name = Symbol::intern(new_name);
978+
}
979+
found = true;
980+
break;
981+
}
982+
}
983+
if !found {
953984
// Add if not found
985+
let new_name = new_name.as_ref().map(|s| &**s); // &Option<String> -> Option<&str>
954986
let lib = NativeLibrary {
955-
name: Symbol::intern(name),
987+
name: Symbol::intern(new_name.unwrap_or(name)),
956988
kind: kind,
957989
cfg: None,
958990
foreign_items: Vec::new(),
959991
};
960992
register_native_lib(self.sess, self.cstore, None, lib);
961-
}
993+
}
962994
}
963995
self.register_statically_included_foreign_items();
964996
self.register_dllimport_foreign_items();

src/librustc_metadata/cstore.rs

-17
Original file line numberDiff line numberDiff line change
@@ -232,23 +232,6 @@ impl CStore {
232232
self.used_libraries.borrow_mut().push(lib);
233233
}
234234

235-
// Update kind and, optionally, the name of all native libaries (there may be more than one)
236-
// with the specified name.
237-
pub fn update_used_library(&self, name: &str, new_name: Option<&str>,
238-
new_kind: NativeLibraryKind) -> bool {
239-
let mut found = false;
240-
for item in self.used_libraries.borrow_mut().iter_mut() {
241-
if item.name == name {
242-
item.kind = new_kind;
243-
if let Some(new_name) = new_name {
244-
item.name = Symbol::intern(new_name);
245-
}
246-
found = true;
247-
}
248-
}
249-
found
250-
}
251-
252235
pub fn get_used_libraries(&self) -> &RefCell<Vec<NativeLibrary>> {
253236
&self.used_libraries
254237
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2016 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+
// compile-flags: -l foo:bar
12+
// error-pattern: renaming of the library `foo` was specified
13+
14+
#![crate_type = "lib"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2016 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+
// compile-flags: -l foo:bar -l foo:baz
12+
// error-pattern: multiple renamings were specified for library
13+
14+
#![crate_type = "lib"]
15+
16+
#[link(name = "foo")]
17+
extern "C" {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2016 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+
// compile-flags: -l foo:
12+
// error-pattern: an empty renaming target was specified for library
13+
14+
#![crate_type = "lib"]
15+
16+
#[link(name = "foo")]
17+
extern "C" {}

0 commit comments

Comments
 (0)