Skip to content
Permalink
Browse files

Move code from `parser` to `diagnostics`

  • Loading branch information...
estebank committed May 31, 2019
1 parent 1ee45da commit ad0d3b5d4064cfd9013c498299a53b3f1586553e
Showing with 40 additions and 36 deletions.
  1. +38 −3 src/libsyntax/parse/diagnostics.rs
  2. +2 −33 src/libsyntax/parse/parser.rs
@@ -1,7 +1,6 @@
use crate::ast;
use crate::ast::{
BlockCheckMode, BinOpKind, Expr, ExprKind, Item, ItemKind, Pat, PatKind, PathSegment, QSelf,
Ty, TyKind, VariantData, Ident,
self, Arg, BinOpKind, BindingMode, BlockCheckMode, Expr, ExprKind, Ident, Item, ItemKind,
Mutability, Pat, PatKind, PathSegment, QSelf, Ty, TyKind, VariantData,
};
use crate::parse::{SeqSep, token, PResult, Parser};
use crate::parse::parser::{BlockMode, PathStyle, SemiColonMode, TokenType, TokenExpectType};
@@ -12,9 +11,25 @@ use crate::symbol::{kw, sym};
use crate::ThinVec;
use crate::util::parser::AssocOp;
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
use rustc_data_structures::fx::FxHashSet;
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
use log::{debug, trace};

/// Creates a placeholder argument.
crate fn dummy_arg(ident: Ident) -> Arg {
let pat = P(Pat {
id: ast::DUMMY_NODE_ID,
node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), ident, None),
span: ident.span,
});
let ty = Ty {
node: TyKind::Err,
span: ident.span,
id: ast::DUMMY_NODE_ID
};
Arg { ty: P(ty), pat: pat, id: ast::DUMMY_NODE_ID, source: ast::ArgSource::Normal }
}

pub enum Error {
FileNotFoundForModule {
mod_name: String,
@@ -1217,4 +1232,24 @@ impl<'a> Parser<'a> {
err.span_label(span, "expected expression");
err
}

/// Replace duplicated recovered arguments with `_` pattern to avoid unecessary errors.
crate fn deduplicate_recovered_arg_names(&self, fn_inputs: &mut Vec<Arg>) {
let mut seen_inputs = FxHashSet::default();
for input in fn_inputs.iter_mut() {
let opt_ident = if let (PatKind::Ident(_, ident, _), TyKind::Err) = (
&input.pat.node, &input.ty.node,
) {
Some(*ident)
} else {
None
};
if let Some(ident) = opt_ident {
if seen_inputs.contains(&ident) {
input.pat.node = PatKind::Wild;
}
seen_inputs.insert(ident);
}
}
}
}
@@ -47,11 +47,10 @@ use crate::parse::PResult;
use crate::ThinVec;
use crate::tokenstream::{self, DelimSpan, TokenTree, TokenStream, TreeAndJoint};
use crate::symbol::{kw, sym, Symbol};
use crate::parse::diagnostics::Error;
use crate::parse::diagnostics::{Error, dummy_arg};

use errors::{Applicability, DiagnosticBuilder, DiagnosticId, FatalError};
use rustc_target::spec::abi::{self, Abi};
use rustc_data_structures::fx::FxHashSet;
use syntax_pos::{Span, BytePos, DUMMY_SP, FileName, hygiene::CompilerDesugaringKind};
use log::debug;

@@ -452,21 +451,6 @@ impl From<P<Expr>> for LhsExpr {
}
}

/// Creates a placeholder argument.
fn dummy_arg(ident: Ident) -> Arg {
let pat = P(Pat {
id: ast::DUMMY_NODE_ID,
node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), ident, None),
span: ident.span,
});
let ty = Ty {
node: TyKind::Err,
span: ident.span,
id: ast::DUMMY_NODE_ID
};
Arg { ty: P(ty), pat: pat, id: ast::DUMMY_NODE_ID, source: ast::ArgSource::Normal }
}

#[derive(Copy, Clone, Debug)]
crate enum TokenExpectType {
Expect,
@@ -5617,22 +5601,7 @@ impl<'a> Parser<'a> {
self.expect(&token::CloseDelim(token::Paren))?;
}
// Replace duplicated recovered arguments with `_` pattern to avoid unecessary errors.
let mut seen_inputs = FxHashSet::default();
for input in fn_inputs.iter_mut() {
let opt_ident = if let (PatKind::Ident(_, ident, _), TyKind::Err) = (
&input.pat.node, &input.ty,
) {
Some(*ident)
} else {
None
};
if let Some(ident) = opt_ident {
if seen_inputs.contains(&ident) {
input.pat.node = PatKind::Wild;
}
seen_inputs.insert(ident);
}
}
self.deduplicate_recovered_arg_names(&mut fn_inputs);

Ok(P(FnDecl {
inputs: fn_inputs,

0 comments on commit ad0d3b5

Please sign in to comment.
You can’t perform that action at this time.