Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,11 +869,11 @@ pub enum PatKind {
Struct(Option<Box<QSelf>>, Path, ThinVec<PatField>, PatFieldsRest),

/// A tuple struct/variant pattern (`Variant(x, y, .., z)`).
TupleStruct(Option<Box<QSelf>>, Path, ThinVec<Box<Pat>>),
TupleStruct(Option<Box<QSelf>>, Path, ThinVec<Pat>),

/// An or-pattern `A | B | C`.
/// Invariant: `pats.len() >= 2`.
Or(ThinVec<Box<Pat>>),
Or(ThinVec<Pat>),

/// A possibly qualified path pattern.
/// Unqualified path patterns `A::B::C` can legally refer to variants, structs, constants
Expand All @@ -882,7 +882,7 @@ pub enum PatKind {
Path(Option<Box<QSelf>>, Path),

/// A tuple pattern (`(a, b)`).
Tuple(ThinVec<Box<Pat>>),
Tuple(ThinVec<Pat>),

/// A `box` pattern.
Box(Box<Pat>),
Expand All @@ -900,7 +900,7 @@ pub enum PatKind {
Range(Option<Box<Expr>>, Option<Box<Expr>>, Spanned<RangeEnd>),

/// A slice pattern `[a, b, c]`.
Slice(ThinVec<Box<Pat>>),
Slice(ThinVec<Pat>),

/// A rest pattern `..`.
///
Expand Down Expand Up @@ -2579,7 +2579,7 @@ pub enum TyPatKind {
/// A range pattern (e.g., `1...2`, `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
Range(Option<Box<AnonConst>>, Option<Box<AnonConst>>, Spanned<RangeEnd>),

Or(ThinVec<Box<TyPat>>),
Or(ThinVec<TyPat>),

/// Placeholder for a pattern that wasn't syntactically well formed in some way.
Err(ErrorGuaranteed),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,9 @@ macro_rules! common_visitor_and_walkers {
ThinVec<(NodeId, Path)>,
ThinVec<PathSegment>,
ThinVec<PreciseCapturingArg>,
ThinVec<Box<Pat>>,
ThinVec<Pat>,
ThinVec<Box<Ty>>,
ThinVec<Box<TyPat>>,
ThinVec<TyPat>,
);

// This macro generates `impl Visitable` and `impl MutVisitable` that forward to `Walkable`
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

fn lower_pat_tuple(
&mut self,
pats: &[Box<Pat>],
pats: &[Pat],
ctx: &str,
) -> (&'hir [hir::Pat<'hir>], hir::DotDotPos) {
let mut elems = Vec::with_capacity(pats.len());
Expand Down Expand Up @@ -209,7 +209,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
/// When encountering `($binding_mode $ident @)? ..` (`slice`),
/// this is interpreted as a sub-slice pattern semantically.
/// Patterns that follow, which are not like `slice` -- or an error occurs, are in `after`.
fn lower_pat_slice(&mut self, pats: &[Box<Pat>]) -> hir::PatKind<'hir> {
fn lower_pat_slice(&mut self, pats: &[Pat]) -> hir::PatKind<'hir> {
let mut before = Vec::new();
let mut after = Vec::new();
let mut slice = None;
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ impl<'a> TraitDef<'a> {
struct_def: &'a VariantData,
prefixes: &[String],
by_ref: ByRef,
) -> ThinVec<Box<ast::Pat>> {
) -> ThinVec<ast::Pat> {
prefixes
.iter()
.map(|prefix| {
Expand Down Expand Up @@ -1543,7 +1543,7 @@ impl<'a> TraitDef<'a> {
attrs: ast::AttrVec::new(),
id: ast::DUMMY_NODE_ID,
span: pat.span.with_ctxt(self.span.ctxt()),
pat,
pat: Box::new(pat),
is_placeholder: false,
}
})
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_builtin_macros/src/pattern_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn parse_pat_ty<'a>(

let pat = pat_to_ty_pat(
cx,
*parser.parse_pat_no_top_guard(
parser.parse_pat_no_top_guard(
None,
RecoverComma::No,
RecoverColon::No,
Expand All @@ -44,22 +44,22 @@ fn parse_pat_ty<'a>(
parser.unexpected()?;
}

Ok((ty, pat))
Ok((ty, Box::new(pat)))
}

fn ty_pat(kind: TyPatKind, span: Span) -> Box<TyPat> {
Box::new(TyPat { id: DUMMY_NODE_ID, kind, span, tokens: None })
fn ty_pat(kind: TyPatKind, span: Span) -> TyPat {
TyPat { id: DUMMY_NODE_ID, kind, span, tokens: None }
}

fn pat_to_ty_pat(cx: &mut ExtCtxt<'_>, pat: ast::Pat) -> Box<TyPat> {
fn pat_to_ty_pat(cx: &mut ExtCtxt<'_>, pat: ast::Pat) -> TyPat {
let kind = match pat.kind {
ast::PatKind::Range(start, end, include_end) => TyPatKind::Range(
start.map(|value| Box::new(AnonConst { id: DUMMY_NODE_ID, value })),
end.map(|value| Box::new(AnonConst { id: DUMMY_NODE_ID, value })),
include_end,
),
ast::PatKind::Or(variants) => {
TyPatKind::Or(variants.into_iter().map(|pat| pat_to_ty_pat(cx, *pat)).collect())
TyPatKind::Or(variants.into_iter().map(|pat| pat_to_ty_pat(cx, pat)).collect())
}
ast::PatKind::Err(guar) => TyPatKind::Err(guar),
_ => TyPatKind::Err(cx.dcx().span_err(pat.span, "pattern not supported in pattern types")),
Expand Down
36 changes: 18 additions & 18 deletions compiler/rustc_expand/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl<'a> ExtCtxt<'a> {
};
let local = Box::new(ast::Local {
super_: None,
pat,
pat: Box::new(pat),
ty,
id: ast::DUMMY_NODE_ID,
kind: LocalKind::Init(ex),
Expand All @@ -249,7 +249,7 @@ impl<'a> ExtCtxt<'a> {
pub fn stmt_let_type_only(&self, span: Span, ty: Box<ast::Ty>) -> ast::Stmt {
let local = Box::new(ast::Local {
super_: None,
pat: self.pat_wild(span),
pat: Box::new(self.pat_wild(span)),
ty: Some(ty),
id: ast::DUMMY_NODE_ID,
kind: LocalKind::Decl,
Expand Down Expand Up @@ -528,16 +528,16 @@ impl<'a> ExtCtxt<'a> {
self.expr_match(sp, head, thin_vec![ok_arm, err_arm])
}

pub fn pat(&self, span: Span, kind: PatKind) -> Box<ast::Pat> {
Box::new(ast::Pat { id: ast::DUMMY_NODE_ID, kind, span, tokens: None })
pub fn pat(&self, span: Span, kind: PatKind) -> ast::Pat {
ast::Pat { id: ast::DUMMY_NODE_ID, kind, span, tokens: None }
}
pub fn pat_wild(&self, span: Span) -> Box<ast::Pat> {
pub fn pat_wild(&self, span: Span) -> ast::Pat {
self.pat(span, PatKind::Wild)
}
pub fn pat_lit(&self, span: Span, expr: Box<ast::Expr>) -> Box<ast::Pat> {
pub fn pat_lit(&self, span: Span, expr: Box<ast::Expr>) -> ast::Pat {
self.pat(span, PatKind::Expr(expr))
}
pub fn pat_ident(&self, span: Span, ident: Ident) -> Box<ast::Pat> {
pub fn pat_ident(&self, span: Span, ident: Ident) -> ast::Pat {
self.pat_ident_binding_mode(span, ident, ast::BindingMode::NONE)
}

Expand All @@ -546,43 +546,43 @@ impl<'a> ExtCtxt<'a> {
span: Span,
ident: Ident,
ann: ast::BindingMode,
) -> Box<ast::Pat> {
) -> ast::Pat {
let pat = PatKind::Ident(ann, ident.with_span_pos(span), None);
self.pat(span, pat)
}
pub fn pat_path(&self, span: Span, path: ast::Path) -> Box<ast::Pat> {
pub fn pat_path(&self, span: Span, path: ast::Path) -> ast::Pat {
self.pat(span, PatKind::Path(None, path))
}
pub fn pat_tuple_struct(
&self,
span: Span,
path: ast::Path,
subpats: ThinVec<Box<ast::Pat>>,
) -> Box<ast::Pat> {
subpats: ThinVec<ast::Pat>,
) -> ast::Pat {
self.pat(span, PatKind::TupleStruct(None, path, subpats))
}
pub fn pat_struct(
&self,
span: Span,
path: ast::Path,
field_pats: ThinVec<ast::PatField>,
) -> Box<ast::Pat> {
) -> ast::Pat {
self.pat(span, PatKind::Struct(None, path, field_pats, ast::PatFieldsRest::None))
}
pub fn pat_tuple(&self, span: Span, pats: ThinVec<Box<ast::Pat>>) -> Box<ast::Pat> {
pub fn pat_tuple(&self, span: Span, pats: ThinVec<ast::Pat>) -> ast::Pat {
self.pat(span, PatKind::Tuple(pats))
}

pub fn pat_some(&self, span: Span, pat: Box<ast::Pat>) -> Box<ast::Pat> {
pub fn pat_some(&self, span: Span, pat: ast::Pat) -> ast::Pat {
let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
let path = self.path_global(span, some);
self.pat_tuple_struct(span, path, thin_vec![pat])
}

pub fn arm(&self, span: Span, pat: Box<ast::Pat>, expr: Box<ast::Expr>) -> ast::Arm {
pub fn arm(&self, span: Span, pat: ast::Pat, expr: Box<ast::Expr>) -> ast::Arm {
ast::Arm {
attrs: AttrVec::new(),
pat,
pat: Box::new(pat),
guard: None,
body: Some(expr),
span,
Expand Down Expand Up @@ -661,11 +661,11 @@ impl<'a> ExtCtxt<'a> {
}

pub fn param(&self, span: Span, ident: Ident, ty: Box<ast::Ty>) -> ast::Param {
let arg_pat = self.pat_ident(span, ident);
let pat = Box::new(self.pat_ident(span, ident));
ast::Param {
attrs: AttrVec::default(),
id: ast::DUMMY_NODE_ID,
pat: arg_pat,
pat,
span,
ty,
is_placeholder: false,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,12 +1152,12 @@ pub fn parse_ast_fragment<'a>(
}
}
AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_allow_top_guard(
AstFragmentKind::Pat => AstFragment::Pat(Box::new(this.parse_pat_allow_top_guard(
None,
RecoverComma::No,
RecoverColon::Yes,
CommaRecoveryMode::LikelyTuple,
)?),
)?)),
AstFragmentKind::Crate => AstFragment::Crate(this.parse_crate_mod()?),
AstFragmentKind::Arms
| AstFragmentKind::ExprFields
Expand Down
46 changes: 32 additions & 14 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ pub(super) trait RecoverQPath: Sized + 'static {
fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self;
}

impl<T: RecoverQPath> RecoverQPath for Box<T> {
const PATH_STYLE: PathStyle = T::PATH_STYLE;
fn to_ty(&self) -> Option<Box<Ty>> {
T::to_ty(self)
}
fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {
Box::new(T::recovered(qself, path))
}
}

impl RecoverQPath for Ty {
const PATH_STYLE: PathStyle = PathStyle::Type;
fn to_ty(&self) -> Option<Box<Ty>> {
Expand Down Expand Up @@ -1833,17 +1843,19 @@ impl<'a> Parser<'a> {
/// tail, and combines them into a `<Ty>::AssocItem` expression/pattern/type.
pub(super) fn maybe_recover_from_bad_qpath<T: RecoverQPath>(
&mut self,
base: Box<T>,
) -> PResult<'a, Box<T>> {
if !self.may_recover() {
return Ok(base);
base: T,
) -> PResult<'a, T> {
// Do not add `::` to expected tokens.
if self.may_recover() && self.token == token::PathSep {
return self.recover_from_bad_qpath(base);
}
Ok(base)
}

// Do not add `::` to expected tokens.
if self.token == token::PathSep {
if let Some(ty) = base.to_ty() {
return self.maybe_recover_from_bad_qpath_stage_2(ty.span, ty);
}
#[cold]
fn recover_from_bad_qpath<T: RecoverQPath>(&mut self, base: T) -> PResult<'a, T> {
if let Some(ty) = base.to_ty() {
return self.maybe_recover_from_bad_qpath_stage_2(ty.span, ty);
}
Ok(base)
}
Expand All @@ -1854,7 +1866,7 @@ impl<'a> Parser<'a> {
&mut self,
ty_span: Span,
ty: Box<Ty>,
) -> PResult<'a, Box<T>> {
) -> PResult<'a, T> {
self.expect(exp!(PathSep))?;

let mut path = ast::Path { segments: ThinVec::new(), span: DUMMY_SP, tokens: None };
Expand All @@ -1867,7 +1879,7 @@ impl<'a> Parser<'a> {
});

let path_span = ty_span.shrink_to_hi(); // Use an empty path since `position == 0`.
Ok(Box::new(T::recovered(Some(Box::new(QSelf { ty, path_span, position: 0 })), path)))
Ok(T::recovered(Some(Box::new(QSelf { ty, path_span, position: 0 })), path))
}

/// This function gets called in places where a semicolon is NOT expected and if there's a
Expand Down Expand Up @@ -2360,6 +2372,7 @@ impl<'a> Parser<'a> {
None
}

#[cold]
pub(super) fn recover_arg_parse(&mut self) -> PResult<'a, (Box<ast::Pat>, Box<ast::Ty>)> {
let pat = self.parse_pat_no_top_alt(Some(Expected::ArgumentName), None)?;
self.expect(exp!(Colon))?;
Expand Down Expand Up @@ -2740,11 +2753,12 @@ impl<'a> Parser<'a> {

/// Some special error handling for the "top-level" patterns in a match arm,
/// `for` loop, `let`, &c. (in contrast to subpatterns within such).
pub(crate) fn maybe_recover_colon_colon_in_pat_typo(
#[cold]
pub(crate) fn recover_colon_colon_in_pat_typo(
&mut self,
mut first_pat: Box<Pat>,
mut first_pat: Pat,
expected: Option<Expected>,
) -> Box<Pat> {
) -> Pat {
if token::Colon != self.token.kind {
return first_pat;
}
Expand Down Expand Up @@ -2925,7 +2939,11 @@ impl<'a> Parser<'a> {
if self.token != token::Comma {
return Ok(());
}
self.recover_unexpected_comma(lo, rt)
}

#[cold]
fn recover_unexpected_comma(&mut self, lo: Span, rt: CommaRecoveryMode) -> PResult<'a, ()> {
// An unexpected comma after a top-level pattern is a clue that the
// user (perhaps more accustomed to some other language) forgot the
// parentheses in what should have been a tuple pattern; return a
Expand Down
Loading
Loading