From 601e04534f4f8117f4d121bdd9bfb5ed100c72e6 Mon Sep 17 00:00:00 2001 From: Kamal Marhubi Date: Mon, 8 Feb 2016 14:55:38 -0500 Subject: [PATCH 1/5] Add compile-fail test for invalid cfg predicate in attribute Refs #31495 --- src/test/compile-fail/cfg-attr-invalid-predicate.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/test/compile-fail/cfg-attr-invalid-predicate.rs diff --git a/src/test/compile-fail/cfg-attr-invalid-predicate.rs b/src/test/compile-fail/cfg-attr-invalid-predicate.rs new file mode 100644 index 0000000000000..09fe6cec49c2e --- /dev/null +++ b/src/test/compile-fail/cfg-attr-invalid-predicate.rs @@ -0,0 +1,12 @@ +// 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. + +#[cfg(foo(bar))] //~ ERROR invalid predicate `foo` +fn main() {} From 9951ac4be90848bbc12e8187886047ae35795c8d Mon Sep 17 00:00:00 2001 From: Kamal Marhubi Date: Mon, 8 Feb 2016 15:43:01 -0500 Subject: [PATCH 2/5] driver: Pass session options to CompilerCallbacks::early_callback() --- src/librustc_driver/lib.rs | 8 +++++++- src/test/run-pass-fulldeps/compiler-calls.rs | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 70bd938321a8e..fbed5f31725ee 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -164,7 +164,11 @@ pub fn run_compiler<'a>(args: &[String], let descriptions = diagnostics_registry(); - do_or_return!(callbacks.early_callback(&matches, &descriptions, sopts.error_format), None); + do_or_return!(callbacks.early_callback(&matches, + &sopts, + &descriptions, + sopts.error_format), + None); let (odir, ofile) = make_output(&matches); let (input, input_file_path) = match make_input(&matches.free) { @@ -251,6 +255,7 @@ pub trait CompilerCalls<'a> { // else (e.g., selecting input and output). fn early_callback(&mut self, _: &getopts::Matches, + _: &config::Options, _: &diagnostics::registry::Registry, _: ErrorOutputType) -> Compilation { @@ -327,6 +332,7 @@ pub struct RustcDefaultCalls; impl<'a> CompilerCalls<'a> for RustcDefaultCalls { fn early_callback(&mut self, matches: &getopts::Matches, + _sopts: &config::Options, descriptions: &diagnostics::registry::Registry, output: ErrorOutputType) -> Compilation { diff --git a/src/test/run-pass-fulldeps/compiler-calls.rs b/src/test/run-pass-fulldeps/compiler-calls.rs index 56481dc646a9c..42784e009ee44 100644 --- a/src/test/run-pass-fulldeps/compiler-calls.rs +++ b/src/test/run-pass-fulldeps/compiler-calls.rs @@ -34,6 +34,7 @@ struct TestCalls { impl<'a> CompilerCalls<'a> for TestCalls { fn early_callback(&mut self, _: &getopts::Matches, + _: &config::Options, _: &diagnostics::registry::Registry, _: config::ErrorOutputType) -> Compilation { From 4c4bb5ff5c5d660c565d6d79f2204e23b673d39e Mon Sep 17 00:00:00 2001 From: Kamal Marhubi Date: Mon, 8 Feb 2016 15:53:05 -0500 Subject: [PATCH 3/5] driver: Extract handling of --explain to separate function --- src/librustc_driver/lib.rs | 43 ++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index fbed5f31725ee..5b0ee3cf973b0 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -329,6 +329,25 @@ pub trait CompilerCalls<'a> { #[derive(Copy, Clone)] pub struct RustcDefaultCalls; +fn handle_explain(code: &str, + descriptions: &diagnostics::registry::Registry, + output: ErrorOutputType) { + let normalised = if !code.starts_with("E") { + format!("E{0:0>4}", code) + } else { + code.to_string() + }; + match descriptions.find_description(&normalised) { + Some(ref description) => { + // Slice off the leading newline and print. + print!("{}", &description[1..]); + } + None => { + early_error(output, &format!("no extended information for {}", code)); + } + } +} + impl<'a> CompilerCalls<'a> for RustcDefaultCalls { fn early_callback(&mut self, matches: &getopts::Matches, @@ -336,28 +355,12 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls { descriptions: &diagnostics::registry::Registry, output: ErrorOutputType) -> Compilation { - match matches.opt_str("explain") { - Some(ref code) => { - let normalised = if !code.starts_with("E") { - format!("E{0:0>4}", code) - } else { - code.to_string() - }; - match descriptions.find_description(&normalised) { - Some(ref description) => { - // Slice off the leading newline and print. - print!("{}", &description[1..]); - } - None => { - early_error(output, &format!("no extended information for {}", code)); - } - } - return Compilation::Stop; - } - None => (), + if let Some(ref code) = matches.opt_str("explain") { + handle_explain(code, descriptions, output); + return Compilation::Stop; } - return Compilation::Continue; + Compilation::Continue } fn no_input(&mut self, From 6d2c866e22ad1999d4a16b6b7345e031ef74655c Mon Sep 17 00:00:00 2001 From: Kamal Marhubi Date: Mon, 8 Feb 2016 16:38:35 -0500 Subject: [PATCH 4/5] 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 5b0ee3cf973b0..c4a1272e6d04d 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 0000000000000..0a09b65700ab0 --- /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() {} From c32c7c24860eb6ed65bdec2491ba99b6eb5550b8 Mon Sep 17 00:00:00 2001 From: Kamal Marhubi Date: Mon, 8 Feb 2016 18:47:03 -0500 Subject: [PATCH 5/5] driver: Include invalid predicate in error message --- src/librustc_driver/lib.rs | 25 ++++++++++++++++++++----- src/test/compile-fail/issue-31495.rs | 2 +- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index c4a1272e6d04d..eb50e4ef99f6b 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -350,15 +350,30 @@ fn handle_explain(code: &str, fn check_cfg(sopts: &config::Options, output: ErrorOutputType) { - fn is_meta_list(item: &ast::MetaItem) -> bool { + let mut emitter: Box = match output { + config::ErrorOutputType::HumanReadable(color_config) => { + Box::new(errors::emitter::BasicEmitter::stderr(color_config)) + } + config::ErrorOutputType::Json => Box::new(errors::json::JsonEmitter::basic()), + }; + + let mut saw_invalid_predicate = false; + for item in sopts.cfg.iter() { match item.node { - ast::MetaItem_::MetaList(..) => true, - _ => false, + ast::MetaList(ref pred, _) => { + saw_invalid_predicate = true; + emitter.emit(None, + &format!("invalid predicate in --cfg command line argument: `{}`", + pred), + None, + errors::Level::Fatal); + } + _ => {}, } } - if sopts.cfg.iter().any(|item| is_meta_list(&*item)) { - early_error(output, "predicates are not allowed in --cfg"); + if saw_invalid_predicate { + panic!(errors::FatalError); } } diff --git a/src/test/compile-fail/issue-31495.rs b/src/test/compile-fail/issue-31495.rs index 0a09b65700ab0..794b8bb86bb57 100644 --- a/src/test/compile-fail/issue-31495.rs +++ b/src/test/compile-fail/issue-31495.rs @@ -9,5 +9,5 @@ // except according to those terms. // compile-flags: --cfg foo(bar) -// error-pattern: predicates are not allowed in --cfg +// error-pattern: invalid predicate in --cfg command line argument: `foo` fn main() {}