Skip to content

Commit b122064

Browse files
committed
Stricter matching of --cfg options on rustc
Includes compile-fail test to check that it fails on incomplete `--cfg` matches. Fixes #31497.
1 parent a888333 commit b122064

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/librustc/session/config.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use syntax::attr;
2828
use syntax::attr::AttrMetaMethods;
2929
use syntax::errors::{ColorConfig, Handler};
3030
use syntax::parse;
31+
use syntax::parse::lexer::Reader;
3132
use syntax::parse::token::InternedString;
3233
use syntax::feature_gate::UnstableFeatures;
3334

@@ -906,10 +907,19 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
906907
// Convert strings provided as --cfg [cfgspec] into a crate_cfg
907908
pub fn parse_cfgspecs(cfgspecs: Vec<String> ) -> ast::CrateConfig {
908909
cfgspecs.into_iter().map(|s| {
909-
parse::parse_meta_from_source_str("cfgspec".to_string(),
910-
s.to_string(),
911-
Vec::new(),
912-
&parse::ParseSess::new())
910+
let sess = parse::ParseSess::new();
911+
let mut parser = parse::new_parser_from_source_str(&sess,
912+
Vec::new(),
913+
"cfgspec".to_string(),
914+
s.to_string());
915+
let meta_item = panictry!(parser.parse_meta_item());
916+
917+
if !parser.reader.is_eof() {
918+
early_error(ErrorOutputType::default(), &format!("invalid --cfg argument: {}",
919+
s))
920+
}
921+
922+
meta_item
913923
}).collect::<ast::CrateConfig>()
914924
}
915925

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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: --cfg a{b}
12+
// error-pattern: invalid --cfg argument: a{b}
13+
fn main() {}

0 commit comments

Comments
 (0)