Auto merge of #83188 - petrochenkov:field, r=lcnr
ast/hir: Rename field-related structures I always forget what `ast::Field` and `ast::StructField` mean despite working with AST for long time, so this PR changes the naming to less confusing and more consistent. - `StructField` -> `FieldDef` ("field definition") - `Field` -> `ExprField` ("expression field", not "field expression") - `FieldPat` -> `PatField` ("pattern field", not "field pattern") Various visiting and other methods working with the fields are renamed correspondingly too. The second commit reduces the size of `ExprKind` by boxing fields of `ExprKind::Struct` in preparation for https://github.com/rust-lang/rust/pull/80080.
This commit is contained in:
commit
b4adc21c4f
61 changed files with 358 additions and 326 deletions
|
@ -10,7 +10,7 @@ use rustc_ast::tokenstream::Spacing;
|
|||
use rustc_ast::util::classify;
|
||||
use rustc_ast::util::literal::LitError;
|
||||
use rustc_ast::util::parser::{prec_let_scrutinee_needs_par, AssocOp, Fixity};
|
||||
use rustc_ast::{self as ast, AttrStyle, AttrVec, CaptureBy, Field, Lit, UnOp, DUMMY_NODE_ID};
|
||||
use rustc_ast::{self as ast, AttrStyle, AttrVec, CaptureBy, ExprField, Lit, UnOp, DUMMY_NODE_ID};
|
||||
use rustc_ast::{AnonConst, BinOp, BinOpKind, FnDecl, FnRetTy, MacCall, Param, Ty, TyKind};
|
||||
use rustc_ast::{Arm, Async, BlockCheckMode, Expr, ExprKind, Label, Movability, RangeLimits};
|
||||
use rustc_ast_pretty::pprust;
|
||||
|
@ -2316,7 +2316,7 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
let recovery_field = self.find_struct_error_after_field_looking_code();
|
||||
let parsed_field = match self.parse_field() {
|
||||
let parsed_field = match self.parse_expr_field() {
|
||||
Ok(f) => Some(f),
|
||||
Err(mut e) => {
|
||||
if pth == kw::Async {
|
||||
|
@ -2373,18 +2373,22 @@ impl<'a> Parser<'a> {
|
|||
|
||||
let span = pth.span.to(self.token.span);
|
||||
self.expect(&token::CloseDelim(token::Brace))?;
|
||||
let expr = if recover_async { ExprKind::Err } else { ExprKind::Struct(pth, fields, base) };
|
||||
let expr = if recover_async {
|
||||
ExprKind::Err
|
||||
} else {
|
||||
ExprKind::Struct(P(ast::StructExpr { path: pth, fields, rest: base }))
|
||||
};
|
||||
Ok(self.mk_expr(span, expr, attrs))
|
||||
}
|
||||
|
||||
/// Use in case of error after field-looking code: `S { foo: () with a }`.
|
||||
fn find_struct_error_after_field_looking_code(&self) -> Option<Field> {
|
||||
fn find_struct_error_after_field_looking_code(&self) -> Option<ExprField> {
|
||||
match self.token.ident() {
|
||||
Some((ident, is_raw))
|
||||
if (is_raw || !ident.is_reserved())
|
||||
&& self.look_ahead(1, |t| *t == token::Colon) =>
|
||||
{
|
||||
Some(ast::Field {
|
||||
Some(ast::ExprField {
|
||||
ident,
|
||||
span: self.token.span,
|
||||
expr: self.mk_expr_err(self.token.span),
|
||||
|
@ -2418,7 +2422,7 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
/// Parses `ident (COLON expr)?`.
|
||||
fn parse_field(&mut self) -> PResult<'a, Field> {
|
||||
fn parse_expr_field(&mut self) -> PResult<'a, ExprField> {
|
||||
let attrs = self.parse_outer_attributes()?;
|
||||
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
|
||||
let lo = this.token.span;
|
||||
|
@ -2438,7 +2442,7 @@ impl<'a> Parser<'a> {
|
|||
};
|
||||
|
||||
Ok((
|
||||
ast::Field {
|
||||
ast::ExprField {
|
||||
ident,
|
||||
span: lo.to(expr.span),
|
||||
expr,
|
||||
|
|
|
@ -9,7 +9,7 @@ use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
|
|||
use rustc_ast::{self as ast, AttrVec, Attribute, DUMMY_NODE_ID};
|
||||
use rustc_ast::{Async, Const, Defaultness, IsAuto, Mutability, Unsafe, UseTree, UseTreeKind};
|
||||
use rustc_ast::{BindingMode, Block, FnDecl, FnSig, Param, SelfKind};
|
||||
use rustc_ast::{EnumDef, Generics, StructField, TraitRef, Ty, TyKind, Variant, VariantData};
|
||||
use rustc_ast::{EnumDef, FieldDef, Generics, TraitRef, Ty, TyKind, Variant, VariantData};
|
||||
use rustc_ast::{FnHeader, ForeignItem, Path, PathSegment, Visibility, VisibilityKind};
|
||||
use rustc_ast::{MacArgs, MacCall, MacDelimiter};
|
||||
use rustc_ast_pretty::pprust;
|
||||
|
@ -1231,14 +1231,12 @@ impl<'a> Parser<'a> {
|
|||
Ok((class_name, ItemKind::Union(vdata, generics)))
|
||||
}
|
||||
|
||||
fn parse_record_struct_body(
|
||||
&mut self,
|
||||
) -> PResult<'a, (Vec<StructField>, /* recovered */ bool)> {
|
||||
fn parse_record_struct_body(&mut self) -> PResult<'a, (Vec<FieldDef>, /* recovered */ bool)> {
|
||||
let mut fields = Vec::new();
|
||||
let mut recovered = false;
|
||||
if self.eat(&token::OpenDelim(token::Brace)) {
|
||||
while self.token != token::CloseDelim(token::Brace) {
|
||||
let field = self.parse_struct_decl_field().map_err(|e| {
|
||||
let field = self.parse_field_def().map_err(|e| {
|
||||
self.consume_block(token::Brace, ConsumeClosingDelim::No);
|
||||
recovered = true;
|
||||
e
|
||||
|
@ -1263,7 +1261,7 @@ impl<'a> Parser<'a> {
|
|||
Ok((fields, recovered))
|
||||
}
|
||||
|
||||
fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
|
||||
fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<FieldDef>> {
|
||||
// This is the case where we find `struct Foo<T>(T) where T: Copy;`
|
||||
// Unit like structs are handled in parse_item_struct function
|
||||
self.parse_paren_comma_seq(|p| {
|
||||
|
@ -1274,7 +1272,7 @@ impl<'a> Parser<'a> {
|
|||
let ty = p.parse_ty()?;
|
||||
|
||||
Ok((
|
||||
StructField {
|
||||
FieldDef {
|
||||
span: lo.to(ty.span),
|
||||
vis,
|
||||
ident: None,
|
||||
|
@ -1291,7 +1289,7 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
/// Parses an element of a struct declaration.
|
||||
fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
|
||||
fn parse_field_def(&mut self) -> PResult<'a, FieldDef> {
|
||||
let attrs = self.parse_outer_attributes()?;
|
||||
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
|
||||
let lo = this.token.span;
|
||||
|
@ -1306,7 +1304,7 @@ impl<'a> Parser<'a> {
|
|||
lo: Span,
|
||||
vis: Visibility,
|
||||
attrs: Vec<Attribute>,
|
||||
) -> PResult<'a, StructField> {
|
||||
) -> PResult<'a, FieldDef> {
|
||||
let mut seen_comma: bool = false;
|
||||
let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
|
||||
if self.token == token::Comma {
|
||||
|
@ -1398,11 +1396,11 @@ impl<'a> Parser<'a> {
|
|||
lo: Span,
|
||||
vis: Visibility,
|
||||
attrs: Vec<Attribute>,
|
||||
) -> PResult<'a, StructField> {
|
||||
) -> PResult<'a, FieldDef> {
|
||||
let name = self.parse_ident_common(false)?;
|
||||
self.expect(&token::Colon)?;
|
||||
let ty = self.parse_ty()?;
|
||||
Ok(StructField {
|
||||
Ok(FieldDef {
|
||||
span: lo.to(self.prev_token.span),
|
||||
ident: Some(name),
|
||||
vis,
|
||||
|
|
|
@ -3,7 +3,7 @@ use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
|
|||
use rustc_ast::mut_visit::{noop_visit_pat, MutVisitor};
|
||||
use rustc_ast::ptr::P;
|
||||
use rustc_ast::token;
|
||||
use rustc_ast::{self as ast, AttrVec, Attribute, FieldPat, MacCall, Pat, PatKind, RangeEnd};
|
||||
use rustc_ast::{self as ast, AttrVec, Attribute, MacCall, Pat, PatField, PatKind, RangeEnd};
|
||||
use rustc_ast::{BindingMode, Expr, ExprKind, Mutability, Path, QSelf, RangeSyntax};
|
||||
use rustc_ast_pretty::pprust;
|
||||
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, PResult};
|
||||
|
@ -928,7 +928,7 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
/// Parses the fields of a struct-like pattern.
|
||||
fn parse_pat_fields(&mut self) -> PResult<'a, (Vec<FieldPat>, bool)> {
|
||||
fn parse_pat_fields(&mut self) -> PResult<'a, (Vec<PatField>, bool)> {
|
||||
let mut fields = Vec::new();
|
||||
let mut etc = false;
|
||||
let mut ate_comma = true;
|
||||
|
@ -1072,7 +1072,7 @@ impl<'a> Parser<'a> {
|
|||
.emit();
|
||||
}
|
||||
|
||||
fn parse_pat_field(&mut self, lo: Span, attrs: Vec<Attribute>) -> PResult<'a, FieldPat> {
|
||||
fn parse_pat_field(&mut self, lo: Span, attrs: Vec<Attribute>) -> PResult<'a, PatField> {
|
||||
// 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) {
|
||||
|
@ -1104,7 +1104,7 @@ impl<'a> Parser<'a> {
|
|||
(subpat, fieldname, true)
|
||||
};
|
||||
|
||||
Ok(FieldPat {
|
||||
Ok(PatField {
|
||||
ident: fieldname,
|
||||
pat: subpat,
|
||||
is_shorthand,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue