Rollup merge of #132221 - ehuss:lint-docs, r=compiler-errors

Clean up some comments on lint implementation

This updates some doc comments that have gotten very out of date. Some of these macros were removed or renamed in #57726 and #104863 and others. Manual emitting of lints was significantly reworked when the `Diagnostic` infrastructure was added.

Rather than try to replicate the high-level documentation, I added pointers to the rustc-dev-guide.

I linkified some types so that if they are renamed/removed without updating the docs, it will break CI.
This commit is contained in:
Matthias Krüger 2024-10-27 19:49:08 +01:00 committed by GitHub
commit 4649a1b141
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 18 additions and 66 deletions

View file

@ -4,21 +4,14 @@
//! AST visitor. Also see `rustc_session::lint::builtin`, which contains the //! AST visitor. Also see `rustc_session::lint::builtin`, which contains the
//! definitions of lints that are emitted directly inside the main compiler. //! definitions of lints that are emitted directly inside the main compiler.
//! //!
//! To add a new lint to rustc, declare it here using `declare_lint!()`. //! To add a new lint to rustc, declare it here using [`declare_lint!`].
//! Then add code to emit the new lint in the appropriate circumstances. //! Then add code to emit the new lint in the appropriate circumstances.
//! You can do that in an existing `LintPass` if it makes sense, or in a
//! new `LintPass`, or using `Session::add_lint` elsewhere in the
//! compiler. Only do the latter if the check can't be written cleanly as a
//! `LintPass` (also, note that such lints will need to be defined in
//! `rustc_session::lint::builtin`, not here).
//! //!
//! If you define a new `EarlyLintPass`, you will also need to add it to the //! If you define a new [`EarlyLintPass`], you will also need to add it to the
//! `add_early_builtin!` or `add_early_builtin_with_new!` invocation in //! [`crate::early_lint_methods!`] invocation in `lib.rs`.
//! `lib.rs`. Use the former for unit-like structs and the latter for structs
//! with a `pub fn new()`.
//! //!
//! If you define a new `LateLintPass`, you will also need to add it to the //! If you define a new [`LateLintPass`], you will also need to add it to the
//! `late_lint_methods!` invocation in `lib.rs`. //! [`crate::late_lint_methods!`] invocation in `lib.rs`.
use std::fmt::Write; use std::fmt::Write;

View file

@ -1,18 +1,7 @@
//! Implementation of lint checking. //! Basic types for managing and implementing lints.
//! //!
//! The lint checking is mostly consolidated into one pass which runs //! See <https://rustc-dev-guide.rust-lang.org/diagnostics.html> for an
//! after all other analyses. Throughout compilation, lint warnings //! overview of how lints are implemented.
//! can be added via the `add_lint` method on the Session structure. This
//! requires a span and an ID of the node that the lint is being added to. The
//! lint isn't actually emitted at that time because it is unknown what the
//! actual lint level at that location is.
//!
//! To actually emit lint warnings/errors, a separate pass is used.
//! A context keeps track of the current state of all lint levels.
//! Upon entering a node of the ast which can modify the lint settings, the
//! previous lint state is pushed onto a stack and the ast is then recursed
//! upon. As the ast is traversed, this keeps track of the current lint level
//! for all lint attributes.
use std::cell::Cell; use std::cell::Cell;
use std::{iter, slice}; use std::{iter, slice};
@ -52,9 +41,6 @@ type LateLintPassFactory =
dyn for<'tcx> Fn(TyCtxt<'tcx>) -> LateLintPassObject<'tcx> + sync::DynSend + sync::DynSync; dyn for<'tcx> Fn(TyCtxt<'tcx>) -> LateLintPassObject<'tcx> + sync::DynSend + sync::DynSync;
/// Information about the registered lints. /// Information about the registered lints.
///
/// This is basically the subset of `Context` that we can
/// build early in the compile pipeline.
pub struct LintStore { pub struct LintStore {
/// Registered lints. /// Registered lints.
lints: Vec<&'static Lint>, lints: Vec<&'static Lint>,

View file

@ -1,18 +1,8 @@
//! Implementation of lint checking. //! Implementation of the early lint pass.
//! //!
//! The lint checking is mostly consolidated into one pass which runs //! The early lint pass works on AST nodes after macro expansion and name
//! after all other analyses. Throughout compilation, lint warnings //! resolution, just before AST lowering. These lints are for purely
//! can be added via the `add_lint` method on the Session structure. This //! syntactical lints.
//! requires a span and an ID of the node that the lint is being added to. The
//! lint isn't actually emitted at that time because it is unknown what the
//! actual lint level at that location is.
//!
//! To actually emit lint warnings/errors, a separate pass is used.
//! A context keeps track of the current state of all lint levels.
//! Upon entering a node of the ast which can modify the lint settings, the
//! previous lint state is pushed onto a stack and the ast is then recursed
//! upon. As the ast is traversed, this keeps track of the current lint level
//! for all lint attributes.
use rustc_ast::ptr::P; use rustc_ast::ptr::P;
use rustc_ast::visit::{self as ast_visit, Visitor, walk_list}; use rustc_ast::visit::{self as ast_visit, Visitor, walk_list};

View file

@ -1,18 +1,7 @@
//! Implementation of lint checking. //! Implementation of the late lint pass.
//! //!
//! The lint checking is mostly consolidated into one pass which runs //! The late lint pass Works on HIR nodes, towards the end of analysis (after
//! after all other analyses. Throughout compilation, lint warnings //! borrow checking, etc.). These lints have full type information available.
//! can be added via the `add_lint` method on the Session structure. This
//! requires a span and an ID of the node that the lint is being added to. The
//! lint isn't actually emitted at that time because it is unknown what the
//! actual lint level at that location is.
//!
//! To actually emit lint warnings/errors, a separate pass is used.
//! A context keeps track of the current state of all lint levels.
//! Upon entering a node of the ast which can modify the lint settings, the
//! previous lint state is pushed onto a stack and the ast is then recursed
//! upon. As the ast is traversed, this keeps track of the current lint level
//! for all lint attributes.
use std::any::Any; use std::any::Any;
use std::cell::Cell; use std::cell::Cell;

View file

@ -6,20 +6,14 @@
//! other phases of the compiler, which are generally required to hold in order //! other phases of the compiler, which are generally required to hold in order
//! to compile the program at all. //! to compile the program at all.
//! //!
//! Most lints can be written as [LintPass] instances. These run after //! Most lints can be written as [`LintPass`] instances. These run after
//! all other analyses. The `LintPass`es built into rustc are defined //! all other analyses. The `LintPass`es built into rustc are defined
//! within [rustc_session::lint::builtin], //! within [rustc_session::lint::builtin],
//! which has further comments on how to add such a lint. //! which has further comments on how to add such a lint.
//! rustc can also load external lint plugins, as is done for Clippy. //! rustc can also load external lint plugins, as is done for Clippy.
//! //!
//! Some of rustc's lints are defined elsewhere in the compiler and work by //! See <https://rustc-dev-guide.rust-lang.org/diagnostics.html> for an
//! calling `add_lint()` on the overall `Session` object. This works when //! overview of how lints are implemented.
//! it happens before the main lint pass, which emits the lints stored by
//! `add_lint()`. To emit lints after the main lint pass (from codegen, for
//! example) requires more effort. See `emit_lint` and `GatherNodeLevels`
//! in `context.rs`.
//!
//! Some code also exists in [rustc_session::lint], [rustc_middle::lint].
//! //!
//! ## Note //! ## Note
//! //!