Move code from parser
to diagnostics
This commit is contained in:
parent
1ee45da2b9
commit
ad0d3b5d40
2 changed files with 40 additions and 36 deletions
|
@ -1,7 +1,6 @@
|
||||||
use crate::ast;
|
|
||||||
use crate::ast::{
|
use crate::ast::{
|
||||||
BlockCheckMode, BinOpKind, Expr, ExprKind, Item, ItemKind, Pat, PatKind, PathSegment, QSelf,
|
self, Arg, BinOpKind, BindingMode, BlockCheckMode, Expr, ExprKind, Ident, Item, ItemKind,
|
||||||
Ty, TyKind, VariantData, Ident,
|
Mutability, Pat, PatKind, PathSegment, QSelf, Ty, TyKind, VariantData,
|
||||||
};
|
};
|
||||||
use crate::parse::{SeqSep, token, PResult, Parser};
|
use crate::parse::{SeqSep, token, PResult, Parser};
|
||||||
use crate::parse::parser::{BlockMode, PathStyle, SemiColonMode, TokenType, TokenExpectType};
|
use crate::parse::parser::{BlockMode, PathStyle, SemiColonMode, TokenType, TokenExpectType};
|
||||||
|
@ -12,9 +11,25 @@ use crate::symbol::{kw, sym};
|
||||||
use crate::ThinVec;
|
use crate::ThinVec;
|
||||||
use crate::util::parser::AssocOp;
|
use crate::util::parser::AssocOp;
|
||||||
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
|
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
|
||||||
|
use rustc_data_structures::fx::FxHashSet;
|
||||||
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
|
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
|
||||||
use log::{debug, trace};
|
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 {
|
pub enum Error {
|
||||||
FileNotFoundForModule {
|
FileNotFoundForModule {
|
||||||
mod_name: String,
|
mod_name: String,
|
||||||
|
@ -1217,4 +1232,24 @@ impl<'a> Parser<'a> {
|
||||||
err.span_label(span, "expected expression");
|
err.span_label(span, "expected expression");
|
||||||
err
|
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::ThinVec;
|
||||||
use crate::tokenstream::{self, DelimSpan, TokenTree, TokenStream, TreeAndJoint};
|
use crate::tokenstream::{self, DelimSpan, TokenTree, TokenStream, TreeAndJoint};
|
||||||
use crate::symbol::{kw, sym, Symbol};
|
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 errors::{Applicability, DiagnosticBuilder, DiagnosticId, FatalError};
|
||||||
use rustc_target::spec::abi::{self, Abi};
|
use rustc_target::spec::abi::{self, Abi};
|
||||||
use rustc_data_structures::fx::FxHashSet;
|
|
||||||
use syntax_pos::{Span, BytePos, DUMMY_SP, FileName, hygiene::CompilerDesugaringKind};
|
use syntax_pos::{Span, BytePos, DUMMY_SP, FileName, hygiene::CompilerDesugaringKind};
|
||||||
use log::debug;
|
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)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
crate enum TokenExpectType {
|
crate enum TokenExpectType {
|
||||||
Expect,
|
Expect,
|
||||||
|
@ -5617,22 +5601,7 @@ impl<'a> Parser<'a> {
|
||||||
self.expect(&token::CloseDelim(token::Paren))?;
|
self.expect(&token::CloseDelim(token::Paren))?;
|
||||||
}
|
}
|
||||||
// Replace duplicated recovered arguments with `_` pattern to avoid unecessary errors.
|
// Replace duplicated recovered arguments with `_` pattern to avoid unecessary errors.
|
||||||
let mut seen_inputs = FxHashSet::default();
|
self.deduplicate_recovered_arg_names(&mut fn_inputs);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(P(FnDecl {
|
Ok(P(FnDecl {
|
||||||
inputs: fn_inputs,
|
inputs: fn_inputs,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue