Skip to content

Commit a535a31

Browse files
committed
Move ast::Item::ident into ast::ItemKind.
`ast::Item` has an `ident` field. - It's always non-empty for these item kinds: `ExternCrate`, `Static`, `Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`, `Trait`, `TraitAlias`, `MacroDef`, `Delegation`. - It's always empty for these item kinds: `Use`, `ForeignMod`, `GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`. There is a similar story for `AssocItemKind` and `ForeignItemKind`. Some sites that handle items check for an empty ident, some don't. This is a very C-like way of doing things, but this is Rust, we have sum types, we can do this properly and never forget to check for the exceptional case and never YOLO possibly empty identifiers (or possibly dummy spans) around and hope that things will work out. The commit is large but it's mostly obvious plumbing work. Some notable things. - `ast::Item` got 8 bytes bigger. This could be avoided by boxing the fields within some of the `ast::ItemKind` variants (specifically: `Struct`, `Union`, `Enum`). I might do that in a follow-up; this commit is big enough already. - For the visitors: `FnKind` no longer needs an `ident` field because the `Fn` within how has one. - In the parser, the `ItemInfo` typedef is no longer needed. It was used in various places to return an `Ident` alongside an `ItemKind`, but now the `Ident` (if present) is within the `ItemKind`. - In a few places I renamed identifier variables called `name` (or `foo_name`) as `ident` (or `foo_ident`), to better match the type, and because `name` is normally used for `Symbol`s. It's confusing to see something like `foo_name.name`.
1 parent 6bcd711 commit a535a31

File tree

4 files changed

+83
-74
lines changed

4 files changed

+83
-74
lines changed

src/items.rs

