Opt out of CTFE if the 'const_eval_limit' is set to 0

This commit is contained in:
Christoph Schmidler 2020-01-21 16:46:07 +01:00
parent 288e142737
commit c94c74e2d9
6 changed files with 26 additions and 10 deletions

View file

@ -2,6 +2,6 @@
The tracking issue for this feature is: [#67217]
[#57563]: https://github.com/rust-lang/rust/issues/67217
[#67217]: https://github.com/rust-lang/rust/issues/67217
The `const_eval_limit` allows someone to limit the evaluation steps the CTFE undertakes to evaluate a `const fn`.
The `const_eval_limit` allows someone to limit the evaluation steps the CTFE undertakes to evaluate a `const fn`.

View file

@ -89,7 +89,7 @@ pub(super) fn mk_eval_cx<'mir, 'tcx>(
InterpCx::new(
tcx.at(span),
param_env,
CompileTimeInterpreter::new(),
CompileTimeInterpreter::new(*tcx.sess.const_eval_limit.get()),
MemoryExtra { can_access_statics },
)
}
@ -297,7 +297,7 @@ pub fn const_eval_raw_provider<'tcx>(
let mut ecx = InterpCx::new(
tcx.at(span),
key.param_env,
CompileTimeInterpreter::new(),
CompileTimeInterpreter::new(*tcx.sess.const_eval_limit.get()),
MemoryExtra { can_access_statics: is_static },
);

View file

@ -3,6 +3,7 @@ use rustc::ty::layout::HasTyCtxt;
use rustc::ty::{self, Ty};
use std::borrow::{Borrow, Cow};
use std::collections::hash_map::Entry;
use std::convert::TryFrom;
use std::hash::Hash;
use rustc_data_structures::fx::FxHashMap;
@ -85,9 +86,6 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
}
}
/// Number of steps until the detector even starts doing anything.
/// Also, a warning is shown to the user when this number is reached.
const STEPS_UNTIL_DETECTOR_ENABLED: isize = 1_000_000;
/// The number of steps between loop detector snapshots.
/// Should be a power of two for performance reasons.
const DETECTOR_SNAPSHOT_PERIOD: isize = 256;
@ -100,6 +98,8 @@ pub struct CompileTimeInterpreter<'mir, 'tcx> {
/// detector period.
pub(super) steps_since_detector_enabled: isize,
pub(super) is_detector_enabled: bool,
/// Extra state to detect loops.
pub(super) loop_detector: snapshot::InfiniteLoopDetector<'mir, 'tcx>,
}
@ -111,10 +111,14 @@ pub struct MemoryExtra {
}
impl<'mir, 'tcx> CompileTimeInterpreter<'mir, 'tcx> {
pub(super) fn new() -> Self {
pub(super) fn new(const_eval_limit: usize) -> Self {
let steps_until_detector_enabled =
isize::try_from(const_eval_limit).unwrap_or(std::isize::MAX);
CompileTimeInterpreter {
loop_detector: Default::default(),
steps_since_detector_enabled: -STEPS_UNTIL_DETECTOR_ENABLED,
steps_since_detector_enabled: -steps_until_detector_enabled,
is_detector_enabled: const_eval_limit != 0,
}
}
}
@ -343,6 +347,10 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
}
fn before_terminator(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
if !ecx.machine.is_detector_enabled {
return Ok(());
}
{
let steps = &mut ecx.machine.steps_since_detector_enabled;

View file

@ -1,8 +1,10 @@
// only-x86_64
// check-pass
#![feature(const_eval_limit)]
#![const_eval_limit="2"]
const CONSTANT: usize = limit();
//~^ WARNING Constant evaluating a complex constant, this might take some time
fn main() {
assert_eq!(CONSTANT, 1764);

View file

@ -0,0 +1,6 @@
warning: Constant evaluating a complex constant, this might take some time
--> $DIR/const_eval_limit_reached.rs:6:1
|
LL | const CONSTANT: usize = limit();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View file

@ -4,7 +4,7 @@ error[E0658]: the `#[const_eval_limit]` attribute is an experimental feature
LL | #![const_eval_limit="42"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/67217
= note: see issue #67217 <https://github.com/rust-lang/rust/issues/67217> for more information
= help: add `#![feature(const_eval_limit)]` to the crate attributes to enable
error: aborting due to previous error