Avoid depending on panic in const fn, to fix compilaton on Rust <= 1.56.

This commit is contained in:
Dan Gohman
2022-05-21 09:06:39 -07:00
parent 7df75af6e0
commit 3d0daf0334
2 changed files with 27 additions and 0 deletions
+25
View File
@@ -6,6 +6,11 @@ fn main() {
// which, outside of `std`, are only available on nightly.
use_feature_or_nothing("rustc_attrs");
// Rust 1.56 and earlier don't support panic in const fn.
if has_panic_in_const_fn() {
use_feature("panic_in_const_fn")
}
// Don't rerun this on changes other than build.rs, as we only depend on
// the rustc version.
println!("cargo:rerun-if-changed=build.rs");
@@ -40,3 +45,23 @@ fn has_feature(feature: &str) -> bool {
child.wait().unwrap().success()
}
/// Test whether the rustc at `var("RUSTC")` supports panic in `const fn`.
fn has_panic_in_const_fn() -> bool {
let out_dir = var("OUT_DIR").unwrap();
let rustc = var("RUSTC").unwrap();
let mut child = std::process::Command::new(rustc)
.arg("--crate-type=rlib") // Don't require `main`.
.arg("--emit=metadata") // Do as little as possible but still parse.
.arg("--out-dir")
.arg(out_dir) // Put the output somewhere inconsequential.
.arg("-") // Read from stdin.
.stdin(std::process::Stdio::piped()) // Stdin is a pipe.
.spawn()
.unwrap();
writeln!(child.stdin.take().unwrap(), "const fn foo() {{ panic!() }}").unwrap();
child.wait().unwrap().success()
}
+2
View File
@@ -444,7 +444,9 @@ impl BorrowedFd<'_> {
/// the returned `BorrowedFd`, and it must not have the value `-1`.
#[inline]
pub const unsafe fn borrow_raw(fd: RawFd) -> Self {
#[cfg(panic_in_const_fn)]
debug_assert!(fd != -1_i32 as RawFd);
Self {
fd,
_phantom: PhantomData,