Configure test's unstable feature gate when built outside of bootstrap

This uses a build probe to figure out the current toolchain version -
the only alternative to this is bespoke logic in Cargo to tell `test`
when the toolchain is stable/unstable.

The behaviour should be the same as when using
`CFG_DISABLE_UNSTABLE_FEATURES` unless an arbitrary channel is provided
to bootstrap, which I believe necessitates a fork anyway.
diff --git a/library/test/build.rs b/library/test/build.rs
new file mode 100644
index 0000000..a2bc893
--- /dev/null
+++ b/library/test/build.rs
@@ -0,0 +1,11 @@
+fn main() {
+    println!("cargo:rustc-check-cfg=cfg(enable_unstable_features)");
+
+    let rustc = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".into());
+    let version = std::process::Command::new(rustc).arg("-vV").output().unwrap();
+    let stdout = String::from_utf8(version.stdout).unwrap();
+
+    if stdout.contains("nightly") || stdout.contains("dev") {
+        println!("cargo:rustc-cfg=enable_unstable_features");
+    }
+}
diff --git a/library/test/src/cli.rs b/library/test/src/cli.rs
index 35291cc..1727859 100644
--- a/library/test/src/cli.rs
+++ b/library/test/src/cli.rs
@@ -314,15 +314,14 @@ fn parse_opts_impl(matches: getopts::Matches) -> OptRes {
     Ok(test_opts)
 }
 
-// FIXME: Copied from librustc_ast until linkage errors are resolved. Issue #47566
 fn is_nightly() -> bool {
-    // Whether this is a feature-staged build, i.e., on the beta or stable channel
-    let disable_unstable_features =
-        option_env!("CFG_DISABLE_UNSTABLE_FEATURES").map(|s| s != "0").unwrap_or(false);
-    // Whether we should enable unstable features for bootstrapping
+    // Whether the current rustc version should allow unstable features
+    let enable_unstable_features = cfg!(enable_unstable_features);
+
+    // The runtime override for unstable features
     let bootstrap = env::var("RUSTC_BOOTSTRAP").is_ok();
 
-    bootstrap || !disable_unstable_features
+    bootstrap || enable_unstable_features
 }
 
 // Gets the CLI options associated with `report-time` feature.
diff --git a/src/bootstrap/src/core/build_steps/compile.rs b/src/bootstrap/src/core/build_steps/compile.rs
index 7107140..405ab9f 100644
--- a/src/bootstrap/src/core/build_steps/compile.rs
+++ b/src/bootstrap/src/core/build_steps/compile.rs
@@ -626,12 +626,6 @@ pub fn std_cargo(
         CompilerBuiltins::BuildRustOnly => "",
     };
 
-    // `libtest` uses this to know whether or not to support
-    // `-Zunstable-options`.
-    if !builder.unstable_features() {
-        cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1");
-    }
-
     for krate in crates {
         cargo.args(["-p", krate]);
     }