Skip to content

Commit c64eecf

Browse files
committed
Auto merge of #66994 - Centril:stmt-polish, r=estebank
refactor expr & stmt parsing + improve recovery Summary of important changes (best read commit-by-commit, ignoring whitespace changes): - `AttrVec` is introduces as an alias for `ThinVec<Attribute>` - `parse_expr_bottom` and `parse_stmt` are thoroughly refactored. - Extract diagnostics logic for `vec![...]` in a pattern context. - Recovery is added for `do catch { ... }` - Recovery is added for `'label: non_block_expr` - Recovery is added for `var $local`, `auto $local`, and `mut $local`. Fixes #65257. - Recovery is added for `e1 and e2` and `e1 or e2`. - ~~`macro_legacy_warnings` is turned into an error (has been a warning for 3 years!)~~ - Fixes #63396 by forward-porting #64105 which now works thanks to added recovery. - `ui-fulldeps/ast_stmt_expr_attr.rs` is turned into UI and pretty tests. - Recovery is fixed for `#[attr] if expr {}` r? @estebank
2 parents 9ff30a7 + 621661f commit c64eecf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1764
-1187
lines changed

src/librustc/hir/lowering.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ use crate::util::nodemap::{DefIdMap, NodeMap};
5353
use errors::Applicability;
5454
use rustc_data_structures::fx::FxHashSet;
5555
use rustc_index::vec::IndexVec;
56-
use rustc_data_structures::thin_vec::ThinVec;
5756
use rustc_data_structures::sync::Lrc;
5857

5958
use std::collections::BTreeMap;
@@ -1205,7 +1204,7 @@ impl<'a> LoweringContext<'a> {
12051204
id: ty.id,
12061205
kind: ExprKind::Path(qself.clone(), path.clone()),
12071206
span: ty.span,
1208-
attrs: ThinVec::new(),
1207+
attrs: AttrVec::new(),
12091208
};
12101209

