Skip to content

Commit 337af5e

Browse files
committed
Prepare const_limit feature gate and attribute
1 parent 75cf41a commit 337af5e

File tree

8 files changed

+45
-1
lines changed

8 files changed

+45
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `const_limit`
2+
3+
The tracking issue for this feature is: [#67217]
4+
5+
[#57563]: https://github.com/rust-lang/rust/issues/67217
6+
7+
The `const_limit` allows someone to limit the evaluation steps the CTFE undertakes to evaluate a `const fn`.

src/librustc/middle/recursion_limit.rs renamed to src/librustc/middle/limits.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Recursion limit.
1+
// Registering limits, recursion_limit, type_length_limit and const_limit
22
//
33
// There are various parts of the compiler that must impose arbitrary limits
44
// on how deeply they recurse to prevent stack overflow. Users can override
@@ -16,6 +16,7 @@ use rustc_data_structures::sync::Once;
1616
pub fn update_limits(sess: &Session, krate: &ast::Crate) {
1717
update_limit(sess, krate, &sess.recursion_limit, sym::recursion_limit, 128);
1818
update_limit(sess, krate, &sess.type_length_limit, sym::type_length_limit, 1048576);
19+
update_limit(sess, krate, &sess.const_limit, sym::const_limit, 128);
1920
}
2021

2122
fn update_limit(

src/librustc_feature/active.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,9 @@ declare_features! (
532532
/// Allows using `&mut` in constant functions.
533533
(active, const_mut_refs, "1.41.0", Some(57349), None),
534534

535+
// Allows limiting the evaluation steps of const expressions
536+
(active, const_limit, "1.41.0", Some(67217), None),
537+
535538
/// Allows the use of `loop` and `while` in constants.
536539
(active, const_loop, "1.41.0", Some(52000), None),
537540

src/librustc_feature/builtin_attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
239239
// Limits:
240240
ungated!(recursion_limit, CrateLevel, template!(NameValueStr: "N")),
241241
ungated!(type_length_limit, CrateLevel, template!(NameValueStr: "N")),
242+
gated!(
243+
const_limit, CrateLevel, template!(NameValueStr: "N"), const_limit,
244+
experimental!(const_limit)
245+
),
242246

243247
// Entry point:
244248
ungated!(main, Normal, template!(Word)),

src/librustc_session/session.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ pub struct Session {
8888
/// The maximum length of types during monomorphization.
8989
pub type_length_limit: Once<usize>,
9090

91+
/// The maximum blocks a const expression can evaluate.
92+
pub const_limit: Once<usize>,
93+
9194
/// Map from imported macro spans (which consist of
9295
/// the localized span for the macro body) to the
9396
/// macro name and definition span in the source crate.
@@ -1053,6 +1056,7 @@ fn build_session_(
10531056
features: Once::new(),
10541057
recursion_limit: Once::new(),
10551058
type_length_limit: Once::new(),
1059+
const_limit: Once::new(),
10561060
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),
10571061
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
10581062
cgu_reuse_tracker,

src/librustc_span/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ symbols! {
208208
console,
209209
const_compare_raw_pointers,
210210
const_constructor,
211+
const_limit,
211212
const_extern_fn,
212213
const_fn,
213214
const_fn_union,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![const_limit="1"]
2+
//~^ ERROR the `#[const_limit]` attribute is an experimental feature [E0658]
3+
4+
const CONSTANT: usize = limit();
5+
6+
fn main() {}
7+
8+
const fn limit() -> usize {
9+
let x = 42;
10+
11+
x * 42
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: the `#[const_limit]` attribute is an experimental feature
2+
--> $DIR/feature-gate-const_limit.rs:1:1
3+
|
4+
LL | #![const_limit="1"]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/67217
8+
= help: add `#![feature(const_limit)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)