Skip to content

Commit

Permalink
Rollup merge of rust-lang#131706 - GKFX:fix-const-hacks, r=tgross35
Browse files Browse the repository at this point in the history
Fix two const-hacks

Fix two pieces of code marked `FIXME(const-hack)` related to const_option rust-lang#67441.
  • Loading branch information
matthiaskrgr authored Oct 15, 2024
2 parents 54072ab + 63f3836 commit da7ca22
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
8 changes: 3 additions & 5 deletions core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,9 @@ impl Duration {
// SAFETY: nanos < NANOS_PER_SEC, therefore nanos is within the valid range
Duration { secs, nanos: unsafe { Nanoseconds(nanos) } }
} else {
// FIXME(const-hack): use `.expect` once that is possible.
let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) {
Some(secs) => secs,
None => panic!("overflow in Duration::new"),
};
let secs = secs
.checked_add((nanos / NANOS_PER_SEC) as u64)
.expect("overflow in Duration::new");
let nanos = nanos % NANOS_PER_SEC;
// SAFETY: nanos % NANOS_PER_SEC < NANOS_PER_SEC, therefore nanos is within the valid range
Duration { secs, nanos: unsafe { Nanoseconds(nanos) } }
Expand Down
19 changes: 4 additions & 15 deletions std/src/sys/pal/windows/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ use crate::sys_common::AsInner;
use crate::sys_common::wstr::WStrUnits;
use crate::{fmt, io, iter, vec};

/// This is the const equivalent to `NonZero::new(n).unwrap()`
///
/// FIXME(const-hack): This can be removed once `Option::unwrap` is stably const.
/// See the `const_option` feature (#67441).
const fn non_zero_u16(n: u16) -> NonZero<u16> {
match NonZero::new(n) {
Some(n) => n,
None => panic!("called `unwrap` on a `None` value"),
}
}

pub fn args() -> Args {
// SAFETY: `GetCommandLineW` returns a pointer to a null terminated UTF-16
// string so it's safe for `WStrUnits` to use.
Expand Down Expand Up @@ -66,10 +55,10 @@ fn parse_lp_cmd_line<'a, F: Fn() -> OsString>(
lp_cmd_line: Option<WStrUnits<'a>>,
exe_name: F,
) -> Vec<OsString> {
const BACKSLASH: NonZero<u16> = non_zero_u16(b'\\' as u16);
const QUOTE: NonZero<u16> = non_zero_u16(b'"' as u16);
const TAB: NonZero<u16> = non_zero_u16(b'\t' as u16);
const SPACE: NonZero<u16> = non_zero_u16(b' ' as u16);
const BACKSLASH: NonZero<u16> = NonZero::new(b'\\' as u16).unwrap();
const QUOTE: NonZero<u16> = NonZero::new(b'"' as u16).unwrap();
const TAB: NonZero<u16> = NonZero::new(b'\t' as u16).unwrap();
const SPACE: NonZero<u16> = NonZero::new(b' ' as u16).unwrap();

let mut ret_val = Vec::new();
// If the cmd line pointer is null or it points to an empty string then
Expand Down

0 comments on commit da7ca22

Please sign in to comment.