Skip to content

[beta] Rollup backports #54037

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 4 commits into from
Sep 9, 2018
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
39 changes: 25 additions & 14 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use rustc_data_structures::stable_hasher::ToStableHashKey;
use lint;
use middle::cstore;

use syntax::ast::{self, IntTy, UintTy};
use syntax::ast::{self, IntTy, UintTy, MetaItemKind};
use syntax::codemap::{FileName, FilePathMapping};
use syntax::edition::{Edition, EDITION_NAME_LIST, DEFAULT_EDITION};
use syntax::parse::token;
Expand Down Expand Up @@ -1746,22 +1746,33 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> ast::CrateConfig {
let mut parser =
parse::new_parser_from_source_str(&sess, FileName::CfgSpec, s.to_string());

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

if parser.token != token::Eof {
early_error(
ErrorOutputType::default(),
&format!("invalid --cfg argument: {}", s),
)
} else if meta_item.is_meta_item_list() {
let msg = format!(
"invalid predicate in --cfg command line argument: `{}`",
meta_item.ident
);
early_error(ErrorOutputType::default(), &msg)
match &mut parser.parse_meta_item() {
Ok(meta_item) if parser.token == token::Eof => {
if meta_item.ident.segments.len() != 1 {
error!("argument key must be an identifier");
}
match &meta_item.node {
MetaItemKind::List(..) => {
error!(r#"expected `key` or `key="value"`"#);
}
MetaItemKind::NameValue(lit) if !lit.node.is_str() => {
error!("argument value must be a string");
}
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
return (meta_item.name(), meta_item.value_str());
}
}
}
Ok(..) => {}
Err(err) => err.cancel(),
}

(meta_item.name(), meta_item.value_str())
error!(r#"expected `key` or `key="value"`"#);
})
.collect::<ast::CrateConfig>()
}
Expand Down
16 changes: 15 additions & 1 deletion src/libsyntax/attr/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,21 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
if let (Some(feats), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) {
gated_cfg.check_and_emit(sess, feats);
}
sess.config.contains(&(cfg.name(), cfg.value_str()))
let error = |span, msg| { sess.span_diagnostic.span_err(span, msg); true };
if cfg.ident.segments.len() != 1 {
return error(cfg.ident.span, "`cfg` predicate key must be an identifier");
}
match &cfg.node {
MetaItemKind::List(..) => {
error(cfg.span, "unexpected parentheses after `cfg` predicate key")
}
MetaItemKind::NameValue(lit) if !lit.node.is_str() => {
error(lit.span, "literal in `cfg` predicate value must be a string")
}
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
sess.config.contains(&(cfg.name(), cfg.value_str()))
}
}
})
}

Expand Down
44 changes: 32 additions & 12 deletions src/libsyntax/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,45 @@ impl<'a> StripUnconfigured<'a> {
return false;
}

