Skip to content

Commit f98a9a6

Browse files
committed
Rollup merge of rust-lang#47372 - topecongiro:issue-43925, r=alexcrichton
Avoid panicking when invalid argument is passed to cfg(..) Closes rust-lang#43925. Closes rust-lang#43926.
2 parents afb1e19 + d088b25 commit f98a9a6

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

src/librustc_metadata/native_libs.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,19 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for Collector<'a, 'tcx> {
9292
let cfg = items.iter().find(|k| {
9393
k.check_name("cfg")
9494
}).and_then(|a| a.meta_item_list());
95-
let cfg = cfg.map(|list| {
96-
list[0].meta_item().unwrap().clone()
97-
});
95+
let cfg = if let Some(list) = cfg {
96+
if list.is_empty() {
97+
self.tcx.sess.span_err(m.span(), "`cfg()` must have an argument");
98+
return;
99+
} else if let cfg @ Some(..) = list[0].meta_item() {
100+
cfg.cloned()
101+
} else {
102+
self.tcx.sess.span_err(list[0].span(), "invalid argument for `cfg(..)`");
103+
return;
104+
}
105+
} else {
106+
None
107+
};
98108
let foreign_items = fm.items.iter()
99109
.map(|it| self.tcx.hir.local_def_id(it.id))
100110
.collect();

src/test/compile-fail/issue-43925.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 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+
#![feature(attr_literals)]
12+
13+
#[link(name="foo", cfg("rlib"))] //~ ERROR invalid argument for `cfg(..)`
14+
extern {}
15+
16+
fn main() {}

src/test/compile-fail/issue-43926.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2018 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+
#[link(name="foo", cfg())] //~ ERROR `cfg()` must have an argument
12+
extern {}
13+
14+
fn main() {}

0 commit comments

Comments
 (0)