Skip to content

Commit

Permalink
Stricter matching of --cfg options on rustc
Browse files Browse the repository at this point in the history
Includes compile-fail test to check that it fails on incomplete
`--cfg` matches.

Fixes #31497.
  • Loading branch information
dirk committed Feb 15, 2016
1 parent a888333 commit b122064
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/librustc/session/config.rs
Expand Up @@ -28,6 +28,7 @@ use syntax::attr;
use syntax::attr::AttrMetaMethods;
use syntax::errors::{ColorConfig, Handler};
use syntax::parse;
use syntax::parse::lexer::Reader;
use syntax::parse::token::InternedString;
use syntax::feature_gate::UnstableFeatures;

Expand Down Expand Up @@ -906,10 +907,19 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
// Convert strings provided as --cfg [cfgspec] into a crate_cfg
pub fn parse_cfgspecs(cfgspecs: Vec<String> ) -> ast::CrateConfig {
cfgspecs.into_iter().map(|s| {
parse::parse_meta_from_source_str("cfgspec".to_string(),
s.to_string(),
Vec::new(),
&parse::ParseSess::new())
let sess = parse::ParseSess::new();
let mut parser = parse::new_parser_from_source_str(&sess,
Vec::new(),
"cfgspec".to_string(),
s.to_string());
let meta_item = panictry!(parser.parse_meta_item());

if !parser.reader.is_eof() {
early_error(ErrorOutputType::default(), &format!("invalid --cfg argument: {}",
s))
}

meta_item
}).collect::<ast::CrateConfig>()
}

Expand Down
13 changes: 13 additions & 0 deletions src/test/compile-fail/cfg-arg-invalid.rs
@@ -0,0 +1,13 @@
// 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: --cfg a{b}
// error-pattern: invalid --cfg argument: a{b}
fn main() {}

0 comments on commit b122064

Please sign in to comment.