Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit abc3073

Browse files
committedFeb 26, 2020
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

Lines changed: 14 additions & 1 deletion
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

Lines changed: 3 additions & 6 deletions
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

Lines changed: 63 additions & 0 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 23 additions & 22 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 15 additions & 17 deletions
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>

‎src/libcore/tests/num/flt2dec/mod.rs

Lines changed: 224 additions & 236 deletions
Large diffs are not rendered by default.

‎src/librustc_metadata/rmeta/decoder.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -408,20 +408,12 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
408408
{
409409
last_source_file
410410
} else {
411-
let mut a = 0;
412-
let mut b = imported_source_files.len();
413-
414-
while b - a > 1 {
415-
let m = (a + b) / 2;
416-
if imported_source_files[m].original_start_pos > lo {
417-
b = m;
418-
} else {
419-
a = m;
420-
}
421-
}
411+
let index = imported_source_files
412+
.binary_search_by_key(&lo, |source_file| source_file.original_start_pos)
413+
.unwrap_or_else(|index| index - 1);
422414

423-
self.last_source_file_index = a;
424-
&imported_source_files[a]
415+
self.last_source_file_index = index;
416+
&imported_source_files[index]
425417
}
426418
};
427419

‎src/librustc_parse/parser/stmt.rs

Lines changed: 87 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -35,61 +35,32 @@ impl<'a> Parser<'a> {
3535
let attrs = self.parse_outer_attributes()?;
3636
let lo = self.token.span;
3737

38-
if self.eat_keyword(kw::Let) {
39-
return self.parse_local_mk(lo, attrs.into()).map(Some);
40-
}
41-
if self.is_kw_followed_by_ident(kw::Mut) {
42-
return self.recover_stmt_local(lo, attrs.into(), "missing keyword", "let mut");
43-
}
44-
if self.is_kw_followed_by_ident(kw::Auto) {
38+
let stmt = if self.eat_keyword(kw::Let) {
39+
self.parse_local_mk(lo, attrs.into())?
40+
} else if self.is_kw_followed_by_ident(kw::Mut) {
41+
self.recover_stmt_local(lo, attrs.into(), "missing keyword", "let mut")?
42+
} else if self.is_kw_followed_by_ident(kw::Auto) {
4543
self.bump(); // `auto`
4644
let msg = "write `let` instead of `auto` to introduce a new variable";
47-
return self.recover_stmt_local(lo, attrs.into(), msg, "let");
48-
}
49-
if self.is_kw_followed_by_ident(sym::var) {
45+
self.recover_stmt_local(lo, attrs.into(), msg, "let")?
46+
} else if self.is_kw_followed_by_ident(sym::var) {
5047
self.bump(); // `var`
5148
let msg = "write `let` instead of `var` to introduce a new variable";
52-
return self.recover_stmt_local(lo, attrs.into(), msg, "let");
53-
}
54-
55-
// Starts like a simple path, being careful to avoid contextual keywords,
56-
// e.g., `union`, items with `crate` visibility, or `auto trait` items.
57-
// We aim to parse an arbitrary path `a::b` but not something that starts like a path
58-
// (1 token), but it fact not a path. Also, we avoid stealing syntax from `parse_item_`.
59-
if self.token.is_path_start() && !self.token.is_qpath_start() && !self.is_path_start_item()
49+
self.recover_stmt_local(lo, attrs.into(), msg, "let")?
50+
} else if self.token.is_path_start()
51+
&& !self.token.is_qpath_start()
52+
&& !self.is_path_start_item()
6053
{
61-
let path = self.parse_path(PathStyle::Expr)?;
62-
63-
if self.eat(&token::Not) {
64-
return self.parse_stmt_mac(lo, attrs.into(), path);
65-
}
66-
67-
let expr = if self.check(&token::OpenDelim(token::Brace)) {
68-
self.parse_struct_expr(lo, path, AttrVec::new())?
69-
} else {
70-
let hi = self.prev_span;
71-
self.mk_expr(lo.to(hi), ExprKind::Path(None, path), AttrVec::new())
72-
};
73-
74-
let expr = self.with_res(Restrictions::STMT_EXPR, |this| {
75-
let expr = this.parse_dot_or_call_expr_with(expr, lo, attrs.into())?;
76-
this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr))
77-
})?;
78-
return Ok(Some(self.mk_stmt(lo.to(self.prev_span), StmtKind::Expr(expr))));
79-
}
80-
81-
// FIXME: Bad copy of attrs
82-
let old_directory_ownership =
83-
mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
84-
let item = self.parse_item_common(attrs.clone(), false, true, |_| true)?;
85-
self.directory.ownership = old_directory_ownership;
86-
87-
if let Some(item) = item {
88-
return Ok(Some(self.mk_stmt(lo.to(item.span), StmtKind::Item(P(item)))));
89-
}
90-
91-
// Do not attempt to parse an expression if we're done here.
92-
if self.token == token::Semi {
54+
// We have avoided contextual keywords like `union`, items with `crate` visibility,
55+
// or `auto trait` items. We aim to parse an arbitrary path `a::b` but not something
56+
// that starts like a path (1 token), but it fact not a path.
57+
// Also, we avoid stealing syntax from `parse_item_`.
58+
self.parse_stmt_path_start(lo, attrs)?
59+
} else if let Some(item) = self.parse_stmt_item(attrs.clone())? {
60+
// FIXME: Bad copy of attrs
61+
self.mk_stmt(lo.to(item.span), StmtKind::Item(P(item)))
62+
} else if self.token == token::Semi {
63+
// Do not attempt to parse an expression if we're done here.
9364
self.error_outer_attrs(&attrs);
9465
self.bump();
9566
let mut last_semi = lo;
@@ -104,27 +75,49 @@ impl<'a> Parser<'a> {
10475
ExprKind::Tup(Vec::new()),
10576
AttrVec::new(),
10677
));
107-
return Ok(Some(self.mk_stmt(lo.to(last_semi), kind)));
108-
}
109-
110-
if self.token == token::CloseDelim(token::Brace) {
78+
self.mk_stmt(lo.to(last_semi), kind)
79+
} else if self.token != token::CloseDelim(token::Brace) {
80+
// Remainder are line-expr stmts.
81+
let e = self.parse_expr_res(Restrictions::STMT_EXPR, Some(attrs.into()))?;
82+
self.mk_stmt(lo.to(e.span), StmtKind::Expr(e))
83+
} else {
11184
self.error_outer_attrs(&attrs);
11285
return Ok(None);
86+
};
87+
Ok(Some(stmt))
88+
}
89+
90+
fn parse_stmt_item(&mut self, attrs: Vec<Attribute>) -> PResult<'a, Option<ast::Item>> {
91+
let old = mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
92+
let item = self.parse_item_common(attrs.clone(), false, true, |_| true)?;
93+
self.directory.ownership = old;
94+
Ok(item)
95+
}
96+
97+
fn parse_stmt_path_start(&mut self, lo: Span, attrs: Vec<Attribute>) -> PResult<'a, Stmt> {
98+
let path = self.parse_path(PathStyle::Expr)?;
99+
100+
if self.eat(&token::Not) {
101+
return self.parse_stmt_mac(lo, attrs.into(), path);
113102
}
114103

