Skip to content

Commit c1b7b40

Browse files
committed
Add runnable assert equal hook
1 parent 919d668 commit c1b7b40

5 files changed

Lines changed: 32 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ If a lowered package contains `fn main() -> Unit` or `fn main() -> Result<Unit,
418418

419419
`rss verify-rust <file.rss> --out-dir <directory>` keeps the generated package used for backend checking, including `rsscript-source-map.json`, so unmappable rustc diagnostics can be inspected against the generated Rust.
420420

421-
The first observable runtime boundary is `Log.write(message: read String)`, which lowers to an explicit runtime output hook.
421+
The first observable runtime boundaries are `Log.write(message: read String)` and `Assert.equal(left: read String, right: read String)`, which lower to explicit runtime hooks.
422422

423423
Smallest runnable example:
424424

examples/assert_equal.rss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() -> Unit {
2+
Assert.equal(left: read "rss", right: read "rss")
3+
Log.write(message: read "assert equal passed")
4+
return Unit
5+
}

runtime/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ pub fn log_write(message: &str) {
9393
println!("{message}");
9494
}
9595

96+
pub fn assert_equal(left: &str, right: &str) {
97+
assert_eq!(left, right);
98+
}
99+
96100
pub struct GcRead<'a, T>(Ref<'a, T>);
97101

98102
impl<T> Deref for GcRead<'_, T> {

src/rust_lower.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ fn is_native_boundary(effect: &EffectDecl) -> bool {
10161016
fn lower_callee(callee: &Callee) -> String {
10171017
match callee {
10181018
callee if is_log_write_callee(callee) => "rsscript_runtime::log_write".to_string(),
1019+
callee if is_assert_equal_callee(callee) => "rsscript_runtime::assert_equal".to_string(),
10191020
Callee::Name(name) => rust_ident(name),
10201021
Callee::Qualified { namespace, name } if type_root_name(namespace) == "ResourcePool" => {
10211022
format!("rsscript_runtime::ResourcePool::{}", rust_ident(name))
@@ -1030,6 +1031,10 @@ fn is_log_write_callee(callee: &Callee) -> bool {
10301031
matches!(callee, Callee::Qualified { namespace, name } if type_root_name(namespace) == "Log" && name == "write")
10311032
}
10321033

1034+
fn is_assert_equal_callee(callee: &Callee) -> bool {
1035+
matches!(callee, Callee::Qualified { namespace, name } if type_root_name(namespace) == "Assert" && name == "equal")
1036+
}
1037+
10331038
fn is_resource_pool_borrow_callee(callee: &Callee) -> bool {
10341039
matches!(callee, Callee::Qualified { namespace, name } if type_root_name(namespace) == "ResourcePool" && name == "borrow")
10351040
}

tests/checker.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,23 @@ fn main() -> Unit {
278278
assert!(rust.contains("rsscript_runtime::log_write(&\"hello RSScript\".to_string());"));
279279
}
280280

281+
#[test]
282+
fn rust_lowering_maps_assert_equal_to_runtime_hook() {
283+
let source = r#"
284+
fn main() -> Unit {
285+
Assert.equal(left: read "rss", right: read "rss")
286+
return Unit
287+
}
288+
"#;
289+
let rust = lower_source_to_rust("assert.rss", source).expect("source should lower");
290+
291+
assert!(
292+
rust.contains(
293+
"rsscript_runtime::assert_equal(&\"rss\".to_string(), &\"rss\".to_string());"
294+
)
295+
);
296+
}
297+
281298
#[test]
282299
fn rust_lowering_maps_try_operator_to_rust_result_propagation() {
283300
let source = r#"

0 commit comments

Comments
 (0)