Skip to content

Commit dbef353

Browse files
committed
Auto merge of #69182 - Dylan-DPC:rollup-ifsa9fx, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - #64069 (Added From<Vec<NonZeroU8>> for CString) - #66721 (implement LowerExp and UpperExp for integers) - #69106 (Fix std::fs::copy on WASI target) - #69154 (Avoid calling `fn_sig` on closures) - #69166 (Check `has_typeck_tables` before calling `typeck_tables_of`) - #69180 (Suggest a comma if a struct initializer field fails to parse) Failed merges: r? @ghost
2 parents 19288dd + e9db061 commit dbef353

File tree

14 files changed

+387
-7
lines changed

14 files changed

+387
-7
lines changed

src/libcore/fmt/num.rs

+163
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use crate::fmt;
66
use crate::mem::MaybeUninit;
7+
use crate::num::flt2dec;
78
use crate::ops::{Div, Rem, Sub};
89
use crate::ptr;
910
use crate::slice;
@@ -256,6 +257,161 @@ macro_rules! impl_Display {
256257
};
257258
}
258259

260+
macro_rules! impl_Exp {
261+
($($t:ident),* as $u:ident via $conv_fn:ident named $name:ident) => {
262+
fn $name(
263+
mut n: $u,
264+
is_nonnegative: bool,
265+
upper: bool,
266+
f: &mut fmt::Formatter<'_>
267+
) -> fmt::Result {
268+
let (mut n, mut exponent, trailing_zeros, added_precision) = {
269+
let mut exponent = 0;
270+
// count and remove trailing decimal zeroes
271+
while n % 10 == 0 && n >= 10 {
272+
n /= 10;
273+
exponent += 1;
274+
}
275+
let trailing_zeros = exponent;
276+
277+
let (added_precision, subtracted_precision) = match f.precision() {
278+
Some(fmt_prec) => {
279+
// number of decimal digits minus 1
280+
let mut tmp = n;
281+
let mut prec = 0;
282+
while tmp >= 10 {
283+
tmp /= 10;
284+
prec += 1;
285+
}
286+
(fmt_prec.saturating_sub(prec), prec.saturating_sub(fmt_prec))
287+
}
288+
None => (0,0)
289+
};
290+
for _ in 1..subtracted_precision {
291+
n/=10;
292+
exponent += 1;
293+
}
294+
if subtracted_precision != 0 {
295+
let rem = n % 10;
296+
n /= 10;
297+
exponent += 1;
298+
// round up last digit
299+
if rem >= 5 {
300+
n += 1;
301+
}
302+
}
303+
(n, exponent, trailing_zeros, added_precision)
304+
};
305+
306+
// 39 digits (worst case u128) + . = 40
307+
let mut buf = [MaybeUninit::<u8>::uninit(); 40];
308+
let mut curr = buf.len() as isize; //index for buf
309+
let buf_ptr = MaybeUninit::first_ptr_mut(&mut buf);
310+
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
311+
312+
// decode 2 chars at a time
313+
while n >= 100 {
314+
let d1 = ((n % 100) as isize) << 1;
315+
curr -= 2;
316+
unsafe {
317+
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
318+
}
319+
n /= 100;
320+
exponent += 2;
321+
}
322+
// n is <= 99, so at most 2 chars long
323+
let mut n = n as isize; // possibly reduce 64bit math
324+
// decode second-to-last character
325+
if n >= 10 {
326+
curr -= 1;
327+
unsafe {
328+
*buf_ptr.offset(curr) = (n as u8 % 10_u8) + b'0';
329+
}
330+
n /= 10;
331+
exponent += 1;
332+
}
333+
// add decimal point iff >1 mantissa digit will be printed
334+
if exponent != trailing_zeros || added_precision != 0 {
335+
curr -= 1;
336+
unsafe {
337+
*buf_ptr.offset(curr) = b'.';
338+
}
339+
}
340+
341+
let buf_slice = unsafe {
342+
// decode last character
343+
curr -= 1;
344+
*buf_ptr.offset(curr) = (n as u8) + b'0';
345+
346+
let len = buf.len() - curr as usize;
347+
slice::from_raw_parts(buf_ptr.offset(curr), len)
348+
};
349+
350+
// stores 'e' (or 'E') and the up to 2-digit exponent
351+
let mut exp_buf = [MaybeUninit::<u8>::uninit(); 3];
352+
let exp_ptr = MaybeUninit::first_ptr_mut(&mut exp_buf);
353+
let exp_slice = unsafe {
354+
*exp_ptr.offset(0) = if upper {b'E'} else {b'e'};
355+
let len = if exponent < 10 {
356+
*exp_ptr.offset(1) = (exponent as u8) + b'0';
357+
2
358+
} else {
359+
let off = exponent << 1;
360+
ptr::copy_nonoverlapping(lut_ptr.offset(off), exp_ptr.offset(1), 2);
361+
3
362+
};
363+
slice::from_raw_parts(exp_ptr, len)
364+
};
365+
366+
let parts = &[
367+
flt2dec::Part::Copy(buf_slice),
368+
flt2dec::Part::Zero(added_precision),
369+
flt2dec::Part::Copy(exp_slice)
370+
];
371+
let sign = if !is_nonnegative {
372+
&b"-"[..]
373+
} else if f.sign_plus() {
374+
&b"+"[..]
375+
} else {
376+
&b""[..]
377+
};
378+
let formatted = flt2dec::Formatted{sign, parts};
379+
f.pad_formatted_parts(&formatted)
380+
}
381+
382+
$(
383+
#[stable(feature = "integer_exp_format", since = "1.42.0")]
384+
impl fmt::LowerExp for $t {
385+
#[allow(unused_comparisons)]
386+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
387+
let is_nonnegative = *self >= 0;
388+
let n = if is_nonnegative {
389+
self.$conv_fn()
390+
} else {
391+
// convert the negative num to positive by summing 1 to it's 2 complement
392+
(!self.$conv_fn()).wrapping_add(1)
393+
};
394+
$name(n, is_nonnegative, false, f)
395+
}
396+
})*
397+
$(
398+
#[stable(feature = "integer_exp_format", since = "1.42.0")]
399+
impl fmt::UpperExp for $t {
400+
#[allow(unused_comparisons)]
401+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
402+
let is_nonnegative = *self >= 0;
403+
let n = if is_nonnegative {
404+
self.$conv_fn()
405+
} else {
406+
// convert the negative num to positive by summing 1 to it's 2 complement
407+
(!self.$conv_fn()).wrapping_add(1)
408+
};
409+
$name(n, is_nonnegative, true, f)
410+
}
411+
})*
412+
};
413+
}
414+
259415
// Include wasm32 in here since it doesn't reflect the native pointer size, and
260416
// often cares strongly about getting a smaller code size.
261417
#[cfg(any(target_pointer_width = "64", target_arch = "wasm32"))]
@@ -265,13 +421,20 @@ mod imp {
265421
i8, u8, i16, u16, i32, u32, i64, u64, usize, isize
266422
as u64 via to_u64 named fmt_u64
267423
);
424+
impl_Exp!(
425+
i8, u8, i16, u16, i32, u32, i64, u64, usize, isize
426+
as u64 via to_u64 named exp_u64
427+
);
268428
}
269429

