Skip to content

Commit e491d2b

Browse files
committed
Auto merge of #105291 - nnethercote:remove-LintPassObjects, r=cjgillot
Remove `{Early,Late}LintPassObjects`. `EarlyContextAndPass` wraps a single early lint pass. We aggregate multiple passes into that single pass by using `EarlyLintPassObjects`. This commit removes `EarlyLintPassObjects` by changing `EarlyContextAndPass` into `EarlyContextAndPasses`. I.e. it just removes a level of indirection. This makes the code simpler and slightly faster. The commit does likewise for late lints. r? `@cjgillot`
2 parents d43674e + 8980d9a commit e491d2b

File tree

2 files changed

+51
-100
lines changed

2 files changed

+51
-100
lines changed

compiler/rustc_lint/src/early.rs

+42-65
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,23 @@ use rustc_ast::ptr::P;
2020
use rustc_ast::visit::{self as ast_visit, Visitor};
2121
use rustc_ast::{self as ast, walk_list, HasAttrs};
2222
use rustc_middle::ty::RegisteredTools;
23-
use rustc_session::lint::{BufferedEarlyLint, LintBuffer, LintPass};
23+
use rustc_session::lint::{BufferedEarlyLint, LintBuffer};
2424
use rustc_session::Session;
2525
use rustc_span::symbol::Ident;
2626
use rustc_span::Span;
2727

