Skip to content

Commit e960b5e

Browse files
committed
Auto merge of #104863 - nnethercote:reduce-lint-macros, r=cjgillot
Reduce macro usage for lints r? `@cjgillot`
2 parents cef44f5 + 406dace commit e960b5e

24 files changed

+255
-551
lines changed

compiler/rustc_driver/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,8 @@ fn run_compiler(
245245
interface::run_compiler(config, |compiler| {
246246
let sopts = &compiler.session().opts;
247247
if sopts.describe_lints {
248-
let mut lint_store = rustc_lint::new_lint_store(
249-
sopts.unstable_opts.no_interleave_lints,
250-
compiler.session().enable_internal_lints(),
251-
);
248+
let mut lint_store =
249+
rustc_lint::new_lint_store(compiler.session().enable_internal_lints());
252250
let registered_lints =
253251
if let Some(register_lints) = compiler.register_lints() {
254252
register_lints(compiler.session(), &mut lint_store);

compiler/rustc_interface/src/passes.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,7 @@ pub fn register_plugins<'a>(
207207
});
208208
}
209209

210-
let mut lint_store = rustc_lint::new_lint_store(
211-
sess.opts.unstable_opts.no_interleave_lints,
212-
sess.enable_internal_lints(),
213-
);
210+
let mut lint_store = rustc_lint::new_lint_store(sess.enable_internal_lints());
214211
register_lints(sess, &mut lint_store);
215212

216213
let registrars =

compiler/rustc_interface/src/tests.rs

-1
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,6 @@ fn test_unstable_options_tracking_hash() {
666666
untracked!(mir_pretty_relative_line_numbers, true);
667667
untracked!(nll_facts, true);
668668
untracked!(no_analysis, true);
669-
untracked!(no_interleave_lints, true);
670669
untracked!(no_leak_check, true);
671670
untracked!(no_parallel_llvm, true);
672671
untracked!(parse_only, true);

compiler/rustc_lint/src/early.rs

+17-76
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ use rustc_session::Session;
2525
use rustc_span::symbol::Ident;
2626
use rustc_span::Span;
2727

28-
use std::slice;
29-
3028
macro_rules! run_early_pass { ($cx:expr, $f:ident, $($args:expr),*) => ({
3129
$cx.pass.$f(&$cx.context, $($args),*);
3230
}) }
@@ -300,20 +298,14 @@ impl LintPass for EarlyLintPassObjects<'_> {
300298
}
301299
}
302300

303-
macro_rules! expand_early_lint_pass_impl_methods {
304-
([$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
305-
$(fn $name(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
306-
for obj in self.lints.iter_mut() {
307-
obj.$name(context, $($param),*);
308-
}
309-
})*
310-
)
311-
}
312-
313301
macro_rules! early_lint_pass_impl {
314-
([], [$($methods:tt)*]) => (
302+
([], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
315303
impl EarlyLintPass for EarlyLintPassObjects<'_> {
316-
expand_early_lint_pass_impl_methods!([$($methods)*]);
304+
$(fn $name(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
305+
for obj in self.lints.iter_mut() {
306+
obj.$name(context, $($param),*);
307+
}
308+
})*
317309
}
318310
)
319311
}
@@ -371,87 +363,36 @@ impl<'a> EarlyCheckNode<'a> for (ast::NodeId, &'a [ast::Attribute], &'a [P<ast::
371363
}
372364
}
373365

374-
fn early_lint_node<'a>(
375-
sess: &Session,
376-
warn_about_weird_lints: bool,
377-
lint_store: &LintStore,
378-
registered_tools: &RegisteredTools,
379-
buffered: LintBuffer,
380-
pass: impl EarlyLintPass,
381-
check_node: impl EarlyCheckNode<'a>,
382-
) -> LintBuffer {
383-
let mut cx = EarlyContextAndPass {
384-
context: EarlyContext::new(
385-
sess,
386-
warn_about_weird_lints,
387-
lint_store,
388-
registered_tools,
389-
buffered,
390-
),
391-
pass,
392-
};
393-
394-
cx.with_lint_attrs(check_node.id(), check_node.attrs(), |cx| check_node.check(cx));
395-
cx.context.buffered
396-
}
397-
398366
pub fn check_ast_node<'a>(
399367
sess: &Session,
400368
pre_expansion: bool,
401369
lint_store: &LintStore,
402370
registered_tools: &RegisteredTools,
403371
lint_buffer: Option<LintBuffer>,
404-
builtin_lints: impl EarlyLintPass,
372+
builtin_lints: impl EarlyLintPass + 'static,
405373
check_node: impl EarlyCheckNode<'a>,
406374
) {
407375
let passes =
408376
if pre_expansion { &lint_store.pre_expansion_passes } else { &lint_store.early_passes };
409377
let mut passes: Vec<_> = passes.iter().map(|p| (p)()).collect();
410-
let mut buffered = lint_buffer.unwrap_or_default();
411-
412-
if sess.opts.unstable_opts.no_interleave_lints {
413-
for (i, pass) in passes.iter_mut().enumerate() {
414-
buffered =
415-
sess.prof.verbose_generic_activity_with_arg("run_lint", pass.name()).run(|| {
416-
early_lint_node(
417-
sess,
418-
!pre_expansion && i == 0,
419-
lint_store,
420-
registered_tools,
421-
buffered,
422-
EarlyLintPassObjects { lints: slice::from_mut(pass) },
423-
check_node,
424-
)
425-
});
426-
}
427-
} else {
428-
buffered = early_lint_node(
378+
passes.push(Box::new(builtin_lints));
379+
380+
let mut cx = EarlyContextAndPass {
381+
context: EarlyContext::new(
429382
sess,
430383
!pre_expansion,
431384
lint_store,
432385
registered_tools,
433-
buffered,
434-
builtin_lints,
435-
check_node,
436-
);
437-
438-
if !passes.is_empty() {
439-
buffered = early_lint_node(
440-
sess,
441-
false,
442-
lint_store,
443-
registered_tools,
444-
buffered,
445-
EarlyLintPassObjects { lints: &mut passes[..] },
446-
check_node,
447-
);
448-
}
449-
}
386+
lint_buffer.unwrap_or_default(),
387+
),
388+
pass: EarlyLintPassObjects { lints: &mut passes[..] },
389+
};
390+
cx.with_lint_attrs(check_node.id(), check_node.attrs(), |cx| check_node.check(cx));
450391

451392
// All of the buffered lints should have been emitted at this point.
452393
// If not, that means that we somehow buffered a lint for a node id
453394
// that was not lint-checked (perhaps it doesn't exist?). This is a bug.
454-
for (id, lints) in buffered.map {
395+
for (id, lints) in cx.context.buffered.map {
455396
for early_lint in lints {
456397
sess.delay_span_bug(
457398
early_lint.span,

compiler/rustc_lint/src/late.rs

+23-77
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use rustc_span::Span;
2828

2929
use std::any::Any;
3030
use std::cell::Cell;
31-
use std::slice;
3231

3332
/// Extract the `LintStore` from the query context.
3433
/// This function exists because we've erased `LintStore` as `dyn Any` in the context.
@@ -313,45 +312,42 @@ impl LintPass for LateLintPassObjects<'_, '_> {
313312
}
314313
}
315314

316-
macro_rules! expand_late_lint_pass_impl_methods {
317-
([$hir:tt], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
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-
326315
macro_rules! late_lint_pass_impl {
327-
([], [$hir:tt], $methods:tt) => {
316+
([], [$hir:tt], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => {
328317
impl<$hir> LateLintPass<$hir> for LateLintPassObjects<'_, $hir> {
329-
expand_late_lint_pass_impl_methods!([$hir], $methods);
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+
})*
330323
}
331324
};
332325
}
333326

334327
crate::late_lint_methods!(late_lint_pass_impl, [], ['tcx]);
335328

336-
fn late_lint_mod_pass<'tcx, T: LateLintPass<'tcx>>(
329+
pub(super) fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
337330
tcx: TyCtxt<'tcx>,
338331
module_def_id: LocalDefId,
339-
pass: T,
332+
builtin_lints: T,
340333
) {
341-
let effective_visibilities = &tcx.effective_visibilities(());
342-
343334
let context = LateContext {
344335
tcx,
345336
enclosing_body: None,
346337
cached_typeck_results: Cell::new(None),
347338
param_env: ty::ParamEnv::empty(),
348-
effective_visibilities,
339+
effective_visibilities: &tcx.effective_visibilities(()),
349340
lint_store: unerased_lint_store(tcx),
350341
last_node_with_lint_attrs: tcx.hir().local_def_id_to_hir_id(module_def_id),
351342
generics: None,
352343
only_module: true,
353344
};
354345

346+
let mut passes: Vec<_> =
347+
unerased_lint_store(tcx).late_module_passes.iter().map(|pass| (pass)(tcx)).collect();
348+
passes.push(Box::new(builtin_lints));
349+
let pass = LateLintPassObjects { lints: &mut passes[..] };
350+
355351
let mut cx = LateContextAndPass { context, pass };
356352

357353
let (module, _span, hir_id) = tcx.hir().get_module(module_def_id);
@@ -365,46 +361,29 @@ fn late_lint_mod_pass<'tcx, T: LateLintPass<'tcx>>(
365361
}
366362
}
367363

368-
pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx>>(
369-
tcx: TyCtxt<'tcx>,
370-
module_def_id: LocalDefId,
371-
builtin_lints: T,
372-
) {
373-
if tcx.sess.opts.unstable_opts.no_interleave_lints {
374-
// These passes runs in late_lint_crate with -Z no_interleave_lints
375-
return;
376-
}
377-
378-
late_lint_mod_pass(tcx, module_def_id, builtin_lints);
379-
380-
let mut passes: Vec<_> =
381-
unerased_lint_store(tcx).late_module_passes.iter().map(|pass| (pass)(tcx)).collect();
382-
383-
if !passes.is_empty() {
384-
late_lint_mod_pass(tcx, module_def_id, LateLintPassObjects { lints: &mut passes[..] });
385-
}
386-
}
387-
388-
fn late_lint_pass_crate<'tcx, T: LateLintPass<'tcx>>(tcx: TyCtxt<'tcx>, pass: T) {
389-
let effective_visibilities = &tcx.effective_visibilities(());
390-
364+
fn late_lint_crate<'tcx, T: LateLintPass<'tcx> + 'tcx>(tcx: TyCtxt<'tcx>, builtin_lints: T) {
391365
let context = LateContext {
392366
tcx,
393367
enclosing_body: None,
394368
cached_typeck_results: Cell::new(None),
395369
param_env: ty::ParamEnv::empty(),
396-
effective_visibilities,
370+
effective_visibilities: &tcx.effective_visibilities(()),
397371
lint_store: unerased_lint_store(tcx),
398372
last_node_with_lint_attrs: hir::CRATE_HIR_ID,
399373
generics: None,
400374
only_module: false,
401375
};
402376

377+
let mut passes =
378+
unerased_lint_store(tcx).late_passes.iter().map(|p| (p)(tcx)).collect::<Vec<_>>();
379+
passes.push(Box::new(builtin_lints));
380+
let pass = LateLintPassObjects { lints: &mut passes[..] };
381+
403382
let mut cx = LateContextAndPass { context, pass };
404383

405384
// Visit the whole crate.
406385
cx.with_lint_attrs(hir::CRATE_HIR_ID, |cx| {
407-
// since the root module isn't visited as an item (because it isn't an
386+
// Since the root module isn't visited as an item (because it isn't an
408387
// item), warn for it here.
409388
lint_callback!(cx, check_crate,);
410389
tcx.hir().walk_toplevel_module(cx);
@@ -413,41 +392,8 @@ fn late_lint_pass_crate<'tcx, T: LateLintPass<'tcx>>(tcx: TyCtxt<'tcx>, pass: T)
413392
})
414393
}
415394

416-
fn late_lint_crate<'tcx, T: LateLintPass<'tcx>>(tcx: TyCtxt<'tcx>, builtin_lints: T) {
417-
let mut passes =
418-
unerased_lint_store(tcx).late_passes.iter().map(|p| (p)(tcx)).collect::<Vec<_>>();
419-
420-
if !tcx.sess.opts.unstable_opts.no_interleave_lints {
421-
if !passes.is_empty() {
422-
late_lint_pass_crate(tcx, LateLintPassObjects { lints: &mut passes[..] });
423-
}
424-
425-
late_lint_pass_crate(tcx, builtin_lints);
426-
} else {
427-
for pass in &mut passes {
428-
tcx.sess.prof.verbose_generic_activity_with_arg("run_late_lint", pass.name()).run(
429-
|| {
430-
late_lint_pass_crate(tcx, LateLintPassObjects { lints: slice::from_mut(pass) });
431-
},
432-
);
433-
}
434-
435-
let mut passes: Vec<_> =
436-
unerased_lint_store(tcx).late_module_passes.iter().map(|pass| (pass)(tcx)).collect();
437-
438-
for pass in &mut passes {
439-
tcx.sess
440-
.prof
441-
.verbose_generic_activity_with_arg("run_late_module_lint", pass.name())
442-
.run(|| {
443-
late_lint_pass_crate(tcx, LateLintPassObjects { lints: slice::from_mut(pass) });
444-
});
445-
}
446-
}
447-
}
448-
449395
/// Performs lint checking on a crate.
450-
pub fn check_crate<'tcx, T: LateLintPass<'tcx>>(
396+
pub fn check_crate<'tcx, T: LateLintPass<'tcx> + 'tcx>(
451397
tcx: TyCtxt<'tcx>,
452398
builtin_lints: impl FnOnce() -> T + Send,
453399
) {

0 commit comments

Comments
 (0)