Skip to content

Commit 052b3fd

Browse files
committed
Auto merge of #31499 - kamalmarhubi:cfg-flag-invalid-cfgs, r=brson
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
2 parents 32d962d + c32c7c2 commit 052b3fd

File tree

4 files changed

+86
-21
lines changed

4 files changed

+86
-21
lines changed

src/librustc_driver/lib.rs

+60-21
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ pub fn run_compiler<'a>(args: &[String],
164164

165165
let descriptions = diagnostics_registry();
166166

167-
do_or_return!(callbacks.early_callback(&matches, &descriptions, sopts.error_format), None);
167+
do_or_return!(callbacks.early_callback(&matches,
168+
&sopts,
169+
&descriptions,
170+
sopts.error_format),
171+
None);
168172

169173
let (odir, ofile) = make_output(&matches);
170174
let (input, input_file_path) = match make_input(&matches.free) {
@@ -251,6 +255,7 @@ pub trait CompilerCalls<'a> {
251255
// else (e.g., selecting input and output).
252256
fn early_callback(&mut self,
253257
_: &getopts::Matches,
258+
_: &config::Options,
254259
_: &diagnostics::registry::Registry,
255260
_: ErrorOutputType)
256261
-> Compilation {
@@ -324,34 +329,68 @@ pub trait CompilerCalls<'a> {
324329
#[derive(Copy, Clone)]
325330
pub struct RustcDefaultCalls;
326331

332+
fn handle_explain(code: &str,
333+
descriptions: &diagnostics::registry::Registry,
334+
output: ErrorOutputType) {
335+
let normalised = if !code.starts_with("E") {
336+
format!("E{0:0>4}", code)
337+
} else {
338+
code.to_string()
339+
};
340+
match descriptions.find_description(&normalised) {
341+
Some(ref description) => {
342+
// Slice off the leading newline and print.
343+
print!("{}", &description[1..]);
344+
}
345+
None => {
346+
early_error(output, &format!("no extended information for {}", code));
347+
}
348+
}
349+
}
350+
351+
fn check_cfg(sopts: &config::Options,
352+
output: ErrorOutputType) {
353+
let mut emitter: Box<Emitter> = match output {
354+
config::ErrorOutputType::HumanReadable(color_config) => {
355+
Box::new(errors::emitter::BasicEmitter::stderr(color_config))
356+
}
357+
config::ErrorOutputType::Json => Box::new(errors::json::JsonEmitter::basic()),
358+
};
359+
360+
let mut saw_invalid_predicate = false;
361+
for item in sopts.cfg.iter() {
362+
match item.node {
363+
ast::MetaList(ref pred, _) => {
364+
saw_invalid_predicate = true;
365+
emitter.emit(None,
366+
&format!("invalid predicate in --cfg command line argument: `{}`",
367+
pred),
368+
None,
369+
errors::Level::Fatal);
370+
}
371+
_ => {},
372+
}
373+
}
374+
375+
if saw_invalid_predicate {
376+
panic!(errors::FatalError);
377+
}
378+
}
379+
327380
impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
328381
fn early_callback(&mut self,
329382
matches: &getopts::Matches,
383+
sopts: &config::Options,
330384
descriptions: &diagnostics::registry::Registry,
331385
output: ErrorOutputType)
332386
-> Compilation {
333-
match matches.opt_str("explain") {
334-
Some(ref code) => {
335-
let normalised = if !code.starts_with("E") {
336-
format!("E{0:0>4}", code)
337-
} else {
338-
code.to_string()
339-
};
340-
match descriptions.find_description(&normalised) {
341-
Some(ref description) => {
342-
// Slice off the leading newline and print.
343-
print!("{}", &description[1..]);
344-
}
345-
None => {
346-
early_error(output, &format!("no extended information for {}", code));
347-
}
348-
}
349-
return Compilation::Stop;
350-
}
351-
None => (),
387+
if let Some(ref code) = matches.opt_str("explain") {
388+
handle_explain(code, descriptions, output);
389+
return Compilation::Stop;
352390
}
353391

354-
return Compilation::Continue;
392+
check_cfg(sopts, output);
393+
Compilation::Continue
355394
}
356395

357396
fn no_input(&mut self,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[cfg(foo(bar))] //~ ERROR invalid predicate `foo`
12+
fn main() {}

src/test/compile-fail/issue-31495.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --cfg foo(bar)
12+
// error-pattern: invalid predicate in --cfg command line argument: `foo`
13+
fn main() {}

src/test/run-pass-fulldeps/compiler-calls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct TestCalls {
3434
impl<'a> CompilerCalls<'a> for TestCalls {
3535
fn early_callback(&mut self,
3636
_: &getopts::Matches,
37+
_: &config::Options,
3738
_: &diagnostics::registry::Registry,
3839
_: config::ErrorOutputType)
3940
-> Compilation {

0 commit comments

Comments
 (0)