Skip to content

Commit 0c051c8

Browse files
authored
Rollup merge of #136671 - nnethercote:middle-limits, r=Nadrieril
Overhaul `rustc_middle::limits` In particular, to make `pattern_complexity` work more like other limits, which then enables some other simplifications. r? ``@Nadrieril``
2 parents f3a4f1a + 7a8c0fc commit 0c051c8

File tree

28 files changed

+100
-97
lines changed

28 files changed

+100
-97
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
11491149
"the `#[omit_gdb_pretty_printer_section]` attribute is just used for the Rust test suite",
11501150
),
11511151
rustc_attr!(
1152-
TEST, pattern_complexity, CrateLevel, template!(NameValueStr: "N"),
1152+
TEST, pattern_complexity_limit, CrateLevel, template!(NameValueStr: "N"),
11531153
ErrorFollowing, EncodeCrossCrate::No,
11541154
),
11551155
];

compiler/rustc_feature/src/unstable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ declare_features! (
227227
/// Allows using `#[omit_gdb_pretty_printer_section]`.
228228
(internal, omit_gdb_pretty_printer_section, "1.5.0", None),
229229
/// Set the maximum pattern complexity allowed (not limited by default).
230-
(internal, pattern_complexity, "1.78.0", None),
230+
(internal, pattern_complexity_limit, "1.78.0", None),
231231
/// Allows using pattern types.
232232
(internal, pattern_types, "1.79.0", Some(123646)),
233233
/// Allows using `#[prelude_import]` on glob `use` items.

compiler/rustc_interface/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ interface_ignoring_out_dir = ignoring --out-dir flag due to -o flag
3333
interface_input_file_would_be_overwritten =
3434
the input file "{$path}" would be overwritten by the generated executable
3535
36+
interface_limit_invalid =
37+
`limit` must be a non-negative integer
38+
.label = {$error_str}
39+
3640
interface_mixed_bin_crate =
3741
cannot mix `bin` crate type with others
3842

compiler/rustc_interface/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,13 @@ pub(crate) struct AbiRequiredTargetFeature<'a> {
127127
pub feature: &'a str,
128128
pub enabled: &'a str,
129129
}
130+
131+
#[derive(Diagnostic)]
132+
#[diag(interface_limit_invalid)]
133+
pub(crate) struct LimitInvalid<'a> {
134+
#[primary_span]
135+
pub span: Span,
136+
#[label]
137+
pub value_span: Span,
138+
pub error_str: &'a str,
139+
}

compiler/rustc_interface/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
mod callbacks;
1111
pub mod errors;
1212
pub mod interface;
13+
mod limits;
1314
pub mod passes;
1415
mod proc_macro_decls;
1516
mod queries;
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,66 @@
11
//! Registering limits:
2-
//! * recursion_limit,
3-
//! * move_size_limit, and
4-
//! * type_length_limit
2+
//! - recursion_limit: there are various parts of the compiler that must impose arbitrary limits
3+
//! on how deeply they recurse to prevent stack overflow.
4+
//! - move_size_limit
5+
//! - type_length_limit
6+
//! - pattern_complexity_limit
57
//!
6-
//! There are various parts of the compiler that must impose arbitrary limits
7-
//! on how deeply they recurse to prevent stack overflow. Users can override
8-
//! this via an attribute on the crate like `#![recursion_limit="22"]`. This pass
9-
//! just peeks and looks for that attribute.
8+
//! Users can override these limits via an attribute on the crate like
9+
//! `#![recursion_limit="22"]`. This pass just looks for those attributes.
1010
1111
use std::num::IntErrorKind;
1212

1313
use rustc_ast::attr::AttributeExt;
14+
use rustc_middle::bug;
15+
use rustc_middle::query::Providers;
1416
use rustc_session::{Limit, Limits, Session};
1517
use rustc_span::{Symbol, sym};
1618

17-
use crate::error::LimitInvalid;
18-
use crate::query::Providers;
19+
use crate::errors::LimitInvalid;
1920

