From 6d2c866e22ad1999d4a16b6b7345e031ef74655c Mon Sep 17 00:00:00 2001 From: Kamal Marhubi Date: Mon, 8 Feb 2016 16:38:35 -0500 Subject: [PATCH] driver: Disallow predicates in --cfg specs A spec like `#[cfg(foo(bar))]` is not allowed as an attribute. This makes the same spec be rejected by the compiler if passed in as a `--cfg` argument. Fixes #31495 --- src/librustc_driver/lib.rs | 17 ++++++++++++++++- src/test/compile-fail/issue-31495.rs | 13 +++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/test/compile-fail/issue-31495.rs diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 5b0ee3cf973b..c4a1272e6d04 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -348,10 +348,24 @@ fn handle_explain(code: &str, } } +fn check_cfg(sopts: &config::Options, + output: ErrorOutputType) { + fn is_meta_list(item: &ast::MetaItem) -> bool { + match item.node { + ast::MetaItem_::MetaList(..) => true, + _ => false, + } + } + + if sopts.cfg.iter().any(|item| is_meta_list(&*item)) { + early_error(output, "predicates are not allowed in --cfg"); + } +} + impl<'a> CompilerCalls<'a> for RustcDefaultCalls { fn early_callback(&mut self, matches: &getopts::Matches, - _sopts: &config::Options, + sopts: &config::Options, descriptions: &diagnostics::registry::Registry, output: ErrorOutputType) -> Compilation { @@ -360,6 +374,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls { return Compilation::Stop; } + check_cfg(sopts, output); Compilation::Continue } diff --git a/src/test/compile-fail/issue-31495.rs b/src/test/compile-fail/issue-31495.rs new file mode 100644 index 000000000000..0a09b65700ab --- /dev/null +++ b/src/test/compile-fail/issue-31495.rs @@ -0,0 +1,13 @@ +// Copyright 2015 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 foo(bar) +// error-pattern: predicates are not allowed in --cfg +fn main() {}