Files
2026-11-27 06:03:56 +00:00

190 lines
8.9 KiB
Rust

use super::*;
#[test]
fn capture_preserves_nominal_errors_and_parameter_failure_propagation() {
let source = "type Problem { error Missing(message: Text) }\ntype Result { Ready(value: Int), error Failed(problem: Problem) }\nraise() (value: Int) { fail(Problem.Missing(\"missing\")) }\nmaybe(value: Int) (next: Int) { check value >= 1 else Problem.Missing(\"small\")\nnext = value }\nf() (out: Result) { out = result(raise()) }\ng() (out: Result) { out = result(maybe(raise())) }";
let program = check(source).unwrap();
assert!(program.functions[2].failures.is_empty());
assert_eq!(
program.functions[3].failures,
[Failure::Choice(1, 0)].into()
);
let Body::Statements { statements, .. } = &program.functions[2].body else {
panic!()
};
let Statement::Assign { value, .. } = &statements[0] else {
panic!()
};
let ExpressionKind::Capture(capture) = &value.kind else {
panic!()
};
assert_eq!(capture.errors, vec![(Failure::Choice(1, 0), 1, true)]);
rejected(
&source.replace("Failed(problem: Problem)", "Failed(problem: Int)"),
"C005",
"nominally matching",
);
rejected(
&source.replace(
"Failed(problem: Problem)",
"Failed(problem: Problem), error Again(problem: Problem)",
),
"C005",
"nominally matching",
);
rejected(
"type Result { Ready(value: Int) }\nf() (x: Int) { x = 1 }\ng() (value: Result) { value = result(f()) }",
"C012",
"infallible",
);
}
#[test]
fn capture_checks_zero_and_multiple_success_outputs_and_ranges() {
check("type Result { Done, error Failed }\nf() () { fail(Result.Failed) }\ng() (out: Result) { out = result(f()) }").unwrap();
check("type Result { Done(x: Int, text: Text), error Failed }\nf() (x: Int, text: Text) { fail(Result.Failed) }\ng() (out: Result) { out = result(f()) }").unwrap();
rejected(
"type Result { Done(x: Int >= 1), error Failed }\nf(input: Int) (x: Int) { check input != 0 else Result.Failed\nx = input }\ng(input: Int) (out: Result) { out = result(f(input)) }",
"C005",
"success fields",
);
}
const ERRORS: &str = "type Result { Ready(value: Int), error Failed(message: Text) }\ntype Box { result: Result, label: Text }\nraise() (value: Int) { fail(Result.Failed(\"bad\")) }\ncapture() (value: Result) { value = result(raise()) }\n";
#[test]
fn error_values_cannot_be_lost_by_assignment_projection_or_collection_access() {
for body in [
"f() () { lost = capture() }",
"f() (value: Result) { value = capture()\nvalue = Result.Ready(1) }",
"f() (value: Text) { box = Box { result = capture(), label = \"x\" }\nvalue = box.label }",
"f() (value: Int) { values = [capture()]\nvalue = length(values) }",
"f(value: Result) () {}",
"f(old: Result, next: Result?) (value: Result?) { value = next }",
] {
rejected(
&(ERRORS.to_owned() + body),
"C012",
"failure is not handled",
);
}
}
#[test]
fn returned_errors_recovery_and_aliases_keep_their_obligations() {
for body in [
"f() (value: Result) { value = capture() }",
"f() (value: Result?) { values: List<Result?> = [capture()]\nvalue = get(values, 0) }",
"f() (value: Result) { old = capture()\nvalue = Result.Failed(\"replacement\") }",
"identity(value: Result) (next: Result) { next = value }\nf() (value: Result) { first = capture()\nsecond = identity(first)\nvalue = first }",
"f(value: Result) (yes: Bool) recover(\"optional lookup is ignored\") { yes = true }",
"rule read(value: Result) (number: Int)\n| value | number |\n| Ready(number) | number |\n| Failed(message) | fail(value) |\n",
"rule read(value: Result) (next: Result)\n| value | next |\n| Ready(number) | Ready(number) |\n| Failed(message) | value |\n",
] {
check(&(ERRORS.to_owned() + body)).unwrap();
}
rejected(
&(ERRORS.to_owned()
+ "rule read(value: Result) (number: Int)\n| value | number |\n| Ready(number) | number |\n| Failed(message) | length(message) |\n"),
"C012",
"failure is not handled",
);
}
#[test]
fn conditional_recovery_does_not_handle_the_skipped_path() {
let recovery = "handled(value: Result) (yes: Bool) recover(\"optional probe is discarded\") { yes = true }\n";
for expression in [
"false and handled(value)",
"true or handled(value)",
"flag and handled(value)",
] {
rejected(
&format!(
"{ERRORS}{recovery}f(flag: Bool) (yes: Bool) {{ value = capture()\nyes = {expression} }}"
),
"C012",
"failure is not handled",
);
}
check(&format!(
"{ERRORS}{recovery}f() (yes: Bool) {{ value = capture()\nyes = true and handled(value) }}"
))
.unwrap();
}
#[test]
fn choice_complements_and_nullable_error_paths_use_proved_partitions() {
check(
"type Choice { A, B }\nrule f(value: Choice) (x: Int)\n| value | x |\n| A, other | 1 |\n",
)
.unwrap();
check(&format!("{ERRORS}rule read(value: Result?) (x: Int)\n| value | x |\n| null | 0 |\n| Ready(number) | number |\n| Failed(message) | fail(value) |\n")).unwrap();
rejected(
&format!("{ERRORS}f() (x: Int) {{ x = raise()\nx = 1 }}"),
"C012",
"unreachable",
);
}
#[test]
fn failure_analysis_skips_plain_records_and_reports_structural_limits() {
let mut source = String::from("type Root { error Failed }\n");
for index in 0..140 {
let inner = if index == 0 {
"Root".into()
} else {
format!("Level{}", index - 1)
};
source.push_str(&format!("type Level{index} {{ inner: {inner} }}\n"));
}
source.push_str("f(value: Level139) (out: Level139) { out = value }");
rejected(&source, "C015", "has not been proved");
check(&source.replace("error Failed", "value: Int")).unwrap();
check("type Box { values: List<Int> }\nf(box: Box) (out: List<Int>) { out = append(box.values, 1) }").unwrap();
}
#[test]
fn selecting_one_field_cannot_erase_an_error_routed_to_another_branch() {
let prefix = "type State { Ready, error Broken }\ntype Pair { left: State, right: State }\nrule route(flag: Bool, state: State) (out: Pair)\n| flag | state | out |\n| - | Ready | Pair { left = State.Ready, right = State.Ready } |\n| true | Broken | Pair { left = state, right = State.Ready } |\n| false | Broken | Pair { left = State.Ready, right = state } |\n";
check(&format!(
"{prefix}whole(flag: Bool, state: State) (out: Pair) {{ out = route(flag, state) }}"
))
.unwrap();
rejected(
&format!(
"{prefix}partial(flag: Bool, state: State) (out: State) {{ pair = route(flag, state)\nout = pair.left }}"
),
"C012",
"failure is not handled",
);
for body in [
"keep(flag: Bool, state: State) (out: State) { alias = state\npair = route(flag, state)\nout = alias }",
"copy(left: State, right: State) (out: Pair) { out = Pair { left = left, right = right } }\nkeep(state: State) (out: State) { pair = copy(state, state)\nout = pair.left }",
"ignore(state: State) () recover(\"optional result discarded\") {}\nkeep(flag: Bool, state: State) () { pair = route(flag, state)\nignore(pair.left)\nignore(pair.right) }",
"wrap(flag: Bool, state: State) (out: Pair) { out = route(flag, state) }\nkeep(flag: Bool, state: State) (out: State) { pair = wrap(flag, state)\nout = state }",
"loop keep(flag: Bool, states: List<State>) (out: List<Pair>)\n| each state in states | result |\n| out | list route(flag, state) |\n",
] {
check(&format!("{prefix}{body}")).unwrap();
}
for body in [
"wrap(flag: Bool, state: State) (out: Pair) { out = route(flag, state) }\npartial(flag: Bool, state: State) (out: State) { pair = wrap(flag, state)\nout = pair.left }",
"ignore(state: State) () recover(\"optional result discarded\") {}\npartial(flag: Bool, state: State) () { pair = route(flag, state)\nignore(pair.left) }",
"partial(flag: Bool, state: State) (out: State) { pair = route(flag, state)\nagain = route(flag, state)\nout = pair.left }",
"loop partial(flag: Bool, states: List<State>) (out: Pair?)\n| each state in states | result |\n| out | first route(flag, state) |\n",
] {
rejected(&format!("{prefix}{body}"), "C012", "failure is not handled");
}
}
#[test]
fn equivalent_error_routes_do_not_multiply_obligations() {
let mut source = String::from(
"type State { Ready, error Broken }\nrule keep(flag: Bool, state: State) (out: State)\n| flag | state | out |\n| - | Ready | state |\n| true | Broken | state |\n| false | Broken | state |\nf(flag: Bool, state: State) (out: State) { out = state\n",
);
for _ in 0..100 {
source.push_str("out = keep(flag, out)\n");
}
source.push('}');
check(&source).unwrap();
}