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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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;
Lines changed: 25 additions & 28 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 0 additions & 4 deletions
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

Lines changed: 0 additions & 10 deletions
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

Lines changed: 0 additions & 5 deletions
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-
}

0 commit comments

Comments
 (0)