270430
#[cfg(not(any(target_pointer_width = "64", target_arch = "wasm32")))]
271431
mod imp {
272432
use super::*;
273433
impl_Display!(i8, u8, i16, u16, i32, u32, isize, usize as u32 via to_u32 named fmt_u32);
274434
impl_Display!(i64, u64 as u64 via to_u64 named fmt_u64);
435+
impl_Exp!(i8, u8, i16, u16, i32, u32, isize, usize as u32 via to_u32 named exp_u32);
436+
impl_Exp!(i64, u64 as u64 via to_u64 named exp_u64);
275437
}
276438

277439
impl_Display!(i128, u128 as u128 via to_u128 named fmt_u128);
440+
impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128);

src/libcore/tests/fmt/num.rs

+80
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ fn test_format_int() {
3838
assert_eq!(format!("{:o}", 1i16), "1");
3939
assert_eq!(format!("{:o}", 1i32), "1");
4040
assert_eq!(format!("{:o}", 1i64), "1");
41+
assert_eq!(format!("{:e}", 1isize), "1e0");
42+
assert_eq!(format!("{:e}", 1i8), "1e0");
43+
assert_eq!(format!("{:e}", 1i16), "1e0");
44+
assert_eq!(format!("{:e}", 1i32), "1e0");
45+
assert_eq!(format!("{:e}", 1i64), "1e0");
46+
assert_eq!(format!("{:E}", 1isize), "1E0");
47+
assert_eq!(format!("{:E}", 1i8), "1E0");
48+
assert_eq!(format!("{:E}", 1i16), "1E0");
49+
assert_eq!(format!("{:E}", 1i32), "1E0");
50+
assert_eq!(format!("{:E}", 1i64), "1E0");
4151

4252
assert_eq!(format!("{}", 1usize), "1");
4353
assert_eq!(format!("{}", 1u8), "1");
@@ -69,13 +79,79 @@ fn test_format_int() {
6979
assert_eq!(format!("{:o}", 1u16), "1");
7080
assert_eq!(format!("{:o}", 1u32), "1");
7181
assert_eq!(format!("{:o}", 1u64), "1");
82+
assert_eq!(format!("{:e}", 1u8), "1e0");
83+
assert_eq!(format!("{:e}", 1u16), "1e0");
84+
assert_eq!(format!("{:e}", 1u32), "1e0");
85+
assert_eq!(format!("{:e}", 1u64), "1e0");
86+
assert_eq!(format!("{:E}", 1u8), "1E0");
87+
assert_eq!(format!("{:E}", 1u16), "1E0");
88+
assert_eq!(format!("{:E}", 1u32), "1E0");
89+
assert_eq!(format!("{:E}", 1u64), "1E0");
7290

7391
// Test a larger number
7492
assert_eq!(format!("{:b}", 55), "110111");
7593
assert_eq!(format!("{:o}", 55), "67");
7694
assert_eq!(format!("{}", 55), "55");
7795
assert_eq!(format!("{:x}", 55), "37");
7896
assert_eq!(format!("{:X}", 55), "37");
97+
assert_eq!(format!("{:e}", 55), "5.5e1");
98+
assert_eq!(format!("{:E}", 55), "5.5E1");
99+
assert_eq!(format!("{:e}", 10000000000u64), "1e10");
100+
assert_eq!(format!("{:E}", 10000000000u64), "1E10");
101+
assert_eq!(format!("{:e}", 10000000001u64), "1.0000000001e10");
102+
assert_eq!(format!("{:E}", 10000000001u64), "1.0000000001E10");
103+
}
104+
105+
#[test]
106+
fn test_format_int_exp_limits() {
107+
use core::{i128, i16, i32, i64, i8, u128, u16, u32, u64, u8};
108+
assert_eq!(format!("{:e}", i8::MIN), "-1.28e2");
109+
assert_eq!(format!("{:e}", i8::MAX), "1.27e2");
110+
assert_eq!(format!("{:e}", i16::MIN), "-3.2768e4");
111+
assert_eq!(format!("{:e}", i16::MAX), "3.2767e4");
112+
assert_eq!(format!("{:e}", i32::MIN), "-2.147483648e9");
113+
assert_eq!(format!("{:e}", i32::MAX), "2.147483647e9");
114+
assert_eq!(format!("{:e}", i64::MIN), "-9.223372036854775808e18");
115+
assert_eq!(format!("{:e}", i64::MAX), "9.223372036854775807e18");
116+
assert_eq!(format!("{:e}", i128::MIN), "-1.70141183460469231731687303715884105728e38");
117+
assert_eq!(format!("{:e}", i128::MAX), "1.70141183460469231731687303715884105727e38");
118+
119+
assert_eq!(format!("{:e}", u8::MAX), "2.55e2");
120+
assert_eq!(format!("{:e}", u16::MAX), "6.5535e4");
121+
assert_eq!(format!("{:e}", u32::MAX), "4.294967295e9");
122+
assert_eq!(format!("{:e}", u64::MAX), "1.8446744073709551615e19");
123+
assert_eq!(format!("{:e}", u128::MAX), "3.40282366920938463463374607431768211455e38");
124+
}
125+
126+
#[test]
127+
fn test_format_int_exp_precision() {
128+
use core::{i128, i16, i32, i64, i8};
129+
130+
//test that float and integer match
131+
let big_int: u32 = 314_159_265;
132+
assert_eq!(format!("{:.1e}", big_int), format!("{:.1e}", f64::from(big_int)));
133+
134+
//test adding precision
135+
assert_eq!(format!("{:.10e}", i8::MIN), "-1.2800000000e2");
136+
assert_eq!(format!("{:.10e}", i16::MIN), "-3.2768000000e4");
137+
assert_eq!(format!("{:.10e}", i32::MIN), "-2.1474836480e9");
138+
assert_eq!(format!("{:.20e}", i64::MIN), "-9.22337203685477580800e18");
139+
assert_eq!(format!("{:.40e}", i128::MIN), "-1.7014118346046923173168730371588410572800e38");
140+
141+
//test rounding
142+
assert_eq!(format!("{:.1e}", i8::MIN), "-1.3e2");
143+
assert_eq!(format!("{:.1e}", i16::MIN), "-3.3e4");
144+
assert_eq!(format!("{:.1e}", i32::MIN), "-2.1e9");
145+
assert_eq!(format!("{:.1e}", i64::MIN), "-9.2e18");
146+
assert_eq!(format!("{:.1e}", i128::MIN), "-1.7e38");
147+
148+
//test huge precision
149+
assert_eq!(format!("{:.1000e}", 1), format!("1.{}e0", "0".repeat(1000)));
150+
//test zero precision
151+
assert_eq!(format!("{:.0e}", 1), format!("1e0",));
152+
153+
//test padding with precision (and sign)
154+
assert_eq!(format!("{:+10.3e}", 1), " +1.000e0");
79155
}
80156

81157
#[test]
@@ -86,13 +162,17 @@ fn test_format_int_zero() {
86162
assert_eq!(format!("{:o}", 0), "0");
87163
assert_eq!(format!("{:x}", 0), "0");
88164
assert_eq!(format!("{:X}", 0), "0");
165+
assert_eq!(format!("{:e}", 0), "0e0");
166+
assert_eq!(format!("{:E}", 0), "0E0");
89167

90168
assert_eq!(format!("{}", 0u32), "0");
91169
assert_eq!(format!("{:?}", 0u32), "0");
92170
assert_eq!(format!("{:b}", 0u32), "0");
93171
assert_eq!(format!("{:o}", 0u32), "0");
94172
assert_eq!(format!("{:x}", 0u32), "0");
95173
assert_eq!(format!("{:X}", 0u32), "0");
174+
assert_eq!(format!("{:e}", 0u32), "0e0");
175+
assert_eq!(format!("{:E}", 0u32), "0E0");
96176
}
97177

98178
#[test]

src/librustc_mir/const_eval/eval_queries.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,10 @@ pub fn const_eval_raw_provider<'tcx>(
288288
let cid = key.value;
289289
let def_id = cid.instance.def.def_id();
290290

291-
if def_id.is_local() && tcx.typeck_tables_of(def_id).tainted_by_errors {
291+
if def_id.is_local()
292+
&& tcx.has_typeck_tables(def_id)
293+
&& tcx.typeck_tables_of(def_id).tainted_by_errors
294+
{
292295
return Err(ErrorHandled::Reported);
293296
}
294297

src/librustc_parse/parser/expr.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1832,10 +1832,16 @@ impl<'a> Parser<'a> {
18321832
}
18331833
}
18341834
Err(mut e) => {
1835+
e.span_label(struct_sp, "while parsing this struct");
18351836
if let Some(f) = recovery_field {
18361837
fields.push(f);
1838+
e.span_suggestion(
1839+
self.prev_span.shrink_to_hi(),
1840+
"try adding a comma",
1841+
",".into(),
1842+
Applicability::MachineApplicable,
1843+
);
18371844
}
1838-
e.span_label(struct_sp, "while parsing this struct");
18391845
e.emit();
18401846
self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore);
18411847
self.eat(&token::Comma);

src/librustc_typeck/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2280,7 +2280,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
22802280
} else if attr.check_name(sym::thread_local) {
22812281
codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL;
22822282
} else if attr.check_name(sym::track_caller) {
2283-
if tcx.fn_sig(id).abi() != abi::Abi::Rust {
2283+
if tcx.is_closure(id) || tcx.fn_sig(id).abi() != abi::Abi::Rust {
22842284
struct_span_err!(tcx.sess, attr.span, E0737, "`#[track_caller]` requires Rust ABI")
22852285
.emit();
22862286
}
@@ -2301,7 +2301,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
23012301
codegen_fn_attrs.export_name = Some(s);
23022302
}
23032303
} else if attr.check_name(sym::target_feature) {
2304-
if tcx.fn_sig(id).unsafety() == Unsafety::Normal {
2304+
if tcx.is_closure(id) || tcx.fn_sig(id).unsafety() == Unsafety::Normal {
23052305
let msg = "`#[target_feature(..)]` can only be applied to `unsafe` functions";
23062306
tcx.sess
23072307
.struct_span_err(attr.span, msg)

src/libstd/ffi/c_str.rs

+27
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::fmt::{self, Write};
66
use crate::io;
77
use crate::mem;
88
use crate::memchr;
9+
use crate::num::NonZeroU8;
910
use crate::ops;
1011
use crate::os::raw::c_char;
1112
use crate::ptr;
@@ -741,6 +742,32 @@ impl From<Box<CStr>> for CString {
741742
}
742743
}
743744

745+
#[stable(feature = "cstring_from_vec_of_nonzerou8", since = "1.43.0")]
746+
impl From<Vec<NonZeroU8>> for CString {
747+
/// Converts a [`Vec`]`<`[`NonZeroU8`]`>` into a [`CString`] without
748+
/// copying nor checking for inner null bytes.
749+
///
750+
/// [`CString`]: ../ffi/struct.CString.html
751+
/// [`NonZeroU8`]: ../num/struct.NonZeroU8.html
752+
/// [`Vec`]: ../vec/struct.Vec.html
753+
#[inline]
754+
fn from(v: Vec<NonZeroU8>) -> CString {
755+
unsafe {
756+
// Transmute `Vec<NonZeroU8>` to `Vec<u8>`.
757+
let v: Vec<u8> = {
758+
// Safety:
759+
// - transmuting between `NonZeroU8` and `u8` is sound;
760+
// - `alloc::Layout<NonZeroU8> == alloc::Layout<u8>`.
761+
let (ptr, len, cap): (*mut NonZeroU8, _, _) = Vec::into_raw_parts(v);
762+
Vec::from_raw_parts(ptr.cast::<u8>(), len, cap)
763+
};
764+
// Safety: `v` cannot contain null bytes, given the type-level
765+
// invariant of `NonZeroU8`.
766+
CString::from_vec_unchecked(v)
767+
}
768+
}
769+
}
770+
744771
#[stable(feature = "more_box_slice_clone", since = "1.29.0")]
745772
impl Clone for Box<CStr> {
746773
#[inline]

0 commit comments

Comments
 (0)