12111210
let ct = self.with_new_scopes(|this| {
@@ -2751,7 +2750,7 @@ impl<'a> LoweringContext<'a> {
27512750
/// has no attributes and is not targeted by a `break`.
27522751
fn lower_block_expr(&mut self, b: &Block) -> hir::Expr {
27532752
let block = self.lower_block(b, false);
2754-
self.expr_block(block, ThinVec::new())
2753+
self.expr_block(block, AttrVec::new())
27552754
}
27562755

27572756
fn lower_pat(&mut self, p: &Pat) -> P<hir::Pat> {
@@ -3102,7 +3101,7 @@ impl<'a> LoweringContext<'a> {
31023101

31033102
fn stmt_let_pat(
31043103
&mut self,
3105-
attrs: ThinVec<Attribute>,
3104+
attrs: AttrVec,
31063105
span: Span,
31073106
init: Option<P<hir::Expr>>,
31083107
pat: P<hir::Pat>,

src/librustc/hir/lowering/expr.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -1318,8 +1318,7 @@ impl LoweringContext<'_> {
13181318
&mut self,
13191319
span: Span,
13201320
expr: P<hir::Expr>,
1321-
attrs: ThinVec<Attribute>
1322-
) -> hir::Expr {
1321+
attrs: AttrVec) -> hir::Expr {
13231322
self.expr(span, hir::ExprKind::DropTemps(expr), attrs)
13241323
}
13251324

@@ -1333,7 +1332,7 @@ impl LoweringContext<'_> {
13331332
self.expr(span, hir::ExprKind::Match(arg, arms, source), ThinVec::new())
13341333
}
13351334

1336-
fn expr_break(&mut self, span: Span, attrs: ThinVec<Attribute>) -> P<hir::Expr> {
1335+
fn expr_break(&mut self, span: Span, attrs: AttrVec) -> P<hir::Expr> {
13371336
let expr_break = hir::ExprKind::Break(self.lower_loop_destination(None), None);
13381337
P(self.expr(span, expr_break, attrs))
13391338
}
@@ -1404,7 +1403,7 @@ impl LoweringContext<'_> {
14041403
span: Span,
14051404
components: &[Symbol],
14061405
params: Option<P<hir::GenericArgs>>,
1407-
attrs: ThinVec<Attribute>,
1406+
attrs: AttrVec,
14081407
) -> hir::Expr {
14091408
let path = self.std_path(span, components, params, true);
14101409
self.expr(
@@ -1423,7 +1422,7 @@ impl LoweringContext<'_> {
14231422
span: Span,
14241423
ident: Ident,
14251424
binding: hir::HirId,
1426-
attrs: ThinVec<Attribute>,
1425+
attrs: AttrVec,
14271426
) -> hir::Expr {
14281427
let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
14291428
None,
@@ -1459,16 +1458,11 @@ impl LoweringContext<'_> {
14591458
self.expr_block(P(blk), ThinVec::new())
14601459
}
14611460

1462-
pub(super) fn expr_block(&mut self, b: P<hir::Block>, attrs: ThinVec<Attribute>) -> hir::Expr {
1461+
pub(super) fn expr_block(&mut self, b: P<hir::Block>, attrs: AttrVec) -> hir::Expr {
14631462
self.expr(b.span, hir::ExprKind::Block(b, None), attrs)
14641463
}
14651464

1466-
pub(super) fn expr(
1467-
&mut self,
1468-
span: Span,
1469-
kind: hir::ExprKind,
1470-
attrs: ThinVec<Attribute>
1471-
) -> hir::Expr {
1465+
pub(super) fn expr(&mut self, span: Span, kind: hir::ExprKind, attrs: AttrVec) -> hir::Expr {
14721466
hir::Expr { hir_id: self.next_id(), kind, span, attrs }
14731467
}
14741468

src/librustc/hir/lowering/item.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::hir::def_id::DefId;
1111
use crate::hir::def::{Res, DefKind};
1212
use crate::util::nodemap::NodeMap;
1313

14-
use rustc_data_structures::thin_vec::ThinVec;
1514
use rustc_target::spec::abi;
1615

1716
use std::collections::BTreeSet;
@@ -899,7 +898,7 @@ impl LoweringContext<'_> {
899898

900899
/// Construct `ExprKind::Err` for the given `span`.
901900
fn expr_err(&mut self, span: Span) -> hir::Expr {
902-
self.expr(span, hir::ExprKind::Err, ThinVec::new())
901+
self.expr(span, hir::ExprKind::Err, AttrVec::new())
903902
}
904903

905904
fn lower_impl_item(&mut self, i: &AssocItem) -> hir::ImplItem {
@@ -1182,7 +1181,7 @@ impl LoweringContext<'_> {
11821181
//
11831182
// If this is the simple case, this parameter will end up being the same as the
11841183
// original parameter, but with a different pattern id.
1185-
let mut stmt_attrs = ThinVec::new();
1184+
let mut stmt_attrs = AttrVec::new();
11861185
stmt_attrs.extend(parameter.attrs.iter().cloned());
11871186
let (new_parameter_pat, new_parameter_id) = this.pat_ident(desugared_span, ident);
11881187
let new_parameter = hir::Param {
@@ -1226,7 +1225,7 @@ impl LoweringContext<'_> {
12261225
desugared_span, ident, hir::BindingAnnotation::Mutable);
12271226
let move_expr = this.expr_ident(desugared_span, ident, new_parameter_id);
12281227
let move_stmt = this.stmt_let_pat(
1229-
ThinVec::new(),
1228+
AttrVec::new(),
12301229
desugared_span,
12311230
Some(P(move_expr)),
12321231
move_pat,
@@ -1271,7 +1270,7 @@ impl LoweringContext<'_> {
12711270
let user_body = this.expr_drop_temps(
12721271
desugared_span,
12731272
P(user_body),
1274-
ThinVec::new(),
1273+
AttrVec::new(),
12751274
);
12761275

12771276
// As noted above, create the final block like
@@ -1288,9 +1287,9 @@ impl LoweringContext<'_> {
12881287
statements.into(),
12891288
Some(P(user_body)),
12901289
);
1291-
this.expr_block(P(body), ThinVec::new())
1290+
this.expr_block(P(body), AttrVec::new())
12921291
});
1293-
(HirVec::from(parameters), this.expr(body_span, async_expr, ThinVec::new()))
1292+
(HirVec::from(parameters), this.expr(body_span, async_expr, AttrVec::new()))
12941293
})
12951294
}
12961295

src/librustc/hir/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use errors::FatalError;
2020
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
2121
use syntax::source_map::Spanned;
2222
use syntax::ast::{self, CrateSugar, Ident, Name, NodeId, AsmDialect};
23-
use syntax::ast::{Attribute, Label, LitKind, StrStyle, FloatTy, IntTy, UintTy};
23+
use syntax::ast::{AttrVec, Attribute, Label, LitKind, StrStyle, FloatTy, IntTy, UintTy};
2424
pub use syntax::ast::{Mutability, Constness, Unsafety, Movability, CaptureBy};
2525
pub use syntax::ast::{IsAuto, ImplPolarity, BorrowKind};
2626
use syntax::attr::{InlineAttr, OptimizeAttr};
@@ -29,7 +29,6 @@ use syntax::tokenstream::TokenStream;
2929
use syntax::util::parser::ExprPrecedence;
3030
use rustc_target::spec::abi::Abi;
3131
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
32-
use rustc_data_structures::thin_vec::ThinVec;
3332
use rustc_macros::HashStable;
3433
use rustc_serialize::{self, Encoder, Encodable, Decoder, Decodable};
3534
use std::collections::{BTreeSet, BTreeMap};
@@ -1274,7 +1273,7 @@ pub struct Local {
12741273
pub init: Option<P<Expr>>,
12751274
pub hir_id: HirId,
12761275
pub span: Span,
1277-
pub attrs: ThinVec<Attribute>,
1276+
pub attrs: AttrVec,
12781277
/// Can be `ForLoopDesugar` if the `let` statement is part of a `for` loop
12791278
/// desugaring. Otherwise will be `Normal`.
12801279
pub source: LocalSource,
@@ -1459,7 +1458,7 @@ pub struct AnonConst {
14591458
pub struct Expr {
14601459
pub hir_id: HirId,
14611460
pub kind: ExprKind,
1462-
pub attrs: ThinVec<Attribute>,
1461+
pub attrs: AttrVec,
14631462
pub span: Span,
14641463
}
14651464

src/librustc_interface/util.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_data_structures::jobserver;
1010
use rustc_data_structures::sync::{Lock, Lrc};
1111
use rustc_data_structures::stable_hasher::StableHasher;
1212
use rustc_data_structures::fingerprint::Fingerprint;
13-
use rustc_data_structures::thin_vec::ThinVec;
1413
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
1514
use rustc_errors::registry::Registry;
1615
use rustc_metadata::dynamic_lib::DynamicLibrary;
@@ -24,7 +23,7 @@ use std::ops::DerefMut;
2423
use smallvec::SmallVec;
2524
use syntax::ptr::P;
2625
use syntax::mut_visit::{*, MutVisitor, visit_clobber};
27-
use syntax::ast::BlockCheckMode;
26+
use syntax::ast::{AttrVec, BlockCheckMode};
2827
use syntax::util::lev_distance::find_best_match_for_name;
2928
use syntax::source_map::{FileLoader, RealFileLoader, SourceMap};
3029
use syntax::symbol::{Symbol, sym};
@@ -741,7 +740,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
741740
id: resolver.next_node_id(),
742741
kind: ast::ExprKind::Block(P(b), None),
743742
span: syntax_pos::DUMMY_SP,
744-
attrs: ThinVec::new(),
743+
attrs: AttrVec::new(),
745744
});
746745

747746
ast::Stmt {
@@ -756,7 +755,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
756755
kind: ast::ExprKind::Loop(P(empty_block), None),
757756
id: self.resolver.next_node_id(),
758757
span: syntax_pos::DUMMY_SP,
759-
attrs: ThinVec::new(),
758+
attrs: AttrVec::new(),
760759
});
761760

762761
let loop_stmt = ast::Stmt {

src/librustc_parse/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
#![feature(bool_to_option)]
44
#![feature(crate_visibility_modifier)]
5+
#![feature(slice_patterns)]
56

67
use syntax::ast;
78
use syntax::print::pprust;

src/librustc_parse/parser/attr.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{SeqSep, Parser, TokenType, PathStyle};
1+
use super::{Parser, TokenType, PathStyle};
22
use rustc_errors::PResult;
33
use syntax::attr;
44
use syntax::ast;
@@ -301,8 +301,10 @@ impl<'a> Parser<'a> {
301301
crate fn parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind> {
302302
Ok(if self.eat(&token::Eq) {
303303
ast::MetaItemKind::NameValue(self.parse_unsuffixed_lit()?)
304-
} else if self.eat(&token::OpenDelim(token::Paren)) {
305-
ast::MetaItemKind::List(self.parse_meta_seq()?)
304+
} else if self.check(&token::OpenDelim(token::Paren)) {
305+
// Matches `meta_seq = ( COMMASEP(meta_item_inner) )`.
306+
let (list, _) = self.parse_paren_comma_seq(|p| p.parse_meta_item_inner())?;
307+
ast::MetaItemKind::List(list)
306308
} else {
307309
ast::MetaItemKind::Word
308310
})
@@ -311,28 +313,17 @@ impl<'a> Parser<'a> {
311313
/// Matches `meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;`.
312314
fn parse_meta_item_inner(&mut self) -> PResult<'a, ast::NestedMetaItem> {
313315
match self.parse_unsuffixed_lit() {
314-
Ok(lit) => {
315-
return Ok(ast::NestedMetaItem::Literal(lit))
316-
}
316+
Ok(lit) => return Ok(ast::NestedMetaItem::Literal(lit)),
317317
Err(ref mut err) => err.cancel(),
318318
}
319319

320320
match self.parse_meta_item() {
321-
Ok(mi) => {
322-
return Ok(ast::NestedMetaItem::MetaItem(mi))
323-
}
321+
Ok(mi) => return Ok(ast::NestedMetaItem::MetaItem(mi)),
324322
Err(ref mut err) => err.cancel(),
325323
}
326324

327325
let found = self.this_token_to_string();
328326
let msg = format!("expected unsuffixed literal or identifier, found `{}`", found);
329327
Err(self.diagnostic().struct_span_err(self.token.span, &msg))
330328
}
331-
332-
/// Matches `meta_seq = ( COMMASEP(meta_item_inner) )`.
333-
fn parse_meta_seq(&mut self) -> PResult<'a, Vec<ast::NestedMetaItem>> {
334-
self.parse_seq_to_end(&token::CloseDelim(token::Paren),
335-
SeqSep::trailing_allowed(token::Comma),
336-
|p: &mut Parser<'a>| p.parse_meta_item_inner())
337-
}
338329
}

0 commit comments

Comments
 (0)