Auto merge of #106227 - bryangarza:ctfe-limit, r=oli-obk

Use stable metric for const eval limit instead of current terminator-based logic

This patch adds a `MirPass` that inserts a new MIR instruction `ConstEvalCounter` to any loops and function calls in the CFG. This instruction is used during Const Eval to count against the `const_eval_limit`, and emit the `StepLimitReached` error, replacing the current logic which uses Terminators only.

The new method of counting loops and function calls should be more stable across compiler versions (i.e., not cause crates that compiled successfully before, to no longer compile when changes to the MIR generation/optimization are made).

Also see: #103877
This commit is contained in:
bors 2023-01-29 04:11:27 +00:00
commit 3cdd0197e7
50 changed files with 400 additions and 20 deletions

View file

@ -393,6 +393,7 @@ fn statement_effect(
| mir::StatementKind::AscribeUserType(..)
| mir::StatementKind::Coverage(..)
| mir::StatementKind::Intrinsic(..)
| mir::StatementKind::ConstEvalCounter
| mir::StatementKind::Nop => {}
}
}

View file

@ -91,7 +91,8 @@ fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
LocalMutationIsAllowed::Yes,
);
}
StatementKind::Nop
StatementKind::ConstEvalCounter
| StatementKind::Nop
| StatementKind::Retag { .. }
| StatementKind::Deinit(..)
| StatementKind::SetDiscriminant { .. } => {

View file

@ -609,7 +609,8 @@ fn visit_statement_before_primary_effect(
StatementKind::AscribeUserType(..)
// Doesn't have any language semantics
| StatementKind::Coverage(..)
// Does not actually affect borrowck
// These do not actually affect borrowck
| StatementKind::ConstEvalCounter
| StatementKind::StorageLive(..) => {}
StatementKind::StorageDead(local) => {
self.access_place(

View file

@ -1258,6 +1258,7 @@ fn check_stmt(&mut self, body: &Body<'tcx>, stmt: &Statement<'tcx>, location: Lo
| StatementKind::StorageDead(..)
| StatementKind::Retag { .. }
| StatementKind::Coverage(..)
| StatementKind::ConstEvalCounter
| StatementKind::Nop => {}
StatementKind::Deinit(..) | StatementKind::SetDiscriminant { .. } => {
bug!("Statement not allowed in this MIR phase")

View file

@ -794,6 +794,7 @@ fn is_fat_ptr<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool {
StatementKind::StorageLive(_)
| StatementKind::StorageDead(_)
| StatementKind::Deinit(_)
| StatementKind::ConstEvalCounter
| StatementKind::Nop
| StatementKind::FakeRead(..)
| StatementKind::Retag { .. }

View file

@ -530,6 +530,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
| StatementKind::Retag(_, _)
| StatementKind::AscribeUserType(_, _)
| StatementKind::Coverage(_)
| StatementKind::ConstEvalCounter
| StatementKind::Nop => {}
}
}

View file

@ -91,6 +91,7 @@ pub fn codegen_statement(&mut self, bx: &mut Bx, statement: &mir::Statement<'tcx
mir::StatementKind::FakeRead(..)
| mir::StatementKind::Retag { .. }
| mir::StatementKind::AscribeUserType(..)
| mir::StatementKind::ConstEvalCounter
| mir::StatementKind::Nop => {}
}
}

View file

@ -561,8 +561,8 @@ fn binary_ptr_op(
throw_unsup_format!("pointer arithmetic or comparison is not supported at compile-time");
}
fn before_terminator(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
// The step limit has already been hit in a previous call to `before_terminator`.
fn increment_const_eval_counter(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
// The step limit has already been hit in a previous call to `increment_const_eval_counter`.
if ecx.machine.steps_remaining == 0 {
return Ok(());
}

View file

@ -244,12 +244,18 @@ fn access_local_mut<'a>(
}
/// Called before a basic block terminator is executed.
/// You can use this to detect endlessly running programs.
#[inline]
fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
Ok(())
}
/// Called when the interpreter encounters a `StatementKind::ConstEvalCounter` instruction.
/// You can use this to detect long or endlessly running programs.
#[inline]
fn increment_const_eval_counter(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
Ok(())
}
/// Called before a global allocation is accessed.
/// `def_id` is `Some` if this is the "lazy" allocation of a static.
#[inline]

View file

@ -129,6 +129,10 @@ pub fn statement(&mut self, stmt: &mir::Statement<'tcx>) -> InterpResult<'tcx> {
// FIXME(#73156): Handle source code coverage in const eval
Coverage(..) => {}
ConstEvalCounter => {
M::increment_const_eval_counter(self)?;
}
// Defined to do nothing. These are added by optimization passes, to avoid changing the
// size of MIR constantly.
Nop => {}

View file

@ -693,6 +693,7 @@ fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
| StatementKind::AscribeUserType(..)
| StatementKind::Coverage(..)
| StatementKind::Intrinsic(..)
| StatementKind::ConstEvalCounter
| StatementKind::Nop => {}
}
}

View file

@ -761,6 +761,7 @@ fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
StatementKind::StorageLive(..)
| StatementKind::StorageDead(..)
| StatementKind::Coverage(_)
| StatementKind::ConstEvalCounter
| StatementKind::Nop => {}
}

View file

@ -135,7 +135,47 @@ pub fn dominators<G: ControlFlowGraph>(graph: G) -> Dominators<G::Node> {
// This loop computes the semi[w] for w.
semi[w] = w;
for v in graph.predecessors(pre_order_to_real[w]) {
// Reachable vertices may have unreachable predecessors, so ignore any of them
// TL;DR: Reachable vertices may have unreachable predecessors, so ignore any of them.
//
// Ignore blocks which are not connected to the entry block.
//
// The algorithm that was used to traverse the graph and build the
// `pre_order_to_real` and `real_to_pre_order` vectors does so by
// starting from the entry block and following the successors.
// Therefore, any blocks not reachable from the entry block will be
// set to `None` in the `pre_order_to_real` vector.
//
// For example, in this graph, A and B should be skipped:
//
// ┌─────┐
// │ │
// └──┬──┘
// │
// ┌──▼──┐ ┌─────┐
// │ │ │ A │
// └──┬──┘ └──┬──┘
// │ │
// ┌───────┴───────┐ │
// │ │ │
// ┌──▼──┐ ┌──▼──┐ ┌──▼──┐
// │ │ │ │ │ B │
// └──┬──┘ └──┬──┘ └──┬──┘
// │ └──────┬─────┘
// ┌──▼──┐ │
// │ │ │
// └──┬──┘ ┌──▼──┐
// │ │ │
// │ └─────┘
// ┌──▼──┐
// │ │
// └──┬──┘
// │
// ┌──▼──┐
// │ │
// └─────┘
//
// ...this may be the case if a MirPass modifies the CFG to remove
// or rearrange certain blocks/edges.
let Some(v) = real_to_pre_order[v] else {
continue
};
@ -264,13 +304,18 @@ fn compress(
}
}
/// Tracks the list of dominators for each node.
#[derive(Clone, Debug)]
pub struct Dominators<N: Idx> {
post_order_rank: IndexVec<N, usize>,
// Even though we track only the immediate dominator of each node, it's
// possible to get its full list of dominators by looking up the dominator
// of each dominator. (See the `impl Iterator for Iter` definition).
immediate_dominators: IndexVec<N, Option<N>>,
}
impl<Node: Idx> Dominators<Node> {
/// Whether the given Node has an immediate dominator.
pub fn is_reachable(&self, node: Node) -> bool {
self.immediate_dominators[node].is_some()
}
@ -280,6 +325,8 @@ pub fn immediate_dominator(&self, node: Node) -> Node {
self.immediate_dominators[node].unwrap()
}
/// Provides an iterator over each dominator up the CFG, for the given Node.
/// See the `impl Iterator for Iter` definition to understand how this works.
pub fn dominators(&self, node: Node) -> Iter<'_, Node> {
assert!(self.is_reachable(node), "node {node:?} is not reachable");
Iter { dominators: self, node: Some(node) }

View file

@ -802,6 +802,7 @@ macro_rules! tracked {
tracked!(teach, true);
tracked!(thinlto, Some(true));
tracked!(thir_unsafeck, true);
tracked!(tiny_const_eval_limit, true);
tracked!(tls_model, Some(TlsModel::GeneralDynamic));
tracked!(trait_solver, TraitSolver::Chalk);
tracked!(translate_remapped_path_to_local_path, false);

View file

@ -1463,6 +1463,7 @@ fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
}
Coverage(box ref coverage) => write!(fmt, "Coverage::{:?}", coverage.kind),
Intrinsic(box ref intrinsic) => write!(fmt, "{intrinsic}"),
ConstEvalCounter => write!(fmt, "ConstEvalCounter"),
Nop => write!(fmt, "nop"),
}
}

View file

@ -250,6 +250,7 @@ pub fn statement_kind_name(statement: &Statement<'_>) -> &'static str {
AscribeUserType(..) => "AscribeUserType",
Coverage(..) => "Coverage",
Intrinsic(..) => "Intrinsic",
ConstEvalCounter => "ConstEvalCounter",
Nop => "Nop",
}
}

View file

@ -355,6 +355,12 @@ pub enum StatementKind<'tcx> {
/// This avoids adding a new block and a terminator for simple intrinsics.
Intrinsic(Box<NonDivergingIntrinsic<'tcx>>),
/// Instructs the const eval interpreter to increment a counter; this counter is used to track
/// how many steps the interpreter has taken. It is used to prevent the user from writing const
/// code that runs for too long or infinitely. Other than in the const eval interpreter, this
/// is a no-op.
ConstEvalCounter,
/// No-op. Useful for deleting instructions without affecting statement indices.
Nop,
}

View file

@ -427,6 +427,7 @@ fn super_statement(&mut self,
}
}
}
StatementKind::ConstEvalCounter => {}
StatementKind::Nop => {}
}
}

