Skip to content

Commit 67b9a5b

Browse files
authored
Rollup merge of #86592 - jhpratt:non_exhaustive, r=JohnTitor
Use `#[non_exhaustive]` where appropriate Due to the std/alloc split, it is not possible to make `alloc::collections::TryReserveError::AllocError` non-exhaustive without having an unstable, doc-hidden method to construct (which negates the benefits from `#[non_exhaustive]`). `@rustbot` label +C-cleanup +T-libs +S-waiting-on-review
2 parents 7b661a6 + 3f14f4b commit 67b9a5b

File tree

8 files changed

+29
-39
lines changed

8 files changed

+29
-39
lines changed

compiler/rustc_metadata/src/dynamic_lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,12 @@ mod dl {
7070
use std::sync::{Mutex, MutexGuard};
7171

7272
pub fn lock() -> MutexGuard<'static, Guard> {
73-
static LOCK: SyncLazy<Mutex<Guard>> = SyncLazy::new(|| Mutex::new(Guard { _priv: () }));
73+
static LOCK: SyncLazy<Mutex<Guard>> = SyncLazy::new(|| Mutex::new(Guard));
7474
LOCK.lock().unwrap()
7575
}
7676

77-
pub struct Guard {
78-
_priv: (),
79-
}
77+
#[non_exhaustive]
78+
pub struct Guard;
8079

