Skip to content

Commit 508fb37

Browse files
authored
Rollup merge of rust-lang#92584 - lcnr:query-stable-lint, r=estebank
add rustc lint, warning when iterating over hashmaps 2 first introduced in rust-lang#89558 and reverted in rust-lang#90380 due to its perf impact r? `@estebank`
2 parents 9872329 + 4bbe970 commit 508fb37

File tree

42 files changed

+310
-79
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+310
-79
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#![feature(box_patterns)]
3535
#![feature(never_type)]
3636
#![recursion_limit = "256"]
37+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
3738

3839
use rustc_ast::token::{self, Token};
3940
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree};

compiler/rustc_ast_passes/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![feature(box_patterns)]
99
#![feature(let_else)]
1010
#![recursion_limit = "256"]
11+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
1112

1213
pub mod ast_validation;
1314
pub mod feature_gate;

compiler/rustc_borrowck/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(trusted_step)]
1010
#![feature(try_blocks)]
1111
#![recursion_limit = "256"]
12+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
1213

1314
#[macro_use]
1415
extern crate rustc_middle;

compiler/rustc_builtin_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![feature(proc_macro_internals)]
1313
#![feature(proc_macro_quote)]
1414
#![recursion_limit = "256"]
15+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
1516

1617
extern crate proc_macro;
1718

compiler/rustc_codegen_llvm/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(extern_types)]
1212
#![feature(nll)]
1313
#![recursion_limit = "256"]
14+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
1415

1516
use back::write::{create_informational_target_machine, create_target_machine};
1617

compiler/rustc_codegen_ssa/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![feature(nll)]
88
#![feature(associated_type_bounds)]
99
#![recursion_limit = "256"]
10+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
1011

1112
//! This crate contains codegen code that is used by all codegen backends (LLVM and others).
1213
//! The backend-agnostic functions of this crate use functions defined in various traits that

compiler/rustc_const_eval/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Rust MIR: a lowered representation of Rust.
2222
#![feature(trusted_step)]
2323
#![feature(try_blocks)]
2424
#![recursion_limit = "256"]
25+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
2526

2627
#[macro_use]
2728
extern crate tracing;

compiler/rustc_data_structures/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#![feature(vec_into_raw_parts)]
2727
#![allow(rustc::default_hash_types)]
2828
#![deny(unaligned_references)]
29+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
2930

3031
#[macro_use]
3132
extern crate tracing;

compiler/rustc_driver/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![feature(nll)]
99
#![feature(once_cell)]
1010
#![recursion_limit = "256"]
11+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
1112

1213
#[macro_use]
1314
extern crate tracing;
@@ -845,7 +846,7 @@ Available lint options:
845846
let builtin = sort_lints(sess, builtin);
846847

847848
let (plugin_groups, builtin_groups): (Vec<_>, _) =
848-
lint_store.get_lint_groups().iter().cloned().partition(|&(.., p)| p);
849+
lint_store.get_lint_groups().partition(|&(.., p)| p);
849850
let plugin_groups = sort_lint_groups(plugin_groups);
850851
let builtin_groups = sort_lint_groups(builtin_groups);
851852

compiler/rustc_errors/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![feature(if_let_guard)]
99
#![feature(let_else)]
1010
#![feature(nll)]
11+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
1112

1213
#[macro_use]
1314
extern crate rustc_macros;

compiler/rustc_expand/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(proc_macro_span)]
1010
#![feature(try_blocks)]
1111
#![recursion_limit = "256"]
12+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
1213

1314
#[macro_use]
1415
extern crate rustc_macros;

compiler/rustc_feature/src/builtin_attrs.rs

+3
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,9 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
581581
rustc_attr!(
582582
rustc_trivial_field_reads, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE
583583
),
584+
// Used by the `rustc::potential_query_instability` lint to warn methods which
585+
// might not be stable during incremental compilation.
586+
rustc_attr!(rustc_lint_query_instability, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
584587

585588
// ==========================================================================
586589
// Internal attributes, Const related:

compiler/rustc_incremental/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(let_else)]
66
#![feature(nll)]
77
#![recursion_limit = "256"]
8+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
89

910
#[macro_use]
1011
extern crate rustc_middle;

compiler/rustc_infer/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#![feature(min_specialization)]
2424
#![feature(label_break_value)]
2525
#![recursion_limit = "512"] // For rustdoc
26+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
2627

2728
#[macro_use]
2829
extern crate rustc_macros;

compiler/rustc_interface/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![feature(nll)]
77
#![feature(once_cell)]
88
#![recursion_limit = "256"]
9+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
910

1011
mod callbacks;
1112
pub mod interface;

compiler/rustc_lint/src/context.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ impl LintStore {
143143
&self.lints
144144
}
145145