View file

@ -77,6 +77,8 @@
use std::mem;
use std::ops::{Bound, Deref};
const TINY_CONST_EVAL_LIMIT: Limit = Limit(20);
pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync {
/// Creates a new `OnDiskCache` instance from the serialized data in `data`.
fn new(sess: &'tcx Session, data: Mmap, start_pos: usize) -> Self
@ -1104,7 +1106,11 @@ pub fn move_size_limit(self) -> Limit {
}
pub fn const_eval_limit(self) -> Limit {
self.limits(()).const_eval_limit
if self.sess.opts.unstable_opts.tiny_const_eval_limit {
TINY_CONST_EVAL_LIMIT
} else {
self.limits(()).const_eval_limit
}
}
pub fn all_traits(self) -> impl Iterator<Item = DefId> + 'tcx {

View file

@ -271,6 +271,7 @@ fn apply_statement_effect(
| StatementKind::AscribeUserType(..)
| StatementKind::Coverage(..)
| StatementKind::Intrinsic(..)
| StatementKind::ConstEvalCounter
| StatementKind::Nop => None,
};
if let Some(destination) = destination {

View file

@ -141,6 +141,7 @@ fn before_statement_effect(
StatementKind::AscribeUserType(..)
| StatementKind::Coverage(..)
| StatementKind::FakeRead(..)
| StatementKind::ConstEvalCounter
| StatementKind::Nop
| StatementKind::Retag(..)
| StatementKind::Intrinsic(..)

View file

@ -331,6 +331,7 @@ fn gather_statement(&mut self, stmt: &Statement<'tcx>) {
| StatementKind::AscribeUserType(..)
| StatementKind::Coverage(..)
| StatementKind::Intrinsic(..)
| StatementKind::ConstEvalCounter
| StatementKind::Nop => {}
}
}

View file

@ -84,7 +84,8 @@ fn super_statement(&self, statement: &Statement<'tcx>, state: &mut State<Self::V
StatementKind::Retag(..) => {
// We don't track references.
}
StatementKind::Nop
StatementKind::ConstEvalCounter
| StatementKind::Nop
| StatementKind::FakeRead(..)
| StatementKind::Coverage(..)
| StatementKind::AscribeUserType(..) => (),

View file

@ -104,6 +104,7 @@ fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
| StatementKind::AscribeUserType(..)
| StatementKind::Coverage(..)
| StatementKind::Intrinsic(..)
| StatementKind::ConstEvalCounter
| StatementKind::Nop => {
// safe (at least as emitted during MIR construction)
}

View file

@ -802,6 +802,8 @@ pub(super) fn filtered_statement_span(statement: &Statement<'_>) -> Option<Span>
| StatementKind::StorageDead(_)
// Coverage should not be encountered, but don't inject coverage coverage
| StatementKind::Coverage(_)
// Ignore `ConstEvalCounter`s
| StatementKind::ConstEvalCounter
// Ignore `Nop`s
| StatementKind::Nop => None,

View file

@ -0,0 +1,59 @@
//! A pass that inserts the `ConstEvalCounter` instruction into any blocks that have a back edge
//! (thus indicating there is a loop in the CFG), or whose terminator is a function call.
use crate::MirPass;
use rustc_data_structures::graph::dominators::Dominators;
use rustc_middle::mir::{
BasicBlock, BasicBlockData, Body, Statement, StatementKind, TerminatorKind,
};
use rustc_middle::ty::TyCtxt;
pub struct CtfeLimit;
impl<'tcx> MirPass<'tcx> for CtfeLimit {
#[instrument(skip(self, _tcx, body))]
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let doms = body.basic_blocks.dominators();
let indices: Vec<BasicBlock> = body
.basic_blocks
.iter_enumerated()
.filter_map(|(node, node_data)| {
if matches!(node_data.terminator().kind, TerminatorKind::Call { .. })
// Back edges in a CFG indicate loops
|| has_back_edge(&doms, node, &node_data)
{
Some(node)
} else {
None
}
})
.collect();
for index in indices {
insert_counter(
body.basic_blocks_mut()
.get_mut(index)
.expect("basic_blocks index {index} should exist"),
);
}
}
}
fn has_back_edge(
doms: &Dominators<BasicBlock>,
node: BasicBlock,
node_data: &BasicBlockData<'_>,
) -> bool {
if !doms.is_reachable(node) {
return false;
}
// Check if any of the dominators of the node are also the node's successor.
doms.dominators(node)
.any(|dom| node_data.terminator().successors().into_iter().any(|succ| succ == dom))
}
fn insert_counter(basic_block_data: &mut BasicBlockData<'_>) {
basic_block_data.statements.push(Statement {
source_info: basic_block_data.terminator().source_info,
kind: StatementKind::ConstEvalCounter,
});
}

View file

@ -53,6 +53,7 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
| StatementKind::StorageDead(_)
| StatementKind::Coverage(_)
| StatementKind::Intrinsic(_)
| StatementKind::ConstEvalCounter
| StatementKind::Nop => (),
StatementKind::FakeRead(_) | StatementKind::AscribeUserType(_, _) => {

View file

@ -577,6 +577,7 @@ fn for_statement<'tcx>(&mut self, statement: &StatementKind<'tcx>, body: &Body<'
self.add_place(**place);
}
StatementKind::Intrinsic(_)
| StatementKind::ConstEvalCounter
| StatementKind::Nop
| StatementKind::Coverage(_)
| StatementKind::StorageLive(_)

View file

@ -1657,6 +1657,7 @@ fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
| StatementKind::AscribeUserType(..)
| StatementKind::Coverage(..)
| StatementKind::Intrinsic(..)
| StatementKind::ConstEvalCounter
| StatementKind::Nop => {}
}
}

View file

@ -55,6 +55,7 @@
mod const_prop;
mod const_prop_lint;
mod coverage;
mod ctfe_limit;
mod dataflow_const_prop;
mod dead_store_elimination;
mod deaggregator;
@ -410,6 +411,8 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -
}
}
pm::run_passes(tcx, &mut body, &[&ctfe_limit::CtfeLimit], None);
debug_assert!(!body.has_free_regions(), "Free regions in MIR for CTFE");
body