8180
impl Guard {
8281
pub fn get(&mut self) -> Result<(), String> {

library/core/src/alloc/layout.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Layout {
6060
#[inline]
6161
pub const fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutError> {
6262
if !align.is_power_of_two() {
63-
return Err(LayoutError { private: () });
63+
return Err(LayoutError);
6464
}
6565

6666
// (power-of-two implies align != 0.)
@@ -78,7 +78,7 @@ impl Layout {
7878
// Above implies that checking for summation overflow is both
7979
// necessary and sufficient.
8080
if size > usize::MAX - (align - 1) {
81-
return Err(LayoutError { private: () });
81+
return Err(LayoutError);
8282
}
8383

8484
// SAFETY: the conditions for `from_size_align_unchecked` have been
@@ -288,7 +288,7 @@ impl Layout {
288288
// > must not overflow (i.e., the rounded value must be less than
289289
// > `usize::MAX`)
290290
let padded_size = self.size() + self.padding_needed_for(self.align());
291-
let alloc_size = padded_size.checked_mul(n).ok_or(LayoutError { private: () })?;
291+
let alloc_size = padded_size.checked_mul(n).ok_or(LayoutError)?;
292292

293293
// SAFETY: self.align is already known to be valid and alloc_size has been
294294
// padded already.
@@ -346,8 +346,8 @@ impl Layout {
346346
let new_align = cmp::max(self.align(), next.align());
347347
let pad = self.padding_needed_for(next.align());
348348

349-
let offset = self.size().checked_add(pad).ok_or(LayoutError { private: () })?;
350-
let new_size = offset.checked_add(next.size()).ok_or(LayoutError { private: () })?;
349+
let offset = self.size().checked_add(pad).ok_or(LayoutError)?;
350+
let new_size = offset.checked_add(next.size()).ok_or(LayoutError)?;
351351

352352
let layout = Layout::from_size_align(new_size, new_align)?;
353353
Ok((layout, offset))
@@ -368,7 +368,7 @@ impl Layout {
368368
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
369369
#[inline]
370370
pub fn repeat_packed(&self, n: usize) -> Result<Self, LayoutError> {
371-
let size = self.size().checked_mul(n).ok_or(LayoutError { private: () })?;
371+
let size = self.size().checked_mul(n).ok_or(LayoutError)?;
372372
Layout::from_size_align(size, self.align())
373373
}
374374

@@ -381,7 +381,7 @@ impl Layout {
381381
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
382382
#[inline]
383383
pub fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> {
384-
let new_size = self.size().checked_add(next.size()).ok_or(LayoutError { private: () })?;
384+
let new_size = self.size().checked_add(next.size()).ok_or(LayoutError)?;
385385
Layout::from_size_align(new_size, self.align())
386386
}
387387

@@ -409,10 +409,9 @@ pub type LayoutErr = LayoutError;
409409
/// or some other `Layout` constructor
410410
/// do not satisfy its documented constraints.
411411
#[stable(feature = "alloc_layout_error", since = "1.50.0")]
412+
#[non_exhaustive]
412413
#[derive(Clone, PartialEq, Eq, Debug)]
413-
pub struct LayoutError {
414-
private: (),
415-
}
414+
pub struct LayoutError;
416415

417416
// (we need this for downstream impl of trait Error)
418417
#[stable(feature = "alloc_layout", since = "1.28.0")]

library/core/src/cell.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,8 @@ pub struct RefCell<T: ?Sized> {
593593

594594
/// An error returned by [`RefCell::try_borrow`].
595595
#[stable(feature = "try_borrow", since = "1.13.0")]
596+
#[non_exhaustive]
596597
pub struct BorrowError {
597-
_private: (),
598598
#[cfg(feature = "debug_refcell")]
599599
location: &'static crate::panic::Location<'static>,
600600
}
@@ -620,8 +620,8 @@ impl Display for BorrowError {
620620

621621
/// An error returned by [`RefCell::try_borrow_mut`].
622622
#[stable(feature = "try_borrow", since = "1.13.0")]
623+
#[non_exhaustive]
623624
pub struct BorrowMutError {
624-
_private: (),
625625
#[cfg(feature = "debug_refcell")]
626626
location: &'static crate::panic::Location<'static>,
627627
}
@@ -872,7 +872,6 @@ impl<T: ?Sized> RefCell<T> {
872872
Ok(Ref { value: unsafe { &*self.value.get() }, borrow: b })
873873
}
874874
None => Err(BorrowError {
875-
_private: (),
876875
// If a borrow occured, then we must already have an outstanding borrow,
877876
// so `borrowed_at` will be `Some`
878877
#[cfg(feature = "debug_refcell")]
@@ -958,7 +957,6 @@ impl<T: ?Sized> RefCell<T> {
958957
Ok(RefMut { value: unsafe { &mut *self.value.get() }, borrow: b })
959958
}
960959
None => Err(BorrowMutError {
961-
_private: (),
962960
// If a borrow occured, then we must already have an outstanding borrow,
963961
// so `borrowed_at` will be `Some`
964962
#[cfg(feature = "debug_refcell")]
@@ -1080,7 +1078,6 @@ impl<T: ?Sized> RefCell<T> {
10801078
Ok(unsafe { &*self.value.get() })
10811079
} else {
10821080
Err(BorrowError {
1083-
_private: (),
10841081
// If a borrow occured, then we must already have an outstanding borrow,
10851082
// so `borrowed_at` will be `Some`
10861083
#[cfg(feature = "debug_refcell")]

library/core/src/str/error.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,9 @@ impl fmt::Display for Utf8Error {
118118
///
119119
/// [`from_str`]: super::FromStr::from_str
120120
#[derive(Debug, Clone, PartialEq, Eq)]
121+
#[non_exhaustive]
121122
#[stable(feature = "rust1", since = "1.0.0")]
122-
pub struct ParseBoolError {
123-
pub(super) _priv: (),
124-
}
123+
pub struct ParseBoolError;
125124

126125
#[stable(feature = "rust1", since = "1.0.0")]
127126
impl fmt::Display for ParseBoolError {

library/core/src/str/traits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ impl FromStr for bool {
585585
match s {
586586
"true" => Ok(true),
587587
"false" => Ok(false),
588-
_ => Err(ParseBoolError { _priv: () }),
588+
_ => Err(ParseBoolError),
589589
}
590590
}
591591
}

library/proc_macro/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,13 @@ impl !Sync for TokenStream {}
8585

8686
/// Error returned from `TokenStream::from_str`.
8787
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
88+
#[non_exhaustive]
8889
#[derive(Debug)]
89-
pub struct LexError {
90-
_inner: (),
91-
}
90+
pub struct LexError;
9291

9392
impl LexError {
9493
fn new() -> Self {
95-
LexError { _inner: () }
94+
LexError
9695
}
9796
}
9897

library/std/src/io/util.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ use crate::io::{
1313
/// This struct is generally created by calling [`empty()`]. Please see
1414
/// the documentation of [`empty()`] for more details.
1515
#[stable(feature = "rust1", since = "1.0.0")]
16-
pub struct Empty {
17-
_priv: (),
18-
}
16+
#[non_exhaustive]
17+
pub struct Empty;
1918

2019
/// Constructs a new handle to an empty reader.
2120
///
@@ -35,7 +34,7 @@ pub struct Empty {
3534
#[stable(feature = "rust1", since = "1.0.0")]
3635
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
3736
pub const fn empty() -> Empty {
38-
Empty { _priv: () }
37+
Empty
3938
}
4039

4140
#[stable(feature = "rust1", since = "1.0.0")]
@@ -172,9 +171,8 @@ impl fmt::Debug for Repeat {
172171
/// This struct is generally created by calling [`sink`]. Please
173172
/// see the documentation of [`sink()`] for more details.
174173
#[stable(feature = "rust1", since = "1.0.0")]
175-
pub struct Sink {
176-
_priv: (),
177-
}
174+
#[non_exhaustive]
175+
pub struct Sink;
178176

179177
/// Creates an instance of a writer which will successfully consume all data.
180178
///
@@ -195,7 +193,7 @@ pub struct Sink {
195193
#[stable(feature = "rust1", since = "1.0.0")]
196194
#[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
197195
pub const fn sink() -> Sink {
198-
Sink { _priv: () }
196+
Sink
199197
}
200198

201199
#[stable(feature = "rust1", since = "1.0.0")]

library/std/src/thread/local.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,9 @@ macro_rules! __thread_local_inner {
324324

325325
/// An error returned by [`LocalKey::try_with`](struct.LocalKey.html#method.try_with).
326326
#[stable(feature = "thread_local_try_with", since = "1.26.0")]
327+
#[non_exhaustive]
327328
#[derive(Clone, Copy, Eq, PartialEq)]
328-
pub struct AccessError {
329-
_private: (),
330-
}
329+
pub struct AccessError;
331330

332331
#[stable(feature = "thread_local_try_with", since = "1.26.0")]
333332
impl fmt::Debug for AccessError {
@@ -396,7 +395,7 @@ impl<T: 'static> LocalKey<T> {
396395
F: FnOnce(&T) -> R,
397396
{
398397
unsafe {
399-
let thread_local = (self.inner)().ok_or(AccessError { _private: () })?;
398+
let thread_local = (self.inner)().ok_or(AccessError)?;
400399
Ok(f(thread_local))
401400
}
402401
}

0 commit comments

Comments
 (0)