Auto merge of #116731 - Alexendoo:hash-untracked-state, r=oli-obk

Add `Config::hash_untracked_state` callback

For context, I'm looking to use [late module passes](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/context/struct.LintStore.html#structfield.late_module_passes) in Clippy which unlike regular late passes run incrementally per module

However we have a config file which can change between runs, we need changes to that to invalidate the `lint_mod` query. This PR adds a side channel for us to hash some extra state into `Options` in order to do that

This does not make any changes to Clippy, I plan to do that in a PR to the Clippy repo along with some other required changes

An alternative implementation would be to add a new query to track this state and override the `lint_mod` query in Clippy to first call that

cc `@rust-lang/clippy`
This commit is contained in:
bors 2023-10-16 16:33:42 +00:00
commit 4af886f8ab
7 changed files with 28 additions and 7 deletions

View File

@ -312,6 +312,7 @@ fn run_compiler(
locale_resources: DEFAULT_LOCALE_RESOURCES, locale_resources: DEFAULT_LOCALE_RESOURCES,
lint_caps: Default::default(), lint_caps: Default::default(),
parse_sess_created: None, parse_sess_created: None,
hash_untracked_state: None,
register_lints: None, register_lints: None,
override_queries: None, override_queries: None,
make_codegen_backend, make_codegen_backend,

View File

@ -5,6 +5,7 @@ use rustc_ast::{self as ast, LitKind, MetaItemKind};
use rustc_codegen_ssa::traits::CodegenBackend; use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::defer; use rustc_data_structures::defer;
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_errors::registry::Registry; use rustc_errors::registry::Registry;
use rustc_errors::{ErrorGuaranteed, Handler}; use rustc_errors::{ErrorGuaranteed, Handler};
@ -260,6 +261,12 @@ pub struct Config {
/// This is a callback from the driver that is called when [`ParseSess`] is created. /// This is a callback from the driver that is called when [`ParseSess`] is created.
pub parse_sess_created: Option<Box<dyn FnOnce(&mut ParseSess) + Send>>, pub parse_sess_created: Option<Box<dyn FnOnce(&mut ParseSess) + Send>>,
/// This is a callback to hash otherwise untracked state used by the caller, if the
/// hash changes between runs the incremental cache will be cleared.
///
/// e.g. used by Clippy to hash its config file
pub hash_untracked_state: Option<Box<dyn FnOnce(&Session, &mut StableHasher) + Send>>,
/// This is a callback from the driver that is called when we're registering lints; /// This is a callback from the driver that is called when we're registering lints;
/// it is called during plugin registration when we have the LintStore in a non-shared state. /// it is called during plugin registration when we have the LintStore in a non-shared state.
/// ///
@ -269,8 +276,6 @@ pub struct Config {
/// This is a callback from the driver that is called just after we have populated /// This is a callback from the driver that is called just after we have populated
/// the list of queries. /// the list of queries.
///
/// The second parameter is local providers and the third parameter is external providers.
pub override_queries: Option<fn(&Session, &mut Providers)>, pub override_queries: Option<fn(&Session, &mut Providers)>,
/// This is a callback from the driver that is called to create a codegen backend. /// This is a callback from the driver that is called to create a codegen backend.
@ -330,6 +335,12 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
parse_sess_created(&mut sess.parse_sess); parse_sess_created(&mut sess.parse_sess);
} }
if let Some(hash_untracked_state) = config.hash_untracked_state {
let mut hasher = StableHasher::new();
hash_untracked_state(&sess, &mut hasher);
sess.opts.untracked_state_hash = hasher.finish()
}
let compiler = Compiler { let compiler = Compiler {
sess: Lrc::new(sess), sess: Lrc::new(sess),
codegen_backend: Lrc::from(codegen_backend), codegen_backend: Lrc::from(codegen_backend),

View File

@ -1047,6 +1047,7 @@ impl Default for Options {
target_triple: TargetTriple::from_triple(host_triple()), target_triple: TargetTriple::from_triple(host_triple()),
test: false, test: false,
incremental: None, incremental: None,
untracked_state_hash: Default::default(),
unstable_opts: Default::default(), unstable_opts: Default::default(),
prints: Vec::new(), prints: Vec::new(),
cg: Default::default(), cg: Default::default(),
@ -2889,6 +2890,7 @@ pub fn build_session_options(
target_triple, target_triple,
test, test,
incremental, incremental,
untracked_state_hash: Default::default(),
unstable_opts, unstable_opts,
prints, prints,
cg, cg,
@ -3167,17 +3169,17 @@ impl PpMode {
/// we have an opt-in scheme here, so one is hopefully forced to think about /// we have an opt-in scheme here, so one is hopefully forced to think about
/// how the hash should be calculated when adding a new command-line argument. /// how the hash should be calculated when adding a new command-line argument.
pub(crate) mod dep_tracking { pub(crate) mod dep_tracking {
use super::Polonius;
use super::{ use super::{
BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, DebugInfoCompression, BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, DebugInfoCompression,
ErrorOutputType, InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail, ErrorOutputType, InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail,
LtoCli, OomStrategy, OptLevel, OutFileName, OutputType, OutputTypes, Passes, LtoCli, OomStrategy, OptLevel, OutFileName, OutputType, OutputTypes, Polonius,
ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath, ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath,
SymbolManglingVersion, TraitSolver, TrimmedDefPaths, SymbolManglingVersion, TraitSolver, TrimmedDefPaths,
}; };
use crate::lint; use crate::lint;
use crate::options::WasiExecModel; use crate::options::WasiExecModel;
use crate::utils::{NativeLib, NativeLibKind}; use crate::utils::NativeLib;
use rustc_data_structures::stable_hasher::Hash64;
use rustc_errors::LanguageIdentifier; use rustc_errors::LanguageIdentifier;
use rustc_feature::UnstableFeatures; use rustc_feature::UnstableFeatures;
use rustc_span::edition::Edition; use rustc_span::edition::Edition;
@ -3233,6 +3235,7 @@ pub(crate) mod dep_tracking {
usize, usize,
NonZeroUsize, NonZeroUsize,
u64, u64,
Hash64,
String, String,
PathBuf, PathBuf,
lint::Level, lint::Level,
@ -3247,14 +3250,12 @@ pub(crate) mod dep_tracking {
MergeFunctions, MergeFunctions,
PanicStrategy, PanicStrategy,
RelroLevel, RelroLevel,
Passes,
OptLevel, OptLevel,
LtoCli, LtoCli,
DebugInfo, DebugInfo,
DebugInfoCompression, DebugInfoCompression,
UnstableFeatures, UnstableFeatures,
NativeLib, NativeLib,
NativeLibKind,
SanitizerSet, SanitizerSet,
CFGuard, CFGuard,
CFProtection, CFProtection,

View File

@ -4,6 +4,7 @@ use crate::search_paths::SearchPath;
use crate::utils::NativeLib; use crate::utils::NativeLib;
use crate::{lint, EarlyErrorHandler}; use crate::{lint, EarlyErrorHandler};
use rustc_data_structures::profiling::TimePassesFormat; use rustc_data_structures::profiling::TimePassesFormat;
use rustc_data_structures::stable_hasher::Hash64;
use rustc_errors::ColorConfig; use rustc_errors::ColorConfig;
use rustc_errors::{LanguageIdentifier, TerminalUrl}; use rustc_errors::{LanguageIdentifier, TerminalUrl};
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, SanitizerSet}; use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, SanitizerSet};
@ -158,6 +159,10 @@ top_level_options!(
/// directory to store intermediate results. /// directory to store intermediate results.
incremental: Option<PathBuf> [UNTRACKED], incremental: Option<PathBuf> [UNTRACKED],
assert_incr_state: Option<IncrementalStateAssertion> [UNTRACKED], assert_incr_state: Option<IncrementalStateAssertion> [UNTRACKED],
/// Set by the `Config::hash_untracked_state` callback for custom
/// drivers to invalidate the incremental cache
#[rustc_lint_opt_deny_field_access("should only be used via `Config::hash_untracked_state`")]
untracked_state_hash: Hash64 [TRACKED_NO_CRATE_HASH],
unstable_opts: UnstableOptions [SUBSTRUCT], unstable_opts: UnstableOptions [SUBSTRUCT],
prints: Vec<PrintRequest> [UNTRACKED], prints: Vec<PrintRequest> [UNTRACKED],

View File

@ -262,6 +262,7 @@ pub(crate) fn create_config(
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES, locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
lint_caps, lint_caps,
parse_sess_created: None, parse_sess_created: None,
hash_untracked_state: None,
register_lints: Some(Box::new(crate::lint::register_lints)), register_lints: Some(Box::new(crate::lint::register_lints)),
override_queries: Some(|_sess, providers| { override_queries: Some(|_sess, providers| {
// We do not register late module lints, so this only runs `MissingDoc`. // We do not register late module lints, so this only runs `MissingDoc`.

View File

@ -104,6 +104,7 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES, locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
lint_caps, lint_caps,
parse_sess_created: None, parse_sess_created: None,
hash_untracked_state: None,
register_lints: Some(Box::new(crate::lint::register_lints)), register_lints: Some(Box::new(crate::lint::register_lints)),
override_queries: None, override_queries: None,
make_codegen_backend: None, make_codegen_backend: None,

View File

@ -57,6 +57,7 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
locale_resources: &[], locale_resources: &[],
lint_caps: Default::default(), lint_caps: Default::default(),
parse_sess_created: None, parse_sess_created: None,
hash_untracked_state: None,
register_lints: None, register_lints: None,
override_queries: None, override_queries: None,
make_codegen_backend: None, make_codegen_backend: None,