Remove Spanned from {ast,hir}::FieldPat

This commit is contained in:
Vadim Petrochenkov
2019-08-15 02:35:36 +03:00
parent 433b1e36e1
commit a6182711ef
24 changed files with 72 additions and 92 deletions
+1 -1
View File
@@ -136,7 +136,7 @@ fn pat(&mut self, pat: &hir::Pat, pred: CFGIndex) -> CFGIndex {
}
PatKind::Struct(_, ref subpats, _) => {
let pats_exit = self.pats_all(subpats.iter().map(|f| &f.node.pat), pred);
let pats_exit = self.pats_all(subpats.iter().map(|f| &f.pat), pred);
self.add_ast_node(pat.hir_id.local_id, &[pats_exit])
}
+3 -3
View File
@@ -704,9 +704,9 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
PatKind::Struct(ref qpath, ref fields, _) => {
visitor.visit_qpath(qpath, pattern.hir_id, pattern.span);
for field in fields {
visitor.visit_id(field.node.hir_id);
visitor.visit_ident(field.node.ident);
visitor.visit_pat(&field.node.pat)
visitor.visit_id(field.hir_id);
visitor.visit_ident(field.ident);
visitor.visit_pat(&field.pat)
}
}
PatKind::Tuple(ref tuple_elements, _) => {
+6 -10
View File
@@ -2691,16 +2691,12 @@ fn lower_pat(&mut self, p: &Pat) -> P<hir::Pat> {
let fs = fields
.iter()
.map(|f| {
Spanned {
span: f.span,
node: hir::FieldPat {
hir_id: self.next_id(),
ident: f.node.ident,
pat: self.lower_pat(&f.node.pat),
is_shorthand: f.node.is_shorthand,
},
}
.map(|f| hir::FieldPat {
hir_id: self.next_id(),
ident: f.ident,
pat: self.lower_pat(&f.pat),
is_shorthand: f.is_shorthand,
span: f.span,
})
.collect();
hir::PatKind::Struct(qpath, fs, etc)
+3 -2
View File
@@ -877,7 +877,7 @@ fn walk_<G>(&self, it: &mut G) -> bool
match self.node {
PatKind::Binding(.., Some(ref p)) => p.walk_(it),
PatKind::Struct(_, ref fields, _) => {
fields.iter().all(|field| field.node.pat.walk_(it))
fields.iter().all(|field| field.pat.walk_(it))
}
PatKind::TupleStruct(_, ref s, _) | PatKind::Tuple(ref s, _) => {
s.iter().all(|p| p.walk_(it))
@@ -923,6 +923,7 @@ pub struct FieldPat {
/// The pattern the field is destructured to.
pub pat: P<Pat>,
pub is_shorthand: bool,
pub span: Span,
}
/// Explicit binding annotations given in the HIR for a binding. Note
@@ -968,7 +969,7 @@ pub enum PatKind {
/// A struct or struct variant pattern (e.g., `Variant {x, y, ..}`).
/// The `bool` is `true` in the presence of a `..`.
Struct(QPath, HirVec<Spanned<FieldPat>>, bool),
Struct(QPath, HirVec<FieldPat>, bool),
/// A tuple struct/variant pattern `Variant(x, y, .., z)`.
/// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
+4 -4
View File
@@ -1670,14 +1670,14 @@ pub fn print_pat(&mut self, pat: &hir::Pat) {
&fields[..],
|s, f| {
s.cbox(INDENT_UNIT);
if !f.node.is_shorthand {
s.print_ident(f.node.ident);
if !f.is_shorthand {
s.print_ident(f.ident);
s.word_nbsp(":");
}
s.print_pat(&f.node.pat);
s.print_pat(&f.pat);
s.end()
},
|f| f.node.pat.span);
|f| f.pat.span);
if etc {
if !fields.is_empty() {
self.word_space(",");
-4
View File
@@ -153,8 +153,6 @@ fn hash_stable<W: StableHasherResult>(&self,
}
}
impl_stable_hash_for_spanned!(hir::FieldPat);
impl_stable_hash_for_spanned!(hir::BinOpKind);
impl_stable_hash_for!(struct hir::Stmt {
@@ -187,8 +185,6 @@ fn hash_stable<W: StableHasherResult>(&self,
impl_stable_hash_for_spanned!(usize);
impl_stable_hash_for_spanned!(ast::Ident);
impl_stable_hash_for!(struct ast::Ident {
name,
span,
+4 -6
View File
@@ -17,8 +17,7 @@
use rustc_data_structures::fx::FxHashMap;
use syntax::{ast, source_map};
use syntax::attr;
use syntax::{ast, attr};
use syntax::symbol::sym;
use syntax_pos;
@@ -119,17 +118,16 @@ fn handle_field_access(&mut self, lhs: &hir::Expr, hir_id: hir::HirId) {
}
}
fn handle_field_pattern_match(&mut self, lhs: &hir::Pat, res: Res,
pats: &[source_map::Spanned<hir::FieldPat>]) {
fn handle_field_pattern_match(&mut self, lhs: &hir::Pat, res: Res, pats: &[hir::FieldPat]) {
let variant = match self.tables.node_type(lhs.hir_id).sty {
ty::Adt(adt, _) => adt.variant_of_res(res),
_ => span_bug!(lhs.span, "non-ADT in struct pattern")
};
for pat in pats {
if let PatKind::Wild = pat.node.pat.node {
if let PatKind::Wild = pat.pat.node {
continue;
}
let index = self.tcx.field_index(pat.node.hir_id, self.tables);
let index = self.tcx.field_index(pat.hir_id, self.tables);
self.insert_def_id(variant.fields[index].did);
}
}
+2 -2
View File
@@ -418,8 +418,8 @@ fn add_from_pat<'tcx>(ir: &mut IrMaps<'tcx>, pat: &P<hir::Pat>) {
}
Struct(_, ref fields, _) => {
for field in fields {
if field.node.is_shorthand {
shorthand_field_ids.insert(field.node.pat.hir_id);
if field.is_shorthand {
shorthand_field_ids.insert(field.pat.hir_id);
}
}
}
+4 -4
View File
@@ -1282,11 +1282,11 @@ fn cat_pattern_<F>(&self, mut cmt: cmt<'tcx>, pat: &hir::Pat, op: &mut F) -> McR
};
for fp in field_pats {
let field_ty = self.pat_ty_adjusted(&fp.node.pat)?; // see (*2)
let f_index = self.tcx.field_index(fp.node.hir_id, self.tables);
let field_ty = self.pat_ty_adjusted(&fp.pat)?; // see (*2)
let f_index = self.tcx.field_index(fp.hir_id, self.tables);
let cmt_field = Rc::new(self.cat_field(pat, cmt.clone(), f_index,
fp.node.ident, field_ty));
self.cat_pattern_(cmt_field, &fp.node.pat, op)?;
fp.ident, field_ty));
self.cat_pattern_(cmt_field, &fp.pat, op)?;
}
}
+1 -1
View File
@@ -1207,7 +1207,7 @@ fn is_binding_pat(pat: &hir::Pat) -> bool {
PatKind::Binding(hir::BindingAnnotation::RefMut, ..) => true,
PatKind::Struct(_, ref field_pats, _) => {
field_pats.iter().any(|fp| is_binding_pat(&fp.node.pat))
field_pats.iter().any(|fp| is_binding_pat(&fp.pat))
}
PatKind::Slice(ref pats1, ref pats2, ref pats3) => {
+3 -3
View File
@@ -164,7 +164,7 @@ fn check_pat(&mut self, cx: &LateContext<'_, '_>, pat: &hir::Pat) {
.expect("struct pattern type is not an ADT")
.variant_of_res(cx.tables.qpath_res(qpath, pat.hir_id));
for fieldpat in field_pats {
if fieldpat.node.is_shorthand {
if fieldpat.is_shorthand {
continue;
}
if fieldpat.span.ctxt().outer_expn_info().is_some() {
@@ -173,9 +173,9 @@ fn check_pat(&mut self, cx: &LateContext<'_, '_>, pat: &hir::Pat) {
// (Issue #49588)
continue;
}
if let PatKind::Binding(_, _, ident, None) = fieldpat.node.pat.node {
if let PatKind::Binding(_, _, ident, None) = fieldpat.pat.node {
if cx.tcx.find_field_index(ident, &variant) ==
Some(cx.tcx.field_index(fieldpat.node.hir_id, cx.tables)) {
Some(cx.tcx.field_index(fieldpat.hir_id, cx.tables)) {
let mut err = cx.struct_span_lint(NON_SHORTHAND_FIELD_PATTERNS,
fieldpat.span,
&format!("the `{}:` in this pattern is redundant", ident));
+2 -2
View File
@@ -645,9 +645,9 @@ fn lower_pattern_unadjusted(&mut self, pat: &'tcx hir::Pat) -> Pattern<'tcx> {
fields.iter()
.map(|field| {
FieldPattern {
field: Field::new(self.tcx.field_index(field.node.hir_id,
field: Field::new(self.tcx.field_index(field.hir_id,
self.tables)),
pattern: self.lower_pattern(&field.node.pat),
pattern: self.lower_pattern(&field.pat),
}
})
.collect();
+2 -2
View File
@@ -1075,8 +1075,8 @@ fn visit_pat(&mut self, pat: &'tcx hir::Pat) {
let adt = self.tables.pat_ty(pat).ty_adt_def().unwrap();
let variant = adt.variant_of_res(res);
for field in fields {
let use_ctxt = field.node.ident.span;
let index = self.tcx.field_index(field.node.hir_id, self.tables);
let use_ctxt = field.ident.span;
let index = self.tcx.field_index(field.hir_id, self.tables);
self.check_field(use_ctxt, field.span, adt, &variant.fields[index]);
}
}
+2 -2
View File
@@ -32,7 +32,7 @@
ty_to_string
};
use syntax::ptr::P;
use syntax::source_map::{Spanned, DUMMY_SP, respan};
use syntax::source_map::{DUMMY_SP, respan};
use syntax::walk_list;
use syntax_pos::*;
@@ -879,7 +879,7 @@ fn process_pat(&mut self, p: &'l ast::Pat) {
};
let variant = adt.variant_of_res(self.save_ctxt.get_path_res(p.id));
for &Spanned { node: ref field, .. } in fields {
for field in fields {
if let Some(index) = self.tcx.find_field_index(field.ident, variant) {
if !self.span.filter_generated(field.ident.span) {
let span = self.span_from_span(field.ident.span);
+5 -5
View File
@@ -12,7 +12,6 @@
use rustc::ty::{self, Ty, TypeFoldable};
use rustc::ty::subst::Kind;
use syntax::ast;
use syntax::source_map::Spanned;
use syntax::util::lev_distance::find_best_match_for_name;
use syntax_pos::Span;
use syntax_pos::hygiene::DesugaringKind;
@@ -1036,7 +1035,7 @@ fn check_pat_struct(
&self,
pat: &'tcx hir::Pat,
qpath: &hir::QPath,
fields: &'tcx [Spanned<hir::FieldPat>],
fields: &'tcx [hir::FieldPat],
etc: bool,
expected: Ty<'tcx>,
def_bm: ty::BindingMode,
@@ -1048,7 +1047,7 @@ fn check_pat_struct(
variant_ty
} else {
for field in fields {
self.check_pat_walk(&field.node.pat, self.tcx.types.err, def_bm, discrim_span);
self.check_pat_walk(&field.pat, self.tcx.types.err, def_bm, discrim_span);
}
return self.tcx.types.err;
};
@@ -1206,7 +1205,7 @@ fn check_struct_pat_fields(
pat_id: hir::HirId,
span: Span,
variant: &'tcx ty::VariantDef,
fields: &'tcx [Spanned<hir::FieldPat>],
fields: &'tcx [hir::FieldPat],
etc: bool,
def_bm: ty::BindingMode,
) -> bool {
@@ -1231,7 +1230,8 @@ fn check_struct_pat_fields(
let mut inexistent_fields = vec![];
// Typecheck each field.
for &Spanned { node: ref field, span } in fields {
for field in fields {
let span = field.span;
let ident = tcx.adjust_ident(field.ident, variant.def_id);
let field_ty = match used_fields.entry(ident) {
Occupied(occupied) => {
+1 -1
View File
@@ -283,7 +283,7 @@ fn visit_pat(&mut self, p: &'tcx hir::Pat) {
}
hir::PatKind::Struct(_, ref fields, _) => {
for field in fields {
self.visit_field_id(field.node.hir_id);
self.visit_field_id(field.hir_id);
}
}
_ => {}
+2 -3
View File
@@ -29,7 +29,7 @@
use syntax::ast::{self, AttrStyle, Ident};
use syntax::attr;
use syntax::ext::base::MacroKind;
use syntax::source_map::{DUMMY_SP, Spanned};
use syntax::source_map::DUMMY_SP;
use syntax::symbol::{Symbol, kw, sym};
use syntax::symbol::InternedString;
use syntax_pos::{self, Pos, FileName};
@@ -4102,8 +4102,7 @@ fn name_from_pat(p: &hir::Pat) -> String {
PatKind::TupleStruct(ref p, ..) | PatKind::Path(ref p) => qpath_to_string(p),
PatKind::Struct(ref name, ref fields, etc) => {
format!("{} {{ {}{} }}", qpath_to_string(name),
fields.iter().map(|&Spanned { node: ref fp, .. }|
format!("{}: {}", fp.ident, name_from_pat(&*fp.pat)))
fields.iter().map(|fp| format!("{}: {}", fp.ident, name_from_pat(&fp.pat)))
.collect::<Vec<String>>().join(", "),
if etc { ", .." } else { "" }
)
+3 -2
View File
@@ -571,7 +571,7 @@ pub fn walk<F>(&self, it: &mut F) -> bool
match &self.node {
PatKind::Ident(_, _, Some(p)) => p.walk(it),
PatKind::Struct(_, fields, _) => fields.iter().all(|field| field.node.pat.walk(it)),
PatKind::Struct(_, fields, _) => fields.iter().all(|field| field.pat.walk(it)),
PatKind::TupleStruct(_, s) | PatKind::Tuple(s) | PatKind::Slice(s) => {
s.iter().all(|p| p.walk(it))
}
@@ -609,6 +609,7 @@ pub struct FieldPat {
pub is_shorthand: bool,
pub attrs: ThinVec<Attribute>,
pub id: NodeId,
pub span: Span,
}
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, Copy)]
@@ -642,7 +643,7 @@ pub enum PatKind {
/// A struct or struct variant pattern (e.g., `Variant {x, y, ..}`).
/// The `bool` is `true` in the presence of a `..`.
Struct(Path, Vec<Spanned<FieldPat>>, /* recovered */ bool),
Struct(Path, Vec<FieldPat>, /* recovered */ bool),
/// A tuple struct/variant pattern (`Variant(x, y, .., z)`).
TupleStruct(Path, Vec<P<Pat>>),
+1 -1
View File
@@ -575,7 +575,7 @@ pub fn pat_tuple_struct(&self, span: Span, path: ast::Path,
self.pat(span, PatKind::TupleStruct(path, subpats))
}
pub fn pat_struct(&self, span: Span, path: ast::Path,
field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
field_pats: Vec<ast::FieldPat>) -> P<ast::Pat> {
self.pat(span, PatKind::Struct(path, field_pats, false))
}
pub fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
+1 -4
View File
@@ -1042,10 +1042,7 @@ pub fn noop_visit_pat<T: MutVisitor>(pat: &mut P<Pat>, vis: &mut T) {
}
PatKind::Struct(path, fields, _etc) => {
vis.visit_path(path);
for Spanned {
node: FieldPat { ident, pat, is_shorthand: _, attrs, id },
span
} in fields {
for FieldPat { ident, pat, is_shorthand: _, attrs, id, span } in fields {
vis.visit_ident(ident);
vis.visit_id(id);
vis.visit_pat(pat);
+8 -14
View File
@@ -488,7 +488,7 @@ fn parse_pat_tuple_struct(&mut self, qself: Option<QSelf>, path: Path) -> PResul
}
/// Parses the fields of a struct-like pattern.
fn parse_pat_fields(&mut self) -> PResult<'a, (Vec<Spanned<FieldPat>>, bool)> {
fn parse_pat_fields(&mut self) -> PResult<'a, (Vec<FieldPat>, bool)> {
let mut fields = Vec::new();
let mut etc = false;
let mut ate_comma = true;
@@ -620,11 +620,7 @@ fn recover_one_fewer_dotdot(&self) {
.emit();
}
fn parse_pat_field(
&mut self,
lo: Span,
attrs: Vec<Attribute>
) -> PResult<'a, Spanned<FieldPat>> {
fn parse_pat_field(&mut self, lo: Span, attrs: Vec<Attribute>) -> PResult<'a, FieldPat> {
// Check if a colon exists one ahead. This means we're parsing a fieldname.
let hi;
let (subpat, fieldname, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
@@ -659,15 +655,13 @@ fn parse_pat_field(
(subpat, fieldname, true)
};
Ok(Spanned {
Ok(FieldPat {
ident: fieldname,
pat: subpat,
is_shorthand,
attrs: attrs.into(),
id: ast::DUMMY_NODE_ID,
span: lo.to(hi),
node: FieldPat {
ident: fieldname,
pat: subpat,
is_shorthand,
attrs: attrs.into(),
id: ast::DUMMY_NODE_ID,
}
})
}
+4 -4
View File
@@ -2367,14 +2367,14 @@ fn print_qpath(&mut self,
Consistent, &fields[..],
|s, f| {
s.cbox(INDENT_UNIT);
if !f.node.is_shorthand {
s.print_ident(f.node.ident);
if !f.is_shorthand {
s.print_ident(f.ident);
s.word_nbsp(":");
}
s.print_pat(&f.node.pat);
s.print_pat(&f.pat);
s.end();
},
|f| f.node.pat.span);
|f| f.pat.span);
if etc {
if !fields.is_empty() { self.word_space(","); }
self.s.word("..");
+3 -3
View File
@@ -442,9 +442,9 @@ pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
PatKind::Struct(ref path, ref fields, _) => {
visitor.visit_path(path, pattern.id);
for field in fields {
walk_list!(visitor, visit_attribute, field.node.attrs.iter());
visitor.visit_ident(field.node.ident);
visitor.visit_pat(&field.node.pat)
walk_list!(visitor, visit_attribute, field.attrs.iter());
visitor.visit_ident(field.ident);
visitor.visit_pat(&field.pat)
}
}
PatKind::Tuple(ref elems) => {
+7 -9
View File
@@ -187,7 +187,7 @@
use syntax::ast::{VariantData, GenericParamKind, GenericArg};
use syntax::attr;
use syntax::ext::base::{Annotatable, ExtCtxt, SpecialDerives};
use syntax::source_map::{self, respan};
use syntax::source_map::respan;
use syntax::util::map_in_place::MapInPlace;
use syntax::ptr::P;
use syntax::symbol::{Symbol, kw, sym};
@@ -1610,15 +1610,13 @@ fn create_struct_pattern
if ident.is_none() {
cx.span_bug(sp, "a braced struct with unnamed fields in `derive`");
}
source_map::Spanned {
ast::FieldPat {
ident: ident.unwrap(),
is_shorthand: false,
attrs: ThinVec::new(),
id: ast::DUMMY_NODE_ID,
span: pat.span.with_ctxt(self.span.ctxt()),
node: ast::FieldPat {
id: ast::DUMMY_NODE_ID,
ident: ident.unwrap(),
pat,
is_shorthand: false,
attrs: ThinVec::new(),
},
pat,
}
})
.collect();