View file

@ -35,6 +35,7 @@ fn is_nop_landing_pad(
| StatementKind::StorageDead(_)
| StatementKind::AscribeUserType(..)
| StatementKind::Coverage(..)
| StatementKind::ConstEvalCounter
| StatementKind::Nop => {
// These are all noops in a landing pad
}

View file

@ -250,6 +250,7 @@ fn is_likely_const<'tcx>(mut tracked_place: Place<'tcx>, block: &BasicBlockData<
| StatementKind::Coverage(_)
| StatementKind::StorageDead(_)
| StatementKind::Intrinsic(_)
| StatementKind::ConstEvalCounter
| StatementKind::Nop => {}
}
}
@ -318,6 +319,7 @@ fn find_determining_place<'tcx>(
| StatementKind::AscribeUserType(_, _)
| StatementKind::Coverage(_)
| StatementKind::Intrinsic(_)
| StatementKind::ConstEvalCounter
| StatementKind::Nop => {}
// If the discriminant is set, it is always set

View file

@ -517,7 +517,7 @@ fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
self.super_statement(statement, location);
}
StatementKind::Nop => {}
StatementKind::ConstEvalCounter | StatementKind::Nop => {}
StatementKind::StorageLive(_local) | StatementKind::StorageDead(_local) => {}