let mis = if !is_cfg(attr) {
if !is_cfg(attr) {
return true;
} else if let Some(mis) = attr.meta_item_list() {
mis
}

let error = |span, msg, suggestion: &str| {
let mut err = self.sess.span_diagnostic.struct_span_err(span, msg);
if !suggestion.is_empty() {
err.span_suggestion(span, "expected syntax is", suggestion.into());
}
err.emit();
true
};

let meta_item = if let Some(meta_item) = attr.meta() {
meta_item
} else {
return true;
// Not a well-formed meta-item. Why? We don't know.
return error(attr.span, "`cfg` is not a well-formed meta-item",
"#[cfg(/* predicate */)]");
};
let nested_meta_items = if let Some(nested_meta_items) = meta_item.meta_item_list() {
nested_meta_items
} else {
return error(meta_item.span, "`cfg` is not followed by parentheses",
"cfg(/* predicate */)");
};

if mis.len() != 1 {
self.sess.span_diagnostic.span_err(attr.span, "expected 1 cfg-pattern");
return true;
if nested_meta_items.is_empty() {
return error(meta_item.span, "`cfg` predicate is not specified", "");
} else if nested_meta_items.len() > 1 {
return error(nested_meta_items.last().unwrap().span,
"multiple `cfg` predicates are specified", "");
}

if !mis[0].is_meta_item() {
self.sess.span_diagnostic.span_err(mis[0].span, "unexpected literal");
return true;
match nested_meta_items[0].meta_item() {
Some(meta_item) => attr::cfg_matches(meta_item, self.sess, self.features),
None => error(nested_meta_items[0].span,
"`cfg` predicate key cannot be a literal", ""),
}

attr::cfg_matches(mis[0].meta_item().unwrap(), self.sess, self.features)
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/cfg-empty-codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

// compile-flags: --cfg ""

// error-pattern: expected identifier, found
// error-pattern: invalid `--cfg` argument

pub fn main() {
}
13 changes: 13 additions & 0 deletions src/test/ui/cfg-arg-invalid-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2018 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 a(b=c)
// error-pattern: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`)
fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -9,5 +9,5 @@
// except according to those terms.

// compile-flags: --cfg a{b}
// error-pattern: invalid --cfg argument: a{b}
// error-pattern: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`)
fn main() {}
13 changes: 13 additions & 0 deletions src/test/ui/cfg-arg-invalid-3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2018 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 a::b
// error-pattern: invalid `--cfg` argument: `a::b` (argument key must be an identifier)
fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -8,6 +8,6 @@
// 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`
// compile-flags: --cfg a(b)
// error-pattern: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`)
fn main() {}
13 changes: 13 additions & 0 deletions src/test/ui/cfg-arg-invalid-5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2018 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 a=10
// error-pattern: invalid `--cfg` argument: `a=10` (argument value must be a string)
fn main() {}
42 changes: 42 additions & 0 deletions src/test/ui/cfg-attr-syntax-validation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2018 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] //~ ERROR `cfg` is not followed by parentheses
struct S1;

#[cfg = 10] //~ ERROR `cfg` is not followed by parentheses
struct S2;

#[cfg()] //~ ERROR `cfg` predicate is not specified
struct S3;

#[cfg(a, b)] //~ ERROR multiple `cfg` predicates are specified
struct S4;

#[cfg("str")] //~ ERROR `cfg` predicate key cannot be a literal
struct S5;

#[cfg(a::b)] //~ ERROR `cfg` predicate key must be an identifier
struct S6;

#[cfg(a())] //~ ERROR invalid predicate `a`
struct S7;

#[cfg(a = 10)] //~ ERROR literal in `cfg` predicate value must be a string
struct S8;

macro_rules! generate_s9 {
($expr: expr) => {
#[cfg(feature = $expr)] //~ ERROR `cfg` is not a well-formed meta-item
struct S9;
}
}

generate_s9!(concat!("nonexistent"));
60 changes: 60 additions & 0 deletions src/test/ui/cfg-attr-syntax-validation.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
error: `cfg` is not followed by parentheses
--> $DIR/cfg-attr-syntax-validation.rs:11:1
|
LL | #[cfg] //~ ERROR `cfg` is not followed by parentheses
| ^^^^^^ help: expected syntax is: `cfg(/* predicate */)`

error: `cfg` is not followed by parentheses
--> $DIR/cfg-attr-syntax-validation.rs:14:1
|
LL | #[cfg = 10] //~ ERROR `cfg` is not followed by parentheses
| ^^^^^^^^^^^ help: expected syntax is: `cfg(/* predicate */)`

error: `cfg` predicate is not specified
--> $DIR/cfg-attr-syntax-validation.rs:17:1
|
LL | #[cfg()] //~ ERROR `cfg` predicate is not specified
| ^^^^^^^^

error: multiple `cfg` predicates are specified
--> $DIR/cfg-attr-syntax-validation.rs:20:10
|
LL | #[cfg(a, b)] //~ ERROR multiple `cfg` predicates are specified
| ^

error: `cfg` predicate key cannot be a literal
--> $DIR/cfg-attr-syntax-validation.rs:23:7
|
LL | #[cfg("str")] //~ ERROR `cfg` predicate key cannot be a literal
| ^^^^^

error: `cfg` predicate key must be an identifier
--> $DIR/cfg-attr-syntax-validation.rs:26:7
|
LL | #[cfg(a::b)] //~ ERROR `cfg` predicate key must be an identifier
| ^^^^

error[E0537]: invalid predicate `a`
--> $DIR/cfg-attr-syntax-validation.rs:29:7
|
LL | #[cfg(a())] //~ ERROR invalid predicate `a`
| ^^^

error: literal in `cfg` predicate value must be a string
--> $DIR/cfg-attr-syntax-validation.rs:32:11
|
LL | #[cfg(a = 10)] //~ ERROR literal in `cfg` predicate value must be a string
| ^^

error: `cfg` is not a well-formed meta-item
--> $DIR/cfg-attr-syntax-validation.rs:37:9
|
LL | #[cfg(feature = $expr)] //~ ERROR `cfg` is not a well-formed meta-item
| ^^^^^^^^^^^^^^^^^^^^^^^ help: expected syntax is: `#[cfg(/* predicate */)]`
...
LL | generate_s9!(concat!("nonexistent"));
| ------------------------------------- in this macro invocation

error: aborting due to 9 previous errors

For more information about this error, try `rustc --explain E0537`.