meta_language/translation/
aborts.rs1use super::types::Type;
13
14#[must_use]
20pub fn overflow_message(op: &str) -> &'static str {
21 match op {
22 "add" => "attempt to add with overflow",
23 "sub" => "attempt to subtract with overflow",
24 "mul" => "attempt to multiply with overflow",
25 "div" => "attempt to divide with overflow",
26 "rem" => "attempt to calculate the remainder with overflow",
27 "neg" => "attempt to negate with overflow",
28 other => panic!("{other} does not overflow"),
29 }
30}
31
32#[must_use]
34pub fn zero_divisor_message(op: &str, ty: &Type) -> &'static str {
35 if !matches!(ty, Type::Fixed { .. }) {
36 return "Division by zero";
37 }
38 if op == "rem" {
39 "attempt to calculate the remainder with a divisor of zero"
40 } else {
41 "attempt to divide by zero"
42 }
43}
44
45#[must_use]
47pub fn cast_message(message: Option<&str>) -> &str {
48 message.unwrap_or("conversion of a negative integer to a natural")
49}
50
51#[must_use]
53pub fn rust_macro_message(name: &str, message: Option<&str>) -> String {
54 let prefix = match name {
55 "unreachable" => Some("internal error: entered unreachable code"),
56 "unimplemented" => Some("not implemented"),
57 "todo" => Some("not yet implemented"),
58 _ => None,
59 };
60 match (prefix, message) {
61 (prefix, None) => prefix.unwrap_or("explicit panic").to_owned(),
62 (None, Some(message)) => message.to_owned(),
63 (Some(prefix), Some(message)) => format!("{prefix}: {message}"),
64 }
65}