115-
// Remainder are line-expr stmts.
116-
let e = self.parse_expr_res(Restrictions::STMT_EXPR, Some(attrs.into()))?;
117-
Ok(Some(self.mk_stmt(lo.to(e.span), StmtKind::Expr(e))))
104+
let expr = if self.check(&token::OpenDelim(token::Brace)) {
105+
self.parse_struct_expr(lo, path, AttrVec::new())?
106+
} else {
107+
let hi = self.prev_span;
108+
self.mk_expr(lo.to(hi), ExprKind::Path(None, path), AttrVec::new())
109+
};
110+
111+
let expr = self.with_res(Restrictions::STMT_EXPR, |this| {
112+
let expr = this.parse_dot_or_call_expr_with(expr, lo, attrs.into())?;
113+
this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr))
114+
})?;
115+
Ok(self.mk_stmt(lo.to(self.prev_span), StmtKind::Expr(expr)))
118116
}
119117

120118
/// Parses a statement macro `mac!(args)` provided a `path` representing `mac`.
121119
/// At this point, the `!` token after the path has already been eaten.
122-
fn parse_stmt_mac(
123-
&mut self,
124-
lo: Span,
125-
attrs: AttrVec,
126-
path: ast::Path,
127-
) -> PResult<'a, Option<Stmt>> {
120+
fn parse_stmt_mac(&mut self, lo: Span, attrs: AttrVec, path: ast::Path) -> PResult<'a, Stmt> {
128121
let args = self.parse_mac_args()?;
129122
let delim = args.delim();
130123
let hi = self.prev_span;
@@ -145,7 +138,7 @@ impl<'a> Parser<'a> {
145138
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
146139
StmtKind::Expr(e)
147140
};
148-
Ok(Some(self.mk_stmt(lo.to(hi), kind)))
141+
Ok(self.mk_stmt(lo.to(hi), kind))
149142
}
150143

