Skip to content

Commit c94c74e

Browse files
committed
Opt out of CTFE if the 'const_eval_limit' is set to 0
1 parent 288e142 commit c94c74e

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

src/doc/unstable-book/src/language-features/const-eval-limit.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
The tracking issue for this feature is: [#67217]
44

5-
[#57563]: https://github.com/rust-lang/rust/issues/67217
5+
[#67217]: https://github.com/rust-lang/rust/issues/67217
66

7-
The `const_eval_limit` allows someone to limit the evaluation steps the CTFE undertakes to evaluate a `const fn`.
7+
The `const_eval_limit` allows someone to limit the evaluation steps the CTFE undertakes to evaluate a `const fn`.

src/librustc_mir/const_eval/eval_queries.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub(super) fn mk_eval_cx<'mir, 'tcx>(
8989
InterpCx::new(
9090
tcx.at(span),
9191
param_env,
92-
CompileTimeInterpreter::new(),
92+
CompileTimeInterpreter::new(*tcx.sess.const_eval_limit.get()),
9393
MemoryExtra { can_access_statics },
9494
)
9595
}
@@ -297,7 +297,7 @@ pub fn const_eval_raw_provider<'tcx>(
297297
let mut ecx = InterpCx::new(
298298
tcx.at(span),
299299
key.param_env,
300-
CompileTimeInterpreter::new(),
300+
CompileTimeInterpreter::new(*tcx.sess.const_eval_limit.get()),
301301
MemoryExtra { can_access_statics: is_static },
302302
);
303303

src/librustc_mir/const_eval/machine.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc::ty::layout::HasTyCtxt;
33
use rustc::ty::{self, Ty};
44
use std::borrow::{Borrow, Cow};
55
use std::collections::hash_map::Entry;
6+
use std::convert::TryFrom;
67
use std::hash::Hash;
78

89
use rustc_data_structures::fx::FxHashMap;
@@ -85,9 +86,6 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
8586
}
8687
}
8788

88-
/// Number of steps until the detector even starts doing anything.
89-
/// Also, a warning is shown to the user when this number is reached.
90-
const STEPS_UNTIL_DETECTOR_ENABLED: isize = 1_000_000;
9189
/// The number of steps between loop detector snapshots.
9290
/// Should be a power of two for performance reasons.
9391
const DETECTOR_SNAPSHOT_PERIOD: isize = 256;
@@ -100,6 +98,8 @@ pub struct CompileTimeInterpreter<'mir, 'tcx> {
10098
/// detector period.
10199
pub(super) steps_since_detector_enabled: isize,
102100

101+
pub(super) is_detector_enabled: bool,
102+
103103
/// Extra state to detect loops.
104104
pub(super) loop_detector: snapshot::InfiniteLoopDetector<'mir, 'tcx>,
105105
}
@@ -111,10 +111,14 @@ pub struct MemoryExtra {
111111
}
112112

113113
impl<'mir, 'tcx> CompileTimeInterpreter<'mir, 'tcx> {
114-
pub(super) fn new() -> Self {
114+
pub(super) fn new(const_eval_limit: usize) -> Self {
115+
let steps_until_detector_enabled =
116+
isize::try_from(const_eval_limit).unwrap_or(std::isize::MAX);
117+
115118
CompileTimeInterpreter {
116119
loop_detector: Default::default(),
117-
steps_since_detector_enabled: -STEPS_UNTIL_DETECTOR_ENABLED,
120+
steps_since_detector_enabled: -steps_until_detector_enabled,
121+
is_detector_enabled: const_eval_limit != 0,
118122
}
119123
}
120124
}
@@ -343,6 +347,10 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
343347
}
344348

345349
fn before_terminator(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
350+
if !ecx.machine.is_detector_enabled {
351+
return Ok(());
352+
}
353+
346354
{
347355
let steps = &mut ecx.machine.steps_since_detector_enabled;
348356

src/test/ui/consts/const_limit/const_eval_limit_reached.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
// only-x86_64
12
// check-pass
23
#![feature(const_eval_limit)]
34
#![const_eval_limit="2"]
45

56
const CONSTANT: usize = limit();
7+
//~^ WARNING Constant evaluating a complex constant, this might take some time
68

79
fn main() {
810
assert_eq!(CONSTANT, 1764);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
warning: Constant evaluating a complex constant, this might take some time
2+
--> $DIR/const_eval_limit_reached.rs:6:1
3+
|
4+
LL | const CONSTANT: usize = limit();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+

src/test/ui/consts/const_limit/feature-gate-const_eval_limit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0658]: the `#[const_eval_limit]` attribute is an experimental feature
44
LL | #![const_eval_limit="42"]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: for more information, see https://github.com/rust-lang/rust/issues/67217
7+
= note: see issue #67217 <https://github.com/rust-lang/rust/issues/67217> for more information
88
= help: add `#![feature(const_eval_limit)]` to the crate attributes to enable
99

1010
error: aborting due to previous error

0 commit comments

Comments
 (0)