View file

@ -1618,6 +1618,8 @@ pub(crate) fn parse_proc_macro_execution_strategy(
"measure time of each LLVM pass (default: no)"),
time_passes: bool = (false, parse_bool, [UNTRACKED],
"measure time of each rustc pass (default: no)"),
tiny_const_eval_limit: bool = (false, parse_bool, [TRACKED],
"sets a tiny, non-configurable limit for const eval; useful for compiler tests"),
#[rustc_lint_opt_deny_field_access("use `Session::tls_model` instead of this field")]
tls_model: Option<TlsModel> = (None, parse_tls_model, [TRACKED],
"choose the TLS model to use (`rustc --print tls-models` for details)"),

View file

@ -0,0 +1,6 @@
# `tiny-const-eval-limit`
--------------------
The `-Ztiny-const-eval-limit` compiler flag sets a tiny, non-configurable limit for const eval.
This flag should only be used by const eval tests in the rustc test suite.

View file

@ -240,6 +240,7 @@ fn check_statement<'tcx>(
| StatementKind::Retag { .. }
| StatementKind::AscribeUserType(..)
| StatementKind::Coverage(..)
| StatementKind::ConstEvalCounter
| StatementKind::Nop => Ok(()),
}
}

View file

@ -173,6 +173,7 @@
-Z threads=val -- use a thread pool with N threads
-Z time-llvm-passes=val -- measure time of each LLVM pass (default: no)
-Z time-passes=val -- measure time of each rustc pass (default: no)
-Z tiny-const-eval-limit=val -- sets a tiny, non-configurable limit for const eval; useful for compiler tests
-Z tls-model=val -- choose the TLS model to use (`rustc --print tls-models` for details)
-Z trace-macros=val -- for every macro invocation, print its name and arguments (default: no)
-Z track-diagnostics=val -- tracks where in rustc a diagnostic was emitted