+42-36
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,12 @@ impl<'a> FnSig<'a> {
333333
defaultness: ast::Defaultness,
334334
) -> FnSig<'a> {
335335
match *fn_kind {
336-
visit::FnKind::Fn(visit::FnCtxt::Assoc(..), _, vis, ast::Fn { sig, generics, .. }) => {
336+
visit::FnKind::Fn(visit::FnCtxt::Assoc(..), vis, ast::Fn { sig, generics, .. }) => {
337337
let mut fn_sig = FnSig::from_method_sig(sig, generics, vis);
338338
fn_sig.defaultness = defaultness;
339339
fn_sig
340340
}
341-
visit::FnKind::Fn(_, _, vis, ast::Fn { sig, generics, .. }) => FnSig {
341+
visit::FnKind::Fn(_, vis, ast::Fn { sig, generics, .. }) => FnSig {
342342
decl,
343343
generics,
344344
ext: sig.header.ext,
@@ -750,11 +750,10 @@ impl<'a> FmtVisitor<'a> {
750750
(Type(lty), Type(rty))
751751
if both_type(&lty.ty, &rty.ty) || both_opaque(&lty.ty, &rty.ty) =>
752752
{
753-
a.ident.as_str().cmp(b.ident.as_str())
754-
}
755-
(Const(..), Const(..)) | (MacCall(..), MacCall(..)) => {
756-
a.ident.as_str().cmp(b.ident.as_str())
753+
lty.ident.as_str().cmp(rty.ident.as_str())
757754
}
755+
(Const(ca), Const(cb)) => ca.ident.as_str().cmp(cb.ident.as_str()),
756+
(MacCall(..), MacCall(..)) => Ordering::Equal,
758757
(Fn(..), Fn(..)) | (Delegation(..), Delegation(..)) => {
759758
a.span.lo().cmp(&b.span.lo())
760759
}
@@ -1105,14 +1104,16 @@ impl<'a> StructParts<'a> {
11051104
}
11061105

11071106
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
1108-
let (prefix, def, generics) = match item.kind {
1109-
ast::ItemKind::Struct(ref def, ref generics) => ("struct ", def, generics),
1110-
ast::ItemKind::Union(ref def, ref generics) => ("union ", def, generics),
1107+
let (prefix, def, ident, generics) = match item.kind {
1108+
ast::ItemKind::Struct(ident, ref def, ref generics) => {
1109+
("struct ", def, ident, generics)
1110+
}
1111+
ast::ItemKind::Union(ident, ref def, ref generics) => ("union ", def, ident, generics),
11111112
_ => unreachable!(),
11121113
};
11131114
StructParts {
11141115
prefix,
1115-
ident: item.ident,
1116+
ident,
11161117
vis: &item.vis,
11171118
def,
11181119
generics: Some(generics),
@@ -1168,6 +1169,7 @@ pub(crate) fn format_trait(
11681169
let ast::Trait {
11691170
is_auto,
11701171
safety,
1172+
ident,
11711173
ref generics,
11721174
ref bounds,
11731175
ref items,
@@ -1186,13 +1188,13 @@ pub(crate) fn format_trait(
11861188

11871189
let shape = Shape::indented(offset, context.config).offset_left(result.len())?;
11881190
let generics_str =
1189-
rewrite_generics(context, rewrite_ident(context, item.ident), generics, shape).ok()?;
1191+
rewrite_generics(context, rewrite_ident(context, ident), generics, shape).ok()?;
11901192
result.push_str(&generics_str);
11911193

11921194
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
11931195
if !bounds.is_empty() {
11941196
// Retrieve *unnormalized* ident (See #6069)
1195-
let source_ident = context.snippet(item.ident.span);
1197+
let source_ident = context.snippet(ident.span);
11961198
let ident_hi = context.snippet_provider.span_after(item.span, source_ident);
11971199
let bound_hi = bounds.last().unwrap().span().hi();
11981200
let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
@@ -1699,7 +1701,6 @@ struct TyAliasRewriteInfo<'c, 'g>(
16991701
pub(crate) fn rewrite_type_alias<'a>(
17001702
ty_alias_kind: &ast::TyAlias,
17011703
vis: &ast::Visibility,
1702-
ident: Ident,
17031704
context: &RewriteContext<'a>,
17041705
indent: Indent,
17051706
visitor_kind: ItemVisitorKind,
@@ -1709,6 +1710,7 @@ pub(crate) fn rewrite_type_alias<'a>(
17091710

17101711
let ast::TyAlias {
17111712
defaultness,
1713+
ident,
17121714
ref generics,
17131715
ref bounds,
17141716
ref ty,
@@ -2022,14 +2024,23 @@ pub(crate) struct StaticParts<'a> {
20222024

20232025
impl<'a> StaticParts<'a> {
20242026
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
2025-
let (defaultness, prefix, safety, ty, mutability, expr, generics) = match &item.kind {
2026-
ast::ItemKind::Static(s) => {
2027-
(None, "static", s.safety, &s.ty, s.mutability, &s.expr, None)
2028-
}
2027+
let (defaultness, prefix, safety, ident, ty, mutability, expr, generics) = match &item.kind
2028+
{
2029+
ast::ItemKind::Static(s) => (
2030+
None,
2031+
"static",
2032+
s.safety,
2033+
s.ident,
2034+
&s.ty,
2035+
s.mutability,
2036+
&s.expr,
2037+
None,
2038+
),
20292039
ast::ItemKind::Const(c) => (
20302040
Some(c.defaultness),
20312041
"const",
20322042
ast::Safety::Default,
2043+
c.ident,
20332044
&c.ty,
20342045
ast::Mutability::Not,
20352046
&c.expr,
@@ -2041,7 +2052,7 @@ impl<'a> StaticParts<'a> {
20412052
prefix,
20422053
safety,
20432054
vis: &item.vis,
2044-
ident: item.ident,
2055+
ident,
20452056
generics,
20462057
ty,
20472058
mutability,
@@ -2051,7 +2062,7 @@ impl<'a> StaticParts<'a> {
20512062
}
20522063
}
20532064

2054-
pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self {
2065+
pub(crate) fn from_trait_item(ti: &'a ast::AssocItem, ident: Ident) -> Self {
20552066
let (defaultness, ty, expr_opt, generics) = match &ti.kind {
20562067
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr, Some(&c.generics)),
20572068
_ => unreachable!(),
@@ -2060,7 +2071,7 @@ impl<'a> StaticParts<'a> {
20602071
prefix: "const",
20612072
safety: ast::Safety::Default,
20622073
vis: &ti.vis,
2063-
ident: ti.ident,
2074+
ident,
20642075
generics,
20652076
ty,
20662077
mutability: ast::Mutability::Not,
@@ -2070,7 +2081,7 @@ impl<'a> StaticParts<'a> {
20702081
}
20712082
}
20722083

2073-
pub(crate) fn from_impl_item(ii: &'a ast::AssocItem) -> Self {
2084+
pub(crate) fn from_impl_item(ii: &'a ast::AssocItem, ident: Ident) -> Self {
20742085
let (defaultness, ty, expr, generics) = match &ii.kind {
20752086
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr, Some(&c.generics)),
20762087
_ => unreachable!(),
@@ -2079,7 +2090,7 @@ impl<'a> StaticParts<'a> {
20792090
prefix: "const",
20802091
safety: ast::Safety::Default,
20812092
vis: &ii.vis,
2082-
ident: ii.ident,
2093+
ident,
20832094
generics,
20842095
ty,
20852096
mutability: ast::Mutability::Not,
@@ -3440,6 +3451,7 @@ impl Rewrite for ast::ForeignItem {
34403451
let ast::Fn {
34413452
defaultness,
34423453
ref sig,
3454+
ident,
34433455
ref generics,
34443456
ref body,
34453457
..
@@ -3451,7 +3463,8 @@ impl Rewrite for ast::ForeignItem {
34513463
let inner_attrs = inner_attributes(&self.attrs);
34523464
let fn_ctxt = visit::FnCtxt::Foreign;
34533465
visitor.visit_fn(
3454-
visit::FnKind::Fn(fn_ctxt, &self.ident, &self.vis, fn_kind),
3466+
ident,
3467+
visit::FnKind::Fn(fn_ctxt, &self.vis, fn_kind),
34553468
&sig.decl,
34563469
self.span,
34573470
defaultness,
@@ -3462,7 +3475,7 @@ impl Rewrite for ast::ForeignItem {
34623475
rewrite_fn_base(
34633476
context,
34643477
shape.indent,
3465-
self.ident,
3478+
ident,
34663479
&FnSig::from_method_sig(sig, generics, &self.vis),
34673480
span,
34683481
FnBraceStyle::None,
@@ -3481,7 +3494,7 @@ impl Rewrite for ast::ForeignItem {
34813494
vis,
34823495
safety,
34833496
mut_str,
3484-
rewrite_ident(context, self.ident)
3497+
rewrite_ident(context, static_foreign_item.ident)
34853498
);
34863499
// 1 = ;
34873500
rewrite_assign_rhs(
@@ -3497,15 +3510,7 @@ impl Rewrite for ast::ForeignItem {
34973510
}
34983511
ast::ForeignItemKind::TyAlias(ref ty_alias) => {
34993512
let kind = ItemVisitorKind::ForeignItem;
3500-
rewrite_type_alias(
3501-
ty_alias,
3502-
&self.vis,
3503-
self.ident,
3504-
context,
3505-
shape.indent,
3506-
kind,
3507-
self.span,
3508-
)
3513+
rewrite_type_alias(ty_alias, &self.vis, context, shape.indent, kind, self.span)
35093514
}
35103515
ast::ForeignItemKind::MacCall(ref mac) => {
35113516
rewrite_macro(mac, context, shape, MacroPosition::Item)
@@ -3568,12 +3573,13 @@ fn rewrite_attrs(
35683573
pub(crate) fn rewrite_mod(
35693574
context: &RewriteContext<'_>,
35703575
item: &ast::Item,
3576+
ident: Ident,
35713577
attrs_shape: Shape,
35723578
) -> Option<String> {
35733579
let mut result = String::with_capacity(32);
35743580
result.push_str(&*format_visibility(context, &item.vis));
35753581
result.push_str("mod ");
3576-
result.push_str(rewrite_ident(context, item.ident));
3582+
result.push_str(rewrite_ident(context, ident));
35773583
result.push(';');
35783584
rewrite_attrs(context, item, &result, attrs_shape)
35793585
}
@@ -3600,7 +3606,7 @@ pub(crate) fn rewrite_extern_crate(
36003606
pub(crate) fn is_mod_decl(item: &ast::Item) -> bool {
36013607
!matches!(
36023608
item.kind,
3603-
ast::ItemKind::Mod(_, ast::ModKind::Loaded(_, ast::Inline::Yes, _, _))
3609+
ast::ItemKind::Mod(_, _, ast::ModKind::Loaded(_, ast::Inline::Yes, _, _))
36043610
)
36053611
}
36063612

src/modules.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> {
152152
let mut visitor = visitor::CfgIfVisitor::new(self.psess);
153153
visitor.visit_item(&item);
154154
for module_item in visitor.mods() {
155-
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = module_item.item.kind {
155+
if let ast::ItemKind::Mod(_, _, ref sub_mod_kind) = module_item.item.kind {
156156
self.visit_sub_mod(
157157
&module_item.item,
158158
Module::new(
@@ -178,7 +178,7 @@ impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> {
178178
continue;
179179
}
180180

181-
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = item.kind {
181+
if let ast::ItemKind::Mod(_, _, ref sub_mod_kind) = item.kind {
182182
let span = item.span;
183183
self.visit_sub_mod(
184184
&item,
@@ -204,7 +204,7 @@ impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> {
204204
self.visit_cfg_if(Cow::Borrowed(item))?;
205205
}
206206

207-
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = item.kind {
207+
if let ast::ItemKind::Mod(_, _, ref sub_mod_kind) = item.kind {
208208
let span = item.span;
209209
self.visit_sub_mod(
210210
item,
@@ -248,7 +248,7 @@ impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> {
248248
if is_mod_decl(item) {
249249
// mod foo;
250250
// Look for an extern file.
251-
self.find_external_module(item.ident, &item.attrs, sub_mod)
251+
self.find_external_module(item.kind.ident().unwrap(), &item.attrs, sub_mod)
252252
} else {
253253
// An internal module (`mod foo { /* ... */ }`);
254254
Ok(Some(SubModKind::Internal(item)))
@@ -291,7 +291,7 @@ impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> {
291291
self.visit_sub_mod_after_directory_update(sub_mod, Some(directory))
292292
}
293293
SubModKind::Internal(item) => {
294-
self.push_inline_mod_directory(item.ident, &item.attrs);
294+
self.push_inline_mod_directory(item.kind.ident().unwrap(), &item.attrs);
295295
self.visit_sub_mod_after_directory_update(sub_mod, None)
296296
}
297297
SubModKind::MultiExternal(mods) => {

src/reorder.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ use crate::visitor::FmtVisitor;
2525
/// Choose the ordering between the given two items.
2626
fn compare_items(a: &ast::Item, b: &ast::Item) -> Ordering {
2727
match (&a.kind, &b.kind) {
28-
(&ast::ItemKind::Mod(..), &ast::ItemKind::Mod(..)) => {
29-
a.ident.as_str().cmp(b.ident.as_str())
28+
(&ast::ItemKind::Mod(_, a_ident, _), &ast::ItemKind::Mod(_, b_ident, _)) => {
29+
a_ident.as_str().cmp(b_ident.as_str())
3030
}
31-
(&ast::ItemKind::ExternCrate(ref a_name), &ast::ItemKind::ExternCrate(ref b_name)) => {
31+
(
32+
&ast::ItemKind::ExternCrate(ref a_name, a_ident),
33+
&ast::ItemKind::ExternCrate(ref b_name, b_ident),
34+
) => {
3235
// `extern crate foo as bar;`
3336
// ^^^ Comparing this.
34-
let a_orig_name = a_name.unwrap_or(a.ident.name);
35-
let b_orig_name = b_name.unwrap_or(b.ident.name);
37+
let a_orig_name = a_name.unwrap_or(a_ident.name);
38+
let b_orig_name = b_name.unwrap_or(b_ident.name);
3639
let result = a_orig_name.as_str().cmp(b_orig_name.as_str());
3740
if result != Ordering::Equal {
3841
return result;
@@ -44,7 +47,7 @@ fn compare_items(a: &ast::Item, b: &ast::Item) -> Ordering {
4447
(Some(..), None) => Ordering::Greater,
4548
(None, Some(..)) => Ordering::Less,
4649
(None, None) => Ordering::Equal,
47-
(Some(..), Some(..)) => a.ident.as_str().cmp(b.ident.as_str()),
50+
(Some(..), Some(..)) => a_ident.as_str().cmp(b_ident.as_str()),
4851
}
4952
}
5053
_ => unreachable!(),
@@ -69,7 +72,7 @@ fn rewrite_reorderable_item(
6972
) -> Option<String> {
7073
match item.kind {
7174
ast::ItemKind::ExternCrate(..) => rewrite_extern_crate(context, item, shape),
72-
ast::ItemKind::Mod(..) => rewrite_mod(context, item, shape),
75+
ast::ItemKind::Mod(_, ident, _) => rewrite_mod(context, item, ident, shape),
7376
_ => None,
7477
}
7578
}

0 commit comments

Comments
 (0)