use dive_eval::{Ec, ErrorClass, EvalError};
use crate::result::{ErrorSite, Verdict};
const GENERAL_PREFIX: &str = "TLC threw an unexpected exception.\nThis was probably caused by an error in the spec or model.\nSee the User Output or TLC Console for clues to what happened.\nThe exception was a java.lang.RuntimeException\n: ";
const MEASURED_EVAL_EXCEPTIONS: [u32; 3] = [
2132, 2178, 2179, ];
const TOOL_CODES: [u32; 5] = [
2154, 2215, 2247, 2248, 2280, ];
const TOOL_MESSAGES: [&str; 8] = [
"The current version of TLC does not support action composition.",
"Attempted to compute the value of an expression of form\nCHOOSE",
"TLC encountered the non-enumerable quantifier bound",
"A non-boolean expression (",
"In computing next states, a non-boolean expression (",
"The next state relation is not a boolean expression",
"In applying the function\n",
"Attempted to evaluate an expression of form [A]_e, but A was not a boolean.",
];
fn replay_class_known(e: &EvalError) -> bool {
if let Some(receiver) = e.value_method() {
return receiver.source().is_some();
}
let code = e.ec().code();
if e.is_eval_exception() {
MEASURED_EVAL_EXCEPTIONS.contains(&code)
} else if e.ec() == Ec::General {
TOOL_MESSAGES.iter().any(|p| e.message().starts_with(p))
} else {
TOOL_CODES.contains(&code)
}
}
#[doc(hidden)]
#[must_use]
pub fn init_primary_general(
site: ErrorSite,
code: u32,
reported: u32,
verified: bool,
cause: &EvalError,
) -> bool {
site == ErrorSite::Init
&& code == 2103
&& reported == 1000
&& verified
&& cause.is_eval()
&& !cause.is_eval_exception()
&& (cause.ec() == Ec::ExpectedValue
|| (cause.ec() == Ec::JavaMethodOverride
&& matches!(cause.override_cause(), Some((_, Ec::Overflow)))))
}
#[must_use]
#[doc(hidden)]
pub fn ec_name(code: u32) -> &'static str {
match code {
1000 => "GENERAL",
2102 => "TLC_INITIAL_STATE",
2103 => "TLC_NESTED_EXPRESSION",
2105 => "TLC_ASSUMPTION_EVALUATION_ERROR",
2111 => "TLC_INVARIANT_EVALUATION_FAILED",
2113 => "TLC_ACTION_PROPERTY_EVALUATION_FAILED",
2147 => "TLC_FINGERPRINT_EXCEPTION",
2405 => "TLC_POSTCONDITION_EVALUATION_ERROR",
c => crate::eval_code_name(c),
}
}
fn not_eval(e: &EvalError) -> Option<Verdict> {
match e.class() {
ErrorClass::Eval => None,
ErrorClass::Unsupported => Some(Verdict::Unsupported {
feature: e.message().to_owned(),
}),
ErrorClass::ResourceLimit => Some(Verdict::ResourceLimit {
message: e.message().to_owned(),
}),
ErrorClass::Interrupted => Some(Verdict::InternalError {
message: "host interruption has no matching explicit run context".into(),
}),
ErrorClass::Bug => Some(Verdict::InternalError {
message: e.message().to_owned(),
}),
}
}
fn verdict(
site: ErrorSite,
code: u32,
verified: bool,
reported: u32,
message: String,
e: &EvalError,
invariant: Option<String>,
) -> Verdict {
Verdict::EvaluationError {
site,
code,
name: ec_name(code),
reported,
message,
verified,
cause: e.clone(),
invariant,
}
}
fn replayed(e: &EvalError) -> (u32, bool) {
let code = if e
.value_method()
.is_some_and(|failure| failure.source().is_some())
{
2147
} else if e.is_eval_exception() {
e.ec().code()
} else {
2103
};
(code, replay_class_known(e))
}
#[must_use]
#[doc(hidden)]
pub fn search_error(e: &EvalError, site: ErrorSite, name: Option<&str>) -> Verdict {
if let Some(v) = not_eval(e) {
return v;
}
let invariant = name;
let name = name.unwrap_or("?");
match site {
ErrorSite::Invariant => verdict(
site,
2111,
true,
2111,
format!("Evaluating invariant {name} failed.\n{}", e.tool_text()),
e,
invariant.map(str::to_owned),
),
ErrorSite::ImpliedAction => {
let (code, verified) = replayed(e);
verdict(
site,
code,
verified,
2113,
format!(
"Evaluating action property {name} failed.\n{}",
e.tool_text()
),
e,
None,
)
}
ErrorSite::Startup => {
let code = e.ec().code();
verdict(site, code, true, code, e.message().to_owned(), e, None)
}
_ => {
let (code, verified) = replayed(e);
let (reported, message) = if e.is_eval_exception() {
(e.ec().code(), e.message().to_owned())
} else {
(1000, format!("{GENERAL_PREFIX}{}", e.tool_text()))
};
verdict(site, code, verified, reported, message, e, None)
}
}
}
#[must_use]
#[doc(hidden)]
pub fn recurring(v: Verdict) -> Verdict {
match v {
Verdict::EvaluationError {
site,
reported,
message,
cause,
invariant,
..
} => {
let (code, verified) = replayed(&cause);
verdict(site, code, verified, reported, message, &cause, invariant)
}
other => other,
}
}
#[must_use]
#[doc(hidden)]
pub fn replay_error(v: Verdict, e2: &EvalError) -> Verdict {
match v {
Verdict::EvaluationError {
site,
reported,
message,
cause,
invariant,
..
} => {
if let Some(refused) = not_eval(e2) {
return refused;
}
let (code, verified) = replayed(e2);
verdict(site, code, verified, reported, message, &cause, invariant)
}
other => other,
}
}
#[must_use]
#[doc(hidden)]
pub fn init_error(v: Verdict, state: Option<&str>, diagnostic: Option<&EvalError>) -> Verdict {
let Verdict::EvaluationError {
site,
cause,
invariant,
..
} = v
else {
return v;
};
let (reported, message) = match state {
Some(s) if !cause.is_eval_exception() => (
2102,
format!(
"{}\nWhile working on the initial state:\n{s}",
cause.tool_text()
),
),
_ => (1000, cause.tool_text()),
};
let sourced_receiver = diagnostic.filter(|replay| {
replay.is_eval()
&& replay.ec() == cause.ec()
&& replay.exception_kind() == cause.exception_kind()
&& cause.value_method().is_some_and(|original| {
replay.value_method().is_some_and(|actual| {
original.method() == actual.method()
&& original.constructor() == actual.constructor()
&& actual.source().is_some()
})
})
});
let unproved_context =
state.is_some() && site == ErrorSite::Invariant && !cause.is_eval_exception();
let (code, verified) = sourced_receiver.map_or_else(
|| (2103, !unproved_context && replay_class_known(&cause)),
|_| (2147, true),
);
verdict(site, code, verified, reported, message, &cause, invariant)
}
#[must_use]
#[doc(hidden)]
pub fn assumption_error(e: &EvalError, message: String) -> Verdict {
if let Some(v) = not_eval(e) {
return v;
}
verdict(ErrorSite::Assumption, 2105, true, 2105, message, e, None)
}
#[must_use]
#[doc(hidden)]
pub fn postcondition_error(e: &EvalError, name: &str, location: &str) -> Verdict {
if let Some(v) = not_eval(e) {
return v;
}
verdict(
ErrorSite::Postcondition,
2405,
true,
2405,
format!(
"Evaluating postcondition {name} at {location} failed.\n{}",
e.tool_text()
),
e,
None,
)
}