151144
/// Error on outer attributes in this context.
@@ -167,12 +160,12 @@ impl<'a> Parser<'a> {
167160
attrs: AttrVec,
168161
msg: &str,
169162
sugg: &str,
170-
) -> PResult<'a, Option<Stmt>> {
163+
) -> PResult<'a, Stmt> {
171164
let stmt = self.parse_local_mk(lo, attrs)?;
172165
self.struct_span_err(lo, "invalid variable declaration")
173166
.span_suggestion(lo, msg, sugg.to_string(), Applicability::MachineApplicable)
174167
.emit();
175-
Ok(Some(stmt))
168+
Ok(stmt)
176169
}
177170

178171
fn parse_local_mk(&mut self, lo: Span, attrs: AttrVec) -> PResult<'a, Stmt> {
@@ -372,36 +365,36 @@ impl<'a> Parser<'a> {
372365

373366
let mut eat_semi = true;
374367
match stmt.kind {
375-
StmtKind::Expr(ref expr) if self.token != token::Eof => {
376-
// expression without semicolon
377-
if classify::expr_requires_semi_to_be_stmt(expr) {
378-
// Just check for errors and recover; do not eat semicolon yet.
379-
if let Err(mut e) =
380-
self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)])
381-
{
382-
if let TokenKind::DocComment(..) = self.token.kind {
383-
if let Ok(snippet) = self.span_to_snippet(self.token.span) {
384-
let sp = self.token.span;
385-
let marker = &snippet[..3];
386-
let (comment_marker, doc_comment_marker) = marker.split_at(2);
387-
388-
e.span_suggestion(
389-
sp.with_hi(sp.lo() + BytePos(marker.len() as u32)),
390-
&format!(
391-
"add a space before `{}` to use a regular comment",
392-
doc_comment_marker,
393-
),
394-
format!("{} {}", comment_marker, doc_comment_marker),
395-
Applicability::MaybeIncorrect,
396-
);
397-
}
368+
// Expression without semicolon.
369+
StmtKind::Expr(ref expr)
370+
if self.token != token::Eof && classify::expr_requires_semi_to_be_stmt(expr) =>
371+
{
372+
// Just check for errors and recover; do not eat semicolon yet.
373+
if let Err(mut e) =
374+
self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)])
375+
{
376+
if let TokenKind::DocComment(..) = self.token.kind {
377+
if let Ok(snippet) = self.span_to_snippet(self.token.span) {
378+
let sp = self.token.span;
379+
let marker = &snippet[..3];
380+
let (comment_marker, doc_comment_marker) = marker.split_at(2);
381+
382+
e.span_suggestion(
383+
sp.with_hi(sp.lo() + BytePos(marker.len() as u32)),
384+
&format!(
385+
"add a space before `{}` to use a regular comment",
386+
doc_comment_marker,
387+
),
388+
format!("{} {}", comment_marker, doc_comment_marker),
389+
Applicability::MaybeIncorrect,
390+
);
398391
}
399-
e.emit();
400-
self.recover_stmt();
401-
// Don't complain about type errors in body tail after parse error (#57383).
402-
let sp = expr.span.to(self.prev_span);
403-
stmt.kind = StmtKind::Expr(self.mk_expr_err(sp));
404392
}
393+
e.emit();
394+
self.recover_stmt();
395+
// Don't complain about type errors in body tail after parse error (#57383).
396+
let sp = expr.span.to(self.prev_span);
397+
stmt.kind = StmtKind::Expr(self.mk_expr_err(sp));
405398
}
406399
}
407400
StmtKind::Local(..) => {

‎src/test/ui/fmt/send-sync.stderr

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
1-
error[E0277]: `*mut (dyn std::ops::Fn() + 'static)` cannot be shared between threads safely
1+
error[E0277]: `core::fmt::Opaque` cannot be shared between threads safely
22
--> $DIR/send-sync.rs:8:5
33
|
44
LL | fn send<T: Send>(_: T) {}
55
| ---- ---- required by this bound in `send`
66
...
77
LL | send(format_args!("{:?}", c));
8-
| ^^^^ `*mut (dyn std::ops::Fn() + 'static)` cannot be shared between threads safely
8+
| ^^^^ `core::fmt::Opaque` cannot be shared between threads safely
99
|
10-
= help: within `[std::fmt::ArgumentV1<'_>]`, the trait `std::marker::Sync` is not implemented for `*mut (dyn std::ops::Fn() + 'static)`
11-
= note: required because it appears within the type `std::marker::PhantomData<*mut (dyn std::ops::Fn() + 'static)>`
12-
= note: required because it appears within the type `core::fmt::Void`
13-
= note: required because it appears within the type `&core::fmt::Void`
10+
= help: within `[std::fmt::ArgumentV1<'_>]`, the trait `std::marker::Sync` is not implemented for `core::fmt::Opaque`
11+
= note: required because it appears within the type `&core::fmt::Opaque`
1412
= note: required because it appears within the type `std::fmt::ArgumentV1<'_>`
1513
= note: required because it appears within the type `[std::fmt::ArgumentV1<'_>]`
1614
= note: required because of the requirements on the impl of `std::marker::Send` for `&[std::fmt::ArgumentV1<'_>]`
1715
= note: required because it appears within the type `std::fmt::Arguments<'_>`
1816

19-
error[E0277]: `*mut (dyn std::ops::Fn() + 'static)` cannot be shared between threads safely
17+
error[E0277]: `core::fmt::Opaque` cannot be shared between threads safely
2018
--> $DIR/send-sync.rs:9:5
2119
|
2220
LL | fn sync<T: Sync>(_: T) {}
2321
| ---- ---- required by this bound in `sync`
2422
...
2523
LL | sync(format_args!("{:?}", c));
26-
| ^^^^ `*mut (dyn std::ops::Fn() + 'static)` cannot be shared between threads safely
24+
| ^^^^ `core::fmt::Opaque` cannot be shared between threads safely
2725
|
28-
= help: within `std::fmt::Arguments<'_>`, the trait `std::marker::Sync` is not implemented for `*mut (dyn std::ops::Fn() + 'static)`
29-
= note: required because it appears within the type `std::marker::PhantomData<*mut (dyn std::ops::Fn() + 'static)>`
30-
= note: required because it appears within the type `core::fmt::Void`
31-
= note: required because it appears within the type `&core::fmt::Void`
26+
= help: within `std::fmt::Arguments<'_>`, the trait `std::marker::Sync` is not implemented for `core::fmt::Opaque`
27+
= note: required because it appears within the type `&core::fmt::Opaque`
3228
= note: required because it appears within the type `std::fmt::ArgumentV1<'_>`
3329
= note: required because it appears within the type `[std::fmt::ArgumentV1<'_>]`
3430
= note: required because it appears within the type `&[std::fmt::ArgumentV1<'_>]`

0 commit comments

Comments
 (0)
Please sign in to comment.