Skip to content

Commit a6adeae

Browse files
committed
Validate syntax of --cfg command line arguments
1 parent b0a05c5 commit a6adeae

9 files changed

+41
-41
lines changed

src/librustc/session/config.rs

+25-14
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_target::spec::{Target, TargetTriple};
2121
use lint;
2222
use middle::cstore;
2323

24-
use syntax::ast::{self, IntTy, UintTy};
24+
use syntax::ast::{self, IntTy, UintTy, MetaItemKind};
2525
use syntax::source_map::{FileName, FilePathMapping};
2626
use syntax::edition::{Edition, EDITION_NAME_LIST, DEFAULT_EDITION};
2727
use syntax::parse::token;
@@ -1735,22 +1735,33 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> ast::CrateConfig {
17351735
let mut parser =
17361736
parse::new_parser_from_source_str(&sess, FileName::CfgSpec, s.to_string());
17371737

1738-
let meta_item = panictry!(parser.parse_meta_item());
1738+
macro_rules! error {($reason: expr) => {
1739+
early_error(ErrorOutputType::default(),
1740+
&format!(concat!("invalid `--cfg` argument: `{}` (", $reason, ")"), s));
1741+
}}
17391742

1740-
if parser.token != token::Eof {
1741-
early_error(
1742-
ErrorOutputType::default(),
1743-
&format!("invalid --cfg argument: {}", s),
1744-
)
1745-
} else if meta_item.is_meta_item_list() {
1746-
let msg = format!(
1747-
"invalid predicate in --cfg command line argument: `{}`",
1748-
meta_item.ident
1749-
);
1750-
early_error(ErrorOutputType::default(), &msg)
1743+
match &mut parser.parse_meta_item() {
1744+
Ok(meta_item) if parser.token == token::Eof => {
1745+
if meta_item.ident.segments.len() != 1 {
1746+
error!("argument key must be an identifier");
1747+
}
1748+
match &meta_item.node {
1749+
MetaItemKind::List(..) => {
1750+
error!(r#"expected `key` or `key="value"`"#);
1751+
}
1752+
MetaItemKind::NameValue(lit) if !lit.node.is_str() => {
1753+
error!("argument value must be a string");
1754+
}
1755+
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
1756+
return (meta_item.name(), meta_item.value_str());
1757+
}
1758+
}
1759+
}
1760+
Ok(..) => {}
1761+
Err(err) => err.cancel(),
17511762
}
17521763

1753-
(meta_item.name(), meta_item.value_str())
1764+
error!(r#"expected `key` or `key="value"`"#);
17541765
})
17551766
.collect::<ast::CrateConfig>()
17561767
}

src/test/ui/cfg-arg-invalid-1.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// compile-flags: --cfg a(b=c)
2+
// error-pattern: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`)
3+
fn main() {}

src/test/ui/cfg-arg-invalid-2.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// compile-flags: --cfg a{b}
2+
// error-pattern: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`)
3+
fn main() {}

src/test/ui/cfg-arg-invalid-3.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// compile-flags: --cfg a::b
2+
// error-pattern: invalid `--cfg` argument: `a::b` (argument key must be an identifier)
3+
fn main() {}

src/test/ui/cfg-arg-invalid-4.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// compile-flags: --cfg a(b)
2+
// error-pattern: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`)
3+
fn main() {}

src/test/ui/cfg-arg-invalid-5.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// compile-flags: --cfg a=10
2+
// error-pattern: invalid `--cfg` argument: `a=10` (argument value must be a string)
3+
fn main() {}

src/test/ui/cfg-arg-invalid.rs

-13
This file was deleted.

src/test/ui/cfg-empty-codemap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
// compile-flags: --cfg ""
1414

15-
// error-pattern: expected identifier, found
15+
// error-pattern: invalid `--cfg` argument: `""` (expected `key` or `key="value"`)
1616

1717
pub fn main() {
1818
}

src/test/ui/issues/issue-31495.rs

-13
This file was deleted.

0 commit comments

Comments
 (0)