Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast/hir: Rename field-related structures #83188

Merged
merged 2 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ impl Pat {
/// are treated the same as `x: x, y: ref y, z: ref mut z`,
/// except when `is_shorthand` is true.
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct FieldPat {
pub struct PatField {
/// The identifier for the field.
pub ident: Ident,
/// The pattern the field is destructured to.
Expand Down Expand Up @@ -692,7 +692,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<FieldPat>, /* recovered */ bool),
Struct(Path, Vec<PatField>, /* recovered */ bool),

/// A tuple struct/variant pattern (`Variant(x, y, .., z)`).
TupleStruct(Path, Vec<P<Pat>>),
Expand Down Expand Up @@ -1027,9 +1027,9 @@ pub struct Arm {
pub is_placeholder: bool,
}

/// Access of a named (e.g., `obj.foo`) or unnamed (e.g., `obj.0`) struct field.
/// A single field in a struct expression, e.g. `x: value` and `y` in `Foo { x: value, y }`.
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Field {
pub struct ExprField {
pub attrs: AttrVec,
pub id: NodeId,
pub span: Span,
Expand Down Expand Up @@ -1074,7 +1074,7 @@ pub struct Expr {

// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
rustc_data_structures::static_assert_size!(Expr, 120);
rustc_data_structures::static_assert_size!(Expr, 104);

impl Expr {
/// Returns `true` if this expression would be valid somewhere that expects a value;
Expand Down Expand Up @@ -1244,6 +1244,13 @@ pub enum StructRest {
None,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct StructExpr {
pub path: Path,
pub fields: Vec<ExprField>,
pub rest: StructRest,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub enum ExprKind {
/// A `box x` expression.
Expand Down Expand Up @@ -1369,7 +1376,7 @@ pub enum ExprKind {
/// A struct literal expression.
///
/// E.g., `Foo {x: 1, y: 2}`, or `Foo {x: 1, .. rest}`.
Struct(Path, Vec<Field>, StructRest),
Struct(P<StructExpr>),

/// An array literal constructed from one repeated element.
///
Expand Down Expand Up @@ -2519,11 +2526,11 @@ impl VisibilityKind {
}
}

/// Field of a struct.
/// Field definition in a struct, variant or union.
///
/// E.g., `bar: usize` as in `struct Foo { bar: usize }`.
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct StructField {
pub struct FieldDef {
pub attrs: Vec<Attribute>,
pub id: NodeId,
pub span: Span,
Expand All @@ -2540,11 +2547,11 @@ pub enum VariantData {
/// Struct variant.
///
/// E.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
Struct(Vec<StructField>, bool),
Struct(Vec<FieldDef>, bool),
/// Tuple variant.
///
/// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.
Tuple(Vec<StructField>, NodeId),
Tuple(Vec<FieldDef>, NodeId),
/// Unit variant.
///
/// E.g., `Bar = ..` as in `enum Foo { Bar = .. }`.
Expand All @@ -2553,7 +2560,7 @@ pub enum VariantData {

impl VariantData {
/// Return the fields of this variant.
pub fn fields(&self) -> &[StructField] {
pub fn fields(&self) -> &[FieldDef] {
match *self {
VariantData::Struct(ref fields, ..) | VariantData::Tuple(ref fields, _) => fields,
_ => &[],
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_ast/src/ast_like.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::ptr::P;
use super::tokenstream::LazyTokenStream;
use super::{Arm, Field, FieldPat, GenericParam, Param, StructField, Variant};
use super::{Arm, ExprField, FieldDef, GenericParam, Param, PatField, Variant};
use super::{AssocItem, Expr, ForeignItem, Item, Local};
use super::{AttrItem, AttrKind, Block, Pat, Path, Ty, Visibility};
use super::{AttrVec, Attribute, Stmt, StmtKind};
Expand Down Expand Up @@ -187,8 +187,7 @@ derive_has_tokens_and_attrs! {
// These ast nodes only support inert attributes, so they don't
// store tokens (since nothing can observe them)
derive_has_attrs_no_tokens! {
StructField, Arm,
Field, FieldPat, Variant, Param, GenericParam
FieldDef, Arm, ExprField, PatField, Variant, Param, GenericParam
}

// These AST nodes don't support attributes, but can
Expand Down
50 changes: 27 additions & 23 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ pub trait MutVisitor: Sized {
noop_visit_fn_header(header, self);
}

fn flat_map_struct_field(&mut self, sf: StructField) -> SmallVec<[StructField; 1]> {
noop_flat_map_struct_field(sf, self)
fn flat_map_field_def(&mut self, fd: FieldDef) -> SmallVec<[FieldDef; 1]> {
noop_flat_map_field_def(fd, self)
}

fn visit_item_kind(&mut self, i: &mut ItemKind) {
Expand Down Expand Up @@ -254,8 +254,8 @@ pub trait MutVisitor: Sized {
noop_visit_mt(mt, self);
}

fn flat_map_field(&mut self, f: Field) -> SmallVec<[Field; 1]> {
noop_flat_map_field(f, self)
fn flat_map_expr_field(&mut self, f: ExprField) -> SmallVec<[ExprField; 1]> {
noop_flat_map_expr_field(f, self)
}

fn visit_where_clause(&mut self, where_clause: &mut WhereClause) {
Expand All @@ -278,8 +278,8 @@ pub trait MutVisitor: Sized {
// Do nothing.
}

fn flat_map_field_pattern(&mut self, fp: FieldPat) -> SmallVec<[FieldPat; 1]> {
noop_flat_map_field_pattern(fp, self)
fn flat_map_pat_field(&mut self, fp: PatField) -> SmallVec<[PatField; 1]> {
noop_flat_map_pat_field(fp, self)
}
}

Expand Down Expand Up @@ -385,11 +385,11 @@ pub fn visit_delim_span<T: MutVisitor>(dspan: &mut DelimSpan, vis: &mut T) {
vis.visit_span(&mut dspan.close);
}

pub fn noop_flat_map_field_pattern<T: MutVisitor>(
mut fp: FieldPat,
pub fn noop_flat_map_pat_field<T: MutVisitor>(
mut fp: PatField,
vis: &mut T,
) -> SmallVec<[FieldPat; 1]> {
let FieldPat { attrs, id, ident, is_placeholder: _, is_shorthand: _, pat, span } = &mut fp;
) -> SmallVec<[PatField; 1]> {
let PatField { attrs, id, ident, is_placeholder: _, is_shorthand: _, pat, span } = &mut fp;
vis.visit_id(id);
vis.visit_ident(ident);
vis.visit_pat(pat);
Expand Down Expand Up @@ -842,10 +842,10 @@ pub fn noop_visit_where_predicate<T: MutVisitor>(pred: &mut WherePredicate, vis:
pub fn noop_visit_variant_data<T: MutVisitor>(vdata: &mut VariantData, vis: &mut T) {
match vdata {
VariantData::Struct(fields, ..) => {
fields.flat_map_in_place(|field| vis.flat_map_struct_field(field));
fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
}
VariantData::Tuple(fields, id) => {
fields.flat_map_in_place(|field| vis.flat_map_struct_field(field));
fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
vis.visit_id(id);
}
VariantData::Unit(id) => vis.visit_id(id),
Expand All @@ -864,22 +864,25 @@ pub fn noop_visit_poly_trait_ref<T: MutVisitor>(p: &mut PolyTraitRef, vis: &mut
vis.visit_span(span);
}

pub fn noop_flat_map_struct_field<T: MutVisitor>(
mut sf: StructField,
pub fn noop_flat_map_field_def<T: MutVisitor>(
mut fd: FieldDef,
visitor: &mut T,
) -> SmallVec<[StructField; 1]> {
let StructField { span, ident, vis, id, ty, attrs, is_placeholder: _ } = &mut sf;
) -> SmallVec<[FieldDef; 1]> {
let FieldDef { span, ident, vis, id, ty, attrs, is_placeholder: _ } = &mut fd;
visitor.visit_span(span);
visit_opt(ident, |ident| visitor.visit_ident(ident));
visitor.visit_vis(vis);
visitor.visit_id(id);
visitor.visit_ty(ty);
visit_attrs(attrs, visitor);
smallvec![sf]
smallvec![fd]
}

pub fn noop_flat_map_field<T: MutVisitor>(mut f: Field, vis: &mut T) -> SmallVec<[Field; 1]> {
let Field { ident, expr, span, is_shorthand: _, attrs, id, is_placeholder: _ } = &mut f;
pub fn noop_flat_map_expr_field<T: MutVisitor>(
mut f: ExprField,
vis: &mut T,
) -> SmallVec<[ExprField; 1]> {
let ExprField { ident, expr, span, is_shorthand: _, attrs, id, is_placeholder: _ } = &mut f;
vis.visit_ident(ident);
vis.visit_expr(expr);
vis.visit_id(id);
Expand Down Expand Up @@ -1102,7 +1105,7 @@ pub fn noop_visit_pat<T: MutVisitor>(pat: &mut P<Pat>, vis: &mut T) {
}
PatKind::Struct(path, fields, _etc) => {
vis.visit_path(path);
fields.flat_map_in_place(|field| vis.flat_map_field_pattern(field));
fields.flat_map_in_place(|field| vis.flat_map_pat_field(field));
}
PatKind::Box(inner) => vis.visit_pat(inner),
PatKind::Ref(inner, _mutbl) => vis.visit_pat(inner),
Expand Down Expand Up @@ -1283,10 +1286,11 @@ pub fn noop_visit_expr<T: MutVisitor>(
visit_vec(inputs, |(_c, expr)| vis.visit_expr(expr));
}
ExprKind::MacCall(mac) => vis.visit_mac_call(mac),
ExprKind::Struct(path, fields, expr) => {
ExprKind::Struct(se) => {
let StructExpr { path, fields, rest } = se.deref_mut();
vis.visit_path(path);
fields.flat_map_in_place(|field| vis.flat_map_field(field));
match expr {
fields.flat_map_in_place(|field| vis.flat_map_expr_field(field));
match rest {
StructRest::Base(expr) => vis.visit_expr(expr),
StructRest::Rest(_span) => {}
StructRest::None => {}
Expand Down
38 changes: 19 additions & 19 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ pub trait Visitor<'ast>: Sized {
fn visit_variant_data(&mut self, s: &'ast VariantData) {
walk_struct_def(self, s)
}
fn visit_struct_field(&mut self, s: &'ast StructField) {
walk_struct_field(self, s)
fn visit_field_def(&mut self, s: &'ast FieldDef) {
walk_field_def(self, s)
}
fn visit_enum_def(
&mut self,
Expand Down Expand Up @@ -208,11 +208,11 @@ pub trait Visitor<'ast>: Sized {
fn visit_fn_header(&mut self, _header: &'ast FnHeader) {
// Nothing to do
}
fn visit_field(&mut self, f: &'ast Field) {
walk_field(self, f)
fn visit_expr_field(&mut self, f: &'ast ExprField) {
walk_expr_field(self, f)
}
fn visit_field_pattern(&mut self, fp: &'ast FieldPat) {
walk_field_pattern(self, fp)
fn visit_pat_field(&mut self, fp: &'ast PatField) {
walk_pat_field(self, fp)
}
}

Expand Down Expand Up @@ -364,13 +364,13 @@ where
walk_list!(visitor, visit_attribute, &variant.attrs);
}

pub fn walk_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a Field) {
pub fn walk_expr_field<'a, V: Visitor<'a>>(visitor: &mut V, f: &'a ExprField) {
visitor.visit_expr(&f.expr);
visitor.visit_ident(f.ident);
walk_list!(visitor, visit_attribute, f.attrs.iter());
}

pub fn walk_field_pattern<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a FieldPat) {
pub fn walk_pat_field<'a, V: Visitor<'a>>(visitor: &mut V, fp: &'a PatField) {
visitor.visit_ident(fp.ident);
visitor.visit_pat(&fp.pat);
walk_list!(visitor, visit_attribute, fp.attrs.iter());
Expand Down Expand Up @@ -509,7 +509,7 @@ 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);
walk_list!(visitor, visit_field_pattern, fields);
walk_list!(visitor, visit_pat_field, fields);
}
PatKind::Box(ref subpattern)
| PatKind::Ref(ref subpattern, _)
Expand Down Expand Up @@ -668,16 +668,16 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
}

pub fn walk_struct_def<'a, V: Visitor<'a>>(visitor: &mut V, struct_definition: &'a VariantData) {
walk_list!(visitor, visit_struct_field, struct_definition.fields());
walk_list!(visitor, visit_field_def, struct_definition.fields());
}

pub fn walk_struct_field<'a, V: Visitor<'a>>(visitor: &mut V, struct_field: &'a StructField) {
visitor.visit_vis(&struct_field.vis);
if let Some(ident) = struct_field.ident {
pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef) {
visitor.visit_vis(&field.vis);
if let Some(ident) = field.ident {
visitor.visit_ident(ident);
}
visitor.visit_ty(&struct_field.ty);
walk_list!(visitor, visit_attribute, &struct_field.attrs);
visitor.visit_ty(&field.ty);
walk_list!(visitor, visit_attribute, &field.attrs);
}

pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
Expand Down Expand Up @@ -721,10 +721,10 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
visitor.visit_expr(element);
visitor.visit_anon_const(count)
}
ExprKind::Struct(ref path, ref fields, ref optional_base) => {
visitor.visit_path(path, expression.id);
walk_list!(visitor, visit_field, fields);
match optional_base {
ExprKind::Struct(ref se) => {
visitor.visit_path(&se.path, expression.id);
walk_list!(visitor, visit_expr_field, &se.fields);
match &se.rest {
StructRest::Base(expr) => visitor.visit_expr(expr),
StructRest::Rest(_span) => {}
StructRest::None => {}
Expand Down
Loading