Skip to content

Commit

Permalink
Move code from parser to diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed May 31, 2019
1 parent 1ee45da commit ad0d3b5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
41 changes: 38 additions & 3 deletions src/libsyntax/parse/diagnostics.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};
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}
}
}
35 changes: 2 additions & 33 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit ad0d3b5

Please sign in to comment.