Skip to content

Commit 448a97b

Browse files
committed
Auto merge of rust-lang#81405 - bugadani:ast, r=cjgillot
Box the biggest ast::ItemKind variants This PR is a different approach on rust-lang#81400, aiming to save memory in humongous ASTs. The three affected item kind enums are: - `ast::ItemKind` (208 -> 112 bytes) - `ast::AssocItemKind` (176 -> 72 bytes) - `ast::ForeignItemKind` (176 -> 72 bytes)
2 parents 9607b5c + 6fd01e0 commit 448a97b

File tree

5 files changed

+94
-54
lines changed

5 files changed

+94
-54
lines changed

clippy_lints/src/doc.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utils::{implements_trait, is_entrypoint_fn, is_type_diagnostic_item, return_ty, span_lint};
22
use if_chain::if_chain;
33
use itertools::Itertools;
4-
use rustc_ast::ast::{Async, AttrKind, Attribute, FnRetTy, ItemKind};
4+
use rustc_ast::ast::{Async, AttrKind, Attribute, FnKind, FnRetTy, ItemKind};
55
use rustc_ast::token::CommentKind;
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_data_structures::sync::Lrc;
@@ -492,7 +492,9 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
492492
| ItemKind::ExternCrate(..)
493493
| ItemKind::ForeignMod(..) => return false,
494494
// We found a main function ...
495-
ItemKind::Fn(_, sig, _, Some(block)) if item.ident.name == sym::main => {
495+
ItemKind::Fn(box FnKind(_, sig, _, Some(block)))
496+
if item.ident.name == sym::main =>
497+
{
496498
let is_async = matches!(sig.header.asyncness, Async::Yes { .. });
497499
let returns_nothing = match &sig.decl.output {
498500
FnRetTy::Default(..) => true,

clippy_lints/src/excessive_bools.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::utils::{attr_by_name, in_macro, match_path_ast, span_lint_and_help};
2-
use rustc_ast::ast::{AssocItemKind, Extern, FnSig, Item, ItemKind, Ty, TyKind};
2+
use rustc_ast::ast::{
3+
AssocItemKind, Extern, FnKind, FnSig, ImplKind, Item, ItemKind, TraitKind, Ty, TyKind,
4+
};
35
use rustc_lint::{EarlyContext, EarlyLintPass};
46
use rustc_session::{declare_tool_lint, impl_lint_pass};
57
use rustc_span::Span;
@@ -158,18 +160,16 @@ impl EarlyLintPass for ExcessiveBools {
158160
"consider using a state machine or refactoring bools into two-variant enums",
159161
);
160162
}
161-
},
162-
ItemKind::Impl {
163-
of_trait: None, items, ..
164163
}
165-
| ItemKind::Trait(_, _, _, _, items) => {
164+
ItemKind::Impl(box ImplKind { of_trait: None, items, .. })
165+
| ItemKind::Trait(box TraitKind(.., items)) => {
166166
for item in items {
167-
if let AssocItemKind::Fn(_, fn_sig, _, _) = &item.kind {
167+
if let AssocItemKind::Fn(box FnKind(_, fn_sig, _, _)) = &item.kind {
168168
self.check_fn_sig(cx, fn_sig, item.span);
169169
}
170170
}
171-
},
172-
ItemKind::Fn(_, fn_sig, _, _) => self.check_fn_sig(cx, fn_sig, item.span),
171+
}
172+
ItemKind::Fn(box FnKind(_, fn_sig, _, _)) => self.check_fn_sig(cx, fn_sig, item.span),
173173
_ => (),
174174
}
175175
}

clippy_lints/src/non_expressive_names.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::utils::{span_lint, span_lint_and_then};
2-
use rustc_ast::ast::{Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, Item, ItemKind, Local, Pat, PatKind};
2+
use rustc_ast::ast::{
3+
Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, FnKind, Item, ItemKind, Local, Pat,
4+
PatKind,
5+
};
36
use rustc_ast::visit::{walk_block, walk_expr, walk_pat, Visitor};
47
use rustc_lint::{EarlyContext, EarlyLintPass};
58
use rustc_middle::lint::in_external_macro;
@@ -364,7 +367,7 @@ impl EarlyLintPass for NonExpressiveNames {
364367
return;
365368
}
366369

367-
if let ItemKind::Fn(_, ref sig, _, Some(ref blk)) = item.kind {
370+
if let ItemKind::Fn(box FnKind(_, ref sig, _, Some(ref blk))) = item.kind {
368371
do_check(self, cx, &item.attrs, &sig.decl, blk);
369372
}
370373
}
@@ -374,7 +377,7 @@ impl EarlyLintPass for NonExpressiveNames {
374377
return;
375378
}
376379

