mirror of
https://github.com/openharmony/third_party_rust_io-lifetimes.git
synced 2026-07-01 21:04:05 -04:00
Avoid depending on panic in const fn, to fix compilaton on Rust <= 1.56.
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user