Skip to content

Commit d811c2b

Browse files
committed
Lower core surface types to Rust std types
1 parent 0f1c63d commit d811c2b

3 files changed

Lines changed: 49 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 boundaries are `Log.write(message: read String)` and `Assert.equal(left: read String, right: read String)`, which lower to explicit runtime hooks. Simple core operations such as `String.concat(left: read String, right: read String)` keep RSScript `.rssi` signatures for checking and review, but lower directly to Rust standard-library expressions. Built-in boolean literals, numeric operators, comparison operators, and `Option<T>` constructors lower to the corresponding Rust literals, operators, and enum constructors; user-defined operator overloading remains forbidden.
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. Simple core operations such as `String.concat(left: read String, right: read String)` keep RSScript `.rssi` signatures for checking and review, but lower directly to Rust standard-library expressions. Built-in boolean literals, numeric operators, comparison operators, `Option<T>` constructors, and core surface types such as `Bytes`, `Buffer`, `Path`, `List<T>`, `Map<K,V>`, and `Set<T>` lower to the corresponding Rust literals, operators, enum constructors, and standard-library types; user-defined operator overloading remains forbidden.
422422

423423
Smallest runnable example:
424424

src/rust_lower.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,11 +761,24 @@ impl<'a> RustLowerer<'a> {
761761
let lowered = match ty.name.as_str() {
762762
"Unit" => "()".to_string(),
763763
"Bool" => "bool".to_string(),
764+
"Byte" => "u8".to_string(),
765+
"Char" => "char".to_string(),
764766
"Int" => "i64".to_string(),
767+
"Int8" => "i8".to_string(),
768+
"Int16" => "i16".to_string(),
769+
"Int32" => "i32".to_string(),
765770
"Int64" => "i64".to_string(),
771+
"UInt" => "u64".to_string(),
772+
"UInt8" => "u8".to_string(),
773+
"UInt16" => "u16".to_string(),
774+
"UInt32" => "u32".to_string(),
766775
"UInt64" => "u64".to_string(),
767776
"Float" => "f64".to_string(),
777+
"Float32" => "f32".to_string(),
778+
"Float64" => "f64".to_string(),
768779
"String" => "String".to_string(),
780+
"Bytes" | "Buffer" => "Vec<u8>".to_string(),
781+
"Path" => "std::path::PathBuf".to_string(),
769782
"Result" if ty.args.len() == 2 => format!(
770783
"Result<{}, {}>",
771784
self.lower_type_ref(&ty.args[0], ManagedPosition::Nested),
@@ -788,6 +801,12 @@ impl<'a> RustLowerer<'a> {
788801
self.lower_type_ref(&ty.args[0], ManagedPosition::Nested),
789802
self.lower_type_ref(&ty.args[1], ManagedPosition::Nested)
790803
),
804+
"Set" if ty.args.len() == 1 => {
805+
format!(
806+
"std::collections::HashSet<{}>",
807+
self.lower_type_ref(&ty.args[0], ManagedPosition::Nested)
808+
)
809+
}
791810
"ResourcePool" if ty.args.len() == 1 => format!(
792811
"rsscript_runtime::ResourcePool<{}>",
793812
self.lower_type_ref(&ty.args[0], ManagedPosition::Nested)

tests/checker.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,35 @@ pub fn maybe_value(flag: Bool) -> Option<Int> {
282282
assert!(rust.contains("return None;"));
283283
}
284284

285+
#[test]
286+
fn rust_lowering_maps_core_surface_types_to_rust_std_types() {
287+
let source = r#"
288+
struct CoreError {
289+
code: Int
290+
}
291+
292+
pub fn inspect_core(
293+
path: read Path,
294+
bytes: read Bytes,
295+
buffer: mut Buffer,
296+
names: read Set<String>,
297+
counts: read Map<String, Int>,
298+
items: read List<Int>,
299+
) -> Result<fresh Bytes, CoreError> {
300+
return Err(CoreError(code: 0))
301+
}
302+
"#;
303+
let rust = lower_source_to_rust("core-types.rss", source).expect("source should lower");
304+
305+
assert!(rust.contains("path: &std::path::PathBuf"));
306+
assert!(rust.contains("bytes: &Vec<u8>"));
307+
assert!(rust.contains("buffer: &mut Vec<u8>"));
308+
assert!(rust.contains("names: &std::collections::HashSet<String>"));
309+
assert!(rust.contains("counts: &std::collections::HashMap<String, i64>"));
310+
assert!(rust.contains("items: &Vec<i64>"));
311+
assert!(rust.contains("-> Result<Vec<u8>, CoreError>"));
312+
}
313+
285314
#[test]
286315
fn rust_lowering_maps_log_write_to_runtime_output_hook() {
287316
let source = r#"

0 commit comments

Comments
 (0)