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:
commit
3cdd0197e7
50 changed files with 400 additions and 20 deletions
|
@ -393,6 +393,7 @@ impl<'tcx> rustc_mir_dataflow::GenKillAnalysis<'tcx> for Borrows<'_, 'tcx> {
|
|||
| mir::StatementKind::AscribeUserType(..)
|
||||
| mir::StatementKind::Coverage(..)
|
||||
| mir::StatementKind::Intrinsic(..)
|
||||
| mir::StatementKind::ConstEvalCounter
|
||||
| mir::StatementKind::Nop => {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -91,7 +91,8 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
|
|||
LocalMutationIsAllowed::Yes,
|
||||
);
|
||||
}
|
||||
StatementKind::Nop
|
||||
StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop
|
||||
| StatementKind::Retag { .. }
|
||||
| StatementKind::Deinit(..)
|
||||
| StatementKind::SetDiscriminant { .. } => {
|
||||
|
|
|
@ -609,7 +609,8 @@ impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtx
|
|||
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(
|
||||
|
|
|
@ -1258,6 +1258,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
|
|||
| StatementKind::StorageDead(..)
|
||||
| StatementKind::Retag { .. }
|
||||
| StatementKind::Coverage(..)
|
||||
| StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop => {}
|
||||
StatementKind::Deinit(..) | StatementKind::SetDiscriminant { .. } => {
|
||||
bug!("Statement not allowed in this MIR phase")
|
||||
|
|
|
@ -794,6 +794,7 @@ fn codegen_stmt<'tcx>(
|
|||
StatementKind::StorageLive(_)
|
||||
| StatementKind::StorageDead(_)
|
||||
| StatementKind::Deinit(_)
|
||||
| StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop
|
||||
| StatementKind::FakeRead(..)
|
||||
| StatementKind::Retag { .. }
|
||||
|
|
|
@ -530,6 +530,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
|
|||
| StatementKind::Retag(_, _)
|
||||
| StatementKind::AscribeUserType(_, _)
|
||||
| StatementKind::Coverage(_)
|
||||
| StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop => {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -91,6 +91,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
|||
mir::StatementKind::FakeRead(..)
|
||||
| mir::StatementKind::Retag { .. }
|
||||
| mir::StatementKind::AscribeUserType(..)
|
||||
| mir::StatementKind::ConstEvalCounter
|
||||
| mir::StatementKind::Nop => {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -561,8 +561,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
|
|||
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(());
|
||||
}
|
||||
|
|
|
@ -244,12 +244,18 @@ pub trait Machine<'mir, 'tcx>: Sized {
|
|||
}
|
||||
|
||||
/// 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]
|
||||
|
|
|
@ -129,6 +129,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||
// 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 => {}
|
||||
|
|
|
@ -693,6 +693,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
|
|||
| StatementKind::AscribeUserType(..)
|
||||
| StatementKind::Coverage(..)
|
||||
| StatementKind::Intrinsic(..)
|
||||
| StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop => {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -761,6 +761,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
|
|||
StatementKind::StorageLive(..)
|
||||
| StatementKind::StorageDead(..)
|
||||
| StatementKind::Coverage(_)
|
||||
| StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop => {}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 @@ impl<Node: Idx> Dominators<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) }
|
||||
|
|
|
@ -802,6 +802,7 @@ fn test_unstable_options_tracking_hash() {
|
|||
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);
|
||||
|
|
|
@ -1463,6 +1463,7 @@ impl Debug for Statement<'_> {
|
|||
}
|
||||
Coverage(box ref coverage) => write!(fmt, "Coverage::{:?}", coverage.kind),
|
||||
Intrinsic(box ref intrinsic) => write!(fmt, "{intrinsic}"),
|
||||
ConstEvalCounter => write!(fmt, "ConstEvalCounter"),
|
||||
Nop => write!(fmt, "nop"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -250,6 +250,7 @@ pub fn statement_kind_name(statement: &Statement<'_>) -> &'static str {
|
|||
AscribeUserType(..) => "AscribeUserType",
|
||||
Coverage(..) => "Coverage",
|
||||
Intrinsic(..) => "Intrinsic",
|
||||
ConstEvalCounter => "ConstEvalCounter",
|
||||
Nop => "Nop",
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -427,6 +427,7 @@ macro_rules! make_mir_visitor {
|
|||
}
|
||||
}
|
||||
}
|
||||
StatementKind::ConstEvalCounter => {}
|
||||
StatementKind::Nop => {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,6 +77,8 @@ use std::iter;
|
|||
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 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
|
|
@ -271,6 +271,7 @@ impl<'a, 'tcx> Analysis<'tcx> for MaybeTransitiveLiveLocals<'a> {
|
|||
| StatementKind::AscribeUserType(..)
|
||||
| StatementKind::Coverage(..)
|
||||
| StatementKind::Intrinsic(..)
|
||||
| StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop => None,
|
||||
};
|
||||
if let Some(destination) = destination {
|
||||
|
|
|
@ -141,6 +141,7 @@ impl<'mir, 'tcx> crate::GenKillAnalysis<'tcx> for MaybeRequiresStorage<'mir, 'tc
|
|||
StatementKind::AscribeUserType(..)
|
||||
| StatementKind::Coverage(..)
|
||||
| StatementKind::FakeRead(..)
|
||||
| StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop
|
||||
| StatementKind::Retag(..)
|
||||
| StatementKind::Intrinsic(..)
|
||||
|
|
|
@ -331,6 +331,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
|
|||
| StatementKind::AscribeUserType(..)
|
||||
| StatementKind::Coverage(..)
|
||||
| StatementKind::Intrinsic(..)
|
||||
| StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop => {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -84,7 +84,8 @@ pub trait ValueAnalysis<'tcx> {
|
|||
StatementKind::Retag(..) => {
|
||||
// We don't track references.
|
||||
}
|
||||
StatementKind::Nop
|
||||
StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop
|
||||
| StatementKind::FakeRead(..)
|
||||
| StatementKind::Coverage(..)
|
||||
| StatementKind::AscribeUserType(..) => (),
|
||||
|
|
|
@ -104,6 +104,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
|
|||
| StatementKind::AscribeUserType(..)
|
||||
| StatementKind::Coverage(..)
|
||||
| StatementKind::Intrinsic(..)
|
||||
| StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop => {
|
||||
// safe (at least as emitted during MIR construction)
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
||||
|
|
59
compiler/rustc_mir_transform/src/ctfe_limit.rs
Normal file
59
compiler/rustc_mir_transform/src/ctfe_limit.rs
Normal 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,
|
||||
});
|
||||
}
|
|
@ -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(_, _) => {
|
||||
|
|
|
@ -577,6 +577,7 @@ impl WriteInfo {
|
|||
self.add_place(**place);
|
||||
}
|
||||
StatementKind::Intrinsic(_)
|
||||
| StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop
|
||||
| StatementKind::Coverage(_)
|
||||
| StatementKind::StorageLive(_)
|
||||
|
|
|
@ -1657,6 +1657,7 @@ impl<'tcx> Visitor<'tcx> for EnsureGeneratorFieldAssignmentsNeverAlias<'_> {
|
|||
| StatementKind::AscribeUserType(..)
|
||||
| StatementKind::Coverage(..)
|
||||
| StatementKind::Intrinsic(..)
|
||||
| StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop => {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,6 +55,7 @@ mod const_goto;
|
|||
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
|
||||
|
|
|
@ -35,6 +35,7 @@ impl RemoveNoopLandingPads {
|
|||
| StatementKind::StorageDead(_)
|
||||
| StatementKind::AscribeUserType(..)
|
||||
| StatementKind::Coverage(..)
|
||||
| StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop => {
|
||||
// These are all noops in a landing pad
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -517,7 +517,7 @@ impl<'tcx> Visitor<'tcx> for UsedLocals {
|
|||
self.super_statement(statement, location);
|
||||
}
|
||||
|
||||
StatementKind::Nop => {}
|
||||
StatementKind::ConstEvalCounter | StatementKind::Nop => {}
|
||||
|
||||
StatementKind::StorageLive(_local) | StatementKind::StorageDead(_local) => {}
|
||||
|
||||
|
|
|
@ -1618,6 +1618,8 @@ options! {
|
|||
"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)"),
|
||||
|
|
|
@ -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.
|
|
@ -240,6 +240,7 @@ fn check_statement<'tcx>(
|
|||
| StatementKind::Retag { .. }
|
||||
| StatementKind::AscribeUserType(..)
|
||||
| StatementKind::Coverage(..)
|
||||
| StatementKind::ConstEvalCounter
|
||||
| StatementKind::Nop => Ok(()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
36
tests/ui/consts/const-eval/stable-metric/ctfe-fn-call.rs
Normal file
36
tests/ui/consts/const-eval/stable-metric/ctfe-fn-call.rs
Normal 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}");
|
||||
}
|
20
tests/ui/consts/const-eval/stable-metric/ctfe-fn-call.stderr
Normal file
20
tests/ui/consts/const-eval/stable-metric/ctfe-fn-call.stderr
Normal 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`.
|
|
@ -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}");
|
||||
}
|
|
@ -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`.
|
16
tests/ui/consts/const-eval/stable-metric/ctfe-recursion.rs
Normal file
16
tests/ui/consts/const-eval/stable-metric/ctfe-recursion.rs
Normal 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}");
|
||||
}
|
|
@ -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`.
|
15
tests/ui/consts/const-eval/stable-metric/ctfe-simple-loop.rs
Normal file
15
tests/ui/consts/const-eval/stable-metric/ctfe-simple-loop.rs
Normal 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}");
|
||||
}
|
|
@ -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`.
|
|
@ -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<()>,
|
||||
}
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue