1
Fork 0

Format the world

This commit is contained in:
Mark Rousskov 2019-12-22 17:42:04 -05:00
parent 8eb7c58dbb
commit a06baa56b9
1160 changed files with 65934 additions and 74316 deletions

View file

@ -1,14 +1,16 @@
use super::{Parser, TokenType};
use crate::maybe_whole;
use rustc_errors::{PResult, Applicability, pluralize};
use syntax::ast::{self, QSelf, Path, PathSegment, Ident, ParenthesizedArgs, AngleBracketedArgs};
use syntax::ast::{AnonConst, GenericArg, AssocTyConstraint, AssocTyConstraintKind, BlockCheckMode};
use rustc_errors::{pluralize, Applicability, PResult};
use syntax::ast::{self, AngleBracketedArgs, Ident, ParenthesizedArgs, Path, PathSegment, QSelf};
use syntax::ast::{
AnonConst, AssocTyConstraint, AssocTyConstraintKind, BlockCheckMode, GenericArg,
};
use syntax::token::{self, Token};
use syntax_pos::source_map::{Span, BytePos};
use syntax_pos::source_map::{BytePos, Span};
use syntax_pos::symbol::{kw, sym};
use std::mem;
use log::debug;
use std::mem;
/// Specifies how to parse a path.
#[derive(Copy, Clone, PartialEq)]
@ -89,8 +91,8 @@ impl<'a> Parser<'a> {
/// `Fn::(Args)` (with disambiguator)
pub fn parse_path(&mut self, style: PathStyle) -> PResult<'a, Path> {
maybe_whole!(self, NtPath, |path| {
if style == PathStyle::Mod &&
path.segments.iter().any(|segment| segment.args.is_some()) {
if style == PathStyle::Mod && path.segments.iter().any(|segment| segment.args.is_some())
{
self.diagnostic().span_err(path.span, "unexpected generic arguments in path");
}
path
@ -145,51 +147,58 @@ impl<'a> Parser<'a> {
let ident = self.parse_path_segment_ident()?;
let is_args_start = |token: &Token| match token.kind {
token::Lt | token::BinOp(token::Shl) | token::OpenDelim(token::Paren)
token::Lt
| token::BinOp(token::Shl)
| token::OpenDelim(token::Paren)
| token::LArrow => true,
_ => false,
};
let check_args_start = |this: &mut Self| {
this.expected_tokens.extend_from_slice(
&[TokenType::Token(token::Lt), TokenType::Token(token::OpenDelim(token::Paren))]
);
this.expected_tokens.extend_from_slice(&[
TokenType::Token(token::Lt),
TokenType::Token(token::OpenDelim(token::Paren)),
]);
is_args_start(&this.token)
};
Ok(if style == PathStyle::Type && check_args_start(self) ||
style != PathStyle::Mod && self.check(&token::ModSep)
&& self.look_ahead(1, |t| is_args_start(t)) {
// We use `style == PathStyle::Expr` to check if this is in a recursion or not. If
// it isn't, then we reset the unmatched angle bracket count as we're about to start
// parsing a new path.
if style == PathStyle::Expr {
self.unmatched_angle_bracket_count = 0;
self.max_angle_bracket_count = 0;
}
Ok(
if style == PathStyle::Type && check_args_start(self)
|| style != PathStyle::Mod
&& self.check(&token::ModSep)
&& self.look_ahead(1, |t| is_args_start(t))
{
// We use `style == PathStyle::Expr` to check if this is in a recursion or not. If
// it isn't, then we reset the unmatched angle bracket count as we're about to start
// parsing a new path.
if style == PathStyle::Expr {
self.unmatched_angle_bracket_count = 0;
self.max_angle_bracket_count = 0;
}
// Generic arguments are found - `<`, `(`, `::<` or `::(`.
self.eat(&token::ModSep);
let lo = self.token.span;
let args = if self.eat_lt() {
// `<'a, T, A = U>`
let (args, constraints) =
self.parse_generic_args_with_leading_angle_bracket_recovery(style, lo)?;
self.expect_gt()?;
let span = lo.to(self.prev_span);
AngleBracketedArgs { args, constraints, span }.into()
// Generic arguments are found - `<`, `(`, `::<` or `::(`.
self.eat(&token::ModSep);
let lo = self.token.span;
let args = if self.eat_lt() {
// `<'a, T, A = U>`
let (args, constraints) =
self.parse_generic_args_with_leading_angle_bracket_recovery(style, lo)?;
self.expect_gt()?;
let span = lo.to(self.prev_span);
AngleBracketedArgs { args, constraints, span }.into()
} else {
// `(T, U) -> R`
let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?;
let span = ident.span.to(self.prev_span);
let output = self.parse_ret_ty(false, false)?;
ParenthesizedArgs { inputs, output, span }.into()
};
PathSegment { ident, args, id: ast::DUMMY_NODE_ID }
} else {
// `(T, U) -> R`
let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?;
let span = ident.span.to(self.prev_span);
let output = self.parse_ret_ty(false, false)?;
ParenthesizedArgs { inputs, output, span }.into()
};
PathSegment { ident, args, id: ast::DUMMY_NODE_ID }
} else {
// Generic arguments are not found.
PathSegment::from_ident(ident)
})
// Generic arguments are not found.
PathSegment::from_ident(ident)
},
)
}
pub(super) fn parse_path_segment_ident(&mut self) -> PResult<'a, Ident> {
@ -288,11 +297,7 @@ impl<'a> Parser<'a> {
let is_first_invocation = style == PathStyle::Expr;
// Take a snapshot before attempting to parse - we can restore this later.
let snapshot = if is_first_invocation {
Some(self.clone())
} else {
None
};
let snapshot = if is_first_invocation { Some(self.clone()) } else { None };
debug!("parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)");
match self.parse_generic_args() {
@ -319,9 +324,7 @@ impl<'a> Parser<'a> {
}
// Make a span over ${unmatched angle bracket count} characters.
let span = lo.with_hi(
lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count)
);
let span = lo.with_hi(lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count));
self.diagnostic()
.struct_span_err(
span,
@ -343,7 +346,7 @@ impl<'a> Parser<'a> {
// Try again without unmatched angle bracket characters.
self.parse_generic_args()
},
}
Err(e) => Err(e),
}
}
@ -370,9 +373,7 @@ impl<'a> Parser<'a> {
let lo = self.token.span;
let ident = self.parse_ident()?;
let kind = if self.eat(&token::Eq) {
AssocTyConstraintKind::Equality {
ty: self.parse_ty()?,
}
AssocTyConstraintKind::Equality { ty: self.parse_ty()? }
} else if self.eat(&token::Colon) {
AssocTyConstraintKind::Bound {
bounds: self.parse_generic_bounds(Some(self.prev_span))?,
@ -388,18 +389,16 @@ impl<'a> Parser<'a> {
self.sess.gated_spans.gate(sym::associated_type_bounds, span);
}
constraints.push(AssocTyConstraint {
id: ast::DUMMY_NODE_ID,
ident,
kind,
span,
});
constraints.push(AssocTyConstraint { id: ast::DUMMY_NODE_ID, ident, kind, span });
assoc_ty_constraints.push(span);
} else if self.check_const_arg() {
// Parse const argument.
let expr = if let token::OpenDelim(token::Brace) = self.token.kind {
self.parse_block_expr(
None, self.token.span, BlockCheckMode::Default, ast::AttrVec::new()
None,
self.token.span,
BlockCheckMode::Default,
ast::AttrVec::new(),
)?
} else if self.token.is_ident() {
// FIXME(const_generics): to distinguish between idents for types and consts,
@ -415,10 +414,7 @@ impl<'a> Parser<'a> {
} else {
self.parse_literal_maybe_minus()?
};
let value = AnonConst {
id: ast::DUMMY_NODE_ID,
value: expr,
};
let value = AnonConst { id: ast::DUMMY_NODE_ID, value: expr };
args.push(GenericArg::Const(value));
misplaced_assoc_ty_constraints.append(&mut assoc_ty_constraints);
} else if self.check_type() {
@ -426,11 +422,11 @@ impl<'a> Parser<'a> {
args.push(GenericArg::Type(self.parse_ty()?));
misplaced_assoc_ty_constraints.append(&mut assoc_ty_constraints);
} else {
break
break;
}
if !self.eat(&token::Comma) {
break
break;
}
}