Skip to content

Commit abc3073

Browse files
committed
Auto merge of #69484 - Dylan-DPC:rollup-j6ripxy, r=Dylan-DPC
Rollup of 5 pull requests Successful merges: - #68712 (Add methods to 'leak' RefCell borrows as references with the lifetime of the original reference) - #69209 (Miscellaneous cleanup to formatting) - #69381 (Allow getting `no_std` from the config file) - #69434 (rustc_metadata: Use binary search from standard library) - #69447 (Minor refactoring of statement parsing) Failed merges: r? @ghost
2 parents 892cb14 + ae383e2 commit abc3073

File tree

11 files changed

+445
-406
lines changed

11 files changed

+445
-406
lines changed

src/bootstrap/config.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ pub struct Target {
177177
pub no_std: bool,
178178
}
179179

180+
impl Target {
181+
pub fn from_triple(triple: &str) -> Self {
182+
let mut target: Self = Default::default();
183+
if triple.contains("-none-") || triple.contains("nvptx") {
184+
target.no_std = true;
185+
}
186+
target
187+
}
188+
}
180189
/// Structure of the `config.toml` file that configuration is read from.
181190
///
182191
/// This structure uses `Decodable` to automatically decode a TOML configuration
@@ -353,6 +362,7 @@ struct TomlTarget {
353362
musl_root: Option<String>,
354363
wasi_root: Option<String>,
355364
qemu_rootfs: Option<String>,
365+
no_std: Option<bool>,
356366
}
357367

358368
impl Config {
@@ -595,7 +605,7 @@ impl Config {
595605

596606
if let Some(ref t) = toml.target {
597607
for (triple, cfg) in t {
598-
let mut target = Target::default();
608+
let mut target = Target::from_triple(triple);
599609

600610
if let Some(ref s) = cfg.llvm_config {
601611
target.llvm_config = Some(config.src.join(s));
@@ -606,6 +616,9 @@ impl Config {
606616
if let Some(ref s) = cfg.android_ndk {
607617
target.ndk = Some(config.src.join(s));
608618
}
619+
if let Some(s) = cfg.no_std {
620+
target.no_std = s;
621+
}
609622
target.cc = cfg.cc.clone().map(PathBuf::from);
610623
target.cxx = cfg.cxx.clone().map(PathBuf::from);
611624
target.ar = cfg.ar.clone().map(PathBuf::from);

src/bootstrap/sanity.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::process::Command;
1717

1818
use build_helper::{output, t};
1919

20+
use crate::config::Target;
2021
use crate::Build;
2122

2223
struct Finder {
@@ -192,13 +193,9 @@ pub fn check(build: &mut Build) {
192193
panic!("the iOS target is only supported on macOS");
193194
}
194195

195-
if target.contains("-none-") || target.contains("nvptx") {
196-
if build.no_std(*target).is_none() {
197-
let target = build.config.target_config.entry(target.clone()).or_default();
198-
199-
target.no_std = true;
200-
}
196+
build.config.target_config.entry(target.clone()).or_insert(Target::from_triple(target));
201197

198+
if target.contains("-none-") || target.contains("nvptx") {
202199
if build.no_std(*target) == Some(false) {
203200
panic!("All the *-none-* and nvptx* targets are no-std targets")
204201
}

src/libcore/cell.rs

+63
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,38 @@ impl<'b, T: ?Sized> Ref<'b, T> {
12451245
let borrow = orig.borrow.clone();
12461246
(Ref { value: a, borrow }, Ref { value: b, borrow: orig.borrow })
12471247
}
1248+
1249+
/// Convert into a reference to the underlying data.
1250+
///
1251+
/// The underlying `RefCell` can never be mutably borrowed from again and will always appear
1252+
/// already immutably borrowed. It is not a good idea to leak more than a constant number of
1253+
/// references. The `RefCell` can be immutably borrowed again if only a smaller number of leaks
1254+
/// have occurred in total.
1255+
///
1256+
/// This is an associated function that needs to be used as
1257+
/// `Ref::leak(...)`. A method would interfere with methods of the
1258+
/// same name on the contents of a `RefCell` used through `Deref`.
1259+
///
1260+
/// # Examples
1261+
///
1262+
/// ```
1263+
/// #![feature(cell_leak)]
1264+
/// use std::cell::{RefCell, Ref};
1265+
/// let cell = RefCell::new(0);
1266+
///
1267+
/// let value = Ref::leak(cell.borrow());
1268+
/// assert_eq!(*value, 0);
1269+
///
1270+
/// assert!(cell.try_borrow().is_ok());
1271+
/// assert!(cell.try_borrow_mut().is_err());
1272+
/// ```
1273+
#[unstable(feature = "cell_leak", issue = "69099")]
1274+
pub fn leak(orig: Ref<'b, T>) -> &'b T {
1275+
// By forgetting this Ref we ensure that the borrow counter in the RefCell never goes back
1276+
// to UNUSED again. No further mutable references can be created from the original cell.
1277+
mem::forget(orig.borrow);
1278+
orig.value
1279+
}
12481280
}
12491281

12501282
#[unstable(feature = "coerce_unsized", issue = "27732")]
@@ -1330,6 +1362,37 @@ impl<'b, T: ?Sized> RefMut<'b, T> {
13301362
let borrow = orig.borrow.clone();
13311363
(RefMut { value: a, borrow }, RefMut { value: b, borrow: orig.borrow })
13321364
}
1365+
1366+
/// Convert into a mutable reference to the underlying data.
1367+
///
1368+
/// The underlying `RefCell` can not be borrowed from again and will always appear already
1369+
/// mutably borrowed, making the returned reference the only to the interior.
1370+
///
1371+
/// This is an associated function that needs to be used as
1372+
/// `RefMut::leak(...)`. A method would interfere with methods of the
1373+
/// same name on the contents of a `RefCell` used through `Deref`.
1374+
///
1375+
/// # Examples
1376+
///
1377+
/// ```
1378+
/// #![feature(cell_leak)]
1379+
/// use std::cell::{RefCell, RefMut};
1380+
/// let cell = RefCell::new(0);
1381+
///
1382+
/// let value = RefMut::leak(cell.borrow_mut());
1383+
/// assert_eq!(*value, 0);
1384+
/// *value = 1;
1385+
///
1386+
/// assert!(cell.try_borrow_mut().is_err());
1387+
/// ```
1388+
#[unstable(feature = "cell_leak", issue = "69099")]
1389+
pub fn leak(orig: RefMut<'b, T>) -> &'b mut T {
1390+
// By forgetting this BorrowRefMut we ensure that the borrow counter in the RefCell never
1391+
// goes back to UNUSED again. No further references can be created from the original cell,
1392+
// making the current borrow the only reference for the remaining lifetime.
1393+
mem::forget(orig.borrow);
1394+
orig.value
1395+
}
13331396
}
13341397

13351398
struct BorrowRefMut<'b> {

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)