Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc_lint: Prevent triplication of various lints #119388

Merged
merged 5 commits into from
Dec 30, 2023

Conversation

Enselic
Copy link
Member

@Enselic Enselic commented Dec 28, 2023

Prevent triplication of various lints. The triplication happens because we run the same lint three times (or less in some cases):

  • In BuiltinCombinedPreExpansionLintPass
  • In BuiltinCombinedEarlyLintPass
  • In shallow_lint_levels_on()

Only run the lints one time by checking the lint_added_lints bool.

Set your GitHub diff setting to ignore whitespaces changes when reviewing this PR, since I had to enclose a block inside an if.

Closes #73301

(I found this while exploring the code related to this comment.)

So we can apply more kinds of lints to added lints without having to add
another parameter.
Prevent multiple 'ignored unless specified at crate level' lints. The
multiplication happens because we run the same lint three times:
* In BuiltinCombinedEarlyLintPass
* In BuiltinCombinedPreExpansionLintPass
* In shallow_lint_levels_on

Only run the lint one time by checking the `lint_added_lints` bool.
@rustbot
Copy link
Collaborator

rustbot commented Dec 28, 2023

r? @davidtwco

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 28, 2023
@Enselic
Copy link
Member Author

Enselic commented Dec 28, 2023

davidtwco seems to be on vacation, so I'll go ahead and re-roll reviewer.

r? compiler