377-
if let AssocItemKind::Fn(_, ref sig, _, Some(ref blk)) = item.kind {
380+
if let AssocItemKind::Fn(box FnKind(_, ref sig, _, Some(ref blk))) = item.kind {
378381
do_check(self, cx, &item.attrs, &sig.decl, blk);
379382
}
380383
}

clippy_lints/src/utils/ast_utils.rs

+43-24
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,26 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
229229
match (l, r) {
230230
(ExternCrate(l), ExternCrate(r)) => l == r,
231231
(Use(l), Use(r)) => eq_use_tree(l, r),
232-
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
233-
(Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
234-
(Fn(ld, lf, lg, lb), Fn(rd, rf, rg, rb)) => {
235-
eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
236-
},
237-
(Mod(l), Mod(r)) => l.inline == r.inline && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_item_kind)),
232+
(Static(lt, lm, le), Static(rt, rm, re)) => {
233+
lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re)
234+
}
235+
(Const(ld, lt, le), Const(rd, rt, re)) => {
236+
eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re)
237+
}
238+
(Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
239+
eq_defaultness(*ld, *rd)
240+
&& eq_fn_sig(lf, rf)
241+
&& eq_generics(lg, rg)
242+
&& both(lb, rb, |l, r| eq_block(l, r))
243+
}
244+
(Mod(l), Mod(r)) => {
245+
l.inline == r.inline && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_item_kind))
246+
}
238247
(ForeignMod(l), ForeignMod(r)) => {
239248
both(&l.abi, &r.abi, |l, r| eq_str_lit(l, r))
240249
&& over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
241-
},
242-
(TyAlias(ld, lg, lb, lt), TyAlias(rd, rg, rb, rt)) => {
250+
}
251+
(TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
243252
eq_defaultness(*ld, *rd)
244253
&& eq_generics(lg, rg)
245254
&& over(lb, rb, |l, r| eq_generic_bound(l, r))
@@ -250,8 +259,8 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
250259
},
251260
(Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
252261
eq_variant_data(lv, rv) && eq_generics(lg, rg)
253-
},
254-
(Trait(la, lu, lg, lb, li), Trait(ra, ru, rg, rb, ri)) => {
262+
}
263+
(Trait(box TraitKind(la, lu, lg, lb, li)), Trait(box TraitKind(ra, ru, rg, rb, ri))) => {
255264
la == ra
256265
&& matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
257266
&& eq_generics(lg, rg)
@@ -260,7 +269,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
260269
},
261270
(TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, |l, r| eq_generic_bound(l, r)),
262271
(
263-
Impl {
272+
Impl(box ImplKind {
264273
unsafety: lu,
265274
polarity: lp,
266275
defaultness: ld,
@@ -269,8 +278,8 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
269278
of_trait: lot,
270279
self_ty: lst,
271280
items: li,
272-
},
273-
Impl {
281+
}),
282+
Impl(box ImplKind {
274283
unsafety: ru,
275284
polarity: rp,
276285
defaultness: rd,
@@ -279,7 +288,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
279288
of_trait: rot,
280289
self_ty: rst,
281290
items: ri,
282-
},
291+
}),
283292
) => {
284293
matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
285294
&& matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive)
@@ -299,11 +308,16 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
299308
pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
300309
use ForeignItemKind::*;
301310
match (l, r) {
302-
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
303-
(Fn(ld, lf, lg, lb), Fn(rd, rf, rg, rb)) => {
304-
eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
305-
},
306-
(TyAlias(ld, lg, lb, lt), TyAlias(rd, rg, rb, rt)) => {
311+
(Static(lt, lm, le), Static(rt, rm, re)) => {
312+
lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re)
313+
}
314+
(Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
315+
eq_defaultness(*ld, *rd)
316+
&& eq_fn_sig(lf, rf)
317+
&& eq_generics(lg, rg)
318+
&& both(lb, rb, |l, r| eq_block(l, r))
319+
}
320+
(TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
307321
eq_defaultness(*ld, *rd)
308322
&& eq_generics(lg, rg)
309323
&& over(lb, rb, |l, r| eq_generic_bound(l, r))
@@ -317,11 +331,16 @@ pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
317331
pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
318332
use AssocItemKind::*;
319333
match (l, r) {
320-
(Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
321-
(Fn(ld, lf, lg, lb), Fn(rd, rf, rg, rb)) => {
322-
eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
323-
},
324-
(TyAlias(ld, lg, lb, lt), TyAlias(rd, rg, rb, rt)) => {
334+
(Const(ld, lt, le), Const(rd, rt, re)) => {
335+
eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re)
336+
}
337+
(Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
338+
eq_defaultness(*ld, *rd)
339+
&& eq_fn_sig(lf, rf)
340+
&& eq_generics(lg, rg)
341+
&& both(lb, rb, |l, r| eq_block(l, r))
342+
}
343+
(TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
325344
eq_defaultness(*ld, *rd)
326345
&& eq_generics(lg, rg)
327346
&& over(lb, rb, |l, r| eq_generic_bound(l, r))

clippy_lints/src/write.rs

+33-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use std::ops::Range;
33

44
use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then};
55
use if_chain::if_chain;
6-
use rustc_ast::ast::{Expr, ExprKind, Item, ItemKind, LitKind, MacCall, StrLit, StrStyle};
6+
use rustc_ast::ast::{
7+
Expr, ExprKind, ImplKind, Item, ItemKind, LitKind, MacCall, StrLit, StrStyle,
8+
};
79
use rustc_ast::token;
810
use rustc_ast::tokenstream::TokenStream;
911
use rustc_errors::Applicability;
@@ -231,11 +233,7 @@ impl_lint_pass!(Write => [
231233

232234
impl EarlyLintPass for Write {
233235
fn check_item(&mut self, _: &EarlyContext<'_>, item: &Item) {
234-
if let ItemKind::Impl {
235-
of_trait: Some(trait_ref),
236-
..
237-
} = &item.kind
238-
{
236+
if let ItemKind::Impl(box ImplKind { of_trait: Some(trait_ref), .. }) = &item.kind {
239237
let trait_name = trait_ref
240238
.path
241239
.segments
@@ -377,10 +375,15 @@ impl Write {
377375
/// (Some("string to write: {}"), Some(buf))
378376
/// ```
379377
#[allow(clippy::too_many_lines)]
380-
fn check_tts<'a>(&self, cx: &EarlyContext<'a>, tts: TokenStream, is_write: bool) -> (Option<StrLit>, Option<Expr>) {
378+
fn check_tts<'a>(
379+
&self,
380+
cx: &EarlyContext<'a>,
381+
tts: TokenStream,
382+
is_write: bool,
383+
) -> (Option<StrLit>, Option<Expr>) {
381384
use rustc_parse_format::{
382-
AlignUnknown, ArgumentImplicitlyIs, ArgumentIs, ArgumentNamed, CountImplied, FormatSpec, ParseMode, Parser,
383-
Piece,
385+
AlignUnknown, ArgumentImplicitlyIs, ArgumentIs, ArgumentNamed, CountImplied,
386+
FormatSpec, ParseMode, Parser, Piece,
384387
};
385388

386389
let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, false, None);
@@ -410,7 +413,12 @@ impl Write {
410413
if let Piece::NextArgument(arg) = piece {
411414
if !self.in_debug_impl && arg.format.ty == "?" {
412415
// FIXME: modify rustc's fmt string parser to give us the current span
413-
span_lint(cx, USE_DEBUG, parser.prev_token.span, "use of `Debug`-based formatting");
416+
span_lint(
417+
cx,
418+
USE_DEBUG,
419+
parser.prev_token.span,
420+
"use of `Debug`-based formatting",
421+
);
414422
}
415423
args.push(arg);
416424
}
@@ -438,7 +446,9 @@ impl Write {
438446
return (Some(fmtstr), None);
439447
};
440448
match &token_expr.kind {
441-
ExprKind::Lit(lit) if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)) => {
449+
ExprKind::Lit(lit)
450+
if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)) =>
451+
{
442452
let mut all_simple = true;
443453
let mut seen = false;
444454
for arg in &args {
@@ -448,15 +458,15 @@ impl Write {
448458
all_simple &= arg.format == SIMPLE;
449459
seen = true;
450460
}
451-
},
452-
ArgumentNamed(_) => {},
461+
}
462+
ArgumentNamed(_) => {}
453463
}
454464
}
455465
if all_simple && seen {
456466
span_lint(cx, lint, token_expr.span, "literal with an empty format string");
457467
}
458468
idx += 1;
459-
},
469+
}
460470
ExprKind::Assign(lhs, rhs, _) => {
461471
if_chain! {
462472
if let ExprKind::Lit(ref lit) = rhs.kind;
@@ -481,7 +491,7 @@ impl Write {
481491
}
482492
}
483493
}
484-
},
494+
}
485495
_ => idx += 1,
486496
}
487497
}
@@ -513,11 +523,17 @@ impl Write {
513523
cx,
514524
PRINT_WITH_NEWLINE,
515525
mac.span(),
516-
&format!("using `{}!()` with a format string that ends in a single newline", name),
526+
&format!(
527+
"using `{}!()` with a format string that ends in a single newline",
528+
name
529+
),
517530
|err| {
518531
err.multipart_suggestion(
519532
&format!("use `{}!` instead", suggested),
520-
vec![(mac.path.span, suggested), (newline_span(&fmt_str), String::new())],
533+
vec![
534+
(mac.path.span, suggested),
535+
(newline_span(&fmt_str), String::new()),
536+
],
521537
Applicability::MachineApplicable,
522538
);
523539
},

0 commit comments

Comments
 (0)