Skip to content

Commit af7b00b

Browse files
committed
Rollup merge of rust-lang#32682 - petrochenkov:field3, r=Manishearth
The AST part of rust-lang#31937 Unlike HIR, AST still uses `Option` for field names because parser can't know field indexes reliably due to constructions like ``` struct S(#[cfg(false)] u8, u8); // The index of the second field changes from 1 during parsing to 0 after expansion. ``` and I wouldn't like to put the burden of renaming fields on expansion passes and syntax extensions. plugin-[breaking-change] cc rust-lang#31645 r? @Manishearth
2 parents 772c600 + 8fe4290 commit af7b00b

File tree

13 files changed

+84
-164
lines changed

13 files changed

+84
-164
lines changed

src/librustc/lint/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ impl<'a, 'v> ast_visit::Visitor<'v> for EarlyContext<'a> {
978978
}
979979

980980
fn visit_struct_field(&mut self, s: &ast::StructField) {
981-
self.with_lint_attrs(&s.node.attrs, |cx| {
981+
self.with_lint_attrs(&s.attrs, |cx| {
982982
run_lints!(cx, check_struct_field, early_passes, s);
983983
ast_visit::walk_struct_field(cx, s);
984984
})

src/librustc_front/lowering.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -621,12 +621,11 @@ pub fn lower_struct_field(lctx: &LoweringContext,
621621
-> hir::StructField {
622622
hir::StructField {
623623
span: f.span,
624-
id: f.node.id,
625-
name: f.node.ident().map(|ident| ident.name)
626-
.unwrap_or(token::intern(&index.to_string())),
627-
vis: lower_visibility(lctx, f.node.kind.visibility()),
628-
ty: lower_ty(lctx, &f.node.ty),
629-
attrs: lower_attrs(lctx, &f.node.attrs),
624+
id: f.id,
625+
name: f.ident.map(|ident| ident.name).unwrap_or(token::intern(&index.to_string())),
626+
vis: lower_visibility(lctx, &f.vis),
627+
ty: lower_ty(lctx, &f.ty),
628+
attrs: lower_attrs(lctx, &f.attrs),
630629
}
631630
}
632631

src/librustc_save_analysis/dump_visitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ where D: Dump
563563
// fields
564564
for field in def.fields() {
565565
self.process_struct_field_def(field, item.id);
566-
self.visit_ty(&field.node.ty);
566+
self.visit_ty(&field.ty);
567567
}
568568

569569
self.process_generic_params(ty_params, item.span, &qualname, item.id);
@@ -624,7 +624,7 @@ where D: Dump
624624

625625
for field in variant.node.data.fields() {
626626
self.process_struct_field_def(field, variant.node.data.id());
627-
self.visit_ty(&field.node.ty);
627+
self.visit_ty(&field.ty);
628628
}
629629
}
630630
self.process_generic_params(ty_params, item.span, &enum_data.qualname, enum_data.id);

src/librustc_save_analysis/lib.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -246,23 +246,22 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
246246

247247
pub fn get_field_data(&self, field: &ast::StructField,
248248
scope: NodeId) -> Option<VariableData> {
249-
match field.node.kind {
250-
ast::NamedField(ident, _) => {
251-
let qualname = format!("::{}::{}", self.tcx.map.path_to_string(scope), ident);
252-
let typ = self.tcx.node_types().get(&field.node.id).unwrap().to_string();
253-
let sub_span = self.span_utils.sub_span_before_token(field.span, token::Colon);
254-
filter!(self.span_utils, sub_span, field.span, None);
255-
Some(VariableData {
256-
id: field.node.id,
257-
name: ident.to_string(),
258-
qualname: qualname,
259-
span: sub_span.unwrap(),
260-
scope: scope,
261-
value: "".to_owned(),
262-
type_value: typ,
263-
})
264-
}
265-
_ => None,
249+
if let Some(ident) = field.ident {
250+
let qualname = format!("::{}::{}", self.tcx.map.path_to_string(scope), ident);
251+
let typ = self.tcx.node_types().get(&field.id).unwrap().to_string();
252+
let sub_span = self.span_utils.sub_span_before_token(field.span, token::Colon);
253+
filter!(self.span_utils, sub_span, field.span, None);
254+
Some(VariableData {
255+
id: field.id,
256+
name: ident.to_string(),
257+
qualname: qualname,
258+
span: sub_span.unwrap(),
259+
scope: scope,
260+
value: "".to_owned(),
261+
type_value: typ,
262+
})
263+
} else {
264+
None
266265
}
267266
}
268267

src/libsyntax/ast.rs

+4-36
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// The Rust abstract syntax tree.
1212

13-
pub use self::StructFieldKind::*;
1413
pub use self::TyParamBound::*;
1514
pub use self::UnsafeSource::*;
1615
pub use self::ViewPath_::*;
@@ -1877,46 +1876,15 @@ pub enum Visibility {
18771876
}
18781877

18791878
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1880-
pub struct StructField_ {
1881-
pub kind: StructFieldKind,
1879+
pub struct StructField {
1880+
pub span: Span,
1881+
pub ident: Option<Ident>,
1882+
pub vis: Visibility,
18821883
pub id: NodeId,
18831884
pub ty: P<Ty>,
18841885
pub attrs: Vec<Attribute>,
18851886
}
18861887

1887-
impl StructField_ {
1888-
pub fn ident(&self) -> Option<Ident> {
1889-
match self.kind {
1890-
NamedField(ref ident, _) => Some(ident.clone()),
1891-
UnnamedField(_) => None
1892-
}
1893-
}
1894-
}
1895-
1896-
pub type StructField = Spanned<StructField_>;
1897-
1898-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1899-
pub enum StructFieldKind {
1900-
NamedField(Ident, Visibility),
1901-
/// Element of a tuple-like struct
1902-
UnnamedField(Visibility),
1903-
}
1904-
1905-
impl StructFieldKind {
1906-
pub fn is_unnamed(&self) -> bool {
1907-
match *self {
1908-
UnnamedField(..) => true,
1909-
NamedField(..) => false,
1910-
}
1911-
}
1912-
1913-
pub fn visibility(&self) -> &Visibility {
1914-
match *self {
1915-
NamedField(_, ref vis) | UnnamedField(ref vis) => vis
1916-
}
1917-
}
1918-
}
1919-
19201888
/// Fields and Ids of enum variants and structs
19211889
///
19221890
/// For enum variants: `NodeId` represents both an Id of the variant itself (relevant for all

src/libsyntax/ast_util.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,6 @@ pub fn impl_pretty_name(trait_ref: &Option<TraitRef>, ty: Option<&Ty>) -> Ident
9595
token::gensym_ident(&pretty[..])
9696
}
9797

98-
pub fn struct_field_visibility(field: ast::StructField) -> Visibility {
99-
match field.node.kind {
100-
ast::NamedField(_, v) | ast::UnnamedField(v) => v
101-
}
102-
}
103-
10498
// ______________________________________________________________________
10599
// Enumerating the IDs which appear in an AST
106100

@@ -269,7 +263,7 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
269263
}
270264

271265
fn visit_struct_field(&mut self, struct_field: &StructField) {
272-
self.operation.visit_id(struct_field.node.id);
266+
self.operation.visit_id(struct_field.id);
273267
visit::walk_struct_field(self, struct_field)
274268
}
275269

src/libsyntax/config.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,12 @@ fn fold_struct<F>(cx: &mut Context<F>, vdata: ast::VariantData) -> ast::VariantD
180180
match vdata {
181181
ast::VariantData::Struct(fields, id) => {
182182
ast::VariantData::Struct(fields.into_iter().filter(|m| {
183-
(cx.in_cfg)(&m.node.attrs)
183+
(cx.in_cfg)(&m.attrs)
184184
}).collect(), id)
185185
}
186186
ast::VariantData::Tuple(fields, id) => {
187187
ast::VariantData::Tuple(fields.into_iter().filter(|m| {
188-
(cx.in_cfg)(&m.node.attrs)
188+
(cx.in_cfg)(&m.attrs)
189189
}).collect(), id)
190190
}
191191
ast::VariantData::Unit(id) => ast::VariantData::Unit(id)
@@ -434,7 +434,7 @@ impl<'v, 'a, 'b> visit::Visitor<'v> for StmtExprAttrFeatureVisitor<'a, 'b> {
434434
}
435435

436436
fn visit_struct_field(&mut self, s: &'v ast::StructField) {
437-
if node_survives_cfg(&s.node.attrs, self.config) {
437+
if node_survives_cfg(&s.attrs, self.config) {
438438
visit::walk_struct_field(self, s);
439439
}
440440
}

src/libsyntax/ext/build.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1007,12 +1007,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
10071007

10081008
fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
10091009
let fields: Vec<_> = tys.into_iter().map(|ty| {
1010-
Spanned { span: ty.span, node: ast::StructField_ {
1010+
ast::StructField {
1011+
span: ty.span,
10111012
ty: ty,
1012-
kind: ast::UnnamedField(ast::Visibility::Inherited),
1013+
ident: None,
1014+
vis: ast::Visibility::Inherited,
10131015
attrs: Vec::new(),
10141016
id: ast::DUMMY_NODE_ID,
1015-
}}
1017+
}
10161018
}).collect();
10171019

10181020
let vdata = if fields.is_empty() {

src/libsyntax/fold.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -847,15 +847,13 @@ pub fn noop_fold_poly_trait_ref<T: Folder>(p: PolyTraitRef, fld: &mut T) -> Poly
847847
}
848848

849849
pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructField {
850-
let StructField {node: StructField_ {id, kind, ty, attrs}, span} = f;
851-
Spanned {
852-
node: StructField_ {
853-
id: fld.new_id(id),
854-
kind: kind,
855-
ty: fld.fold_ty(ty),
856-
attrs: fold_attrs(attrs, fld),
857-
},
858-
span: fld.new_span(span)
850+
StructField {
851+
span: fld.new_span(f.span),
852+
id: fld.new_id(f.id),
853+
ident: f.ident.map(|ident| fld.fold_ident(ident)),
854+
vis: f.vis,
855+
ty: fld.fold_ty(f.ty),
856+
attrs: fold_attrs(f.attrs, fld),
859857
}
860858
}
861859

src/libsyntax/parse/parser.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use ast::Local;
2929
use ast::MacStmtStyle;
3030
use ast::Mac_;
3131
use ast::{MutTy, Mutability};
32-
use ast::NamedField;
3332
use ast::{Pat, PatKind};
3433
use ast::{PolyTraitRef, QSelf};
3534
use ast::{Stmt, StmtKind};
@@ -38,7 +37,6 @@ use ast::StrStyle;
3837
use ast::SelfKind;
3938
use ast::{Delimited, SequenceRepetition, TokenTree, TraitItem, TraitRef};
4039
use ast::{Ty, TyKind, TypeBinding, TyParam, TyParamBounds};
41-
use ast::UnnamedField;
4240
use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
4341
use ast::{Visibility, WhereClause};
4442
use attr::{ThinAttributes, ThinAttributesExt, AttributesExt};
@@ -3847,12 +3845,14 @@ impl<'a> Parser<'a> {
38473845
let name = self.parse_ident()?;
38483846
self.expect(&token::Colon)?;
38493847
let ty = self.parse_ty_sum()?;
3850-
Ok(spanned(lo, self.last_span.hi, ast::StructField_ {
3851-
kind: NamedField(name, pr),
3848+
Ok(StructField {
3849+
span: mk_sp(lo, self.last_span.hi),
3850+
ident: Some(name),
3851+
vis: pr,
38523852
id: ast::DUMMY_NODE_ID,
38533853
ty: ty,
38543854
attrs: attrs,
3855-
}))
3855+
})
38563856
}
38573857

38583858
/// Emit an expected item after attributes error.
@@ -5246,13 +5246,16 @@ impl<'a> Parser<'a> {
52465246
|p| {
52475247
let attrs = p.parse_outer_attributes()?;
52485248
let lo = p.span.lo;
5249-
let struct_field_ = ast::StructField_ {
5250-
kind: UnnamedField(p.parse_visibility()?),
5249+
let vis = p.parse_visibility()?;
5250+
let ty = p.parse_ty_sum()?;
5251+
Ok(StructField {
5252+
span: mk_sp(lo, p.span.hi),
5253+
vis: vis,
5254+
ident: None,
52515255
id: ast::DUMMY_NODE_ID,
5252-
ty: p.parse_ty_sum()?,
5256+
ty: ty,
52535257
attrs: attrs,
5254-
};
5255-
Ok(spanned(lo, p.span.hi, struct_field_))
5258+
})
52565259
})?;
52575260

52585261
Ok(fields)

src/libsyntax/print/pprust.rs

+11-21
Original file line numberDiff line numberDiff line change
@@ -1407,14 +1407,9 @@ impl<'a> State<'a> {
14071407
self.commasep(
14081408
Inconsistent, struct_def.fields(),
14091409
|s, field| {
1410-
match field.node.kind {
1411-
ast::NamedField(..) => panic!("unexpected named field"),
1412-
ast::UnnamedField(ref vis) => {
1413-
s.print_visibility(vis)?;
1414-
s.maybe_print_comment(field.span.lo)?;
1415-
s.print_type(&field.node.ty)
1416-
}
1417-
}
1410+
s.print_visibility(&field.vis)?;
1411+
s.maybe_print_comment(field.span.lo)?;
1412+
s.print_type(&field.ty)
14181413
}
14191414
)?;
14201415
self.pclose()?;
@@ -1432,19 +1427,14 @@ impl<'a> State<'a> {
14321427
self.hardbreak_if_not_bol()?;
14331428

14341429
for field in struct_def.fields() {
1435-
match field.node.kind {
1436-
ast::UnnamedField(..) => panic!("unexpected unnamed field"),
1437-
ast::NamedField(ident, ref visibility) => {
1438-
self.hardbreak_if_not_bol()?;
1439-
self.maybe_print_comment(field.span.lo)?;
1440-
self.print_outer_attributes(&field.node.attrs)?;
1441-
self.print_visibility(visibility)?;
1442-
self.print_ident(ident)?;
1443-
self.word_nbsp(":")?;
1444-
self.print_type(&field.node.ty)?;
1445-
word(&mut self.s, ",")?;
1446-
}
1447-
}
1430+
self.hardbreak_if_not_bol()?;
1431+
self.maybe_print_comment(field.span.lo)?;
1432+
self.print_outer_attributes(&field.attrs)?;
1433+
self.print_visibility(&field.vis)?;
1434+
self.print_ident(field.ident.unwrap())?;
1435+
self.word_nbsp(":")?;
1436+
self.print_type(&field.ty)?;
1437+
word(&mut self.s, ",")?;
14481438
}
14491439

14501440
self.bclose(span)

src/libsyntax/visit.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,9 @@ pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
619619

620620
pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
621621
struct_field: &'v StructField) {
622-
walk_opt_ident(visitor, struct_field.span, struct_field.node.ident());
623-
visitor.visit_ty(&struct_field.node.ty);
624-
walk_list!(visitor, visit_attribute, &struct_field.node.attrs);
622+
walk_opt_ident(visitor, struct_field.span, struct_field.ident);
623+
visitor.visit_ty(&struct_field.ty);
624+
walk_list!(visitor, visit_attribute, &struct_field.attrs);
625625
}
626626

627627
pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {

0 commit comments

Comments
 (0)