Skip to content

Commit b0e5c7d

Browse files
committed
Auto merge of #74699 - notriddle:fd-non-negative, r=m-ou-se
Mark `-1` as an available niche for file descriptors Based on discussion from <https://internals.rust-lang.org/t/can-the-standard-library-shrink-option-file/12768>, the file descriptor `-1` is chosen based on the POSIX API designs that use it as a sentinel to report errors. A bigger niche could've been chosen, particularly on Linux, but would not necessarily be portable. This PR also adds a test case to ensure that the -1 niche (which is kind of hacky and has no obvious test case) works correctly. It requires the "upper" bound, which is actually -1, to be expressed in two's complement.
2 parents 2ad5292 + 094b1da commit b0e5c7d

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

library/std/src/sys/unix/fd.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ use crate::sys_common::AsInner;
1212
use libc::{c_int, c_void};
1313

1414
#[derive(Debug)]
15+
#[rustc_layout_scalar_valid_range_start(0)]
16+
// libstd/os/raw/mod.rs assures me that every libstd-supported platform has a
17+
// 32-bit c_int. Below is -2, in two's complement, but that only works out
18+
// because c_int is 32 bits.
19+
#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
1520
pub struct FileDesc {
1621
fd: c_int,
1722
}
@@ -63,7 +68,9 @@ const fn max_iov() -> usize {
6368

6469
impl FileDesc {
6570
pub fn new(fd: c_int) -> FileDesc {
66-
FileDesc { fd }
71+
assert_ne!(fd, -1i32);
72+
// SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
73+
unsafe { FileDesc { fd } }
6774
}
6875

6976
pub fn raw(&self) -> c_int {

library/std/src/sys/unix/fd/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::mem::ManuallyDrop;
33

44
#[test]
55
fn limit_vector_count() {
6-
let stdout = ManuallyDrop::new(FileDesc { fd: 1 });
6+
let stdout = ManuallyDrop::new(unsafe { FileDesc { fd: 1 } });
77
let bufs = (0..1500).map(|_| IoSlice::new(&[])).collect::<Vec<_>>();
88
assert!(stdout.write_vectored(&bufs).is_ok());
99
}

src/test/ui/print_type_sizes/niche-filling.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@
1515
// padding and overall computed sizes can be quite different.
1616

1717
#![feature(start)]
18+
#![feature(rustc_attrs)]
1819
#![allow(dead_code)]
1920

2021
use std::num::NonZeroU32;
2122

2223
pub enum MyOption<T> { None, Some(T) }
2324

25+
#[rustc_layout_scalar_valid_range_start(0)]
26+
#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
27+
pub struct MyNotNegativeOne {
28+
_i: i32,
29+
}
30+
2431
impl<T> Default for MyOption<T> {
2532
fn default() -> Self { MyOption::None }
2633
}
@@ -77,17 +84,18 @@ fn start(_: isize, _: *const *const u8) -> isize {
7784
let _a: MyOption<bool> = Default::default();
7885
let _b: MyOption<char> = Default::default();
7986
let _c: MyOption<std::cmp::Ordering> = Default::default();
80-
let _b: MyOption<MyOption<u8>> = Default::default();
87+
let _d: MyOption<MyOption<u8>> = Default::default();
8188
let _e: Enum4<(), char, (), ()> = Enum4::One(());
8289
let _f: Enum4<(), (), bool, ()> = Enum4::One(());
8390
let _g: Enum4<(), (), (), MyOption<u8>> = Enum4::One(());
91+
let _h: MyOption<MyNotNegativeOne> = Default::default();
8492

8593
// Unions do not currently participate in niche filling.
86-
let _h: MyOption<Union2<NonZeroU32, u32>> = Default::default();
94+
let _i: MyOption<Union2<NonZeroU32, u32>> = Default::default();
8795

8896
// ...even when theoretically possible.
89-
let _i: MyOption<Union1<NonZeroU32>> = Default::default();
90-
let _j: MyOption<Union2<NonZeroU32, NonZeroU32>> = Default::default();
97+
let _j: MyOption<Union1<NonZeroU32>> = Default::default();
98+
let _k: MyOption<Union2<NonZeroU32, NonZeroU32>> = Default::default();
9199

92100
0
93101
}

src/test/ui/print_type_sizes/niche-filling.stdout

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ print-type-size variant `Three`: 0 bytes
4343
print-type-size field `.0`: 0 bytes
4444
print-type-size variant `Four`: 0 bytes
4545
print-type-size field `.0`: 0 bytes
46+
print-type-size type: `MyNotNegativeOne`: 4 bytes, alignment: 4 bytes
47+
print-type-size field `._i`: 4 bytes
48+
print-type-size type: `MyOption<MyNotNegativeOne>`: 4 bytes, alignment: 4 bytes
49+
print-type-size variant `Some`: 4 bytes
50+
print-type-size field `.0`: 4 bytes
51+
print-type-size variant `None`: 0 bytes
4652
print-type-size type: `MyOption<char>`: 4 bytes, alignment: 4 bytes
4753
print-type-size variant `Some`: 4 bytes
4854
print-type-size field `.0`: 4 bytes

0 commit comments

Comments
 (0)