Get rid of ast::StructFieldKind
This commit is contained in:
parent
772c600d4d
commit
7f3744f07f
10 changed files with 55 additions and 136 deletions
|
@ -622,9 +622,8 @@ pub fn lower_struct_field(lctx: &LoweringContext,
|
||||||
hir::StructField {
|
hir::StructField {
|
||||||
span: f.span,
|
span: f.span,
|
||||||
id: f.node.id,
|
id: f.node.id,
|
||||||
name: f.node.ident().map(|ident| ident.name)
|
name: f.node.ident.map(|ident| ident.name).unwrap_or(token::intern(&index.to_string())),
|
||||||
.unwrap_or(token::intern(&index.to_string())),
|
vis: lower_visibility(lctx, f.node.vis),
|
||||||
vis: lower_visibility(lctx, f.node.kind.visibility()),
|
|
||||||
ty: lower_ty(lctx, &f.node.ty),
|
ty: lower_ty(lctx, &f.node.ty),
|
||||||
attrs: lower_attrs(lctx, &f.node.attrs),
|
attrs: lower_attrs(lctx, &f.node.attrs),
|
||||||
}
|
}
|
||||||
|
|
|
@ -246,8 +246,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
|
||||||
|
|
||||||
pub fn get_field_data(&self, field: &ast::StructField,
|
pub fn get_field_data(&self, field: &ast::StructField,
|
||||||
scope: NodeId) -> Option<VariableData> {
|
scope: NodeId) -> Option<VariableData> {
|
||||||
match field.node.kind {
|
if let Some(ident) = field.node.ident {
|
||||||
ast::NamedField(ident, _) => {
|
|
||||||
let qualname = format!("::{}::{}", self.tcx.map.path_to_string(scope), ident);
|
let qualname = format!("::{}::{}", self.tcx.map.path_to_string(scope), ident);
|
||||||
let typ = self.tcx.node_types().get(&field.node.id).unwrap().to_string();
|
let typ = self.tcx.node_types().get(&field.node.id).unwrap().to_string();
|
||||||
let sub_span = self.span_utils.sub_span_before_token(field.span, token::Colon);
|
let sub_span = self.span_utils.sub_span_before_token(field.span, token::Colon);
|
||||||
|
@ -261,8 +260,8 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
|
||||||
value: "".to_owned(),
|
value: "".to_owned(),
|
||||||
type_value: typ,
|
type_value: typ,
|
||||||
})
|
})
|
||||||
}
|
} else {
|
||||||
_ => None,
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
|
|
||||||
// The Rust abstract syntax tree.
|
// The Rust abstract syntax tree.
|
||||||
|
|
||||||
pub use self::StructFieldKind::*;
|
|
||||||
pub use self::TyParamBound::*;
|
pub use self::TyParamBound::*;
|
||||||
pub use self::UnsafeSource::*;
|
pub use self::UnsafeSource::*;
|
||||||
pub use self::ViewPath_::*;
|
pub use self::ViewPath_::*;
|
||||||
|
@ -1878,45 +1877,15 @@ pub enum Visibility {
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
||||||
pub struct StructField_ {
|
pub struct StructField_ {
|
||||||
pub kind: StructFieldKind,
|
pub ident: Option<Ident>,
|
||||||
|
pub vis: Visibility,
|
||||||
pub id: NodeId,
|
pub id: NodeId,
|
||||||
pub ty: P<Ty>,
|
pub ty: P<Ty>,
|
||||||
pub attrs: Vec<Attribute>,
|
pub attrs: Vec<Attribute>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StructField_ {
|
|
||||||
pub fn ident(&self) -> Option<Ident> {
|
|
||||||
match self.kind {
|
|
||||||
NamedField(ref ident, _) => Some(ident.clone()),
|
|
||||||
UnnamedField(_) => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type StructField = Spanned<StructField_>;
|
pub type StructField = Spanned<StructField_>;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
||||||
pub enum StructFieldKind {
|
|
||||||
NamedField(Ident, Visibility),
|
|
||||||
/// Element of a tuple-like struct
|
|
||||||
UnnamedField(Visibility),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl StructFieldKind {
|
|
||||||
pub fn is_unnamed(&self) -> bool {
|
|
||||||
match *self {
|
|
||||||
UnnamedField(..) => true,
|
|
||||||
NamedField(..) => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn visibility(&self) -> &Visibility {
|
|
||||||
match *self {
|
|
||||||
NamedField(_, ref vis) | UnnamedField(ref vis) => vis
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Fields and Ids of enum variants and structs
|
/// Fields and Ids of enum variants and structs
|
||||||
///
|
///
|
||||||
/// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all
|
/// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all
|
||||||
|
|
|
@ -95,12 +95,6 @@ pub fn impl_pretty_name(trait_ref: &Option<TraitRef>, ty: Option<&Ty>) -> Ident
|
||||||
token::gensym_ident(&pretty[..])
|
token::gensym_ident(&pretty[..])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn struct_field_visibility(field: ast::StructField) -> Visibility {
|
|
||||||
match field.node.kind {
|
|
||||||
ast::NamedField(_, v) | ast::UnnamedField(v) => v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ______________________________________________________________________
|
// ______________________________________________________________________
|
||||||
// Enumerating the IDs which appear in an AST
|
// Enumerating the IDs which appear in an AST
|
||||||
|
|
||||||
|
|
|
@ -1009,7 +1009,8 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
|
||||||
let fields: Vec<_> = tys.into_iter().map(|ty| {
|
let fields: Vec<_> = tys.into_iter().map(|ty| {
|
||||||
Spanned { span: ty.span, node: ast::StructField_ {
|
Spanned { span: ty.span, node: ast::StructField_ {
|
||||||
ty: ty,
|
ty: ty,
|
||||||
kind: ast::UnnamedField(ast::Visibility::Inherited),
|
ident: None,
|
||||||
|
vis: ast::Visibility::Inherited,
|
||||||
attrs: Vec::new(),
|
attrs: Vec::new(),
|
||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
}}
|
}}
|
||||||
|
|
|
@ -847,15 +847,15 @@ pub fn noop_fold_poly_trait_ref<T: Folder>(p: PolyTraitRef, fld: &mut T) -> Poly
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructField {
|
pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructField {
|
||||||
let StructField {node: StructField_ {id, kind, ty, attrs}, span} = f;
|
|
||||||
Spanned {
|
Spanned {
|
||||||
node: StructField_ {
|
node: StructField_ {
|
||||||
id: fld.new_id(id),
|
id: fld.new_id(f.node.id),
|
||||||
kind: kind,
|
ident: f.node.ident.map(|ident| fld.fold_ident(ident)),
|
||||||
ty: fld.fold_ty(ty),
|
vis: f.node.vis,
|
||||||
attrs: fold_attrs(attrs, fld),
|
ty: fld.fold_ty(f.node.ty),
|
||||||
|
attrs: fold_attrs(f.node.attrs, fld),
|
||||||
},
|
},
|
||||||
span: fld.new_span(span)
|
span: fld.new_span(f.span)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,6 @@ use ast::Local;
|
||||||
use ast::MacStmtStyle;
|
use ast::MacStmtStyle;
|
||||||
use ast::Mac_;
|
use ast::Mac_;
|
||||||
use ast::{MutTy, Mutability};
|
use ast::{MutTy, Mutability};
|
||||||
use ast::NamedField;
|
|
||||||
use ast::{Pat, PatKind};
|
use ast::{Pat, PatKind};
|
||||||
use ast::{PolyTraitRef, QSelf};
|
use ast::{PolyTraitRef, QSelf};
|
||||||
use ast::{Stmt, StmtKind};
|
use ast::{Stmt, StmtKind};
|
||||||
|
@ -38,7 +37,6 @@ use ast::StrStyle;
|
||||||
use ast::SelfKind;
|
use ast::SelfKind;
|
||||||
use ast::{Delimited, SequenceRepetition, TokenTree, TraitItem, TraitRef};
|
use ast::{Delimited, SequenceRepetition, TokenTree, TraitItem, TraitRef};
|
||||||
use ast::{Ty, TyKind, TypeBinding, TyParam, TyParamBounds};
|
use ast::{Ty, TyKind, TypeBinding, TyParam, TyParamBounds};
|
||||||
use ast::UnnamedField;
|
|
||||||
use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
|
use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
|
||||||
use ast::{Visibility, WhereClause};
|
use ast::{Visibility, WhereClause};
|
||||||
use attr::{ThinAttributes, ThinAttributesExt, AttributesExt};
|
use attr::{ThinAttributes, ThinAttributesExt, AttributesExt};
|
||||||
|
@ -3848,7 +3846,8 @@ impl<'a> Parser<'a> {
|
||||||
self.expect(&token::Colon)?;
|
self.expect(&token::Colon)?;
|
||||||
let ty = self.parse_ty_sum()?;
|
let ty = self.parse_ty_sum()?;
|
||||||
Ok(spanned(lo, self.last_span.hi, ast::StructField_ {
|
Ok(spanned(lo, self.last_span.hi, ast::StructField_ {
|
||||||
kind: NamedField(name, pr),
|
ident: Some(name),
|
||||||
|
vis: pr,
|
||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
ty: ty,
|
ty: ty,
|
||||||
attrs: attrs,
|
attrs: attrs,
|
||||||
|
@ -5247,7 +5246,8 @@ impl<'a> Parser<'a> {
|
||||||
let attrs = p.parse_outer_attributes()?;
|
let attrs = p.parse_outer_attributes()?;
|
||||||
let lo = p.span.lo;
|
let lo = p.span.lo;
|
||||||
let struct_field_ = ast::StructField_ {
|
let struct_field_ = ast::StructField_ {
|
||||||
kind: UnnamedField(p.parse_visibility()?),
|
vis: p.parse_visibility()?,
|
||||||
|
ident: None,
|
||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
ty: p.parse_ty_sum()?,
|
ty: p.parse_ty_sum()?,
|
||||||
attrs: attrs,
|
attrs: attrs,
|
||||||
|
|
|
@ -1407,15 +1407,10 @@ impl<'a> State<'a> {
|
||||||
self.commasep(
|
self.commasep(
|
||||||
Inconsistent, struct_def.fields(),
|
Inconsistent, struct_def.fields(),
|
||||||
|s, field| {
|
|s, field| {
|
||||||
match field.node.kind {
|
s.print_visibility(field.node.vis)?;
|
||||||
ast::NamedField(..) => panic!("unexpected named field"),
|
|
||||||
ast::UnnamedField(ref vis) => {
|
|
||||||
s.print_visibility(vis)?;
|
|
||||||
s.maybe_print_comment(field.span.lo)?;
|
s.maybe_print_comment(field.span.lo)?;
|
||||||
s.print_type(&field.node.ty)
|
s.print_type(&field.node.ty)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
)?;
|
)?;
|
||||||
self.pclose()?;
|
self.pclose()?;
|
||||||
}
|
}
|
||||||
|
@ -1432,20 +1427,15 @@ impl<'a> State<'a> {
|
||||||
self.hardbreak_if_not_bol()?;
|
self.hardbreak_if_not_bol()?;
|
||||||
|
|
||||||
for field in struct_def.fields() {
|
for field in struct_def.fields() {
|
||||||
match field.node.kind {
|
|
||||||
ast::UnnamedField(..) => panic!("unexpected unnamed field"),
|
|
||||||
ast::NamedField(ident, ref visibility) => {
|
|
||||||
self.hardbreak_if_not_bol()?;
|
self.hardbreak_if_not_bol()?;
|
||||||
self.maybe_print_comment(field.span.lo)?;
|
self.maybe_print_comment(field.span.lo)?;
|
||||||
self.print_outer_attributes(&field.node.attrs)?;
|
self.print_outer_attributes(&field.node.attrs)?;
|
||||||
self.print_visibility(visibility)?;
|
self.print_visibility(field.node.vis)?;
|
||||||
self.print_ident(ident)?;
|
self.print_ident(field.node.ident.unwrap())?;
|
||||||
self.word_nbsp(":")?;
|
self.word_nbsp(":")?;
|
||||||
self.print_type(&field.node.ty)?;
|
self.print_type(&field.node.ty)?;
|
||||||
word(&mut self.s, ",")?;
|
word(&mut self.s, ",")?;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.bclose(span)
|
self.bclose(span)
|
||||||
}
|
}
|
||||||
|
|
|
@ -619,7 +619,7 @@ pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
|
||||||
|
|
||||||
pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
|
pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
|
||||||
struct_field: &'v StructField) {
|
struct_field: &'v StructField) {
|
||||||
walk_opt_ident(visitor, struct_field.span, struct_field.node.ident());
|
walk_opt_ident(visitor, struct_field.span, struct_field.node.ident);
|
||||||
visitor.visit_ty(&struct_field.node.ty);
|
visitor.visit_ty(&struct_field.node.ty);
|
||||||
walk_list!(visitor, visit_attribute, &struct_field.node.attrs);
|
walk_list!(visitor, visit_attribute, &struct_field.node.attrs);
|
||||||
}
|
}
|
||||||
|
|
|
@ -186,7 +186,6 @@
|
||||||
|
|
||||||
pub use self::StaticFields::*;
|
pub use self::StaticFields::*;
|
||||||
pub use self::SubstructureFields::*;
|
pub use self::SubstructureFields::*;
|
||||||
use self::StructType::*;
|
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
@ -1409,11 +1408,6 @@ impl<'a> MethodDef<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq)] // dogfooding!
|
|
||||||
enum StructType {
|
|
||||||
Unknown, Record, Tuple
|
|
||||||
}
|
|
||||||
|
|
||||||
// general helper methods.
|
// general helper methods.
|
||||||
impl<'a> TraitDef<'a> {
|
impl<'a> TraitDef<'a> {
|
||||||
fn set_expn_info(&self,
|
fn set_expn_info(&self,
|
||||||
|
@ -1441,9 +1435,9 @@ impl<'a> TraitDef<'a> {
|
||||||
let mut just_spans = Vec::new();
|
let mut just_spans = Vec::new();
|
||||||
for field in struct_def.fields(){
|
for field in struct_def.fields(){
|
||||||
let sp = self.set_expn_info(cx, field.span);
|
let sp = self.set_expn_info(cx, field.span);
|
||||||
match field.node.kind {
|
match field.node.ident {
|
||||||
ast::NamedField(ident, _) => named_idents.push((ident, sp)),
|
Some(ident) => named_idents.push((ident, sp)),
|
||||||
ast::UnnamedField(..) => just_spans.push(sp),
|
_ => just_spans.push(sp),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1479,53 +1473,26 @@ impl<'a> TraitDef<'a> {
|
||||||
-> (P<ast::Pat>, Vec<(Span, Option<Ident>,
|
-> (P<ast::Pat>, Vec<(Span, Option<Ident>,
|
||||||
P<Expr>,
|
P<Expr>,
|
||||||
&'a [ast::Attribute])>) {
|
&'a [ast::Attribute])>) {
|
||||||
if struct_def.fields().is_empty() {
|
|
||||||
if struct_def.is_struct() {
|
|
||||||
return (cx.pat_struct(self.span, struct_path, vec![]), vec![]);
|
|
||||||
} else {
|
|
||||||
return (cx.pat_enum(self.span, struct_path, vec![]), vec![]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut paths = Vec::new();
|
let mut paths = Vec::new();
|
||||||
let mut ident_expr = Vec::new();
|
let mut ident_exprs = Vec::new();
|
||||||
let mut struct_type = Unknown;
|
|
||||||
|
|
||||||
for (i, struct_field) in struct_def.fields().iter().enumerate() {
|
for (i, struct_field) in struct_def.fields().iter().enumerate() {
|
||||||
let sp = self.set_expn_info(cx, struct_field.span);
|
let sp = self.set_expn_info(cx, struct_field.span);
|
||||||
let opt_id = match struct_field.node.kind {
|
|
||||||
ast::NamedField(ident, _) if (struct_type == Unknown ||
|
|
||||||
struct_type == Record) => {
|
|
||||||
struct_type = Record;
|
|
||||||
Some(ident)
|
|
||||||
}
|
|
||||||
ast::UnnamedField(..) if (struct_type == Unknown ||
|
|
||||||
struct_type == Tuple) => {
|
|
||||||
struct_type = Tuple;
|
|
||||||
None
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
cx.span_bug(sp, "a struct with named and unnamed fields in `derive`");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let ident = cx.ident_of(&format!("{}_{}", prefix, i));
|
let ident = cx.ident_of(&format!("{}_{}", prefix, i));
|
||||||
paths.push(codemap::Spanned{span: sp, node: ident});
|
paths.push(codemap::Spanned{span: sp, node: ident});
|
||||||
let val = cx.expr_deref(sp, cx.expr_path(cx.path_ident(sp,ident)));
|
let val = cx.expr_deref(sp, cx.expr_path(cx.path_ident(sp,ident)));
|
||||||
let val = cx.expr(sp, ast::ExprKind::Paren(val));
|
let val = cx.expr(sp, ast::ExprKind::Paren(val));
|
||||||
ident_expr.push((sp, opt_id, val, &struct_field.node.attrs[..]));
|
ident_exprs.push((sp, struct_field.node.ident, val, &struct_field.node.attrs[..]));
|
||||||
}
|
}
|
||||||
|
|
||||||
let subpats = self.create_subpatterns(cx, paths, mutbl);
|
let subpats = self.create_subpatterns(cx, paths, mutbl);
|
||||||
|
|
||||||
// struct_type is definitely not Unknown, since struct_def.fields
|
|
||||||
// must be nonempty to reach here
|
|
||||||
let pattern = if struct_def.is_struct() {
|
let pattern = if struct_def.is_struct() {
|
||||||
let field_pats = subpats.into_iter().zip(&ident_expr)
|
let field_pats = subpats.into_iter().zip(&ident_exprs).map(|(pat, &(sp, ident, _, _))| {
|
||||||
.map(|(pat, &(_, id, _, _))| {
|
if ident.is_none() {
|
||||||
// id is guaranteed to be Some
|
cx.span_bug(sp, "a braced struct with unnamed fields in `derive`");
|
||||||
|
}
|
||||||
codemap::Spanned {
|
codemap::Spanned {
|
||||||
span: pat.span,
|
span: pat.span,
|
||||||
node: ast::FieldPat { ident: id.unwrap(), pat: pat, is_shorthand: false },
|
node: ast::FieldPat { ident: ident.unwrap(), pat: pat, is_shorthand: false },
|
||||||
}
|
}
|
||||||
}).collect();
|
}).collect();
|
||||||
cx.pat_struct(self.span, struct_path, field_pats)
|
cx.pat_struct(self.span, struct_path, field_pats)
|
||||||
|
@ -1533,7 +1500,7 @@ impl<'a> TraitDef<'a> {
|
||||||
cx.pat_enum(self.span, struct_path, subpats)
|
cx.pat_enum(self.span, struct_path, subpats)
|
||||||
};
|
};
|
||||||
|
|
||||||
(pattern, ident_expr)
|
(pattern, ident_exprs)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_enum_variant_pattern(&self,
|
fn create_enum_variant_pattern(&self,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue