Skip to content

Commit 4af886f

Browse files
committed
Auto merge of rust-lang#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`
2 parents 98c1e3d + 59f6f04 commit 4af886f

File tree

7 files changed

+28
-7
lines changed

7 files changed

+28
-7
lines changed

compiler/rustc_driver_impl/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ fn run_compiler(
312312
locale_resources: DEFAULT_LOCALE_RESOURCES,
313313
lint_caps: Default::default(),
314314
parse_sess_created: None,
315+
hash_untracked_state: None,
315316
register_lints: None,
316317
override_queries: None,
317318
make_codegen_backend,

compiler/rustc_interface/src/interface.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_ast::{self as ast, LitKind, MetaItemKind};
55
use rustc_codegen_ssa::traits::CodegenBackend;
66
use rustc_data_structures::defer;
77
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8+
use rustc_data_structures::stable_hasher::StableHasher;
89
use rustc_data_structures::sync::Lrc;
910
use rustc_errors::registry::Registry;
1011
use rustc_errors::{ErrorGuaranteed, Handler};
@@ -260,6 +261,12 @@ pub struct Config {
260261
/// This is a callback from the driver that is called when [`ParseSess`] is created.
261262
pub parse_sess_created: Option<Box<dyn FnOnce(&mut ParseSess) + Send>>,
262263

264+
/// This is a callback to hash otherwise untracked state used by the caller, if the
265+
/// hash changes between runs the incremental cache will be cleared.
266+
///
267+
/// e.g. used by Clippy to hash its config file
268+
pub hash_untracked_state: Option<Box<dyn FnOnce(&Session, &mut StableHasher) + Send>>,
269+
263270
/// This is a callback from the driver that is called when we're registering lints;
264271
/// it is called during plugin registration when we have the LintStore in a non-shared state.
265272
///
@@ -269,8 +276,6 @@ pub struct Config {
269276

270277
/// This is a callback from the driver that is called just after we have populated
271278
/// the list of queries.
272-
///
273-
/// The second parameter is local providers and the third parameter is external providers.
274279
pub override_queries: Option<fn(&Session, &mut Providers)>,
275280

276281
/// 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
330335
parse_sess_created(&mut sess.parse_sess);
331336
}
332337

338+
if let Some(hash_untracked_state) = config.hash_untracked_state {
339+
let mut hasher = StableHasher::new();
340+
hash_untracked_state(&sess, &mut hasher);
341+
sess.opts.untracked_state_hash = hasher.finish()
342+
}
343+
333344
let compiler = Compiler {
334345
sess: Lrc::new(sess),
335346
codegen_backend: Lrc::from(codegen_backend),

compiler/rustc_session/src/config.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,7 @@ impl Default for Options {
10471047
target_triple: TargetTriple::from_triple(host_triple()),
10481048
test: false,
10491049
incremental: None,
1050+
untracked_state_hash: Default::default(),
10501051
unstable_opts: Default::default(),
10511052
prints: Vec::new(),
10521053
cg: Default::default(),
@@ -2889,6 +2890,7 @@ pub fn build_session_options(
28892890
target_triple,
28902891
test,
28912892
incremental,
2893+
untracked_state_hash: Default::default(),
28922894
unstable_opts,
28932895
prints,
28942896
cg,
@@ -3167,17 +3169,17 @@ impl PpMode {
31673169
/// we have an opt-in scheme here, so one is hopefully forced to think about
31683170
/// how the hash should be calculated when adding a new command-line argument.
31693171
pub(crate) mod dep_tracking {
3170-
use super::Polonius;
31713172
use super::{
31723173
BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, DebugInfoCompression,
31733174
ErrorOutputType, InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail,
3174-
LtoCli, OomStrategy, OptLevel, OutFileName, OutputType, OutputTypes, Passes,
3175+
LtoCli, OomStrategy, OptLevel, OutFileName, OutputType, OutputTypes, Polonius,
31753176
ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath,
31763177
SymbolManglingVersion, TraitSolver, TrimmedDefPaths,
31773178
};
31783179
use crate::lint;
31793180
use crate::options::WasiExecModel;
3180-
use crate::utils::{NativeLib, NativeLibKind};
3181+
use crate::utils::NativeLib;
3182+
use rustc_data_structures::stable_hasher::Hash64;
31813183
use rustc_errors::LanguageIdentifier;
31823184
use rustc_feature::UnstableFeatures;
31833185
use rustc_span::edition::Edition;
@@ -3233,6 +3235,7 @@ pub(crate) mod dep_tracking {
32333235
usize,
32343236
NonZeroUsize,
32353237
u64,
3238+
Hash64,
32363239
String,
32373240
PathBuf,
32383241
lint::Level,
@@ -3247,14 +3250,12 @@ pub(crate) mod dep_tracking {
32473250
MergeFunctions,
32483251
PanicStrategy,
32493252
RelroLevel,
3250-
Passes,
32513253
OptLevel,
32523254
LtoCli,
32533255
DebugInfo,
32543256
DebugInfoCompression,
32553257
UnstableFeatures,
32563258
NativeLib,
3257-
NativeLibKind,
32583259
SanitizerSet,
32593260
CFGuard,
32603261
CFProtection,

compiler/rustc_session/src/options.rs

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::search_paths::SearchPath;
44
use crate::utils::NativeLib;
55
use crate::{lint, EarlyErrorHandler};
66
use rustc_data_structures::profiling::TimePassesFormat;
7+
use rustc_data_structures::stable_hasher::Hash64;
78
use rustc_errors::ColorConfig;
89
use rustc_errors::{LanguageIdentifier, TerminalUrl};
910
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, SanitizerSet};
@@ -158,6 +159,10 @@ top_level_options!(
158159
/// directory to store intermediate results.
159160
incremental: Option<PathBuf> [UNTRACKED],
160161
assert_incr_state: Option<IncrementalStateAssertion> [UNTRACKED],
162+
/// Set by the `Config::hash_untracked_state` callback for custom
163+
/// drivers to invalidate the incremental cache
164+
#[rustc_lint_opt_deny_field_access("should only be used via `Config::hash_untracked_state`")]
165+
untracked_state_hash: Hash64 [TRACKED_NO_CRATE_HASH],
161166

162167
unstable_opts: UnstableOptions [SUBSTRUCT],
163168
prints: Vec<PrintRequest> [UNTRACKED],

src/librustdoc/core.rs

+1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ pub(crate) fn create_config(
262262
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
263263
lint_caps,
264264
parse_sess_created: None,
265+
hash_untracked_state: None,
265266
register_lints: Some(Box::new(crate::lint::register_lints)),
266267
override_queries: Some(|_sess, providers| {
267268
// We do not register late module lints, so this only runs `MissingDoc`.

src/librustdoc/doctest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
104104
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
105105
lint_caps,
106106
parse_sess_created: None,
107+
hash_untracked_state: None,
107108
register_lints: Some(Box::new(crate::lint::register_lints)),
108109
override_queries: None,
109110
make_codegen_backend: None,

tests/run-make-fulldeps/issue-19371/foo.rs

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
5757
locale_resources: &[],
5858
lint_caps: Default::default(),
5959
parse_sess_created: None,
60+
hash_untracked_state: None,
6061
register_lints: None,
6162
override_queries: None,
6263
make_codegen_backend: None,

0 commit comments

Comments
 (0)