@rustbot rustbot assigned compiler-errors and unassigned davidtwco Dec 28, 2023
@@ -19,9 +15,6 @@ fn main() {
#[allow(non_exhaustive_omitted_patterns)]
//~^ WARNING unknown lint: `non_exhaustive_omitted_patterns`
//~| WARNING unknown lint: `non_exhaustive_omitted_patterns`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it still appear twice?

Copy link
Member Author

@Enselic Enselic Dec 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is emitted twice via with_lint_attrs(), first from visit_stmt() and then from visit_expr() (from inside visit_stmt()). For reference, the code looks like this:

    fn visit_expr(&mut self, e: &'a ast::Expr) {
        self.with_lint_attrs(e.id, &e.attrs, |cx| {
            lint_callback!(cx, check_expr, e);
            ast_visit::walk_expr(cx, e);
        })
    }

   // ...

    fn visit_stmt(&mut self, s: &'a ast::Stmt) {
        // Add the statement's lint attributes to our
        // current state when checking the statement itself.
        // This allows us to handle attributes like
        // `#[allow(unused_doc_comments)]`, which apply to
        // sibling attributes on the same target
        //
        // Note that statements get their attributes from
        // the AST struct that they wrap (e.g. an item)
        self.with_lint_attrs(s.id, s.attrs(), |cx| {
            lint_callback!(cx, check_stmt, s);
            cx.check_id(s.id);
        });
        // The visitor for the AST struct wrapped
        // by the statement (e.g. `Item`) will call
        // `with_lint_attrs`, so do this walk
        // outside of the above `with_lint_attrs` call
        ast_visit::walk_stmt(self, s);
    }

I can look into fixing that, but I would prefer to do it in a separate PR after this PR lands since it is a separate problem.

Copy link
Member Author

@Enselic Enselic Dec 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, here is the top of the backtrace of err.emit() of the second emit:

backtrace
rustc_middle::lint::struct_lint_level::struct_lint_level_impl::<rustc_error_messages::DiagnosticMessage> (\home\martin\src\rust\compiler\rustc_middle\src\lint.rs:406)
rustc_middle::lint::struct_lint_level::<rustc_error_messages::DiagnosticMessage, <rustc_lint::levels::LintLevelsBuilder<rustc_lint::levels::TopDown>>::check_gated_lint::{closure#0}> (\home\martin\src\rust\compiler\rustc_middle\src\lint.rs:408)
<rustc_lint::levels::LintLevelsBuilder<rustc_lint::levels::TopDown>>::check_gated_lint (\home\martin\src\rust\compiler\rustc_lint\src\levels.rs:1064)
<rustc_lint::levels::LintLevelsBuilder<rustc_lint::levels::TopDown>>::add (\home\martin\src\rust\compiler\rustc_lint\src\levels.rs:897)
<rustc_lint::levels::LintLevelsBuilder<rustc_lint::levels::TopDown>>::push (\home\martin\src\rust\compiler\rustc_lint\src\levels.rs:517)
<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass>>::with_lint_attrs::<<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass> as rustc_ast::visit::Visitor>::visit_expr::{closure#0}> (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:66)
<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass> as rustc_ast::visit::Visitor>::visit_expr (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:119)
<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass> as rustc_ast::visit::Visitor>::visit_stmt (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:148)
rustc_ast::visit::walk_block::<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass>> (\home\martin\src\rust\compiler\rustc_ast\src\visit.rs:724)
<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass> as rustc_ast::visit::Visitor>::visit_fn (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:154)
rustc_ast::visit::walk_item::<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass>> (\home\martin\src\rust\compiler\rustc_ast\src\visit.rs:326)
<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass> as rustc_ast::visit::Visitor>::visit_item::{closure#0} (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:89)
<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass>>::with_lint_attrs::<<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass> as rustc_ast::visit::Visitor>::visit_item::{closure#0}>::{closure#0} (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:71)
stacker::maybe_grow::<(), <rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass>>::with_lint_attrs<<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass> as rustc_ast::visit::Visitor>::visit_item::{closure#0}>::{closure#0}> (\home\martin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\stacker-0.1.15\src\lib.rs:55)
rustc_data_structures::stack::ensure_sufficient_stack::<(), <rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass>>::with_lint_attrs<<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass> as rustc_ast::visit::Visitor>::visit_item::{closure#0}>::{closure#0}> (\home\martin\src\rust\compiler\rustc_data_structures\src\stack.rs:17)
<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass>>::with_lint_attrs::<<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass> as rustc_ast::visit::Visitor>::visit_item::{closure#0}> (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:71)
<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass> as rustc_ast::visit::Visitor>::visit_item (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:87)
rustc_ast::visit::walk_crate::<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass>> (\home\martin\src\rust\compiler\rustc_ast\src\visit.rs:272)
<(&rustc_ast::ast::Crate, &[rustc_ast::ast::Attribute]) as rustc_lint::early::EarlyCheckNode>::check::<rustc_lint::BuiltinCombinedEarlyLintPass> (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:356)
rustc_lint::early::check_ast_node_inner::<rustc_lint::BuiltinCombinedEarlyLintPass, (&rustc_ast::ast::Crate, &[rustc_ast::ast::Attribute])>::{closure#0} (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:422)
<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass>>::with_lint_attrs::<rustc_lint::early::check_ast_node_inner<rustc_lint::BuiltinCombinedEarlyLintPass, (&rustc_ast::ast::Crate, &[rustc_ast::ast::Attribute])>::{closure#0}>::{closure#0} (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:71)
stacker::maybe_grow::<(), <rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass>>::with_lint_attrs<rustc_lint::early::check_ast_node_inner<rustc_lint::BuiltinCombinedEarlyLintPass, (&rustc_ast::ast::Crate, &[rustc_ast::ast::Attribute])>::{closure#0}>::{closure#0}> (\home\martin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\stacker-0.1.15\src\lib.rs:55)
rustc_data_structures::stack::ensure_sufficient_stack::<(), <rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass>>::with_lint_attrs<rustc_lint::early::check_ast_node_inner<rustc_lint::BuiltinCombinedEarlyLintPass, (&rustc_ast::ast::Crate, &[rustc_ast::ast::Attribute])>::{closure#0}>::{closure#0}> (\home\martin\src\rust\compiler\rustc_data_structures\src\stack.rs:17)
<rustc_lint::early::EarlyContextAndPass<rustc_lint::BuiltinCombinedEarlyLintPass>>::with_lint_attrs::<rustc_lint::early::check_ast_node_inner<rustc_lint::BuiltinCombinedEarlyLintPass, (&rustc_ast::ast::Crate, &[rustc_ast::ast::Attribute])>::{closure#0}> (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:71)
rustc_lint::early::check_ast_node_inner::<rustc_lint::BuiltinCombinedEarlyLintPass, (&rustc_ast::ast::Crate, &[rustc_ast::ast::Attribute])> (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:422)
rustc_lint::early::check_ast_node::<rustc_lint::BuiltinCombinedEarlyLintPass, (&rustc_ast::ast::Crate, &[rustc_ast::ast::Attribute])> (\home\martin\src\rust\compiler\rustc_lint\src\early.rs:405)
rustc_interface::passes::early_lint_checks (\home\martin\src\rust\compiler\rustc_interface\src\passes.rs:325)
rustc_query_impl::query_impl::early_lint_checks::dynamic_query::{closure#2}::{closure#0} (\home\martin\src\rust\compiler\rustc_query_impl\src\plumbing.rs:589)
rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::early_lint_checks::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>> (\home\martin\src\rust\compiler\rustc_query_impl\src\plumbing.rs:513)
rustc_query_impl::query_impl::early_lint_checks::dynamic_query::{closure#2} (\home\martin\src\rust\compiler\rustc_query_impl\src\plumbing.rs:585)
<rustc_query_impl::query_impl::early_lint_checks::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, ())>>::call_once (\home\martin\src\rust\library\core\src\ops\function.rs:250)
<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false> as rustc_query_system::query::config::QueryConfig<rustc_query_impl::plumbing::QueryCtxt>>::compute (\home\martin\src\rust\compiler\rustc_query_impl\src\lib.rs:119)
rustc_query_system::query::plumbing::execute_job_non_incr::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt>::{closure#0} (\home\martin\src\rust\compiler\rustc_query_system\src\query\plumbing.rs:464)
rustc_middle::ty::context::tls::enter_context::<rustc_query_system::query::plumbing::execute_job_non_incr<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt>::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>>::{closure#0} (\home\martin\src\rust\compiler\rustc_middle\src\ty\context\tls.rs:82)
<std::thread::local::LocalKey<core::cell::Cell<*const ()>>>::try_with::<rustc_middle::ty::context::tls::enter_context<rustc_query_system::query::plumbing::execute_job_non_incr<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt>::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>>::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>> (\home\martin\src\rust\library\std\src\thread\local.rs:270)
<std::thread::local::LocalKey<core::cell::Cell<*const ()>>>::with::<rustc_middle::ty::context::tls::enter_context<rustc_query_system::query::plumbing::execute_job_non_incr<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt>::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>>::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>> (\home\martin\src\rust\library\std\src\thread\local.rs:246)
rustc_middle::ty::context::tls::enter_context::<rustc_query_system::query::plumbing::execute_job_non_incr<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt>::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>> (\home\martin\src\rust\compiler\rustc_middle\src\ty\context\tls.rs:79)
<rustc_query_impl::plumbing::QueryCtxt as rustc_query_system::query::QueryContext>::start_query::<rustc_middle::query::erase::Erased<[u8; 0]>, rustc_query_system::query::plumbing::execute_job_non_incr<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt>::{closure#0}>::{closure#0} (\home\martin\src\rust\compiler\rustc_query_impl\src\plumbing.rs:151)
rustc_middle::ty::context::tls::with_related_context::<<rustc_query_impl::plumbing::QueryCtxt as rustc_query_system::query::QueryContext>::start_query<rustc_middle::query::erase::Erased<[u8; 0]>, rustc_query_system::query::plumbing::execute_job_non_incr<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt>::{closure#0}>::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>>::{closure#0} (\home\martin\src\rust\compiler\rustc_middle\src\ty\context\tls.rs:133)
rustc_middle::ty::context::tls::with_context::<rustc_middle::ty::context::tls::with_related_context<<rustc_query_impl::plumbing::QueryCtxt as rustc_query_system::query::QueryContext>::start_query<rustc_middle::query::erase::Erased<[u8; 0]>, rustc_query_system::query::plumbing::execute_job_non_incr<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt>::{closure#0}>::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>>::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>>::{closure#0} (\home\martin\src\rust\compiler\rustc_middle\src\ty\context\tls.rs:111)

The first err.emit() backtrace looks exactly the same, except it does not have the visit_expr() stack frame.

@@ -31,9 +24,6 @@ fn main() {
#[warn(non_exhaustive_omitted_patterns)]
//~^ WARNING unknown lint: `non_exhaustive_omitted_patterns`
//~| WARNING unknown lint: `non_exhaustive_omitted_patterns`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(See above)

@cjgillot
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Dec 29, 2023

📌 Commit 7ca4e9f has been approved by cjgillot

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 29, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 30, 2023
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#119322 (Couple of random coroutine pass simplifications)
 - rust-lang#119374 (Italicise "bytes" in the docs of some `Vec` methods)
 - rust-lang#119388 (rustc_lint: Prevent triplication of various lints)
 - rust-lang#119406 (Add non-regression test for ATPIT ICE rust-lang#114325)
 - rust-lang#119410 (Rename test to be more descriptive)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit c8d4274 into rust-lang:master Dec 30, 2023
11 checks passed
@rustbot rustbot added this to the 1.77.0 milestone Dec 30, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Dec 30, 2023
Rollup merge of rust-lang#119388 - Enselic:prevent-lint-triplication, r=cjgillot

rustc_lint: Prevent triplication of various lints

Prevent triplication of various lints. The triplication happens because we run the same lint three times (or less in some cases):
* In `BuiltinCombinedPreExpansionLintPass`
* In `BuiltinCombinedEarlyLintPass`
* In `shallow_lint_levels_on()`

Only run the lints one time by checking the `lint_added_lints` bool.

Set your GitHub diff setting to ignore whitespaces changes when reviewing this PR, since I had to enclose a block inside an if.

Closes rust-lang#73301

(I found this while exploring the code related to [this](rust-lang#119251 (comment)) comment.)
@Enselic Enselic deleted the prevent-lint-triplication branch December 31, 2023 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Errors reported in rustc_lint::levels::LintLevelsBuilder::push occurs multiple times
6 participants