Skip to content

driver: Disallow predicates in --cfg specs #31499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 60 additions & 21 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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