28-
macro_rules! run_early_pass { ($cx:expr, $f:ident, $($args:expr),*) => ({
29-
$cx.pass.$f(&$cx.context, $($args),*);
28+
macro_rules! run_early_passes { ($cx:expr, $f:ident, $($args:expr),*) => ({
29+
for pass in $cx.passes.iter_mut() {
30+
pass.$f(&$cx.context, $($args),*);
31+
}
3032
}) }
3133

32-
pub struct EarlyContextAndPass<'a, T: EarlyLintPass> {
34+
pub struct EarlyContextAndPasses<'a> {
3335
context: EarlyContext<'a>,
34-
pass: T,
36+
passes: Vec<EarlyLintPassObject>,
3537
}
3638

37-
impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> {
39+
impl<'a> EarlyContextAndPasses<'a> {
3840
fn check_id(&mut self, id: ast::NodeId) {
3941
for early_lint in self.context.buffered.take(id) {
4042
let BufferedEarlyLint { span, msg, node_id: _, lint_id, diagnostic } = early_lint;
@@ -61,27 +63,27 @@ impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> {
6163

6264
self.check_id(id);
6365
debug!("early context: enter_attrs({:?})", attrs);
64-
run_early_pass!(self, enter_lint_attrs, attrs);
66+
run_early_passes!(self, enter_lint_attrs, attrs);
6567
f(self);
6668
debug!("early context: exit_attrs({:?})", attrs);
67-
run_early_pass!(self, exit_lint_attrs, attrs);
69+
run_early_passes!(self, exit_lint_attrs, attrs);
6870
self.context.builder.pop(push);
6971
}
7072
}
7173

72-
impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> {
74+
impl<'a> ast_visit::Visitor<'a> for EarlyContextAndPasses<'a> {
7375
fn visit_param(&mut self, param: &'a ast::Param) {
7476
self.with_lint_attrs(param.id, &param.attrs, |cx| {
75-
run_early_pass!(cx, check_param, param);
77+
run_early_passes!(cx, check_param, param);
7678
ast_visit::walk_param(cx, param);
7779
});
7880
}
7981

8082
fn visit_item(&mut self, it: &'a ast::Item) {
8183
self.with_lint_attrs(it.id, &it.attrs, |cx| {
82-
run_early_pass!(cx, check_item, it);
84+
run_early_passes!(cx, check_item, it);
8385
ast_visit::walk_item(cx, it);
84-
run_early_pass!(cx, check_item_post, it);
86+
run_early_passes!(cx, check_item_post, it);
8587
})
8688
}
8789

@@ -92,10 +94,10 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
9294
}
9395

9496
fn visit_pat(&mut self, p: &'a ast::Pat) {
95-
run_early_pass!(self, check_pat, p);
97+
run_early_passes!(self, check_pat, p);
9698
self.check_id(p.id);
9799
ast_visit::walk_pat(self, p);
98-
run_early_pass!(self, check_pat_post, p);
100+
run_early_passes!(self, check_pat_post, p);
99101
}
100102

101103
fn visit_pat_field(&mut self, field: &'a ast::PatField) {
@@ -111,7 +113,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
111113

112114
fn visit_expr(&mut self, e: &'a ast::Expr) {
113115
self.with_lint_attrs(e.id, &e.attrs, |cx| {
114-
run_early_pass!(cx, check_expr, e);
116+
run_early_passes!(cx, check_expr, e);
115117
ast_visit::walk_expr(cx, e);
116118
})
117119
}
@@ -132,7 +134,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
132134
// Note that statements get their attributes from
133135
// the AST struct that they wrap (e.g. an item)
134136
self.with_lint_attrs(s.id, s.attrs(), |cx| {
135-
run_early_pass!(cx, check_stmt, s);
137+
run_early_passes!(cx, check_stmt, s);
136138
cx.check_id(s.id);
137139
});
138140
// The visitor for the AST struct wrapped
@@ -143,7 +145,7 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
143145
}
144146

145147
fn visit_fn(&mut self, fk: ast_visit::FnKind<'a>, span: Span, id: ast::NodeId) {
146-
run_early_pass!(self, check_fn, fk, span, id);
148+
run_early_passes!(self, check_fn, fk, span, id);
147149
self.check_id(id);
148150
ast_visit::walk_fn(self, fk);
149151

@@ -171,37 +173,37 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
171173

172174
fn visit_variant(&mut self, v: &'a ast::Variant) {
173175
self.with_lint_attrs(v.id, &v.attrs, |cx| {
174-
run_early_pass!(cx, check_variant, v);
176+
run_early_passes!(cx, check_variant, v);
175177
ast_visit::walk_variant(cx, v);
176178
})
177179
}
178180

179181
fn visit_ty(&mut self, t: &'a ast::Ty) {
180-
run_early_pass!(self, check_ty, t);
182+
run_early_passes!(self, check_ty, t);
181183
self.check_id(t.id);
182184
ast_visit::walk_ty(self, t);
183185
}
184186

185187
fn visit_ident(&mut self, ident: Ident) {
186-
run_early_pass!(self, check_ident, ident);
188+
run_early_passes!(self, check_ident, ident);
187189
}
188190

189191
fn visit_local(&mut self, l: &'a ast::Local) {
190192
self.with_lint_attrs(l.id, &l.attrs, |cx| {
191-
run_early_pass!(cx, check_local, l);
193+
run_early_passes!(cx, check_local, l);
192194
ast_visit::walk_local(cx, l);
193195
})
194196
}
195197

196198
fn visit_block(&mut self, b: &'a ast::Block) {
197-
run_early_pass!(self, check_block, b);
199+
run_early_passes!(self, check_block, b);
198200
self.check_id(b.id);
199201
ast_visit::walk_block(self, b);
200202
}
201203

202204
fn visit_arm(&mut self, a: &'a ast::Arm) {
203205
self.with_lint_attrs(a.id, &a.attrs, |cx| {
204-
run_early_pass!(cx, check_arm, a);
206+
run_early_passes!(cx, check_arm, a);
205207
ast_visit::walk_arm(cx, a);
206208
})
207209
}
@@ -220,19 +222,19 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
220222
}
221223

222224
fn visit_generic_arg(&mut self, arg: &'a ast::GenericArg) {
223-
run_early_pass!(self, check_generic_arg, arg);
225+
run_early_passes!(self, check_generic_arg, arg);
224226
ast_visit::walk_generic_arg(self, arg);
225227
}
226228

227229
fn visit_generic_param(&mut self, param: &'a ast::GenericParam) {
228230
self.with_lint_attrs(param.id, &param.attrs, |cx| {
229-
run_early_pass!(cx, check_generic_param, param);
231+
run_early_passes!(cx, check_generic_param, param);
230232
ast_visit::walk_generic_param(cx, param);
231233
});
232234
}
233235

234236
fn visit_generics(&mut self, g: &'a ast::Generics) {
235-
run_early_pass!(self, check_generics, g);
237+
run_early_passes!(self, check_generics, g);
236238
ast_visit::walk_generics(self, g);
237239
}
238240

@@ -241,18 +243,18 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
241243
}
242244

243245
fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef) {
244-
run_early_pass!(self, check_poly_trait_ref, t);
246+
run_early_passes!(self, check_poly_trait_ref, t);
245247
ast_visit::walk_poly_trait_ref(self, t);
246248
}
247249

248250
fn visit_assoc_item(&mut self, item: &'a ast::AssocItem, ctxt: ast_visit::AssocCtxt) {
249251
self.with_lint_attrs(item.id, &item.attrs, |cx| match ctxt {
250252
ast_visit::AssocCtxt::Trait => {
251-
run_early_pass!(cx, check_trait_item, item);
253+
run_early_passes!(cx, check_trait_item, item);
252254
ast_visit::walk_assoc_item(cx, item, ctxt);
253255
}
254256
ast_visit::AssocCtxt::Impl => {
255-
run_early_pass!(cx, check_impl_item, item);
257+
run_early_passes!(cx, check_impl_item, item);
256258
ast_visit::walk_assoc_item(cx, item, ctxt);
257259
}
258260
});
@@ -273,53 +275,28 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
273275
}
274276

275277
fn visit_attribute(&mut self, attr: &'a ast::Attribute) {
276-
run_early_pass!(self, check_attribute, attr);
278+
run_early_passes!(self, check_attribute, attr);
277279
}
278280

279281
fn visit_mac_def(&mut self, mac: &'a ast::MacroDef, id: ast::NodeId) {
280-
run_early_pass!(self, check_mac_def, mac);
282+
run_early_passes!(self, check_mac_def, mac);
281283
self.check_id(id);
282284
}
283285

284286
fn visit_mac_call(&mut self, mac: &'a ast::MacCall) {
285-
run_early_pass!(self, check_mac, mac);
287+
run_early_passes!(self, check_mac, mac);
286288
ast_visit::walk_mac(self, mac);
287289
}
288290
}
289291

290-
struct EarlyLintPassObjects<'a> {
291-
lints: &'a mut [EarlyLintPassObject],
292-
}
293-
294-
#[allow(rustc::lint_pass_impl_without_macro)]
295-
impl LintPass for EarlyLintPassObjects<'_> {
296-
fn name(&self) -> &'static str {
297-
panic!()
298-
}
299-
}
300-
301-
macro_rules! early_lint_pass_impl {
302-
([], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
303-
impl EarlyLintPass for EarlyLintPassObjects<'_> {
304-
$(fn $name(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
305-
for obj in self.lints.iter_mut() {
306-
obj.$name(context, $($param),*);
307-
}
308-
})*
309-
}
310-
)
311-
}
312-
313-
crate::early_lint_methods!(early_lint_pass_impl, []);
314-
315292
/// Early lints work on different nodes - either on the crate root, or on freshly loaded modules.
316293
/// This trait generalizes over those nodes.
317294
pub trait EarlyCheckNode<'a>: Copy {
318295
fn id(self) -> ast::NodeId;
319296
fn attrs<'b>(self) -> &'b [ast::Attribute]
320297
where
321298
'a: 'b;
322-
fn check<'b>(self, cx: &mut EarlyContextAndPass<'b, impl EarlyLintPass>)
299+
fn check<'b>(self, cx: &mut EarlyContextAndPasses<'b>)
323300
where
324301
'a: 'b;
325302
}
@@ -334,13 +311,13 @@ impl<'a> EarlyCheckNode<'a> for &'a ast::Crate {
334311
{
335312
&self.attrs
336313
}
337-
fn check<'b>(self, cx: &mut EarlyContextAndPass<'b, impl EarlyLintPass>)
314+
fn check<'b>(self, cx: &mut EarlyContextAndPasses<'b>)
338315
where
339316
'a: 'b,
340317
{
341-
run_early_pass!(cx, check_crate, self);
318+
run_early_passes!(cx, check_crate, self);
342319
ast_visit::walk_crate(cx, self);
343-
run_early_pass!(cx, check_crate_post, self);
320+
run_early_passes!(cx, check_crate_post, self);
344321
}
345322
}
346323

@@ -354,7 +331,7 @@ impl<'a> EarlyCheckNode<'a> for (ast::NodeId, &'a [ast::Attribute], &'a [P<ast::
354331
{
355332
self.1
356333
}
357-
fn check<'b>(self, cx: &mut EarlyContextAndPass<'b, impl EarlyLintPass>)
334+
fn check<'b>(self, cx: &mut EarlyContextAndPasses<'b>)
358335
where
359336
'a: 'b,
360337
{
@@ -374,18 +351,18 @@ pub fn check_ast_node<'a>(
374351
) {
375352
let passes =
376353
if pre_expansion { &lint_store.pre_expansion_passes } else { &lint_store.early_passes };
377-
let mut passes: Vec<_> = passes.iter().map(|p| (p)()).collect();
354+
let mut passes: Vec<EarlyLintPassObject> = passes.iter().map(|p| (p)()).collect();
378355
passes.push(Box::new(builtin_lints));
379356

380-
let mut cx = EarlyContextAndPass {
357+
let mut cx = EarlyContextAndPasses {
381358
context: EarlyContext::new(
382359
sess,
383360
!pre_expansion,
384361
lint_store,
385362
registered_tools,
386363
lint_buffer.unwrap_or_default(),
387364
),
388-
pass: EarlyLintPassObjects { lints: &mut passes[..] },
365+
passes,
389366
};
390367
cx.with_lint_attrs(check_node.id(), check_node.attrs(), |cx| check_node.check(cx));
391368

compiler/rustc_lint/src/late.rs

+9-35
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_hir::intravisit as hir_visit;
2323
use rustc_hir::intravisit::Visitor;
2424
use rustc_middle::hir::nested_filter;
2525
use rustc_middle::ty::{self, TyCtxt};
26-
use rustc_session::lint::LintPass;
2726
use rustc_span::Span;
2827

2928
use std::any::Any;
@@ -37,15 +36,17 @@ pub fn unerased_lint_store(tcx: TyCtxt<'_>) -> &LintStore {
3736
}
3837

3938
macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({
40-
$cx.pass.$f(&$cx.context, $($args),*);
39+
for pass in $cx.passes.iter_mut() {
40+
pass.$f(&$cx.context, $($args),*);
41+
}
4142
}) }
4243

43-
struct LateContextAndPass<'tcx, T: LateLintPass<'tcx>> {
44+
struct LateContextAndPasses<'tcx> {
4445
context: LateContext<'tcx>,
45-
pass: T,
46+
passes: Vec<LateLintPassObject<'tcx>>,
4647
}
4748

48-
impl<'tcx, T: LateLintPass<'tcx>> LateContextAndPass<'tcx, T> {
49+
impl<'tcx> LateContextAndPasses<'tcx> {
4950
/// Merge the lints specified by any lint attributes into the
5051
/// current lint context, call the provided function, then reset the
5152
/// lints in effect to their previous state.
@@ -81,7 +82,7 @@ impl<'tcx, T: LateLintPass<'tcx>> LateContextAndPass<'tcx, T> {
8182
}
8283
}
8384

