From b122064a33884a5dab1e5f4710b38ed233d2b4d5 Mon Sep 17 00:00:00 2001 From: Dirk Gadsden Date: Tue, 9 Feb 2016 22:48:47 -0800 Subject: [PATCH] Stricter matching of `--cfg` options on rustc Includes compile-fail test to check that it fails on incomplete `--cfg` matches. Fixes #31497. --- src/librustc/session/config.rs | 18 ++++++++++++++---- src/test/compile-fail/cfg-arg-invalid.rs | 13 +++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 src/test/compile-fail/cfg-arg-invalid.rs diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index c96713a72851a..28422989ea159 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -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; @@ -906,10 +907,19 @@ pub fn rustc_optgroups() -> Vec { // Convert strings provided as --cfg [cfgspec] into a crate_cfg pub fn parse_cfgspecs(cfgspecs: Vec ) -> 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::() } diff --git a/src/test/compile-fail/cfg-arg-invalid.rs b/src/test/compile-fail/cfg-arg-invalid.rs new file mode 100644 index 0000000000000..404630399c601 --- /dev/null +++ b/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 or the MIT license +// , 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() {}