Skip to content

Commit 6d2c866

Browse files
committed
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
1 parent 4c4bb5f commit 6d2c866

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/librustc_driver/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,24 @@ fn handle_explain(code: &str,
348348
}
349349
}
350350

351+
fn check_cfg(sopts: &config::Options,
352+
output: ErrorOutputType) {
353+
fn is_meta_list(item: &ast::MetaItem) -> bool {
354+
match item.node {
355+
ast::MetaItem_::MetaList(..) => true,
356+
_ => false,
357+
}
358+
}
359+
360+
if sopts.cfg.iter().any(|item| is_meta_list(&*item)) {
361+
early_error(output, "predicates are not allowed in --cfg");
362+
}
363+
}
364+
351365
impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
352366
fn early_callback(&mut self,
353367
matches: &getopts::Matches,
354-
_sopts: &config::Options,
368+
sopts: &config::Options,
355369
descriptions: &diagnostics::registry::Registry,
356370
output: ErrorOutputType)
357371
-> Compilation {
@@ -360,6 +374,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
360374
return Compilation::Stop;
361375
}
362376

377+
check_cfg(sopts, output);
363378
Compilation::Continue
364379
}
365380

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

Lines changed: 13 additions & 0 deletions
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: predicates are not allowed in --cfg
13+
fn main() {}

0 commit comments

Comments
 (0)