84-
impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPass<'tcx, T> {
85+
impl<'tcx> hir_visit::Visitor<'tcx> for LateContextAndPasses<'tcx> {
8586
type NestedFilter = nested_filter::All;
8687

8788
/// Because lints are scoped lexically, we want to walk nested
@@ -301,31 +302,6 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
301302
}
302303
}
303304

304-
struct LateLintPassObjects<'a, 'tcx> {
305-
lints: &'a mut [LateLintPassObject<'tcx>],
306-
}
307-
308-
#[allow(rustc::lint_pass_impl_without_macro)]
309-
impl LintPass for LateLintPassObjects<'_, '_> {
310-
fn name(&self) -> &'static str {
311-
panic!()
312-
}
313-
}
314-
315-
macro_rules! late_lint_pass_impl {
316-
([], [$hir:tt], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => {
317-
impl<$hir> LateLintPass<$hir> for LateLintPassObjects<'_, $hir> {
318-
$(fn $name(&mut self, context: &LateContext<$hir>, $($param: $arg),*) {
319-
for obj in self.lints.iter_mut() {
320-
obj.$name(context, $($param),*);
321-
}
322-
})*
323-
}
324-
};
325-
}
326-
327-
crate::late_lint_methods!(late_lint_pass_impl, [], ['tcx]);
328-
329305
pub(super) fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
330306
tcx: TyCtxt<'tcx>,
331307
module_def_id: LocalDefId,
@@ -346,9 +322,8 @@ pub(super) fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
346322
let mut passes: Vec<_> =
347323
unerased_lint_store(tcx).late_module_passes.iter().map(|pass| (pass)(tcx)).collect();
348324
passes.push(Box::new(builtin_lints));
349-
let pass = LateLintPassObjects { lints: &mut passes[..] };
350325

351-
let mut cx = LateContextAndPass { context, pass };
326+
let mut cx = LateContextAndPasses { context, passes };
352327

353328
let (module, _span, hir_id) = tcx.hir().get_module(module_def_id);
354329
cx.process_mod(module, hir_id);
@@ -377,9 +352,8 @@ fn late_lint_crate<'tcx, T: LateLintPass<'tcx> + 'tcx>(tcx: TyCtxt<'tcx>, builti
377352
let mut passes =
378353
unerased_lint_store(tcx).late_passes.iter().map(|p| (p)(tcx)).collect::<Vec<_>>();
379354
passes.push(Box::new(builtin_lints));
380-
let pass = LateLintPassObjects { lints: &mut passes[..] };
381355

382-
let mut cx = LateContextAndPass { context, pass };
356+
let mut cx = LateContextAndPasses { context, passes };
383357

384358
// Visit the whole crate.
385359
cx.with_lint_attrs(hir::CRATE_HIR_ID, |cx| {

0 commit comments

Comments
 (0)