diff --git a/crates/ailang-prose/src/lib.rs b/crates/ailang-prose/src/lib.rs index fe69811..0c71812 100644 --- a/crates/ailang-prose/src/lib.rs +++ b/crates/ailang-prose/src/lib.rs @@ -321,6 +321,8 @@ fn write_const_def(out: &mut String, cd: &ConstDef, level: usize) { write_term(out, &cd.value, level); } +/// Render a class declaration in Rust-flavoured Form-B: +/// `class Name a [extends Super] { }`. fn write_class_def(out: &mut String, c: &ClassDef, level: usize) { write_doc(out, &c.doc, level); indent(out, level); @@ -340,6 +342,9 @@ fn write_class_def(out: &mut String, c: &ClassDef, level: usize) { out.push('}'); } +/// Render one method line of a class: signature plus optional +/// `default { }`. Class methods carry no parameter names +/// in the AST, so slots are named `x`, `x1`, …. fn write_class_method(out: &mut String, m: &ClassMethod, level: usize) { indent(out, level); out.push_str("fn "); @@ -374,6 +379,9 @@ fn write_class_method(out: &mut String, m: &ClassMethod, level: usize) { } } } else { + // Defensive fallback: a non-Fn class-method type. Render the + // bare type so nothing crashes; this path is unreachable for + // typechecked input. out.push_str(") -> "); write_type(out, &m.ty); } @@ -389,6 +397,8 @@ fn write_class_method(out: &mut String, m: &ClassMethod, level: usize) { } } +/// Render an instance declaration in Form-B: +/// `instance Class Type { }`. fn write_instance_def(out: &mut String, i: &InstanceDef, level: usize) { write_doc(out, &i.doc, level); indent(out, level); @@ -404,6 +414,9 @@ fn write_instance_def(out: &mut String, i: &InstanceDef, level: usize) { out.push('}'); } +/// Render one method body of an instance: `fn name { }`. +/// The signature is recoverable from the class declaration via +/// `InstanceDef.class` lookup, so it is intentionally elided. fn write_instance_method(out: &mut String, m: &InstanceMethod, level: usize) { indent(out, level); out.push_str("fn "); diff --git a/crates/ailang-prose/tests/snapshot.rs b/crates/ailang-prose/tests/snapshot.rs index 10c94fc..075e818 100644 --- a/crates/ailang-prose/tests/snapshot.rs +++ b/crates/ailang-prose/tests/snapshot.rs @@ -76,3 +76,8 @@ fn snapshot_bench_list_sum() { fn snapshot_test_22b3_default_e2e() { check_snapshot("test_22b3_default_e2e"); } + +#[test] +fn snapshot_test_22b2_instance_present() { + check_snapshot("test_22b2_instance_present"); +} diff --git a/examples/test_22b2_instance_present.prose.txt b/examples/test_22b2_instance_present.prose.txt new file mode 100644 index 0000000..d7c1459 --- /dev/null +++ b/examples/test_22b2_instance_present.prose.txt @@ -0,0 +1,17 @@ +// module test_22b2_instance_present + +class Show a { + fn show(x: a) -> Str +} + +instance Show Int { + fn show { + |x: a| -> Str { + "n" + } + } +} + +fn main() -> Str { + show(42) +}