20-
pub fn provide(providers: &mut Providers) {
21+
pub(crate) fn provide(providers: &mut Providers) {
2122
providers.limits = |tcx, ()| Limits {
2223
recursion_limit: get_recursion_limit(tcx.hir().krate_attrs(), tcx.sess),
2324
move_size_limit: get_limit(
2425
tcx.hir().krate_attrs(),
2526
tcx.sess,
2627
sym::move_size_limit,
27-
tcx.sess.opts.unstable_opts.move_size_limit.unwrap_or(0),
28+
Limit::new(tcx.sess.opts.unstable_opts.move_size_limit.unwrap_or(0)),
2829
),
2930
type_length_limit: get_limit(
3031
tcx.hir().krate_attrs(),
3132
tcx.sess,
3233
sym::type_length_limit,
33-
2usize.pow(24),
34+
Limit::new(2usize.pow(24)),
35+
),
36+
pattern_complexity_limit: get_limit(
37+
tcx.hir().krate_attrs(),
38+
tcx.sess,
39+
sym::pattern_complexity_limit,
40+
Limit::unlimited(),
3441
),
3542
}
3643
}
3744

38-
pub fn get_recursion_limit(krate_attrs: &[impl AttributeExt], sess: &Session) -> Limit {
39-
get_limit(krate_attrs, sess, sym::recursion_limit, 128)
45+
// This one is separate because it must be read prior to macro expansion.
46+
pub(crate) fn get_recursion_limit(krate_attrs: &[impl AttributeExt], sess: &Session) -> Limit {
47+
get_limit(krate_attrs, sess, sym::recursion_limit, Limit::new(128))
4048
}
4149

4250
fn get_limit(
4351
krate_attrs: &[impl AttributeExt],
4452
sess: &Session,
4553
name: Symbol,
46-
default: usize,
54+
default: Limit,
4755
) -> Limit {
48-
match get_limit_size(krate_attrs, sess, name) {
49-
Some(size) => Limit::new(size),
50-
None => Limit::new(default),
51-
}
52-
}
53-
54-
pub fn get_limit_size(
55-
krate_attrs: &[impl AttributeExt],
56-
sess: &Session,
57-
name: Symbol,
58-
) -> Option<usize> {
5956
for attr in krate_attrs {
6057
if !attr.has_name(name) {
6158
continue;
6259
}
6360

6461
if let Some(sym) = attr.value_str() {
6562
match sym.as_str().parse() {
66-
Ok(n) => return Some(n),
63+
Ok(n) => return Limit::new(n),
6764
Err(e) => {
6865
let error_str = match e.kind() {
6966
IntErrorKind::PosOverflow => "`limit` is too large",
@@ -84,5 +81,5 @@ pub fn get_limit_size(
8481
}
8582
}
8683
}
87-
None
84+
default
8885
}

compiler/rustc_interface/src/passes.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use rustc_trait_selection::traits;
3939
use tracing::{info, instrument};
4040

4141
use crate::interface::Compiler;
42-
use crate::{errors, proc_macro_decls, util};
42+
use crate::{errors, limits, proc_macro_decls, util};
4343

4444
pub fn parse<'a>(sess: &'a Session) -> ast::Crate {
4545
let krate = sess
@@ -687,6 +687,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
687687
|tcx, _| tcx.arena.alloc_from_iter(tcx.resolutions(()).stripped_cfg_items.steal());
688688
providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1;
689689
providers.early_lint_checks = early_lint_checks;
690+
limits::provide(providers);
690691
proc_macro_decls::provide(providers);
691692
rustc_const_eval::provide(providers);
692693
rustc_middle::hir::provide(providers);
@@ -1134,7 +1135,7 @@ fn get_recursion_limit(krate_attrs: &[ast::Attribute], sess: &Session) -> Limit
11341135
// because that would require expanding this while in the middle of expansion, which needs to
11351136
// know the limit before expanding.
11361137
let _ = validate_and_find_value_str_builtin_attr(sym::recursion_limit, sess, krate_attrs);
1137-
rustc_middle::middle::limits::get_recursion_limit(krate_attrs, sess)
1138+
crate::limits::get_recursion_limit(krate_attrs, sess)
11381139
}
11391140

11401141
/// Validate *all* occurrences of the given "[value-str]" built-in attribute and return the first find.

compiler/rustc_middle/messages.ftl

-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ middle_failed_writing_file =
8181
middle_layout_references_error =
8282
the type has an unknown layout
8383
84-
middle_limit_invalid =
85-
`limit` must be a non-negative integer
86-
.label = {$error_str}
87-
8884
middle_opaque_hidden_type_mismatch =
8985
concrete type differs from previous defining opaque type use
9086
.label = expected `{$self_ty}`, got `{$other_ty}`

compiler/rustc_middle/src/error.rs

-10
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,6 @@ pub enum TypeMismatchReason {
6767
},
6868
}
6969

70-
#[derive(Diagnostic)]
71-
#[diag(middle_limit_invalid)]
72-
pub(crate) struct LimitInvalid<'a> {
73-
#[primary_span]
74-
pub span: Span,
75-
#[label]
76-
pub value_span: Span,
77-
pub error_str: &'a str,
78-
}
79-
8070
#[derive(Diagnostic)]
8171
#[diag(middle_recursion_limit_reached)]
8272
#[help]

compiler/rustc_middle/src/middle/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ pub mod lib_features {
3030
}
3131
}
3232
}
33-
pub mod limits;
3433
pub mod privacy;
3534
pub mod region;
3635
pub mod resolve_bound_vars;
3736
pub mod stability;
38-
39-
pub fn provide(providers: &mut crate::query::Providers) {
40-
limits::provide(providers);
41-
}

