Skip to content

Commit

Permalink
Avoid panicking when invalid argument is passed to cfg(..)
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro committed Jan 12, 2018
1 parent 73ac5d6 commit d088b25
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/librustc_metadata/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,19 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for Collector<'a, 'tcx> {
let cfg = items.iter().find(|k| {
k.check_name("cfg")
}).and_then(|a| a.meta_item_list());
let cfg = cfg.map(|list| {
list[0].meta_item().unwrap().clone()
});
let cfg = if let Some(list) = cfg {
if list.is_empty() {
self.tcx.sess.span_err(m.span(), "`cfg()` must have an argument");
return;
} else if let cfg @ Some(..) = list[0].meta_item() {
cfg.cloned()
} else {
self.tcx.sess.span_err(list[0].span(), "invalid argument for `cfg(..)`");
return;
}
} else {
None
};
let foreign_items = fm.items.iter()
.map(|it| self.tcx.hir.local_def_id(it.id))
.collect();
Expand Down
16 changes: 16 additions & 0 deletions src/test/compile-fail/issue-43925.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2018 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.

#![feature(attr_literals)]

#[link(name="foo", cfg("rlib"))] //~ ERROR invalid argument for `cfg(..)`
extern {}

fn main() {}
14 changes: 14 additions & 0 deletions src/test/compile-fail/issue-43926.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2018 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.

#[link(name="foo", cfg())] //~ ERROR `cfg()` must have an argument
extern {}

fn main() {}

0 comments on commit d088b25

Please sign in to comment.