Skip to content

Commit 86b4658

Browse files
committed
Generalize get_nullable_type to allow types where null is all-ones.
Generalize get_nullable_type to accept types that have an all-ones bit pattern as their sentry "null" value. This will allow [`OwnedFd`], [`BorrowedFd`], [`OwnedSocket`], and [`BorrowedSocket`] to be marked with `#[rustc_nonnull_optimization_guaranteed]`, which will allow `Option<OwnedFd>`, `Option<BorrowedFd>`, `Option<OwnedSocket>`, and `Option<BorrowedSocket>` to be used in FFI declarations, as described in the [I/O safety RFC]. For example, it will allow a function like `open` on Unix and `WSASocketW` on Windows to be declared using `Option<OwnedFd>` and `Option<OwnedSocket>` return types, respectively. The actual change to add `#[rustc_nonnull_optimization_guaranteed]` to the abovementioned types will be a separate PR, as it'll depend on having this patch in the stage0 compiler. Also, update the diagnostics to mention that "niche optimizations" are used in libstd as well as libcore, as `rustc_layout_scalar_valid_range_start` and `rustc_layout_scalar_valid_range_end` are already in use in libstd. [`OwnedFd`]: https://github.com/rust-lang/rust/blob/c9dc44be24c58ff13ce46416c4b97ab5c1bd8429/library/std/src/os/fd/owned.rs#L49 [`BorrowedFd`]: https://github.com/rust-lang/rust/blob/c9dc44be24c58ff13ce46416c4b97ab5c1bd8429/library/std/src/os/fd/owned.rs#L29 [`OwnedSocket`]: https://github.com/rust-lang/rust/blob/c9dc44be24c58ff13ce46416c4b97ab5c1bd8429/library/std/src/os/windows/io/socket.rs#L51 [`BorrowedSocket`]: https://github.com/rust-lang/rust/blob/c9dc44be24c58ff13ce46416c4b97ab5c1bd8429/library/std/src/os/windows/io/socket.rs#L29 [I/O safety RFC]: https://github.com/rust-lang/rfcs/blob/master/text/3128-io-safety.md#ownedfd-and-borrowedfdfd-1
1 parent 10913c0 commit 86b4658

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -606,17 +606,17 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
606606
rustc_attr!(
607607
rustc_layout_scalar_valid_range_start, Normal, template!(List: "value"), ErrorFollowing,
608608
"the `#[rustc_layout_scalar_valid_range_start]` attribute is just used to enable \
609-
niche optimizations in libcore and will never be stable",
609+
niche optimizations in libcore and libstd and will never be stable",
610610
),
611611
rustc_attr!(
612612
rustc_layout_scalar_valid_range_end, Normal, template!(List: "value"), ErrorFollowing,
613613
"the `#[rustc_layout_scalar_valid_range_end]` attribute is just used to enable \
614-
niche optimizations in libcore and will never be stable",
614+
niche optimizations in libcore and libstd and will never be stable",
615615
),
616616
rustc_attr!(
617617
rustc_nonnull_optimization_guaranteed, Normal, template!(Word), WarnFollowing,
618618
"the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable \
619-
niche optimizations in libcore and will never be stable",
619+
niche optimizations in libcore and libstd and will never be stable",
620620
),
621621

622622
// ==========================================================================

compiler/rustc_lint/src/types.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,9 @@ crate fn repr_nullable_ptr<'tcx>(
795795
let field_ty_abi = &cx.layout_of(field_ty).unwrap().abi;
796796
if let Abi::Scalar(field_ty_scalar) = field_ty_abi {
797797
match (field_ty_scalar.valid_range.start, field_ty_scalar.valid_range.end) {
798-
(0, _) => unreachable!("Non-null optimisation extended to a non-zero value."),
798+
(0, x) if x == field_ty_scalar.value.size(&cx.tcx).unsigned_int_max() - 1 => {
799+
return Some(get_nullable_type(cx, field_ty).unwrap());
800+
}
799801
(1, _) => {
800802
return Some(get_nullable_type(cx, field_ty).unwrap());
801803
}

src/test/ui/feature-gates/feature-gate-rustc-attrs-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
#[rustc_variance] //~ ERROR the `#[rustc_variance]` attribute is just used for rustc unit tests and will never be stable
44
#[rustc_error] //~ ERROR the `#[rustc_error]` attribute is just used for rustc unit tests and will never be stable
5-
#[rustc_nonnull_optimization_guaranteed] //~ ERROR the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable niche optimizations in libcore and will never be stable
5+
#[rustc_nonnull_optimization_guaranteed] //~ ERROR the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable niche optimizations in libcore and libstd and will never be stable
66

77
fn main() {}

src/test/ui/feature-gates/feature-gate-rustc-attrs-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | #[rustc_error]
1414
|
1515
= help: add `#![feature(rustc_attrs)]` to the crate attributes to enable
1616

17-
error[E0658]: the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable niche optimizations in libcore and will never be stable
17+
error[E0658]: the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable niche optimizations in libcore and libstd and will never be stable
1818
--> $DIR/feature-gate-rustc-attrs-1.rs:5:1
1919
|
2020
LL | #[rustc_nonnull_optimization_guaranteed]

0 commit comments

Comments
 (0)