compiler/rustc_middle/src/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2168,6 +2168,10 @@ impl<'tcx> TyCtxt<'tcx> {
21682168
self.limits(()).move_size_limit
21692169
}
21702170

2171+
pub fn pattern_complexity_limit(self) -> Limit {
2172+
self.limits(()).pattern_complexity_limit
2173+
}
2174+
21712175
/// All traits in the crate graph, including those not visible to the user.
21722176
pub fn all_traits(self) -> impl Iterator<Item = DefId> + 'tcx {
21732177
iter::once(LOCAL_CRATE)

compiler/rustc_middle/src/ty/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2168,7 +2168,6 @@ pub fn provide(providers: &mut Providers) {
21682168
util::provide(providers);
21692169
print::provide(providers);
21702170
super::util::bug::provide(providers);
2171-
super::middle::provide(providers);
21722171
*providers = Providers {
21732172
trait_impls_of: trait_def::trait_impls_of_provider,
21742173
incoherent_impls: trait_def::incoherent_impls_provider,

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_hir::{self as hir, BindingMode, ByRef, HirId};
1010
use rustc_infer::infer::TyCtxtInferExt;
1111
use rustc_lint::Level;
1212
use rustc_middle::bug;
13-
use rustc_middle::middle::limits::get_limit_size;
1413
use rustc_middle::thir::visit::Visitor;
1514
use rustc_middle::thir::*;
1615
use rustc_middle::ty::print::with_no_trimmed_paths;
@@ -25,7 +24,7 @@ use rustc_session::lint::builtin::{
2524
};
2625
use rustc_span::edit_distance::find_best_match_for_name;
2726
use rustc_span::hygiene::DesugaringKind;
28-
use rustc_span::{Ident, Span, sym};
27+
use rustc_span::{Ident, Span};
2928
use rustc_trait_selection::infer::InferCtxtExt;
3029
use tracing::instrument;
3130

@@ -404,18 +403,11 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
404403
arms: &[MatchArm<'p, 'tcx>],
405404
scrut_ty: Ty<'tcx>,
406405
) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
407-
let pattern_complexity_limit =
408-
get_limit_size(cx.tcx.hir().krate_attrs(), cx.tcx.sess, sym::pattern_complexity);
409-
let report = rustc_pattern_analysis::rustc::analyze_match(
410-
&cx,
411-
&arms,
412-
scrut_ty,
413-
pattern_complexity_limit,
414-
)
415-
.map_err(|err| {
416-
self.error = Err(err);
417-
err
418-
})?;
406+
let report =
407+
rustc_pattern_analysis::rustc::analyze_match(&cx, &arms, scrut_ty).map_err(|err| {
408+
self.error = Err(err);
409+
err
410+
})?;
419411

420412
// Warn unreachable subpatterns.
421413
for (arm, is_useful) in report.arm_usefulness.iter() {

compiler/rustc_pattern_analysis/src/rustc.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1084,12 +1084,16 @@ pub fn analyze_match<'p, 'tcx>(
10841084
tycx: &RustcPatCtxt<'p, 'tcx>,
10851085
arms: &[MatchArm<'p, 'tcx>],
10861086
scrut_ty: Ty<'tcx>,
1087-
pattern_complexity_limit: Option<usize>,
10881087
) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
10891088
let scrut_ty = tycx.reveal_opaque_ty(scrut_ty);
10901089
let scrut_validity = PlaceValidity::from_bool(tycx.known_valid_scrutinee);
1091-
let report =
1092-
compute_match_usefulness(tycx, arms, scrut_ty, scrut_validity, pattern_complexity_limit)?;
1090+
let report = compute_match_usefulness(
1091+
tycx,
1092+
arms,
1093+
scrut_ty,
1094+
scrut_validity,
1095+
tycx.tcx.pattern_complexity_limit().0,
1096+
)?;
10931097

10941098
// Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting
10951099
// `if let`s. Only run if the match is exhaustive otherwise the error is redundant.

compiler/rustc_pattern_analysis/src/usefulness.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -795,20 +795,21 @@ struct UsefulnessCtxt<'a, 'p, Cx: PatCx> {
795795
/// Track information about the usefulness of branch patterns (see definition of "branch
796796
/// pattern" at [`BranchPatUsefulness`]).
797797
branch_usefulness: FxHashMap<PatId, BranchPatUsefulness<'p, Cx>>,
798-
complexity_limit: Option<usize>,
798+
// Ideally this field would have type `Limit`, but this crate is used by
799+
// rust-analyzer which cannot have a dependency on `Limit`, because `Limit`
800+
// is from crate `rustc_session` which uses unstable Rust features.
801+
complexity_limit: usize,
799802
complexity_level: usize,
800803
}
801804

802805
impl<'a, 'p, Cx: PatCx> UsefulnessCtxt<'a, 'p, Cx> {
803806
fn increase_complexity_level(&mut self, complexity_add: usize) -> Result<(), Cx::Error> {
804807
self.complexity_level += complexity_add;
805-
if self
806-
.complexity_limit
807-
.is_some_and(|complexity_limit| complexity_limit < self.complexity_level)
808-
{
809-
return self.tycx.complexity_exceeded();
808+
if self.complexity_level <= self.complexity_limit {
809+
Ok(())
810+
} else {
811+
self.tycx.complexity_exceeded()
810812
}
811-
Ok(())
812813
}
813814
}
814815

@@ -1834,7 +1835,7 @@ pub fn compute_match_usefulness<'p, Cx: PatCx>(
18341835
arms: &[MatchArm<'p, Cx>],
18351836
scrut_ty: Cx::Ty,
18361837
scrut_validity: PlaceValidity,
1837-
complexity_limit: Option<usize>,
1838+
complexity_limit: usize,
18381839
) -> Result<UsefulnessReport<'p, Cx>, Cx::Error> {
18391840
let mut cx = UsefulnessCtxt {
18401841
tycx,

compiler/rustc_pattern_analysis/tests/common/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub fn compute_match_usefulness<'p>(
124124
arms: &[MatchArm<'p, Cx>],
125125
ty: Ty,
126126
scrut_validity: PlaceValidity,
127-
complexity_limit: Option<usize>,
127+
complexity_limit: usize,
128128
) -> Result<UsefulnessReport<'p, Cx>, ()> {
129129
init_tracing();
130130
rustc_pattern_analysis::usefulness::compute_match_usefulness(

compiler/rustc_pattern_analysis/tests/complexity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn check(patterns: &[DeconstructedPat<Cx>], complexity_limit: usize) -> Result<(
1414
let ty = *patterns[0].ty();
1515
let arms: Vec<_> =
1616
patterns.iter().map(|pat| MatchArm { pat, has_guard: false, arm_data: () }).collect();
17-
compute_match_usefulness(arms.as_slice(), ty, PlaceValidity::ValidOnly, Some(complexity_limit))
17+
compute_match_usefulness(arms.as_slice(), ty, PlaceValidity::ValidOnly, complexity_limit)
1818
.map(|_report| ())
1919
}
2020

compiler/rustc_pattern_analysis/tests/exhaustiveness.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ fn check(patterns: Vec<DeconstructedPat<Cx>>) -> Vec<WitnessPat<Cx>> {
1414
let arms: Vec<_> =
1515
patterns.iter().map(|pat| MatchArm { pat, has_guard: false, arm_data: () }).collect();
1616
let report =
17-
compute_match_usefulness(arms.as_slice(), ty, PlaceValidity::ValidOnly, None).unwrap();
17+
compute_match_usefulness(arms.as_slice(), ty, PlaceValidity::ValidOnly, usize::MAX)
18+
.unwrap();
1819
report.non_exhaustiveness_witnesses
1920
}
2021

compiler/rustc_pattern_analysis/tests/intersection.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ fn check(patterns: Vec<DeconstructedPat<Cx>>) -> Vec<Vec<usize>> {
1414
let arms: Vec<_> =
1515
patterns.iter().map(|pat| MatchArm { pat, has_guard: false, arm_data: () }).collect();
1616
let report =
17-
compute_match_usefulness(arms.as_slice(), ty, PlaceValidity::ValidOnly, None).unwrap();
17+
compute_match_usefulness(arms.as_slice(), ty, PlaceValidity::ValidOnly, usize::MAX)
18+
.unwrap();
1819
report.arm_intersections.into_iter().map(|bitset| bitset.iter().collect()).collect()
1920
}
2021

compiler/rustc_session/src/session.rs

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ impl Limit {
6767
Limit(value)
6868
}
6969

70+
/// Create a new unlimited limit.
71+
pub fn unlimited() -> Self {
72+
Limit(usize::MAX)
73+
}
74+
7075
/// Check that `value` is within the limit. Ensures that the same comparisons are used
7176
/// throughout the compiler, as mismatches can cause ICEs, see #72540.
7277
#[inline]
@@ -119,6 +124,8 @@ pub struct Limits {
119124
pub move_size_limit: Limit,
120125
/// The maximum length of types during monomorphization.
121126
pub type_length_limit: Limit,
127+
/// The maximum pattern complexity allowed (internal only).
128+
pub pattern_complexity_limit: Limit,
122129
}
123130

124131
pub struct CompilerIO {

0 commit comments

Comments
 (0)