use super::*; #[test] fn handler_specializes_only_used_bindings_and_forwards_static_arguments() { let program = check(r#" identity(value: Int) (out: Int) { out = value } double(value: Int) (out: Int) { out = value * 2 } apply(value: Int, transform: handler(value: Int) (out: Int)) (out: Int) { out = transform(value) } forward(value: Int, transform: handler(value: Int) (out: Int)) (out: Int) { out = apply(value, transform) } first() (out: Int) { out = forward(3, identity) } second() (out: Int) { out = apply(4, double) + apply(5, identity) } unused(transform: handler(value: Int) (out: Int)) (out: Int) { out = transform(1) } "#).unwrap(); assert_eq!(program.functions.len(), 7); assert_eq!( program .functions .iter() .filter(|f| f.signature.source_name.ends_with(".apply")) .count(), 2 ); assert!( program .functions .iter() .all(|f| !f.signature.source_name.ends_with(".unused")) ); assert!( program .functions .iter() .all(|f| !matches!(f.body, Body::Contract)) ); assert!( program .calls .iter() .all(|call| call.target < program.functions.len()) ); let ownership = dever::compiler::check::ownership::analyze(&program); let _ = ownership; } #[test] fn handler_contracts_check_types_named_outputs_and_range_variance() { check(r#" wide(value: Int) (out: Int >= 2) { check value >= 2 else Invalid out = value } apply(value: Int >= 2, transform: handler(value: Int >= 2) (out: Int >= 1)) (out: Int >= 1) { out = transform(value) } start() (out: Int) { out = apply(2, wide) } "#).unwrap(); for target in [ "target(value: Int >= 3) (out: Int >= 1) { out = value }", "target(value: Int) (out: Int) { out = value }", "target(value: Int) (wrong: Int >= 1) { wrong = 1 }", "target(value: Text) (out: Int >= 1) { out = length(value) + 1 }", ] { rejected( &format!( "{target}\napply(transform: handler(value: Int >= 2) (out: Int >= 1)) (out: Int) {{ out = transform(2) }}\nstart() (out: Int) {{ out = apply(target) }}" ), "C005", "handler input", ); } rejected( "unused(transform: handler(value: Int) (out: Text)) (out: Int) { out = transform(1) }", "C005", "expected", ); } #[test] fn handler_is_static_read_only_and_cannot_escape_into_values() { rejected( "bad(transform: handler(value: Int) (out: Int)) (out: Int) { transform = 1\nout = 0 }", "C005", "read-only", ); rejected( "type Bad { transform: handler() () }", "C005", "static input", ); rejected( "bad() (out: handler() ()) { out = bad }", "C005", "static input", ); rejected( "apply(transform: handler() (out: Int)) (out: Int) { out = transform() }\nstart(target: Int) (out: Int) { out = apply(target) }", "C005", "runtime values", ); rejected( "bad(transform: handler() (out: Int)) (out: Int) { saved = transform\nout = 1 }", "C005", "static arguments", ); rejected( "bad(transform: handler() (out: Int)) (out: Int) { out = transform() }\ncmd entry = bad", "C005", "static handler", ); rejected( "apply(transform: handler() (out: Int)) (out: Int) { out = transform() }\nstart() (out: Int) { out = apply(start) }", "C010", "recursive", ); } #[test] fn handler_rule_columns_remain_declarative_and_do_not_generate_runtime_arguments() { let source = r#" identity(value: Int) (out: Int) { out = value } rule apply(value: Int, transform: handler(value: Int) (out: Int)) (out: Int) | value | transform | out | | - | - | transform(value) | start() (out: Int) { out = apply(9, identity) } "#; let program = check(source).unwrap(); assert_eq!(program.functions.len(), 3); rejected( &source.replace("| - | - |", "| - | 1 |"), "C005", "static handler column", ); } #[test] fn handler_failures_propagate_and_captured_errors_keep_handling_obligations() { let source = r#" type Outcome { Done(value: Int), error Bad(message: Text) } checked(value: Int) (out: Int) { check value >= 1 else Outcome.Bad("positive required") out = value } apply(value: Int, transform: handler(value: Int) (out: Int)) (out: Int) { out = transform(value) } capture(value: Int, transform: handler(value: Int) (out: Int)) (out: Outcome) { out = result(transform(value)) } start(value: Int) (out: Int) { out = apply(value, checked) } wrapped(value: Int) (out: Outcome) { out = capture(value, checked) } "#; let program = check(source).unwrap(); let start = program .functions .iter() .find(|f| f.signature.source_name.ends_with(".start")) .unwrap(); assert_eq!(start.failures, [Failure::Choice(1, 1)].into()); rejected( &source.replace( "out = result(transform(value))", "lost: Outcome = result(transform(value))\nout = Done(0)", ), "C012", "not handled", ); rejected( &source.replace("Outcome.Bad(\"positive required\")", "Invalid"), "C005", "matching branch", ); } #[test] fn handler_only_rules_keep_the_source_input_requirement() { check("value() (out: Int) { out = 1 }\nrule apply(transform: handler() (out: Int)) (out: Int)\n| transform | out |\n| - | transform() |\nstart() (out: Int) { out = apply(value) }").unwrap(); rejected( "rule empty() (out: Int)\n| out |\n| 1 |", "C009", "at least one input", ); } #[test] fn stronger_handler_outputs_remove_only_proved_instance_checks() { let program = check("positive(value: Int) (out: Int >= 1) { out = 1 }\napply(value: Int, transform: handler(value: Int) (out: Int)) (out: Int >= 1) { checked = transform(value)\ncheck checked >= 1 else Invalid\nout = checked }\nstart() (out: Int) { out = apply(0, positive) }").unwrap(); let apply = program .functions .iter() .find(|f| f.signature.source_name.ends_with(".apply")) .unwrap(); assert!(apply.failures.is_empty()); let Body::Statements { statements, .. } = &apply.body else { panic!() }; assert!( !statements .iter() .any(|statement| matches!(statement, Statement::Check { .. })) ); rejected( "apply(value: Int >= 1, transform: handler(value: Int) (out: Int)) (out: Int) { check value >= 1 else Invalid\nout = transform(value) }", "C012", "already guaranteed", ); }