Skip to content

Commit

Permalink
Forward -Zallow-features to rustc
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Gjengset committed Mar 24, 2021
1 parent caca4d4 commit 85f7c80
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,19 @@ impl<'cfg> Compilation<'cfg> {
.env("CARGO_PKG_AUTHORS", &pkg.authors().join(":"))
.cwd(pkg.root());

if is_rustc_tool {
if let Some(allow) = &self.config.cli_unstable().allow_features {
let mut arg = String::from("-Zallow-features=");
for (i, a) in allow.iter().enumerate() {
arg.push_str(a);
if i != allow.len() - 1 {
arg.push(',');
}
}
cmd.arg(&arg);
}
}

if self.config.cli_unstable().configurable_env {
// Apply any environment variables from the config
for (key, value) in self.config.env_config()?.iter() {
Expand Down
5 changes: 5 additions & 0 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ where `test-dummy-unstable` is unstable, that features would also be
disallowed by `-Zallow-features=`, and allowed with
`-Zallow-features=test-dummy-unstable`.

The list of features passed to cargo's `-Zallow-features` is also passed
to any Rust tools that cargo ends up calling (like `rustc` or
`rustdoc`). Thus, if you run `cargo -Zallow-features=`, no unstable
Cargo _or_ Rust features can be used.

### extra-link-arg
* Original Pull Request: [#7811](https://github.com/rust-lang/cargo/pull/7811)

Expand Down
64 changes: 64 additions & 0 deletions tests/testsuite/cargo_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ fn allow_features() {
.file("src/lib.rs", "")
.build();

// NOTE: We need to use RUSTC_BOOTSTRAP here since we also need nightly rustc

p.cargo("-Zallow-features=test-dummy-unstable build")
.masquerade_as_nightly_cargo()
.env("RUSTC_BOOTSTRAP", "1")
.with_stderr(
"\
[COMPILING] a [..]
Expand All @@ -143,12 +146,14 @@ fn allow_features() {

p.cargo("-Zallow-features=test-dummy-unstable,print-im-a-teapot -Zprint-im-a-teapot build")
.masquerade_as_nightly_cargo()
.env("RUSTC_BOOTSTRAP", "1")
.with_stdout("im-a-teapot = true")
.with_stderr("[FINISHED] [..]")
.run();

p.cargo("-Zallow-features=test-dummy-unstable -Zprint-im-a-teapot build")
.masquerade_as_nightly_cargo()
.env("RUSTC_BOOTSTRAP", "1")
.with_status(101)
.with_stderr(
"\
Expand All @@ -159,6 +164,7 @@ error: the feature `print-im-a-teapot` is not in the list of allowed features: {

p.cargo("-Zallow-features= build")
.masquerade_as_nightly_cargo()
.env("RUSTC_BOOTSTRAP", "1")
.with_status(101)
.with_stderr(
"\
Expand All @@ -171,6 +177,57 @@ Caused by:
.run();
}

#[cargo_test]
fn allow_features_to_rustc() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "a"
version = "0.0.1"
authors = []
"#,
)
.file(
"src/lib.rs",
r#"
#![feature(test_2018_feature)]
"#,
)
.build();

// NOTE: We need to use RUSTC_BOOTSTRAP here since we also need nightly rustc

p.cargo("-Zallow-features= build")
.masquerade_as_nightly_cargo()
.env("RUSTC_BOOTSTRAP", "1")
.with_status(101)
.with_stderr_contains(
"\
[COMPILING] a [..]
error[E0725]: the feature `test_2018_feature` is not in the list of allowed features
",
)
.with_stderr_contains(
"\
error: could not compile `a`
",
)
.run();

p.cargo("-Zallow-features=test_2018_feature build")
.masquerade_as_nightly_cargo()
.env("RUSTC_BOOTSTRAP", "1")
.with_stderr(
"\
[COMPILING] a [..]
[FINISHED] [..]
",
)
.run();
}

#[cargo_test]
fn allow_features_in_cfg() {
let p = project()
Expand All @@ -196,8 +253,11 @@ fn allow_features_in_cfg() {
.file("src/lib.rs", "")
.build();

// NOTE: We need to use RUSTC_BOOTSTRAP here since we also need nightly rustc

p.cargo("build")
.masquerade_as_nightly_cargo()
.env("RUSTC_BOOTSTRAP", "1")
.with_stderr(
"\
[COMPILING] a [..]
Expand All @@ -208,12 +268,14 @@ fn allow_features_in_cfg() {

p.cargo("-Zprint-im-a-teapot build")
.masquerade_as_nightly_cargo()
.env("RUSTC_BOOTSTRAP", "1")
.with_stdout("im-a-teapot = true")
.with_stderr("[FINISHED] [..]")
.run();

p.cargo("-Zunstable-options build")
.masquerade_as_nightly_cargo()
.env("RUSTC_BOOTSTRAP", "1")
.with_status(101)
.with_stderr(
"\
Expand All @@ -225,6 +287,7 @@ error: the feature `unstable-options` is not in the list of allowed features: {[
// -Zallow-features overrides .cargo/config
p.cargo("-Zallow-features=test-dummy-unstable -Zprint-im-a-teapot build")
.masquerade_as_nightly_cargo()
.env("RUSTC_BOOTSTRAP", "1")
.with_status(101)
.with_stderr(
"\
Expand All @@ -235,6 +298,7 @@ error: the feature `print-im-a-teapot` is not in the list of allowed features: {

p.cargo("-Zallow-features= build")
.masquerade_as_nightly_cargo()
.env("RUSTC_BOOTSTRAP", "1")
.with_status(101)
.with_stderr(
"\
Expand Down

0 comments on commit 85f7c80

Please sign in to comment.