Skip to content

Commit

Permalink
Tighten up error checking of library renames.
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimcn committed Dec 2, 2016
1 parent a9a6f8c commit a23c470
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 22 deletions.
42 changes: 37 additions & 5 deletions src/librustc_metadata/creader.rs
Expand Up @@ -946,19 +946,51 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
}

// Process libs passed on the command line
// First, check for errors
let mut renames = FxHashSet();
for &(ref name, ref new_name, _) in &self.sess.opts.libs {
if let &Some(ref new_name) = new_name {
if new_name.is_empty() {
self.sess.err(
&format!("an empty renaming target was specified for library `{}`",name));
} else if !self.cstore.get_used_libraries().borrow().iter()
.any(|lib| lib.name == name as &str) {
self.sess.err(&format!("renaming of the library `{}` was specified, \
however this crate contains no #[link(...)] \
attributes referencing this library.", name));
} else if renames.contains(name) {
self.sess.err(&format!("multiple renamings were specified for library `{}` .",
name));
} else {
renames.insert(name);
}
}
}
// Update kind and, optionally, the name of all native libaries
// (there may be more than one) with the specified name.
for &(ref name, ref new_name, kind) in &self.sess.opts.libs {
// First, try to update existing lib(s) added via #[link(...)]
let new_name = new_name.as_ref().map(|s| &**s); // &Option<String> -> Option<&str>
if !self.cstore.update_used_library(name, new_name, kind) {
let mut found = false;
for lib in self.cstore.get_used_libraries().borrow_mut().iter_mut() {
if lib.name == name as &str {
lib.kind = kind;
if let &Some(ref new_name) = new_name {
lib.name = Symbol::intern(new_name);
}
found = true;
break;
}
}
if !found {
// Add if not found
let new_name = new_name.as_ref().map(|s| &**s); // &Option<String> -> Option<&str>
let lib = NativeLibrary {
name: Symbol::intern(name),
name: Symbol::intern(new_name.unwrap_or(name)),
kind: kind,
cfg: None,
foreign_items: Vec::new(),
};
register_native_lib(self.sess, self.cstore, None, lib);
}
}
}
self.register_statically_included_foreign_items();
self.register_dllimport_foreign_items();
Expand Down
17 changes: 0 additions & 17 deletions src/librustc_metadata/cstore.rs
Expand Up @@ -232,23 +232,6 @@ impl CStore {
self.used_libraries.borrow_mut().push(lib);
}

// Update kind and, optionally, the name of all native libaries (there may be more than one)
// with the specified name.
pub fn update_used_library(&self, name: &str, new_name: Option<&str>,
new_kind: NativeLibraryKind) -> bool {
let mut found = false;
for item in self.used_libraries.borrow_mut().iter_mut() {
if item.name == name {
item.kind = new_kind;
if let Some(new_name) = new_name {
item.name = Symbol::intern(new_name);
}
found = true;
}
}
found
}

pub fn get_used_libraries(&self) -> &RefCell<Vec<NativeLibrary>> {
&self.used_libraries
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/compile-fail/rfc1717/missing-link-attr.rs
@@ -0,0 +1,14 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -l foo:bar
// error-pattern: renaming of the library `foo` was specified

#![crate_type = "lib"]
17 changes: 17 additions & 0 deletions src/test/compile-fail/rfc1717/multiple-renames.rs
@@ -0,0 +1,17 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -l foo:bar -l foo:baz
// error-pattern: multiple renamings were specified for library

#![crate_type = "lib"]

#[link(name = "foo")]
extern "C" {}
17 changes: 17 additions & 0 deletions src/test/compile-fail/rfc1717/rename-to-empty.rs
@@ -0,0 +1,17 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -l foo:
// error-pattern: an empty renaming target was specified for library

#![crate_type = "lib"]

#[link(name = "foo")]
extern "C" {}

0 comments on commit a23c470

Please sign in to comment.