Skip to content

Commit e028f26

Browse files
authored
Rollup merge of #69209 - Mark-Simulacrum:strip-unsafe, r=dtolnay
Miscellaneous cleanup to formatting Each commit stands alone. This pull request will also resolve #58320.
2 parents 86b9377 + f6bfdc9 commit e028f26

File tree

6 files changed

+273
-292
lines changed

6 files changed

+273
-292
lines changed

src/libcore/fmt/float.rs

-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ where
2929
*num,
3030
sign,
3131
precision,
32-
false,
3332
buf.get_mut(),
3433
parts.get_mut(),
3534
);
@@ -59,7 +58,6 @@ where
5958
*num,
6059
sign,
6160
precision,
62-
false,
6361
buf.get_mut(),
6462
parts.get_mut(),
6563
);

src/libcore/fmt/mod.rs

+23-22
Original file line numberDiff line numberDiff line change
@@ -238,16 +238,8 @@ pub struct Formatter<'a> {
238238
// NB. Argument is essentially an optimized partially applied formatting function,
239239
// equivalent to `exists T.(&T, fn(&T, &mut Formatter<'_>) -> Result`.
240240

241-
struct Void {
242-
_priv: (),
243-
/// Erases all oibits, because `Void` erases the type of the object that
244-
/// will be used to produce formatted output. Since we do not know what
245-
/// oibits the real types have (and they can have any or none), we need to
246-
/// take the most conservative approach and forbid all oibits.
247-
///
248-
/// It was added after #45197 showed that one could share a `!Sync`
249-
/// object across threads by passing it into `format_args!`.
250-
_oibit_remover: PhantomData<*mut dyn Fn()>,
241+
extern "C" {
242+
type Opaque;
251243
}
252244

253245
/// This struct represents the generic "argument" which is taken by the Xprintf
@@ -259,16 +251,23 @@ struct Void {
259251
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
260252
#[doc(hidden)]
261253
pub struct ArgumentV1<'a> {
262-
value: &'a Void,
263-
formatter: fn(&Void, &mut Formatter<'_>) -> Result,
254+
value: &'a Opaque,
255+
formatter: fn(&Opaque, &mut Formatter<'_>) -> Result,
264256
}
265257

266-
impl<'a> ArgumentV1<'a> {
267-
#[inline(never)]
268-
fn show_usize(x: &usize, f: &mut Formatter<'_>) -> Result {
269-
Display::fmt(x, f)
270-
}
258+
// This gurantees a single stable value for the function pointer associated with
259+
// indices/counts in the formatting infrastructure.
260+
//
261+
// Note that a function defined as such would not be correct as functions are
262+
// always tagged unnamed_addr with the current lowering to LLVM IR, so their
263+
// address is not considered important to LLVM and as such the as_usize cast
264+
// could have been miscompiled. In practice, we never call as_usize on non-usize
265+
// containing data (as a matter of static generation of the formatting
266+
// arguments), so this is merely an additional check.
267+
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
268+
static USIZE_MARKER: fn(&usize, &mut Formatter<'_>) -> Result = |_, _| loop {};
271269

270+
impl<'a> ArgumentV1<'a> {
272271
#[doc(hidden)]
273272
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
274273
pub fn new<'b, T>(x: &'b T, f: fn(&T, &mut Formatter<'_>) -> Result) -> ArgumentV1<'b> {
@@ -278,11 +277,13 @@ impl<'a> ArgumentV1<'a> {
278277
#[doc(hidden)]
279278
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
280279
pub fn from_usize(x: &usize) -> ArgumentV1<'_> {
281-
ArgumentV1::new(x, ArgumentV1::show_usize)
280+
ArgumentV1::new(x, USIZE_MARKER)
282281
}
283282

284283
fn as_usize(&self) -> Option<usize> {
285-
if self.formatter as usize == ArgumentV1::show_usize as usize {
284+
if self.formatter as usize == USIZE_MARKER as usize {
285+
// SAFETY: The `formatter` field is only set to USIZE_MARKER if
286+
// the value is a usize, so this is safe
286287
Some(unsafe { *(self.value as *const _ as *const usize) })
287288
} else {
288289
None
@@ -1356,11 +1357,11 @@ impl<'a> Formatter<'a> {
13561357
let mut align = old_align;
13571358
if self.sign_aware_zero_pad() {
13581359
// a sign always goes first
1359-
let sign = unsafe { str::from_utf8_unchecked(formatted.sign) };
1360+
let sign = formatted.sign;
13601361
self.buf.write_str(sign)?;
13611362

13621363
// remove the sign from the formatted parts
1363-
formatted.sign = b"";
1364+
formatted.sign = "";
13641365
width = width.saturating_sub(sign.len());
13651366
align = rt::v1::Alignment::Right;
13661367
self.fill = '0';
@@ -1392,7 +1393,7 @@ impl<'a> Formatter<'a> {
13921393
}
13931394

13941395
if !formatted.sign.is_empty() {
1395-
write_bytes(self.buf, formatted.sign)?;
1396+
self.buf.write_str(formatted.sign)?;
13961397
}
13971398
for part in formatted.parts {
13981399
match *part {

src/libcore/fmt/num.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,11 @@ macro_rules! impl_Exp {
369369
flt2dec::Part::Copy(exp_slice)
370370
];
371371
let sign = if !is_nonnegative {
372-
&b"-"[..]
372+
"-"
373373
} else if f.sign_plus() {
374-
&b"+"[..]
374+
"+"
375375
} else {
376-
&b""[..]
376+
""
377377
};
378378
let formatted = flt2dec::Formatted{sign, parts};
379379
f.pad_formatted_parts(&formatted)

src/libcore/num/flt2dec/mod.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl<'a> Part<'a> {
237237
#[derive(Clone)]
238238
pub struct Formatted<'a> {
239239
/// A byte slice representing a sign, either `""`, `"-"` or `"+"`.
240-
pub sign: &'static [u8],
240+
pub sign: &'static str,
241241
/// Formatted parts to be rendered after a sign and optional zero padding.
242242
pub parts: &'a [Part<'a>],
243243
}
@@ -259,7 +259,7 @@ impl<'a> Formatted<'a> {
259259
if out.len() < self.sign.len() {
260260
return None;
261261
}
262-
out[..self.sign.len()].copy_from_slice(self.sign);
262+
out[..self.sign.len()].copy_from_slice(self.sign.as_bytes());
263263

264264
let mut written = self.sign.len();
265265
for part in self.parts {
@@ -402,38 +402,38 @@ pub enum Sign {
402402
}
403403

404404
/// Returns the static byte string corresponding to the sign to be formatted.
405-
/// It can be either `b""`, `b"+"` or `b"-"`.
406-
fn determine_sign(sign: Sign, decoded: &FullDecoded, negative: bool) -> &'static [u8] {
405+
/// It can be either `""`, `"+"` or `"-"`.
406+
fn determine_sign(sign: Sign, decoded: &FullDecoded, negative: bool) -> &'static str {
407407
match (*decoded, sign) {
408-
(FullDecoded::Nan, _) => b"",
409-
(FullDecoded::Zero, Sign::Minus) => b"",
408+
(FullDecoded::Nan, _) => "",
409+
(FullDecoded::Zero, Sign::Minus) => "",
410410
(FullDecoded::Zero, Sign::MinusRaw) => {
411411
if negative {
412-
b"-"
412+
"-"
413413
} else {
414-
b""
414+
""
415415
}
416416
}
417-
(FullDecoded::Zero, Sign::MinusPlus) => b"+",
417+
(FullDecoded::Zero, Sign::MinusPlus) => "+",
418418
(FullDecoded::Zero, Sign::MinusPlusRaw) => {
419419
if negative {
420-
b"-"
420+
"-"
421421
} else {
422-
b"+"
422+
"+"
423423
}
424424
}
425425
(_, Sign::Minus) | (_, Sign::MinusRaw) => {
426426
if negative {
427-
b"-"
427+
"-"
428428
} else {
429-
b""
429+
""
430430
}
431431
}
432432
(_, Sign::MinusPlus) | (_, Sign::MinusPlusRaw) => {
433433
if negative {
434-
b"-"
434+
"-"
435435
} else {
436-
b"+"
436+
"+"
437437
}
438438
}
439439
}
@@ -462,7 +462,6 @@ pub fn to_shortest_str<'a, T, F>(
462462
v: T,
463463
sign: Sign,
464464
frac_digits: usize,
465-
_upper: bool,
466465
buf: &'a mut [u8],
467466
parts: &'a mut [Part<'a>],
468467
) -> Formatted<'a>
@@ -679,7 +678,6 @@ pub fn to_exact_fixed_str<'a, T, F>(
679678
v: T,
680679
sign: Sign,
681680
frac_digits: usize,
682-
_upper: bool,
683681
buf: &'a mut [u8],
684682
parts: &'a mut [Part<'a>],
685683
) -> Formatted<'a>

0 commit comments

Comments
 (0)