146-
pub fn get_lint_groups<'t>(&'t self) -> Vec<(&'static str, Vec<LintId>, bool)> {
146+
pub fn get_lint_groups<'t>(
147+
&'t self,
148+
) -> impl Iterator<Item = (&'static str, Vec<LintId>, bool)> + 't {
149+
// This function is not used in a way which observes the order of lints.
150+
#[cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
147151
self.lint_groups
148152
.iter()
149153
.filter(|(_, LintGroup { depr, .. })| {
@@ -153,7 +157,6 @@ impl LintStore {
153157
.map(|(k, LintGroup { lint_ids, from_plugin, .. })| {
154158
(*k, lint_ids.clone(), *from_plugin)
155159
})
156-
.collect()
157160
}
158161

159162
pub fn register_early_pass(

compiler/rustc_lint/src/internal.rs

+59-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}
55
use rustc_ast as ast;
66
use rustc_errors::Applicability;
77
use rustc_hir::def::Res;
8-
use rustc_hir::{GenericArg, HirId, Item, ItemKind, Node, Path, PathSegment, QPath, Ty, TyKind};
8+
use rustc_hir::{Expr, ExprKind, GenericArg, Path, PathSegment, QPath};
9+
use rustc_hir::{HirId, Item, ItemKind, Node, Ty, TyKind};
910
use rustc_middle::ty;
1011
use rustc_session::{declare_lint_pass, declare_tool_lint};
1112
use rustc_span::hygiene::{ExpnKind, MacroKind};
@@ -48,6 +49,63 @@ impl LateLintPass<'_> for DefaultHashTypes {
4849
}
4950
}
5051

52+
declare_tool_lint! {
53+
pub rustc::POTENTIAL_QUERY_INSTABILITY,
54+
Allow,
55+
"require explicit opt-in when using potentially unstable methods or functions",
56+
report_in_external_macro: true
57+
}
58+
59+
declare_lint_pass!(QueryStability => [POTENTIAL_QUERY_INSTABILITY]);
60+
61+
impl LateLintPass<'_> for QueryStability {
62+
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
63+
// FIXME(rustdoc): This lint uses typecheck results, causing rustdoc to
64+
// error if there are resolution failures.
65+
//
66+
// As internal lints are currently always run if there are `unstable_options`,
67+
// they are added to the lint store of rustdoc. Internal lints are also
68+
// not used via the `lint_mod` query. Crate lints run outside of a query
69+
// so rustdoc currently doesn't disable them.
70+
//
71+
// Instead of relying on this, either change crate lints to a query disabled by
72+
// rustdoc, only run internal lints if the user is explicitly opting in
73+
// or figure out a different way to avoid running lints for rustdoc.
74+
if cx.tcx.sess.opts.actually_rustdoc {
75+
return;
76+
}
77+
78+
let (span, def_id, substs) = match expr.kind {
79+
ExprKind::MethodCall(segment, _, _)
80+
if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) =>
81+
{
82+
(segment.ident.span, def_id, cx.typeck_results().node_substs(expr.hir_id))
83+
},
84+
_ => {
85+
let &ty::FnDef(def_id, substs) =
86+
cx.typeck_results()
87+
.node_type(expr.hir_id)
88+
.kind() else { return };
89+
(expr.span, def_id, substs)
90+
}
91+
};
92+
if let Ok(Some(instance)) = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, substs) {
93+
let def_id = instance.def_id();
94+
if cx.tcx.has_attr(def_id, sym::rustc_lint_query_instability) {
95+
cx.struct_span_lint(POTENTIAL_QUERY_INSTABILITY, span, |lint| {
96+
let msg = format!(
97+
"using `{}` can result in unstable query results",
98+
cx.tcx.item_name(def_id)
99+
);
100+
lint.build(&msg)
101+
.note("if you believe this case to be fine, allow this lint and add a comment explaining your rationale")
102+
.emit();
103+
})
104+
}
105+
}
106+
}
107+
}
108+
51109
declare_tool_lint! {
52110
pub rustc::USAGE_OF_TY_TYKIND,
53111
Allow,

compiler/rustc_lint/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
#![feature(bool_to_option)]
3131
#![feature(box_patterns)]
3232
#![feature(crate_visibility_modifier)]
33+
#![feature(if_let_guard)]
3334
#![feature(iter_order_by)]
3435
#![feature(let_else)]
3536
#![feature(never_type)]
3637
#![feature(nll)]
3738
#![feature(control_flow_enum)]
3839
#![recursion_limit = "256"]
40+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
3941

4042
#[macro_use]
4143
extern crate rustc_middle;
@@ -493,6 +495,8 @@ fn register_internals(store: &mut LintStore) {
493495
store.register_early_pass(|| Box::new(LintPassImpl));
494496
store.register_lints(&DefaultHashTypes::get_lints());
495497
store.register_late_pass(|| Box::new(DefaultHashTypes));
498+
store.register_lints(&QueryStability::get_lints());
499+
store.register_late_pass(|| Box::new(QueryStability));
496500
store.register_lints(&ExistingDocKeyword::get_lints());
497501
store.register_late_pass(|| Box::new(ExistingDocKeyword));
498502
store.register_lints(&TyTyKind::get_lints());
@@ -505,6 +509,7 @@ fn register_internals(store: &mut LintStore) {
505509
None,
506510
vec![
507511
LintId::of(DEFAULT_HASH_TYPES),
512+
LintId::of(POTENTIAL_QUERY_INSTABILITY),
508513
LintId::of(USAGE_OF_TY_TYKIND),
509514
LintId::of(PASS_BY_VALUE),
510515
LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),

compiler/rustc_metadata/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(try_blocks)]
1010
#![feature(never_type)]
1111
#![recursion_limit = "256"]
12+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
1213

1314
extern crate proc_macro;
1415

compiler/rustc_middle/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#![feature(nonzero_ops)]
5757
#![feature(unwrap_infallible)]
5858
#![recursion_limit = "512"]
59+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
5960

6061
#[macro_use]
6162
extern crate bitflags;

compiler/rustc_mir_build/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(once_cell)]
1010
#![feature(min_specialization)]
1111
#![recursion_limit = "256"]
12+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
1213

1314
#[macro_use]
1415
extern crate tracing;

compiler/rustc_mir_transform/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![feature(trusted_step)]
1111
#![feature(try_blocks)]
1212
#![recursion_limit = "256"]
13+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
1314

1415
#[macro_use]
1516
extern crate tracing;

compiler/rustc_monomorphize/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![feature(control_flow_enum)]
55
#![feature(let_else)]
66
#![recursion_limit = "256"]
7+
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]
78

89
#[macro_use]
910
extern crate tracing;

0 commit comments

Comments
 (0)