Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4 - Make more use of let_chains #94445

Merged
merged 1 commit into from
Feb 28, 2022
Merged
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
15 changes: 7 additions & 8 deletions compiler/rustc_parse/src/lexer/tokentrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,13 @@ struct TokenStreamBuilder {

impl TokenStreamBuilder {
fn push(&mut self, (tree, joint): TreeAndSpacing) {
if let Some((TokenTree::Token(prev_token), Joint)) = self.buf.last() {
if let TokenTree::Token(token) = &tree {
if let Some(glued) = prev_token.glue(token) {
self.buf.pop();
self.buf.push((TokenTree::Token(glued), joint));
return;
}
}
if let Some((TokenTree::Token(prev_token), Joint)) = self.buf.last()
&& let TokenTree::Token(token) = &tree
&& let Some(glued) = prev_token.glue(token)
{
self.buf.pop();
self.buf.push((TokenTree::Token(glued), joint));
return;
}
self.buf.push((tree, joint))
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_parse/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! The main parser interface.
#![feature(array_windows)]
#![feature(box_patterns)]
#![feature(crate_visibility_modifier)]
#![feature(if_let_guard)]
#![feature(box_patterns)]
#![feature(let_chains)]
#![feature(let_else)]
#![recursion_limit = "256"]

Expand Down
78 changes: 39 additions & 39 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,43 +732,42 @@ impl<'a> Parser<'a> {
mut e: DiagnosticBuilder<'a, ErrorReported>,
expr: &mut P<Expr>,
) -> PResult<'a, ()> {
if let ExprKind::Binary(binop, _, _) = &expr.kind {
if let ast::BinOpKind::Lt = binop.node {
if self.eat(&token::Comma) {
let x = self.parse_seq_to_before_end(
&token::Gt,
SeqSep::trailing_allowed(token::Comma),
|p| p.parse_generic_arg(None),
);
match x {
Ok((_, _, false)) => {
if self.eat(&token::Gt) {
e.span_suggestion_verbose(
binop.span.shrink_to_lo(),
TURBOFISH_SUGGESTION_STR,
"::".to_string(),
Applicability::MaybeIncorrect,
)
.emit();
match self.parse_expr() {
Ok(_) => {
*expr =
self.mk_expr_err(expr.span.to(self.prev_token.span));
return Ok(());
}
Err(err) => {
*expr = self.mk_expr_err(expr.span);
err.cancel();
}
}
if let ExprKind::Binary(binop, _, _) = &expr.kind
&& let ast::BinOpKind::Lt = binop.node
&& self.eat(&token::Comma)
{
let x = self.parse_seq_to_before_end(
&token::Gt,
SeqSep::trailing_allowed(token::Comma),
|p| p.parse_generic_arg(None),
);
match x {
Ok((_, _, false)) => {
if self.eat(&token::Gt) {
e.span_suggestion_verbose(
binop.span.shrink_to_lo(),
TURBOFISH_SUGGESTION_STR,
"::".to_string(),
Applicability::MaybeIncorrect,
)
.emit();
match self.parse_expr() {
Ok(_) => {
*expr =
self.mk_expr_err(expr.span.to(self.prev_token.span));
return Ok(());
}
Err(err) => {
*expr = self.mk_expr_err(expr.span);
err.cancel();
}
}
Err(err) => {
err.cancel();
}
_ => {}
}
}
Err(err) => {
err.cancel();
}
_ => {}
}
}
Err(e)
Expand All @@ -784,12 +783,13 @@ impl<'a> Parser<'a> {
outer_op: &Spanned<AssocOp>,
) -> bool /* advanced the cursor */ {
if let ExprKind::Binary(op, ref l1, ref r1) = inner_op.kind {
if let ExprKind::Field(_, ident) = l1.kind {
if ident.as_str().parse::<i32>().is_err() && !matches!(r1.kind, ExprKind::Lit(_)) {
// The parser has encountered `foo.bar<baz`, the likelihood of the turbofish
// suggestion being the only one to apply is high.
return false;
}
if let ExprKind::Field(_, ident) = l1.kind
&& ident.as_str().parse::<i32>().is_err()
&& !matches!(r1.kind, ExprKind::Lit(_))
{
// The parser has encountered `foo.bar<baz`, the likelihood of the turbofish
// suggestion being the only one to apply is high.
return false;
}
let mut enclose = |left: Span, right: Span| {
err.multipart_suggestion(
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,12 @@ impl<'a> Parser<'a> {
) -> PResult<'a, Option<Item>> {
// Don't use `maybe_whole` so that we have precise control
// over when we bump the parser
if let token::Interpolated(nt) = &self.token.kind {
if let token::NtItem(item) = &**nt {
let mut item = item.clone();
self.bump();
if let token::Interpolated(nt) = &self.token.kind && let token::NtItem(item) = &**nt {
let mut item = item.clone();
self.bump();

attrs.prepend_to_nt_inner(&mut item.attrs);
return Ok(Some(item.into_inner()));
}
attrs.prepend_to_nt_inner(&mut item.attrs);
return Ok(Some(item.into_inner()));
};

let mut unclosed_delims = vec![];
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ macro_rules! maybe_whole {
#[macro_export]
macro_rules! maybe_recover_from_interpolated_ty_qpath {
($self: expr, $allow_qpath_recovery: expr) => {
if $allow_qpath_recovery && $self.look_ahead(1, |t| t == &token::ModSep) {
if let token::Interpolated(nt) = &$self.token.kind {
if let token::NtTy(ty) = &**nt {
if $allow_qpath_recovery
&& $self.look_ahead(1, |t| t == &token::ModSep)
&& let token::Interpolated(nt) = &$self.token.kind
&& let token::NtTy(ty) = &**nt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting is a bit odd here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rustfmt stuff and for some reason, #[rustfmt::skip] breaks the build

I can remove this snippet if you want it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If rustfmt says so...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

{
let ty = ty.clone();
$self.bump();
return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
}
}
}
};
}

Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_parse/src/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,13 +658,13 @@ impl<'a> Parser<'a> {
&self,
gen_arg: GenericArg,
) -> Result<(Ident, Option<GenericArgs>), GenericArg> {
if let GenericArg::Type(ty) = &gen_arg {
if let ast::TyKind::Path(qself, path) = &ty.kind {
if qself.is_none() && path.segments.len() == 1 {
let seg = &path.segments[0];
return Ok((seg.ident, seg.args.as_deref().cloned()));
}
}
if let GenericArg::Type(ty) = &gen_arg
&& let ast::TyKind::Path(qself, path) = &ty.kind
&& qself.is_none()
&& path.segments.len() == 1
{
let seg = &path.segments[0];
return Ok((seg.ident, seg.args.as_deref().cloned()));
}
Err(gen_arg)
}
Expand Down
16 changes: 7 additions & 9 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ impl<'a> Parser<'a> {

// Don't use `maybe_whole` so that we have precise control
// over when we bump the parser
if let token::Interpolated(nt) = &self.token.kind {
if let token::NtStmt(stmt) = &**nt {
let mut stmt = stmt.clone();
self.bump();
stmt.visit_attrs(|stmt_attrs| {
attrs.prepend_to_nt_inner(stmt_attrs);
});
return Ok(Some(stmt));
}
if let token::Interpolated(nt) = &self.token.kind && let token::NtStmt(stmt) = &**nt {
let mut stmt = stmt.clone();
self.bump();
stmt.visit_attrs(|stmt_attrs| {
attrs.prepend_to_nt_inner(stmt_attrs);
});
return Ok(Some(stmt));
}

Ok(Some(if self.token.is_keyword(kw::Let) {
Expand Down