Skip to content

Commit

Permalink
Auto merge of #31499 - kamalmarhubi:cfg-flag-invalid-cfgs, r=brson
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Feb 10, 2016
2 parents 32d962d + c32c7c2 commit 052b3fd
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 21 deletions.
81 changes: 60 additions & 21 deletions src/librustc_driver/lib.rs
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -324,34 +329,68 @@ 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));
}
}
}

fn check_cfg(sopts: &config::Options,
output: ErrorOutputType) {
let mut emitter: Box<Emitter> = 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::MetaList(ref pred, _) => {
saw_invalid_predicate = true;
emitter.emit(None,
&format!("invalid predicate in --cfg command line argument: `{}`",
pred),
None,
errors::Level::Fatal);
}
_ => {},
}
}

if saw_invalid_predicate {
panic!(errors::FatalError);
}
}

impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
fn early_callback(&mut self,
matches: &getopts::Matches,
sopts: &config::Options,
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;
check_cfg(sopts, output);
Compilation::Continue
}

fn no_input(&mut self,
Expand Down
12 changes: 12 additions & 0 deletions 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 <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.

#[cfg(foo(bar))] //~ ERROR invalid predicate `foo`
fn main() {}
13 changes: 13 additions & 0 deletions 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 <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 foo(bar)
// error-pattern: invalid predicate in --cfg command line argument: `foo`
fn main() {}
1 change: 1 addition & 0 deletions src/test/run-pass-fulldeps/compiler-calls.rs
Expand Up @@ -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 {
Expand Down

0 comments on commit 052b3fd

Please sign in to comment.