View file

@ -1,8 +1,11 @@
error[E0080]: evaluation of constant value failed
--> $DIR/infinite_loop.rs:6:15
--> $DIR/infinite_loop.rs:6:9
|
LL | while n != 0 {
| ^^^^^^ exceeded interpreter step limit (see `#[const_eval_limit]`)
LL | / while n != 0 {
LL | |
LL | | n = if n % 2 == 0 { n/2 } else { 3*n + 1 };
LL | | }
| |_________^ exceeded interpreter step limit (see `#[const_eval_limit]`)
error: aborting due to previous error

View file

@ -2,8 +2,8 @@ fn main() {
let _ = [(); {
let mut x = &0;
let mut n = 0;
while n < 5 {
n = (n + 1) % 5; //~ ERROR evaluation of constant value failed
while n < 5 { //~ ERROR evaluation of constant value failed [E0080]
n = (n + 1) % 5;
x = &0; // Materialize a new AllocId
}
0

View file

@ -1,8 +1,11 @@
error[E0080]: evaluation of constant value failed
--> $DIR/issue-52475.rs:6:17
--> $DIR/issue-52475.rs:5:9
|
LL | n = (n + 1) % 5;
| ^^^^^^^^^^^ exceeded interpreter step limit (see `#[const_eval_limit]`)
LL | / while n < 5 {
LL | | n = (n + 1) % 5;
LL | | x = &0; // Materialize a new AllocId
LL | | }
| |_________^ exceeded interpreter step limit (see `#[const_eval_limit]`)
error: aborting due to previous error

View file

@ -0,0 +1,36 @@
// check-fail
// compile-flags: -Z tiny-const-eval-limit
const fn foo() {}
const fn call_foo() -> u32 {
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo(); //~ ERROR evaluation of constant value failed [E0080]
0
}
const X: u32 = call_foo();
fn main() {
println!("{X}");
}

View file

@ -0,0 +1,20 @@
error[E0080]: evaluation of constant value failed
--> $DIR/ctfe-fn-call.rs:28:5
|
LL | foo();
| ^^^^^ exceeded interpreter step limit (see `#[const_eval_limit]`)
|
note: inside `call_foo`
--> $DIR/ctfe-fn-call.rs:28:5
|
LL | foo();
| ^^^^^
note: inside `X`
--> $DIR/ctfe-fn-call.rs:32:16
|
LL | const X: u32 = call_foo();
| ^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.

View file

@ -0,0 +1,19 @@
// check-fail
// compile-flags: -Z tiny-const-eval-limit
const fn labelled_loop(n: u32) -> u32 {
let mut i = 0;
'mylabel: loop { //~ ERROR evaluation of constant value failed [E0080]
if i > n {
break 'mylabel
}
i += 1;
}
0
}
const X: u32 = labelled_loop(19);
fn main() {
println!("{X}");
}

View file

@ -0,0 +1,30 @@
error[E0080]: evaluation of constant value failed
--> $DIR/ctfe-labelled-loop.rs:6:5
|
LL | / 'mylabel: loop {
LL | | if i > n {
LL | | break 'mylabel
LL | | }
LL | | i += 1;
LL | | }
| |_____^ exceeded interpreter step limit (see `#[const_eval_limit]`)
|
note: inside `labelled_loop`
--> $DIR/ctfe-labelled-loop.rs:6:5
|
LL | / 'mylabel: loop {
LL | | if i > n {
LL | | break 'mylabel
LL | | }
LL | | i += 1;
LL | | }
| |_____^
note: inside `X`
--> $DIR/ctfe-labelled-loop.rs:15:16
|
LL | const X: u32 = labelled_loop(19);
| ^^^^^^^^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.

View file

@ -0,0 +1,16 @@
// check-fail
// compile-flags: -Z tiny-const-eval-limit
const fn recurse(n: u32) -> u32 {
if n == 0 {
n
} else {
recurse(n - 1) //~ ERROR evaluation of constant value failed [E0080]
}
}
const X: u32 = recurse(19);
fn main() {
println!("{X}");
}

View file

@ -0,0 +1,25 @@
error[E0080]: evaluation of constant value failed
--> $DIR/ctfe-recursion.rs:8:9
|
LL | recurse(n - 1)
| ^^^^^^^^^^^^^^ exceeded interpreter step limit (see `#[const_eval_limit]`)
|
note: inside `recurse`
--> $DIR/ctfe-recursion.rs:8:9
|
LL | recurse(n - 1)
| ^^^^^^^^^^^^^^
note: [... 18 additional calls inside `recurse` ...]
--> $DIR/ctfe-recursion.rs:8:9
|
LL | recurse(n - 1)
| ^^^^^^^^^^^^^^
note: inside `X`
--> $DIR/ctfe-recursion.rs:12:16
|
LL | const X: u32 = recurse(19);
| ^^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.

View file

@ -0,0 +1,15 @@
// check-fail
// compile-flags: -Z tiny-const-eval-limit
const fn simple_loop(n: u32) -> u32 {
let mut index = 0;
while index < n { //~ ERROR evaluation of constant value failed [E0080]
index = index + 1;
}
0
}
const X: u32 = simple_loop(19);
fn main() {
println!("{X}");
}

View file

@ -0,0 +1,24 @@
error[E0080]: evaluation of constant value failed
--> $DIR/ctfe-simple-loop.rs:5:5
|
LL | / while index < n {
LL | | index = index + 1;
LL | | }
| |_____^ exceeded interpreter step limit (see `#[const_eval_limit]`)
|
note: inside `simple_loop`
--> $DIR/ctfe-simple-loop.rs:5:5
|
LL | / while index < n {
LL | | index = index + 1;
LL | | }
| |_____^
note: inside `X`
--> $DIR/ctfe-simple-loop.rs:11:16
|
LL | const X: u32 = simple_loop(19);
| ^^^^^^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.

View file

@ -0,0 +1,19 @@
// check-pass
//
// Exercising an edge case which was found during Stage 2 compilation.
// Compilation would fail for this code when running the `CtfeLimit`
// MirPass (specifically when looking up the dominators).
#![crate_type="lib"]
const DUMMY: Expr = Expr::Path(ExprPath {
attrs: Vec::new(),
path: Vec::new(),
});
pub enum Expr {
Path(ExprPath),
}
pub struct ExprPath {
pub attrs: Vec<()>,
pub path: Vec<()>,
}

View file

@ -1,8 +1,11 @@
error[E0080]: evaluation of constant value failed
--> $DIR/const_eval_limit_reached.rs:6:11
--> $DIR/const_eval_limit_reached.rs:6:5
|
LL | while x != 1000 {
| ^^^^^^^^^ exceeded interpreter step limit (see `#[const_eval_limit]`)
LL | / while x != 1000 {
LL | |
LL | | x += 1;
LL | | }
| |_____^ exceeded interpreter step limit (see `#[const_eval_limit]`)
error: aborting due to previous error