Skip to content

Commit bb85bca

Browse files
committed
Auto merge of #96195 - sunfishcode:sunfishcode/handle-or-error-type, r=joshtriplett
Define a dedicated error type for `HandleOrNull` and `HandleOrInvalid`. Define `NullHandleError` and `InvalidHandleError` types, that implement std::error::Error, and use them as the error types in `HandleOrNull` and `HandleOrInvalid`, This addresses [this concern](#87074 (comment)). This is the same as #95387. r? `@joshtriplett`
2 parents 99b70ee + 531c937 commit bb85bca

5 files changed

+144
-10
lines changed

library/std/src/os/windows/io/handle.rs

+44-8
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub struct OwnedHandle {
7676
/// `NULL`. This ensures that such FFI calls cannot start using the handle without
7777
/// checking for `NULL` first.
7878
///
79-
/// This type concerns any value other than `NULL` to be valid, including `INVALID_HANDLE_VALUE`.
79+
/// This type considers any value other than `NULL` to be valid, including `INVALID_HANDLE_VALUE`.
8080
/// This is because APIs that use `NULL` as their sentry value don't treat `INVALID_HANDLE_VALUE`
8181
/// as special.
8282
///
@@ -96,7 +96,7 @@ pub struct HandleOrNull(OwnedHandle);
9696
/// `INVALID_HANDLE_VALUE`. This ensures that such FFI calls cannot start using the handle without
9797
/// checking for `INVALID_HANDLE_VALUE` first.
9898
///
99-
/// This type concerns any value other than `INVALID_HANDLE_VALUE` to be valid, including `NULL`.
99+
/// This type considers any value other than `INVALID_HANDLE_VALUE` to be valid, including `NULL`.
100100
/// This is because APIs that use `INVALID_HANDLE_VALUE` as their sentry value may return `NULL`
101101
/// under `windows_subsystem = "windows"` or other situations where I/O devices are detached.
102102
///
@@ -143,17 +143,17 @@ impl BorrowedHandle<'_> {
143143
}
144144

145145
impl TryFrom<HandleOrNull> for OwnedHandle {
146-
type Error = ();
146+
type Error = NullHandleError;
147147

148148
#[inline]
149-
fn try_from(handle_or_null: HandleOrNull) -> Result<Self, ()> {
149+
fn try_from(handle_or_null: HandleOrNull) -> Result<Self, NullHandleError> {
150150
let owned_handle = handle_or_null.0;
151151
if owned_handle.handle.is_null() {
152152
// Don't call `CloseHandle`; it'd be harmless, except that it could
153153
// overwrite the `GetLastError` error.
154154
forget(owned_handle);
155155

156-
Err(())
156+
Err(NullHandleError(()))
157157
} else {
158158
Ok(owned_handle)
159159
}
@@ -201,23 +201,59 @@ impl OwnedHandle {
201201
}
202202

203203
impl TryFrom<HandleOrInvalid> for OwnedHandle {
204-
type Error = ();
204+
type Error = InvalidHandleError;
205205

206206
#[inline]
207-
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, ()> {
207+
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, InvalidHandleError> {
208208
let owned_handle = handle_or_invalid.0;
209209
if owned_handle.handle == c::INVALID_HANDLE_VALUE {
210210
// Don't call `CloseHandle`; it'd be harmless, except that it could
211211
// overwrite the `GetLastError` error.
212212
forget(owned_handle);
213213

214-
Err(())
214+
Err(InvalidHandleError(()))
215215
} else {
216216
Ok(owned_handle)
217217
}
218218
}
219219
}
220220

221+
/// This is the error type used by [`HandleOrNull`] when attempting to convert
222+
/// into a handle, to indicate that the value is null.
223+
// The empty field prevents constructing this, and allows extending it in the future.
224+
#[unstable(feature = "io_safety", issue = "87074")]
225+
#[derive(Debug, Clone, PartialEq, Eq)]
226+
pub struct NullHandleError(());
227+
228+
#[unstable(feature = "io_safety", issue = "87074")]
229+
impl fmt::Display for NullHandleError {
230+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
231+
"A HandleOrNull could not be converted to a handle because it was null".fmt(fmt)
232+
}
233+
}
234+
235+
#[unstable(feature = "io_safety", issue = "87074")]
236+
impl crate::error::Error for NullHandleError {}
237+
238+
/// This is the error type used by [`HandleOrInvalid`] when attempting to
239+
/// convert into a handle, to indicate that the value is
240+
/// `INVALID_HANDLE_VALUE`.
241+
// The empty field prevents constructing this, and allows extending it in the future.
242+
#[unstable(feature = "io_safety", issue = "87074")]
243+
#[derive(Debug, Clone, PartialEq, Eq)]
244+
pub struct InvalidHandleError(());
245+
246+
#[unstable(feature = "io_safety", issue = "87074")]
247+
impl fmt::Display for InvalidHandleError {
248+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
249+
"A HandleOrInvalid could not be converted to a handle because it was INVALID_HANDLE_VALUE"
250+
.fmt(fmt)
251+
}
252+
}
253+
254+
#[unstable(feature = "io_safety", issue = "87074")]
255+
impl crate::error::Error for InvalidHandleError {}
256+
221257
impl AsRawHandle for BorrowedHandle<'_> {
222258
#[inline]
223259
fn as_raw_handle(&self) -> RawHandle {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0277]: the trait bound `(): std::error::Error` is not satisfied
2+
--> $DIR/coerce-issue-49593-box-never-windows.rs:18:53
3+
|
4+
LL | /* *mut $0 is coerced to Box<dyn Error> here */ Box::<_ /* ! */>::new(x)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `()`
6+
|
7+
= help: the following other types implement trait `std::error::Error`:
8+
!
9+
&'a T
10+
AccessError
11+
AddrParseError
12+
Arc<T>
13+
BorrowError
14+
BorrowMutError
15+
Box<T>
16+
and 45 others
17+
= note: required for the cast to the object type `dyn std::error::Error`
18+
19+
error[E0277]: the trait bound `(): std::error::Error` is not satisfied
20+
--> $DIR/coerce-issue-49593-box-never-windows.rs:23:49
21+
|
22+
LL | /* *mut $0 is coerced to *mut Error here */ raw_ptr_box::<_ /* ! */>(x)
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `()`
24+
|
25+
= help: the following other types implement trait `std::error::Error`:
26+
!
27+
&'a T
28+
AccessError
29+
AddrParseError
30+
Arc<T>
31+
BorrowError
32+
BorrowMutError
33+
Box<T>
34+
and 45 others
35+
= note: required for the cast to the object type `(dyn std::error::Error + 'static)`
36+
37+
error: aborting due to 2 previous errors
38+
39+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// revisions: nofallback fallback
2+
// only-windows - the number of `Error` impls is platform-dependent
3+
//[fallback] check-pass
4+
//[nofallback] check-fail
5+
6+
#![feature(never_type)]
7+
#![cfg_attr(fallback, feature(never_type_fallback))]
8+
#![allow(unreachable_code)]
9+
10+
use std::error::Error;
11+
use std::mem;
12+
13+
fn raw_ptr_box<T>(t: T) -> *mut T {
14+
panic!()
15+
}
16+
17+
fn foo(x: !) -> Box<dyn Error> {
18+
/* *mut $0 is coerced to Box<dyn Error> here */ Box::<_ /* ! */>::new(x)
19+
//[nofallback]~^ ERROR trait bound `(): std::error::Error` is not satisfied
20+
}
21+
22+
fn foo_raw_ptr(x: !) -> *mut dyn Error {
23+
/* *mut $0 is coerced to *mut Error here */ raw_ptr_box::<_ /* ! */>(x)
24+
//[nofallback]~^ ERROR trait bound `(): std::error::Error` is not satisfied
25+
}
26+
27+
fn no_coercion(d: *mut dyn Error) -> *mut dyn Error {
28+
/* an unsize coercion won't compile here, and it is indeed not used
29+
because there is nothing requiring the _ to be Sized */
30+
d as *mut _
31+
}
32+
33+
trait Xyz {}
34+
struct S;
35+
struct T;
36+
impl Xyz for S {}
37+
impl Xyz for T {}
38+
39+
fn foo_no_never() {
40+
let mut x /* : Option<S> */ = None;
41+
let mut first_iter = false;
42+
loop {
43+
if !first_iter {
44+
let y: Box<dyn Xyz>
45+
= /* Box<$0> is coerced to Box<Xyz> here */ Box::new(x.unwrap());
46+
}
47+
48+
x = Some(S);
49+
first_iter = true;
50+
}
51+
52+
let mut y : Option<S> = None;
53+
// assert types are equal
54+
mem::swap(&mut x, &mut y);
55+
}
56+
57+
fn main() {
58+
}

src/test/ui/coercion/coerce-issue-49593-box-never.nofallback.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: the trait bound `(): std::error::Error` is not satisfied
2-
--> $DIR/coerce-issue-49593-box-never.rs:17:53
2+
--> $DIR/coerce-issue-49593-box-never.rs:18:53
33
|
44
LL | /* *mut $0 is coerced to Box<dyn Error> here */ Box::<_ /* ! */>::new(x)
55
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `()`
@@ -17,7 +17,7 @@ LL | /* *mut $0 is coerced to Box<dyn Error> here */ Box::<_ /* ! */>::new(x
1717
= note: required for the cast to the object type `dyn std::error::Error`
1818

1919
error[E0277]: the trait bound `(): std::error::Error` is not satisfied
20-
--> $DIR/coerce-issue-49593-box-never.rs:22:49
20+
--> $DIR/coerce-issue-49593-box-never.rs:23:49
2121
|
2222
LL | /* *mut $0 is coerced to *mut Error here */ raw_ptr_box::<_ /* ! */>(x)
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `()`

src/test/ui/coercion/coerce-issue-49593-box-never.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// revisions: nofallback fallback
2+
// ignore-windows - the number of `Error` impls is platform-dependent
23
//[fallback] check-pass
34
//[nofallback] check-fail
45

0 commit comments

Comments
 (0)