Skip to content

Commit

Permalink
Auto merge of rust-lang#134862 - compiler-errors:builtin-passes-that-…
Browse files Browse the repository at this point in the history
…dont-need-to-run, r=<try>

Filter out builtin lint passes that don't need to be run

r? `@ghost`
  • Loading branch information
bors committed Dec 28, 2024
2 parents ceb0441 + 7d164e8 commit 13c9a98
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 62 deletions.
22 changes: 11 additions & 11 deletions compiler/rustc_lint/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,12 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
// `check_foo` method in `$methods` within this pass simply calls `check_foo`
// once per `$pass`. Compare with `declare_combined_late_lint_pass`, which is
// similar, but combines lint passes at compile time.
struct RuntimeCombinedLateLintPass<'a, 'tcx> {
passes: &'a mut [LateLintPassObject<'tcx>],
pub struct RuntimeCombinedLateLintPass<'tcx> {
pub passes: Vec<LateLintPassObject<'tcx>>,
}

#[allow(rustc::lint_pass_impl_without_macro)]
impl LintPass for RuntimeCombinedLateLintPass<'_, '_> {
impl LintPass for RuntimeCombinedLateLintPass<'_> {
fn name(&self) -> &'static str {
panic!()
}
Expand All @@ -323,7 +323,7 @@ impl LintPass for RuntimeCombinedLateLintPass<'_, '_> {

macro_rules! impl_late_lint_pass {
([], [$($(#[$attr:meta])* fn $f:ident($($param:ident: $arg:ty),*);)*]) => {
impl<'tcx> LateLintPass<'tcx> for RuntimeCombinedLateLintPass<'_, 'tcx> {
impl<'tcx> LateLintPass<'tcx> for RuntimeCombinedLateLintPass<'tcx> {
$(fn $f(&mut self, context: &LateContext<'tcx>, $($param: $arg),*) {
for pass in self.passes.iter_mut() {
pass.$f(context, $($param),*);
Expand Down Expand Up @@ -360,14 +360,14 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
late_lint_mod_inner(tcx, module_def_id, context, builtin_lints);
} else {
let builtin_lints = Box::new(builtin_lints) as Box<dyn LateLintPass<'tcx>>;
let mut binding = store
let passes = store
.late_module_passes
.iter()
.map(|mk_pass| (mk_pass)(tcx))
.chain(std::iter::once(builtin_lints))
.collect::<Vec<_>>();

let pass = RuntimeCombinedLateLintPass { passes: binding.as_mut_slice() };
let pass = RuntimeCombinedLateLintPass { passes };
late_lint_mod_inner(tcx, module_def_id, context, pass);
}
}
Expand Down Expand Up @@ -398,10 +398,10 @@ fn late_lint_mod_inner<'tcx, T: LateLintPass<'tcx>>(

fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
// Note: `passes` is often empty.
let passes: Vec<_> =
let unfiltered_passes: Vec<_> =
unerased_lint_store(tcx.sess).late_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect();

if passes.is_empty() {
if unfiltered_passes.is_empty() {
return;
}

Expand All @@ -418,7 +418,7 @@ fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {

let lints_that_dont_need_to_run = tcx.lints_that_dont_need_to_run(());

let mut filtered_passes: Vec<Box<dyn LateLintPass<'tcx>>> = passes
let mut passes: Vec<Box<dyn LateLintPass<'tcx>>> = unfiltered_passes
.into_iter()
.filter(|pass| {
let lints = (**pass).get_lints();
Expand All @@ -429,8 +429,8 @@ fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
})
.collect();

filtered_passes.push(Box::new(HardwiredLints));
let pass = RuntimeCombinedLateLintPass { passes: &mut filtered_passes[..] };
passes.push(Box::new(HardwiredLints));
let pass = RuntimeCombinedLateLintPass { passes };
late_lint_crate_inner(tcx, context, pass);
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub use context::{
CheckLintNameResult, EarlyContext, FindLintError, LateContext, LintContext, LintStore,
};
pub use early::{EarlyCheckNode, check_ast_node};
pub use late::{check_crate, late_lint_mod, unerased_lint_store};
pub use late::{RuntimeCombinedLateLintPass, check_crate, late_lint_mod, unerased_lint_store};
pub use passes::{EarlyLintPass, LateLintPass};
pub use rustc_session::lint::Level::{self, *};
pub use rustc_session::lint::{
Expand All @@ -146,7 +146,7 @@ pub fn provide(providers: &mut Providers) {
}

fn lint_mod(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new(tcx));
}

early_lint_methods!(
Expand Down
35 changes: 18 additions & 17 deletions compiler/rustc_lint/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,11 @@ late_lint_methods!(declare_late_lint_pass, []);

impl LateLintPass<'_> for HardwiredLints {}

#[macro_export]
macro_rules! expand_combined_late_lint_pass_method {
([$($pass:ident),*], $self: ident, $name: ident, $params:tt) => ({
$($self.$pass.$name $params;)*
})
}

#[macro_export]
macro_rules! expand_combined_late_lint_pass_methods {
($passes:tt, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
$(fn $name(&mut self, context: &$crate::LateContext<'tcx>, $($param: $arg),*) {
$crate::expand_combined_late_lint_pass_method!($passes, self, $name, (context, $($param),*));
self.inner.$name(context$(, $param)*);
})*
)
}
Expand All @@ -97,15 +90,23 @@ macro_rules! expand_combined_late_lint_pass_methods {
macro_rules! declare_combined_late_lint_pass {
([$v:vis $name:ident, [$($pass:ident: $constructor:expr,)*]], $methods:tt) => (
#[allow(non_snake_case)]
$v struct $name {
$($pass: $pass,)*
$v struct $name<'tcx> {
inner: RuntimeCombinedLateLintPass<'tcx>,
}

impl $name {
$v fn new() -> Self {
Self {
$($pass: $constructor,)*
}
impl<'tcx> $name<'tcx> {
$v fn new(tcx: TyCtxt<'tcx>) -> Self {
let lints_that_dont_need_to_run = tcx.lints_that_dont_need_to_run(());
let mut passes = vec![];
$(passes.push(Box::new($constructor) as Box<dyn LateLintPass<'tcx>>);)*
passes.retain(|pass| {
let lints = (**pass).get_lints();
// Lintless passes are always in
lints.is_empty() ||
// If the pass doesn't have a single needed lint, omit it
!lints.iter().all(|lint| lints_that_dont_need_to_run.contains(&LintId::of(lint)))
});
$name { inner: RuntimeCombinedLateLintPass { passes } }
}

$v fn get_lints() -> $crate::LintVec {
Expand All @@ -115,12 +116,12 @@ macro_rules! declare_combined_late_lint_pass {
}
}

impl<'tcx> $crate::LateLintPass<'tcx> for $name {
impl<'tcx> $crate::LateLintPass<'tcx> for $name<'tcx> {
$crate::expand_combined_late_lint_pass_methods!([$($pass),*], $methods);
}

#[allow(rustc::lint_pass_impl_without_macro)]
impl $crate::LintPass for $name {
impl $crate::LintPass for $name<'_> {
fn name(&self) -> &'static str {
panic!()
}
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/print_type_sizes/multiple_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ pub enum Enum {
Small(SevenBytes),
Large(FiftyBytes),
}

fn main() {
let x = Enum::Small(SevenBytes([0; 7]));
let x = Enum::Large(FiftyBytes([0; 50]));
}
6 changes: 6 additions & 0 deletions tests/ui/print_type_sizes/padding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ enum E2 {
A(i8, i32),
B(S),
}

fn main() {
let s = S { a: true, b: true, g: 0 };
let e1 = E1::A(0, 0);
let e2 = E2::A(0, 0);
}
11 changes: 11 additions & 0 deletions tests/ui/print_type_sizes/repr-align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@ pub struct S {
c: A,
d: i8,
}

fn main() {
let s = S {
a: 0,
b: 0,
c: A(0),
d: 0,
};

let e = E::A(0);
}
5 changes: 5 additions & 0 deletions tests/ui/print_type_sizes/repr_int_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ enum Repru8 {
A(u16),
B,
}

fn main() {
let c = ReprCu8::A(0);
let r = Repru8::A(0);
}
5 changes: 5 additions & 0 deletions tests/ui/print_type_sizes/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ pub enum Enum {
Small(SevenBytes),
Large(FiftyBytes),
}

fn main() {
let x = Enum::Small(SevenBytes([0; 7]));
let x = Enum::Large(FiftyBytes([0; 50]));
}

This file was deleted.

This file was deleted.

0 comments on commit 13c9a98

Please sign in to comment.