diff --git a/build.rs b/build.rs index 8cd5847d..444d4a94 100644 --- a/build.rs +++ b/build.rs @@ -74,7 +74,7 @@ impl HostBindingKind { pub(crate) const SQLITE_RESERVED_TOP_START: u16 = 0xFFFC; pub(crate) const SQLITE_RESERVED_TOP_END: u16 = u16::MAX; pub(crate) const ORDINARY_BLOCK_START: u16 = 0xFFA2; -pub(crate) const SPECIAL_CALL_BLOCK_START: u16 = 0xFF90; +pub(crate) const SPECIAL_CALL_BLOCK_START: u16 = 0xFF8C; pub(crate) const SPECIAL_CALL_BLOCK_END: u16 = 0xFFA1; #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -1100,6 +1100,12 @@ fn render_builtin_catalog( .unwrap(); writeln!(&mut out, " match name {{").unwrap(); for entry in catalog { + // Internal entries are compiler-owned lowering helpers. Their source + // spellings must not resolve from guest source; trusted compiler and + // runtime consumers address them through the generated call index. + if entry.class == CatalogClass::Internal { + continue; + } writeln!( &mut out, " {:?} => Some(Self::{}),", @@ -1724,6 +1730,7 @@ pub(crate) fn callable_param_expr(label: &str) -> String { match label { "any" => "CallableParamType::Any".to_string(), "null" => "CallableParamType::Null".to_string(), + "()" => "CallableParamType::Unit".to_string(), "int" => "CallableParamType::Int".to_string(), "float" => "CallableParamType::Float".to_string(), "bool" => "CallableParamType::Bool".to_string(), @@ -1793,6 +1800,10 @@ fn appended_builtin_order() -> &'static [&'static str] { "__map_iter_close", "__bind_callable", "__detach_local", + "__json_decode_typed", + "__json_encode_typed", + "__logical_render", + "__map_edit", ] } @@ -2162,6 +2173,7 @@ fn callable_docs(name: &str, attrs: &[Attribute]) -> String { fn emit_guest_result_schema_fixture(manifest: &Path, out: &Path) { for (input, output) in [ ("result_sdk_signature.rs", "result_sdk_build_schemas.rs"), + ("option_sdk_signature.rs", "option_sdk_build_schemas.rs"), ( "result_sdk_resource_signature.rs", "result_sdk_resource_build_schemas.rs", @@ -2216,6 +2228,11 @@ fn guest_schema_expr(shape: &pd_host_schema::GuestTypeShape<'_>) -> String { guest_schema_expr(ok), guest_schema_expr(err) ), + S::Option(inner) => format!( + "vm::HostTypeSchema::Option(Box::new({}))", + guest_schema_expr(inner) + ), + S::Unit => "vm::HostTypeSchema::Unit".to_string(), S::Optional(inner) => format!( "vm::HostTypeSchema::Optional(Box::new({}))", guest_schema_expr(inner) @@ -2228,6 +2245,15 @@ fn guest_schema_expr(shape: &pd_host_schema::GuestTypeShape<'_>) -> String { "vm::HostTypeSchema::Resource({})", guest_resource_key_expr(spec) ), + S::Callable { params, result } => format!( + "vm::HostTypeSchema::Callable {{ params: vec![{}], result: Box::new({}) }}", + params + .iter() + .map(guest_schema_expr) + .collect::>() + .join(", "), + guest_schema_expr(result) + ), S::Named(ty) => format!( "<{} as vm::HostNamedStruct>::host_type_schema()", rust_schema_type(ty) @@ -2329,7 +2355,7 @@ pub(crate) fn render_guest_descriptor_model(function: &ItemFn) -> String { fn return_type_label(output: &ReturnType) -> String { match output { - ReturnType::Default => "null".to_string(), + ReturnType::Default => "()".to_string(), ReturnType::Type(_, ty) => pd_host_schema::type_label(ty) .unwrap_or_else(|message| panic!("unsupported callable return type: {message}")), } @@ -2342,6 +2368,7 @@ fn static_return_type_label(output: &ReturnType) -> String { fn value_type_from_label(label: &str) -> &'static str { match label { "null" => "Null", + "()" => "Null", "int" => "Int", "float" => "Float", "bool" => "Bool", diff --git a/crates/pd-host-schema/src/lib.rs b/crates/pd-host-schema/src/lib.rs index 5149decb..12314dd2 100644 --- a/crates/pd-host-schema/src/lib.rs +++ b/crates/pd-host-schema/src/lib.rs @@ -544,7 +544,7 @@ pub fn type_label(ty: &Type) -> Result { } _ => Err("unsupported callable type".to_string()), }, - Type::Tuple(tuple) if tuple.elems.is_empty() => Ok("null".to_string()), + Type::Tuple(tuple) if tuple.elems.is_empty() => Ok("()".to_string()), Type::Path(path) => { let Some(segment) = path.path.segments.last() else { return Err("unsupported callable type".to_string()); @@ -567,7 +567,7 @@ pub fn type_label(ty: &Type) -> Result { "ResourceOwned" => Err( "ResourceOwned is input-only; it cannot be used as a return schema".to_string(), ), - "GuestResult" => Ok(guest_type_shape(ty)?.label()), + "GuestResult" | "GuestOption" => Ok(guest_type_shape(ty)?.label()), "VmCallable" => callable_type_label(segment), "Unknown" | "UnknownValue" => Ok("unknown".to_string()), "CallOutcome" => Ok("unknown".to_string()), @@ -614,9 +614,19 @@ fn parameter_type_label_inner(ty: &Type) -> Result<(String, bool), String> { #[derive(Clone, Debug)] pub enum GuestTypeShape<'a> { Result(Box, Box), + Option(Box), + /// Logical guest `()`, physically carried by the internal Null value. + Unit, Optional(Box), Array(Box), Resource(Box), + /// A typed callable value: the signature is part of the public schema, the + /// value itself owns no resources. Dispatch identity stays with the VM + /// callable and its Program. + Callable { + params: Vec, + result: Box, + }, Scalar(&'static str), Named(&'a Type), } @@ -625,9 +635,19 @@ impl GuestTypeShape<'_> { pub fn label(&self) -> String { match self { Self::Result(ok, err) => format!("Result<{}, {}>", ok.label(), err.label()), + Self::Option(inner) => format!("Option<{}>", inner.label()), + Self::Unit => "()".into(), Self::Optional(inner) => format!("{} | null", inner.label()), Self::Array(inner) => format!("array<{}>", inner.label()), Self::Resource(_) => "resource".into(), + Self::Callable { params, result } => { + let params = params + .iter() + .map(Self::label) + .collect::>() + .join(", "); + format!("fn({params}) -> {}", result.label()) + } Self::Scalar(label) => (*label).into(), Self::Named(ty) => path_last_ident(ty).expect("named path"), } @@ -641,8 +661,12 @@ impl GuestTypeShape<'_> { out.extend(err.resources()); out } - Self::Optional(inner) | Self::Array(inner) => inner.resources(), + Self::Option(inner) | Self::Optional(inner) | Self::Array(inner) => inner.resources(), Self::Resource(spec) => vec![spec], + // A callable value transfers dispatch identity only; signature + // resource types describe invocation arguments, not values this + // payload owns. + Self::Callable { .. } => Vec::new(), _ => Vec::new(), } } @@ -668,6 +692,9 @@ pub fn guest_type_shape(ty: &Type) -> Result, String> { return Err("GuestResult schema exceeds nesting limit".into()); } let ty = unwrap_surface(ty); + if matches!(ty, Type::Tuple(tuple) if tuple.elems.is_empty()) { + return Ok(GuestTypeShape::Unit); + } if let Some((ok, err)) = guest_result_arguments(ty)? { return Ok(GuestTypeShape::Result( Box::new(walk(ok, depth + 1)?), @@ -680,6 +707,34 @@ pub fn guest_type_shape(ty: &Type) -> Result, String> { if let Type::Path(path) = ty { let segment = path.path.segments.last().ok_or("empty type path")?; let ident = segment.ident.to_string(); + if ident == "GuestOption" { + let argument = one_type_argument(segment, &ident)?; + let inner = walk(argument, depth + 1)?; + return Ok(GuestTypeShape::Option(Box::new(inner))); + } + if ident == "VmCallable" { + let PathArguments::AngleBracketed(args) = &segment.arguments else { + return Err("VmCallable requires a function signature".into()); + }; + let Some(GenericArgument::Type(Type::BareFn(function))) = args.args.first() else { + return Err("VmCallable requires fn(...) -> ...".into()); + }; + if args.args.len() != 1 { + return Err("VmCallable requires one function signature".into()); + } + let mut params = Vec::with_capacity(function.inputs.len()); + for input in &function.inputs { + params.push(walk(&input.ty, depth + 1)?); + } + let result = match &function.output { + syn::ReturnType::Default => GuestTypeShape::Unit, + syn::ReturnType::Type(_, ty) => walk(ty, depth + 1)?, + }; + return Ok(GuestTypeShape::Callable { + params, + result: Box::new(result), + }); + } if ident == "Option" || ident == "Vec" { let argument = one_type_argument(segment, &ident)?; if ident == "Vec" && path_last_ident(argument).as_deref() == Some("u8") { @@ -727,6 +782,7 @@ pub fn guest_result_shape(ty: &Type) -> Result>, Strin match unwrap_surface(ty) { Type::Path(path) => path.path.segments.last().is_some_and(|segment| { segment.ident == "GuestResult" + || segment.ident == "GuestOption" || matches!(segment.ident.to_string().as_str(), "Option" | "Vec") && one_type_argument(segment, &segment.ident.to_string()) .is_ok_and(contains) @@ -794,7 +850,7 @@ fn callable_type_label(segment: &syn::PathSegment) -> Result { .map(|input| type_label(&input.ty)) .collect::, _>>()?; let result = match &function.output { - syn::ReturnType::Default => "null".to_string(), + syn::ReturnType::Default => "()".to_string(), syn::ReturnType::Type(_, ty) => type_label(ty)?, }; Ok(format!("fn({}) -> {result}", params.join(", "))) diff --git a/crates/pd-host-schema/tests/guest_option.rs b/crates/pd-host-schema/tests/guest_option.rs new file mode 100644 index 00000000..f38fbd1c --- /dev/null +++ b/crates/pd-host-schema/tests/guest_option.rs @@ -0,0 +1,169 @@ +use pd_host_schema::{ + GuestTypeShape, ResourceMode, guest_result_shape, guest_type_shape, type_label, +}; +use syn::parse_quote; + +#[derive(Debug)] +struct FakeResource; + +fn shape(ty: &syn::Type) -> GuestTypeShape<'_> { + guest_type_shape(ty).expect("typed guest Option schema") +} + +#[test] +fn guest_option_labels_are_distinct_from_legacy_nullable_option() { + let value: syn::Type = parse_quote!(GuestOption>); + let shape = shape(&value); + assert!(matches!(shape, GuestTypeShape::Option(_))); + assert_eq!(shape.label(), "Option>"); + assert_eq!(type_label(&value).unwrap(), "Option>"); + + let nullable: syn::Type = parse_quote!(Option); + assert_eq!(type_label(&nullable).unwrap(), "int | null"); + let (label, optional) = pd_host_schema::parameter_type_label(&nullable).unwrap(); + assert_eq!(label, "int | null"); + assert!(optional); + let (label, optional) = pd_host_schema::parameter_type_label(&value).unwrap(); + assert_eq!(label, "Option>"); + assert!(!optional, "a guest Option is not an omitted argument"); +} + +#[test] +fn guest_option_nests_unit_result_and_nullable_payloads() { + let nested: syn::Type = parse_quote!(GuestOption>>>); + let nested_shape = shape(&nested); + assert_eq!(nested_shape.label(), "Option | null>>"); + let unit: syn::Type = parse_quote!(GuestOption<()>); + let unit_shape = shape(&unit); + assert!(matches!(unit_shape, GuestTypeShape::Option(_))); + assert_eq!(unit_shape.label(), "Option<()>"); + let unit_only: syn::Type = parse_quote!(()); + assert!(matches!(shape(&unit_only), GuestTypeShape::Unit)); + assert_eq!(shape(&unit_only).label(), "()"); + assert_eq!(type_label(&unit_only).unwrap(), "()"); + + let result: syn::Type = parse_quote!(GuestOption>); + let result_shape = shape(&result); + assert_eq!(result_shape.label(), "Option>"); + let nested_result: syn::Type = parse_quote!(GuestResult, String>); + assert_eq!(shape(&nested_result).label(), "Result, string>"); +} + +#[test] +fn guest_option_resources_keep_their_access_mode() { + let _ = FakeResource; + let owned: syn::Type = parse_quote!(GuestOption>); + let owned_shape = shape(&owned); + let resources = owned_shape.resources(); + assert_eq!(resources.len(), 1); + assert_eq!(resources[0].mode, ResourceMode::TakeOwned); + assert_eq!( + owned_shape.resource_mode().unwrap(), + Some(ResourceMode::TakeOwned) + ); + assert_eq!(owned_shape.label(), "Option"); + + let borrowed: syn::Type = parse_quote!(GuestOption>); + assert_eq!( + shape(&borrowed).resource_mode().unwrap(), + Some(ResourceMode::Borrow) + ); + let mixed: syn::Type = + parse_quote!(GuestResult, ResourceOwned>); + assert!( + shape(&mixed).resource_mode().is_err(), + "mixed resource modes must remain refused" + ); + let array: syn::Type = parse_quote!(GuestOption>>); + let error = guest_type_shape(&array).unwrap_err(); + assert!(error.contains("resource arrays"), "{error}"); +} + +#[test] +fn guest_option_callable_payload_uses_the_typed_callable_schema() { + let callable: syn::Type = parse_quote!(GuestOption bool>>); + let option_shape = shape(&callable); + assert_eq!(option_shape.label(), "Option bool>"); + let GuestTypeShape::Option(inner) = &option_shape else { + panic!("callable Option must keep the Option wrapper"); + }; + let GuestTypeShape::Callable { params, result } = inner.as_ref() else { + panic!("callable payload must use the Callable shape"); + }; + assert_eq!(params.len(), 2); + assert_eq!(params[1].label(), "string"); + assert_eq!(result.label(), "bool"); + assert!( + option_shape.resources().is_empty(), + "a callable value owns no resource" + ); + assert_eq!(option_shape.resource_mode().unwrap(), None); + + let unit: syn::Type = parse_quote!(GuestOption ()>>); + assert_eq!(shape(&unit).label(), "Option ()>"); + // `fn()` and `fn() -> ()` are the same Rust function type: an omitted + // return publishes logical Unit, not the low-level Null spelling. + let defaulted: syn::Type = parse_quote!(GuestOption>); + let defaulted_shape = shape(&defaulted); + assert_eq!(defaulted_shape.label(), "Option ()>"); + let GuestTypeShape::Option(inner) = &defaulted_shape else { + panic!("bare callable Option must keep the Option wrapper"); + }; + let GuestTypeShape::Callable { params, result } = inner.as_ref() else { + panic!("bare callable payload must use the Callable shape"); + }; + assert!(params.is_empty()); + assert!(matches!(result.as_ref(), GuestTypeShape::Unit)); + + let defaulted_typed: syn::Type = parse_quote!(GuestOption>); + let explicit_typed: syn::Type = parse_quote!(GuestOption ()>>); + assert_eq!(shape(&defaulted_typed).label(), "Option ()>"); + assert_eq!( + shape(&defaulted_typed).label(), + shape(&explicit_typed).label() + ); + + let nested: syn::Type = parse_quote!(GuestResult ()>>, String>); + assert_eq!(shape(&nested).label(), "Result ()>, string>"); + let nested_defaulted: syn::Type = + parse_quote!(GuestResult>, String>); + assert_eq!( + shape(&nested_defaulted).label(), + "Result ()>, string>" + ); + + let malformed: syn::Type = parse_quote!(GuestOption>); + let error = guest_type_shape(&malformed).unwrap_err(); + assert!(error.contains("fn(...) ->"), "{error}"); +} + +#[test] +fn guest_option_shape_detection_and_arity_are_strict() { + let optional: syn::Type = parse_quote!(GuestOption); + assert!( + guest_result_shape(&optional) + .expect("valid guest Option") + .is_some() + ); + let plain: syn::Type = parse_quote!(i64); + assert!(guest_result_shape(&plain).unwrap().is_none()); + let missing: syn::Type = parse_quote!(GuestOption); + let error = guest_result_shape(&missing).unwrap_err(); + assert!(error.contains("requires one"), "{error}"); + let two: syn::Type = parse_quote!(GuestOption); + let error = guest_type_shape(&two).unwrap_err(); + assert!(error.contains("one generic argument"), "{error}"); + let transport: syn::Type = parse_quote!(GuestOption>); + let error = guest_type_shape(&transport).unwrap_err(); + assert!(error.contains("transport results"), "{error}"); +} + +#[test] +fn callable_unit_labels_use_the_unit_spelling() { + let callable: syn::Type = parse_quote!(VmCallable ()>); + assert_eq!(type_label(&callable).unwrap(), "fn(int) -> ()"); + let bare: syn::Type = parse_quote!(VmCallable); + assert_eq!(type_label(&bare).unwrap(), "fn() -> ()"); + let optional_callable: syn::Type = parse_quote!(Option ()>>); + assert_eq!(type_label(&optional_callable).unwrap(), "fn() -> () | null"); +} diff --git a/crates/rustscript/src/bin/rustscript-lsp.rs b/crates/rustscript/src/bin/rustscript-lsp.rs index 6ca17621..1090930a 100644 --- a/crates/rustscript/src/bin/rustscript-lsp.rs +++ b/crates/rustscript/src/bin/rustscript-lsp.rs @@ -1061,10 +1061,8 @@ impl LspServer { None => { // Unknown source id: fall back to the entry document's URI // and text so a diagnostic is still surfaced. - match self.documents.values().find(|doc| doc.model.is_some()) { - Some(entry) => (entry.uri.as_str(), entry.text.as_str()), - None => return None, - } + let entry = self.documents.values().find(|doc| doc.model.is_some())?; + (entry.uri.as_str(), entry.text.as_str()) } }; let uri = self.uri_for_source_name(name); @@ -2058,7 +2056,7 @@ mod tests { HostTypeSchema::Resource(database_key), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, ) .with_description("close the adapter database"), ); @@ -2075,7 +2073,7 @@ mod tests { assert_eq!( content["content"], serde_json::json!( - "adapter::close(connection: take_owned resource) -> null\n// close the adapter database" + "adapter::close(connection: take_owned resource) -> ()\n// close the adapter database" ) ); } @@ -2124,7 +2122,7 @@ mod tests { HostTypeSchema::Resource(database_key), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, ) .with_description("close the adapter database"), ); @@ -2152,11 +2150,9 @@ mod tests { file_signature["signatures"][0]["documentation"]["value"], serde_json::json!("close the adapter file") ); - assert!( - file_signature["signatures"][0]["label"] - .as_str() - .unwrap_or("") - .contains("resource") + assert_eq!( + file_signature["signatures"][0]["label"], + serde_json::json!("adapter::close(handle: take_owned resource) -> bool") ); let database_signature = server.handle_signature_help(&serde_json::json!({ @@ -2167,32 +2163,35 @@ mod tests { database_signature["signatures"][0]["documentation"]["value"], serde_json::json!("close the adapter database") ); - assert!( - database_signature["signatures"][0]["label"] - .as_str() - .unwrap_or("") - .contains("resource") + assert_eq!( + database_signature["signatures"][0]["label"], + serde_json::json!( + "adapter::close(connection: take_owned resource) -> ()" + ) ); let file_hover = server.handle_hover(&serde_json::json!({ "textDocument": { "uri": uri }, "position": { "line": 3, "character": 2 } })); + let file_hover_text = file_hover["contents"]["value"].as_str().unwrap_or(""); + assert!(file_hover_text.contains("bool"), "{file_hover_text}"); assert!( - file_hover["contents"]["value"] - .as_str() - .unwrap_or("") - .contains("bool") + !(file_hover_text.contains("-> ()") || file_hover_text.contains("\n()\n")), + "{file_hover_text}" ); let database_hover = server.handle_hover(&serde_json::json!({ "textDocument": { "uri": uri }, "position": { "line": 4, "character": 2 } })); + let database_hover_text = database_hover["contents"]["value"].as_str().unwrap_or(""); + assert!( + database_hover_text.contains("-> ()") || database_hover_text.contains("\n()\n"), + "{database_hover_text}" + ); assert!( - database_hover["contents"]["value"] - .as_str() - .unwrap_or("") - .contains("null") + !database_hover_text.contains("null"), + "{database_hover_text}" ); let completions = server.handle_completion(&serde_json::json!({ diff --git a/crates/rustscript/tests/fixtures/sqlite-host-catalog.fixture b/crates/rustscript/tests/fixtures/sqlite-host-catalog.fixture index 63fcc87a..f68401da 100644 --- a/crates/rustscript/tests/fixtures/sqlite-host-catalog.fixture +++ b/crates/rustscript/tests/fixtures/sqlite-host-catalog.fixture @@ -12,92 +12,92 @@ { "name": "path", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "mode", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "root", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "limits", "ty": { - "Optional": { + "Option": { "Named": { "name": "SqliteLimits", "fields": [ { "name": "max_connections", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_statements", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_rows", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_columns", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_result_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_statement_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_parameters", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_parameter_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_pending_operations", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_transaction_ms", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "busy_timeout_ms", "ty": { - "Optional": "Int" + "Option": "Int" } } ] @@ -114,67 +114,67 @@ { "name": "max_connections", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_statements", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_rows", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_columns", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_result_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_statement_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_parameters", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_parameter_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_pending_operations", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_transaction_ms", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "busy_timeout_ms", "ty": { - "Optional": "Int" + "Option": "Int" } } ], @@ -190,25 +190,25 @@ { "name": "int_value", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "float_value", "ty": { - "Optional": "Float" + "Option": "Float" } }, { "name": "text_value", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "blob_value", "ty": { - "Optional": "Bytes" + "Option": "Bytes" } } ], @@ -258,25 +258,25 @@ { "name": "int_value", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "float_value", "ty": { - "Optional": "Float" + "Option": "Float" } }, { "name": "text_value", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "blob_value", "ty": { - "Optional": "Bytes" + "Option": "Bytes" } } ] @@ -296,7 +296,7 @@ { "name": "next_cursor", "ty": { - "Optional": "Int" + "Option": "Int" } } ], @@ -319,25 +319,25 @@ { "name": "int_value", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "float_value", "ty": { - "Optional": "Float" + "Option": "Float" } }, { "name": "text_value", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "blob_value", "ty": { - "Optional": "Bytes" + "Option": "Bytes" } } ] @@ -358,7 +358,7 @@ { "name": "params", "ty": { - "Optional": { + "Option": { "Array": { "Named": { "name": "SqliteValue", @@ -370,25 +370,25 @@ { "name": "int_value", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "float_value", "ty": { - "Optional": "Float" + "Option": "Float" } }, { "name": "text_value", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "blob_value", "ty": { - "Optional": "Bytes" + "Option": "Bytes" } } ] @@ -400,80 +400,80 @@ { "name": "query", "ty": { - "Optional": "Bool" + "Option": "Bool" } }, { "name": "limits", "ty": { - "Optional": { + "Option": { "Named": { "name": "SqliteLimits", "fields": [ { "name": "max_connections", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_statements", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_rows", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_columns", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_result_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_statement_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_parameters", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_parameter_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_pending_operations", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_transaction_ms", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "busy_timeout_ms", "ty": { - "Optional": "Int" + "Option": "Int" } } ] @@ -494,7 +494,7 @@ { "name": "execute", "ty": { - "Optional": { + "Option": { "Named": { "name": "SqliteExecuteResult", "fields": [ @@ -514,7 +514,7 @@ { "name": "query", "ty": { - "Optional": { + "Option": { "Named": { "name": "SqliteQueryResult", "fields": [ @@ -545,25 +545,25 @@ { "name": "int_value", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "float_value", "ty": { - "Optional": "Float" + "Option": "Float" } }, { "name": "text_value", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "blob_value", "ty": { - "Optional": "Bytes" + "Option": "Bytes" } } ] @@ -583,7 +583,7 @@ { "name": "next_cursor", "ty": { - "Optional": "Int" + "Option": "Int" } } ] @@ -608,92 +608,92 @@ { "name": "path", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "mode", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "root", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "limits", "ty": { - "Optional": { + "Option": { "Named": { "name": "SqliteLimits", "fields": [ { "name": "max_connections", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_statements", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_rows", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_columns", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_result_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_statement_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_parameters", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_parameter_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_pending_operations", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_transaction_ms", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "busy_timeout_ms", "ty": { - "Optional": "Int" + "Option": "Int" } } ] @@ -741,25 +741,25 @@ { "name": "int_value", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "float_value", "ty": { - "Optional": "Float" + "Option": "Float" } }, { "name": "text_value", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "blob_value", "ty": { - "Optional": "Bytes" + "Option": "Bytes" } } ] @@ -815,25 +815,25 @@ { "name": "int_value", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "float_value", "ty": { - "Optional": "Float" + "Option": "Float" } }, { "name": "text_value", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "blob_value", "ty": { - "Optional": "Bytes" + "Option": "Bytes" } } ] @@ -851,67 +851,67 @@ { "name": "max_connections", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_statements", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_rows", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_columns", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_result_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_statement_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_parameters", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_parameter_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_pending_operations", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_transaction_ms", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "busy_timeout_ms", "ty": { - "Optional": "Int" + "Option": "Int" } } ] @@ -951,25 +951,25 @@ { "name": "int_value", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "float_value", "ty": { - "Optional": "Float" + "Option": "Float" } }, { "name": "text_value", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "blob_value", "ty": { - "Optional": "Bytes" + "Option": "Bytes" } } ] @@ -989,7 +989,7 @@ { "name": "next_cursor", "ty": { - "Optional": "Int" + "Option": "Int" } } ] @@ -1021,7 +1021,7 @@ { "name": "params", "ty": { - "Optional": { + "Option": { "Array": { "Named": { "name": "SqliteValue", @@ -1033,25 +1033,25 @@ { "name": "int_value", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "float_value", "ty": { - "Optional": "Float" + "Option": "Float" } }, { "name": "text_value", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "blob_value", "ty": { - "Optional": "Bytes" + "Option": "Bytes" } } ] @@ -1063,80 +1063,80 @@ { "name": "query", "ty": { - "Optional": "Bool" + "Option": "Bool" } }, { "name": "limits", "ty": { - "Optional": { + "Option": { "Named": { "name": "SqliteLimits", "fields": [ { "name": "max_connections", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_statements", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_rows", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_columns", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_result_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_statement_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_parameters", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_parameter_bytes", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_pending_operations", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "max_transaction_ms", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "busy_timeout_ms", "ty": { - "Optional": "Int" + "Option": "Int" } } ] @@ -1163,7 +1163,7 @@ { "name": "execute", "ty": { - "Optional": { + "Option": { "Named": { "name": "SqliteExecuteResult", "fields": [ @@ -1183,7 +1183,7 @@ { "name": "query", "ty": { - "Optional": { + "Option": { "Named": { "name": "SqliteQueryResult", "fields": [ @@ -1214,25 +1214,25 @@ { "name": "int_value", "ty": { - "Optional": "Int" + "Option": "Int" } }, { "name": "float_value", "ty": { - "Optional": "Float" + "Option": "Float" } }, { "name": "text_value", "ty": { - "Optional": "String" + "Option": "String" } }, { "name": "blob_value", "ty": { - "Optional": "Bytes" + "Option": "Bytes" } } ] @@ -1252,7 +1252,7 @@ { "name": "next_cursor", "ty": { - "Optional": "Int" + "Option": "Int" } } ] @@ -1277,7 +1277,7 @@ "passing": "TakeOwned" } ], - "return_type": "Null", + "return_type": "Unit", "description": "" } ] diff --git a/docs/callable-runtime.md b/docs/callable-runtime.md index a8e08484..5ed74d10 100644 --- a/docs/callable-runtime.md +++ b/docs/callable-runtime.md @@ -1,6 +1,6 @@ # Script call frames and callable values -RustScript bytecode format version 13 (VMBC v13) carries runtime script call frames, first-class callable values, the static builtin ID catalog, the direct script-call opcode, full host schemas, callable metadata, and an explicit guest named-struct declaration section. Catalog identities use fingerprint format v3. +RustScript bytecode format version 14 (VMBC v14) carries runtime script call frames, first-class callable values, the static builtin ID catalog, the direct script-call opcode, full host schemas, callable metadata, and an explicit guest named-struct declaration section. Catalog identities use fingerprint format v3. ## Bytecode contract @@ -18,7 +18,7 @@ The three call opcodes differ in who owns the callee and what the frame must pro - `callvalue` — the callee is a `Value::Callable` owned by the caller operand stack at the call site, and remains the caller's responsibility after the call. This path carries environments, closures, and any callable whose identity or capture state is runtime-valued. - `callscript` — the callee is owned by program callable metadata (the prototype table). The frame contributes only `argc` arguments and no callable value, but unlike `call` the callee is a script function rather than a builtin, so the call enters a new script frame with its own local base. -VMBC v13 is the current format. Other versions and malformed resource schemas are rejected through the ordinary wire-format validation path. PDRC v6 recordings and AOT artifacts (format 8, ABI 8) embed VMBC; native cache identity includes bytecode ABI 13. +VMBC v14 is the current format. Other versions and malformed resource schemas are rejected through the ordinary wire-format validation path. PDRC v6 recordings and AOT artifacts (format 8, ABI 8) embed VMBC; native cache identity includes bytecode ABI 14. ## Static builtin IDs @@ -27,7 +27,7 @@ Every VM-visible builtin (ordinary, internal, and special-call) has one explicit - **Immutable explicit IDs.** IDs never change once assigned. Adding or reordering catalog entries never renumbers existing entries; new builtins take the next free ID in their documented block (extension `0x0000..=0xFF8F` for future builtins and host imports, special-call `0xFF90..=0xFFA1`, ordinary `0xFFA2..=0xFFFF`). The reserved sentinel gap `0xFF90..=0xFF92` stays unassigned. - **Build-time validation.** The build fails on duplicate IDs, duplicate source names, duplicate Rust variants, out-of-block IDs, class/gate inconsistencies, a discovered runtime callable without an explicit ID, or a catalog entry without a runtime callable. - **Shared std/no-std IDs.** `pd-vm-nostd` dispatches on the same static indices through the checked-in generated mirror `pd-vm-nostd/src/generated_builtin_ids.rs`; the workspace test `static_builtin_ids_are_frozen` fails when the mirror drifts from the catalog. -- **Current format identity.** VMBC and the internal bytecode ABI use version 13, with catalog fingerprint format v3. +- **Current format identity.** VMBC and the internal bytecode ABI use version 14, with catalog fingerprint format v3. ## Runtime model @@ -100,4 +100,4 @@ Whole-program AOT and Trace JIT use the same builtin call path (static catalog I ## Embedded runtime -`pd-vm-nostd` decodes the same VMBC v13 callable metadata and executes callable binding, `callvalue`, `callscript`, recursive frames, captures, and direct host targets using `core` plus `alloc`, dispatching on the identical static builtin IDs via its checked-in generated mirror. +`pd-vm-nostd` decodes the same VMBC v14 callable metadata and executes callable binding, `callvalue`, `callscript`, recursive frames, captures, and direct host targets using `core` plus `alloc`, dispatching on the identical static builtin IDs via its checked-in generated mirror. diff --git a/docs/http-client.md b/docs/http-client.md index fd445da3..650b3b4d 100644 --- a/docs/http-client.md +++ b/docs/http-client.md @@ -54,15 +54,20 @@ let values = http::response::header_values(&response, "set-cookie"); ``` A request builder accepts `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `HEAD`, or -`OPTIONS`. `set_body_bytes` takes bytes; `set_body_text` takes a string. Mutators -retain the builder's configured limits, and sending revalidates against the -current HTTP policy. Headers are ordered entries, preserving duplicates and -within-name value order. `Host`, `Content-Length`, `Transfer-Encoding`, and -`Connection` are client-managed request headers. Response header names are -normalized and canonically sorted. Each accessor string maps one raw octet to -one Unicode scalar U+0000..U+00FF; encoding those scalars back to octets recovers -the original bytes, including non-UTF-8 headers. The request and response bodies, -response head, redirects, concurrency, and elapsed phases are bounded. +`OPTIONS`. `set_body_bytes` takes bytes; `set_body_text` takes a string. The +three request mutators return the logical Unit `()`; no HTTP or SSE public +parameter, result, or callback member publishes a nullable schema. `set_header`, +`set_body_text`, and `set_body_bytes` retain the builder's configured limits, +and sending revalidates against the current HTTP policy. Headers are ordered +entries, preserving duplicates and within-name value order. `Host`, +`Content-Length`, `Transfer-Encoding`, and `Connection` are client-managed +request headers. Response header names are normalized and canonically sorted. +A missing response header lookup returns `[]`; a present header with an empty +value returns `[""]`, so absence and present-empty stay distinct without an +Option result. Each accessor string maps one raw octet to one Unicode scalar +U+0000..U+00FF; encoding those scalars back to octets recovers the original +bytes, including non-UTF-8 headers. The request and response bodies, response +head, redirects, concurrency, and elapsed phases are bounded. ## Server-sent events diff --git a/docs/option.md b/docs/option.md new file mode 100644 index 00000000..f247167d --- /dev/null +++ b/docs/option.md @@ -0,0 +1,211 @@ +# Option and Unit + +RustScript has a logical `Option` type and a logical Unit type `()`. They are +distinct from the VM's internal Null value and from the legacy nullable +`T | null` schema used by older Rust host `Option` parameters. + +## Option values + +```rust +let some: Option = Option::Some(0); +let none: Option = Option::None; + +let value = match some { + Option::Some(inner) => inner, + Option::None => -1, +}; +``` + +`Option::Some` may infer its payload type when the payload itself has a fully +known schema. `Option::None` has no payload from which to infer `T`, so it +requires an `Option` annotation or another contextual type. Missing type +arguments are rejected instead of being invented as `Unknown`. + +A match on a logical Option must provide `Option::Some` and `Option::None`; the +two arms are exhaustive. A wildcard arm may stand in for `Option::None` when the +Some arm is present. Duplicate arms, mixing Result construction arms into the +same match, and ambiguous wildcard combinations are rejected. + +`None`, `Some(None)`, `Some(Some(0))`, `Some(())`, `Some(false)`, `Some(0)` and +`Some("")` are different values. Payload truthiness, zero, the empty string and +the presence of a raw Null never choose the variant. + +## Option carriers + +The logical schema, not the physical shape, defines an Option. At the wire and +runtime boundary an Option is a map with a Boolean `tag`: + +- `tag: true` is `Some`; the active value is stored under `payload`. +- `tag: false` is `None`; a None carrier has no active `payload` entry. +- A non-Boolean or missing tag, extra fields, and a payload on None are + malformed and are rejected by validation. + +`Some(())` uses the internal Null value as its payload. A canonicalizing writer +may omit a Null-valued payload entry, so a tag-only `Some` may be restored only +when the declared payload schema is the logical Unit. A missing payload under +any other payload schema is malformed rather than an inner None. + +## Unit + +The Unit type and value are spelled `()`. An empty block, a function or closure +body with no tail value, and a typed host function with no declared return type +carry logical Unit facts. Unit's physical carrier is the internal Null value, so +`()` remains compatible with existing low-level code that represents a +missing value as Null, but an explicit source `null` is not accepted as Unit and +is not inferred into Unit or Option. + +```rust +fn nothing() { } + +let unit: () = (); +let value = nothing(); // logical Unit +``` + +## Host functions + +The typed host boundary uses `GuestOption` for a guest Option. It converts +bidirectionally with the standard Rust `Option` by variant: + +```rust +use vm::{GuestOption, VmResult}; + +#[pd_host_function(name = "example.lookup")] +fn lookup(key: i64) -> VmResult> { + Ok(if key == 0 { + GuestOption::Some(String::new()) + } else { + GuestOption::None + }) +} +``` + +`GuestOption` is the public guest wrapper; the low-level standard Rust +`Option` host codec keeps its existing single-mode nullable behavior +(`None` is Null, `Some(value)` is the bare value). `GuestOption` is not an +optional argument marker: a logical `None` is an explicit value and is not the +same as an omitted parameter. + +The wrapper recursively supports scalar, Named-struct, array, logical Unit, +`GuestResult`, nested `GuestOption`, typed callable payloads, and legal +owned-resource payloads. A callable payload keeps its full function signature +(parameter and result schemas) under `GuestOption`/`GuestResult`; the callable +value itself owns no resource. `fn()` and `fn() -> ()` are the same Rust +function type, so an omitted return publishes the logical Unit result rather +than Null. A resource array is not supported by the host handle ABI and is +rejected with a capability error; dynamic map/unknown payloads and transport +results such as `VmResult` inside the payload are also rejected. + +A host function that returns no value has the logical Unit return schema, while +the call still supplies an internal Null payload. `VmResult<()>` keeps fatal +transport errors in the outer result, as with every typed host function. + +## Resources + +An Option may own a resource inside `Some`. None has no owner. A selected Some +resource is published, transferred, taken or closed once through the existing +live-VM publication journal; unselected branches do not acquire ownership. +Missing payload, stale handles, wrong owner/kind and failed publication follow +the ordinary resource admission and rollback rules. + +## Wire format + +The current VMBC version is 14 and there are no legacy readers, adapters or +version-dependent writers. Host schemas encode logical Option with tag 15 and +Unit with tag 16; compiler schemas use tag 19 for Option and tag 20 for Unit. +Existing tag numbers and builtin IDs are unchanged. + +The no-std decoder understands the same current Option and Unit schemas for +scalar and nested programs. A cleanup plan that owns a resource still exceeds +the current no-std resource capability and is refused before execution. + +## Option methods + +`Option` provides `map`, `and_then`, `or_else`, `unwrap_or`, +`unwrap_or_else`, `ok_or` and `ok_or_else`. + +```rust +let some: Option = Option::Some(2); +let absent: Option = Option::None; + +let mapped = some.map(|value| value + 1); // Option, wraps U +let flat = some.and_then(|value| Option::Some(value + 1)); // flattens one layer +let kept = absent.or_else(|| Option::Some(3)); // zero-argument lazy fallback +let eager = some.unwrap_or(0); // eager fallback, returns int +let lazy = absent.unwrap_or_else(|| 4); // lazy body, returns int +let as_result = absent.ok_or("missing"); // Result +let lazy_error = absent.ok_or_else(|| "missing"); // lazy error callback +``` + +`map` wraps its callback result even when it is itself an Option or Result. +`and_then` flattens exactly the explicitly returned Option; a bare payload or a +Result return rejects. `or_else` takes a zero-argument callback and keeps the +receiver payload type; it does not copy the Result error-argument signature. +`unwrap_or` and `ok_or` evaluate their arguments eagerly; `unwrap_or_else` and +`ok_or_else` run their callback body lazily only on None. Receiver and argument +creation follow normal left-to-right order, each exactly once. + +`ok_or`/`ok_or_else` produce `Result` and do not implicitly convert +between Option and Result. An eager fallback or error value that may own a +resource is rejected when the unselected branch has no destination for it. A +resource-capturing temporary callback is rejected for the same reason. + +Result values expose explicit conversions: + +```rust +let value: Result = Result::Ok(21); +let ok_value = value.ok(); // Option +let err_value = value.err(); // Option +``` + +`ok`/`err` retain the selected payload as `Some`; the discarded branch must be +resource-free or the conversion is rejected statically. + +## Lexical Option propagation + +`?` on a logical Option passes the Some payload and exits only the current +Option-returning function or closure on None. Later arguments, statements and +side effects do not run after a None exit; already-created owners follow their +declared cleanup. A None inside a closure does not return from its caller. + +```rust +fn relay(flag: bool) -> Option { + let value: Option = if flag => { Option::Some(1) } else => { Option::None }; + let item = value?; + Option::Some(item + 1) +} +``` + +Result and Option propagation do not implicitly mix. Use `.ok_or(...)?` or +`.ok()?` at the boundary; Result `?` keeps its exact error type. Option `?` +accepts a wider enclosing Option payload because None carries no value, while +Some continues with its exact payload type. + +## Fallible projection (`?.`) + +RustScript `?.` produces a logical `Option` of the projected value. A missing +map key, a missing array index, an out-of-range string index or a None receiver +becomes None. A present value is Some, including `Some(false)`, `Some(0)`, +`Some("")`, `Some(())` and a nested `Some(None)`. + +A non-optional receiver performs one fallible projection. An Option receiver +consumes exactly one and_then-equivalent receiver layer; the projected payload +keeps its own schema, so a declared `Option` leaf produces +`Option>` rather than an arbitrary flattening. An absent dynamic key +and a present key holding explicit `Option::None` remain distinct. + +Permitted omitted logical Option fields normalize to None at the RustScript +boundary, while required fields are still checked. An explicit Option::None +field is a tagged value, not a map deletion. The internal VM keeps its raw Null +value and other frontends keep their legacy nullable contracts; RustScript +itself rejects explicit null literals and legacy `T?` annotations. + +## Deliberate limits + +This scope does not add `is_some`, `inspect`, `map_or` or other unrequested +catalogue operations. RustScript source JSON calls use the typed boundary: a +decode needs an explicit concrete type argument and an encode is lowered from +its checked argument schema, or the call fails at compile time. The untyped +public `json::decode`/`json::encode` API stays available only to the internal +VM and other frontends. Standard typed hosts publish logical `GuestOption` and +Unit under their declared schemas, and value-position JSON that cannot +distinguish Unit from None is still rejected. diff --git a/docs/result.md b/docs/result.md index 8782ab92..f1de8487 100644 --- a/docs/result.md +++ b/docs/result.md @@ -64,8 +64,8 @@ use the same lexical boundary. The enclosing Ok type may differ from the operand Ok type; the error type must match without implicit conversion. Root scripts, non-Result-returning functions, non-Result operands, optional Result -operands, and incompatible error types reject `?`. `T?` remains an optional type, -and `object?.field` remains optional access. Other parser dialects do not gain +operands, and incompatible error types reject `?`. RSS rejects type-position `T?`; +rewrite it as `Option`. `object?.field` remains optional access. Other parser dialects do not gain this syntax unless they enable the Result dialect capability. There is no implicit error conversion, generic Try trait, or guest throw/catch/finally. @@ -158,6 +158,24 @@ The `?` operand has error type `int` after `map_err`; `relay` also returns `Result`. The original string error becomes `7` before propagation, and the final match observes that propagated error. +## Convert Result to Option + +`Result.ok()` returns `Option` and `Result.err()` returns +`Option`. The selected branch payload is retained as `Some`; the other branch +becomes `None`. + +```rust +let value: Result = Result::Ok(21); +let ok_value = value.ok(); +let err_value = value.err(); +let selected = match ok_value { Option::Some(inner) => inner, Option::None => -1 }; +selected; +``` + +The discarded branch has no destination for an owned payload, so a +resource-typed or unresolved-generic discarded branch is rejected statically. +The retained payload keeps its exact schema and ownership state. + ## Ownership and cleanup: explicit normal close, automatic Err cleanup A Result containing a resource in either parameter is move-only. Construction @@ -234,8 +252,9 @@ invalid representation, and outer `VmError` never become catchable guest Errs. Host descriptors and build-script schema generation preserve both parameters and resource effects. Borrowed-resource returns remain rejected, including nested GuestResult combinations. Existing HTTP/database business return types are not -implicitly migrated. `Result<(), E>` continues to use the existing Null-backed -unit payload; a distinct guest `()` schema is outside this contract. +implicitly migrated. `Result<(), E>` continues to use the Null-backed unit +payload; guest `()` is a distinct logical Unit schema whose physical carrier is +also the internal Null value (see [Option and Unit](option.md)). ### Calling an RSS Result from Rust @@ -317,8 +336,8 @@ Plugins can construct `TypeSchema::Result { ok, err }`, `ResultMatch`, and `ResultTry` IR. Preserve the full schemas through typing and ownership passes; do not substitute Unknown or invent a named-map identity. -The current VMBC contract is **v13 only**. Std writer/reader and no-std reader use -one current schema layout. Non-v13, unknown schema tags, malformed carriers, and +The current VMBC contract is **v14 only**. Std writer/reader and no-std reader use +one current schema layout. Non-v14, unknown schema tags, malformed carriers, and invalid schema budgets are rejected; there is no legacy reader, adapter, or version-dependent writer. The physical carrier is the existing Map with a Boolean `tag` (`true` = Ok) and the active `payload`. Missing payload means Null only if diff --git a/docs/sqlite.md b/docs/sqlite.md index 00c7729c..1b8b7c21 100644 --- a/docs/sqlite.md +++ b/docs/sqlite.md @@ -29,29 +29,33 @@ Execution also requires an async host bridge and the SQLite module registered. ```rust use sqlite; -let db = sqlite::open({ - path: ":memory:", - mode: "memory", - limits: { max_rows: 100 }, -}); +let limits: SqliteLimits = { max_rows: Option::Some(100) }; +let options: SqliteOpenOptions = { + path: Option::Some(":memory:"), + mode: Option::Some("memory"), + limits: Option::Some(limits), +}; +let db = sqlite::open(options); ``` -All fields are optional in object literals. A missing field or `null` keeps the host default, -except that `path` remains required at runtime for a usable connection. +Every field is a logical `Option`. `Option::None` keeps the host default; `Option::Some(value)` +explicitly supplies it, including `Some(false)`, `Some(0)`, and `Some("")`. A `path` of +`Option::None` is still rejected at runtime for a usable connection. | Field | Type | Runtime behavior | | --- | --- | --- | -| `path` | optional string | Required and non-empty; resolved under the embedding policy root | -| `mode` | optional string | `memory`, `read_only`, `read_write`, or `read_write_create`; the last is the default | -| `root` | optional string | Must match the embedding policy root when supplied | -| `limits` | optional `SqliteLimits` | Per-connection ceilings; omitted fields keep the host ceiling | +| `path` | `Option` | Required and non-empty when `Some`; resolved under the embedding policy root | +| `mode` | `Option` | `memory`, `read_only`, `read_write`, or `read_write_create`; `None` defaults to the last | +| `root` | `Option` | Must match the embedding policy root when `Some` | +| `limits` | `Option` | Per-connection ceilings; `None` keeps the host ceiling | -Unknown fields are rejected by the named-struct compiler contract. +Unknown fields are outside the named-struct contract; do not rely on them. ## Limits (`SqliteLimits`) -Every limit is an optional int. Omitted or `null` fields keep the embedding ceiling. Supplied -limits must be positive and cannot exceed that ceiling. +Every limit is `Option`. `Option::None` keeps the embedding ceiling; `Option::Some(0)` is a +provided invalid value rather than a default. Supplied limits must be positive and cannot exceed +that ceiling. - `max_connections` - `max_statements` @@ -65,7 +69,7 @@ limits must be positive and cannot exceed that ceiling. - `max_transaction_ms` - `busy_timeout_ms` -An empty `{}` is valid. Extra fields are rejected. +An empty `{}` is valid for a typed `SqliteLimits` local. Fields outside the named-struct contract are not part of the public schema. ## Values (`SqliteValue`) @@ -74,10 +78,10 @@ Parameters and result cells use the same tagged named struct: ```text SqliteValue { kind: string, - int_value: optional, - float_value: optional, - text_value: optional, - blob_value: optional, + int_value: Option, + float_value: Option, + text_value: Option, + blob_value: Option, } ``` @@ -85,21 +89,21 @@ The five `kind` values and their selected payloads are: | `kind` | Required payload | Other payloads | | --- | --- | --- | -| `"null"` | none | must be `null` or omitted | -| `"int"` | `int_value` | must be `null` or omitted | -| `"float"` | `float_value` | must be `null` or omitted | -| `"text"` | `text_value` | must be `null` or omitted | -| `"blob"` | `blob_value` | must be `null` or omitted | +| `"null"` | none | all payloads `Option::None` | +| `"int"` | `int_value` | other payloads `Option::None` | +| `"float"` | `float_value` | other payloads `Option::None` | +| `"text"` | `text_value` | other payloads `Option::None` | +| `"blob"` | `blob_value` | other payloads `Option::None` | For example: ```rust let values = [ { kind: "null" }, - { kind: "int", int_value: 7 }, - { kind: "float", float_value: 1.5 }, - { kind: "text", text_value: "hello" }, - { kind: "blob", blob_value: bytes::from_hex("000102") }, + { kind: "int", int_value: Option::Some(7) }, + { kind: "float", float_value: Option::Some(1.5) }, + { kind: "text", text_value: Option::Some("hello") }, + { kind: "blob", blob_value: Option::Some(bytes::from_hex("000102")) }, ]; ``` @@ -108,6 +112,20 @@ selected payload, multiple non-null payloads, and a selected payload with the wr `"null"` carries no payload. SQLite TEXT that is not valid UTF-8 is returned as `kind: "blob"` with its original bytes. +## Absence boundary + +SQLite exposes three distinct absence forms and they are not interchangeable: + +- Configuration and statement entries use logical `Option`: `Option::None` keeps the + documented default and `Option::Some(value)` supplies it. A physical `null` scalar is + not a second reading of a present public field. +- A SQL `NULL` result cell remains the tagged `SqliteValue { kind: "null", ... }` + business value; its four payload slots are logical `Option::None`. It is not an + absent row and it is not rewritten into an option-of-cell. +- Result-value absence uses logical `Option`: `SqliteQueryResult.next_cursor` and + the unselected `SqliteTransactionResult` branch are `Option::Some`/`Option::None` + values, matched with `match` rather than a sentinel. + ## Execute (`SqliteExecuteResult`) `sqlite::execute` takes an `array` and returns a `SqliteExecuteResult`: @@ -116,7 +134,7 @@ with its original bytes. let inserted = sqlite::execute( &db, "INSERT INTO t (value) VALUES (?1)", - [{ kind: "int", int_value: 7 }], + [{ kind: "int", int_value: Option::Some(7) }], ); let affected = inserted.rows_affected; let rowid = inserted.last_insert_rowid; @@ -135,17 +153,13 @@ limits before the adapter call is awaited. `sqlite::query` takes an `array` and returns a `SqliteQueryResult`: ```rust -let queried = sqlite::query( - &db, - "SELECT value FROM t ORDER BY rowid", - [], - { max_rows: 32 }, -); +let limits: SqliteLimits = { max_rows: Option::Some(32) }; +let queried = sqlite::query(&db, "SELECT value FROM t ORDER BY rowid", [], limits); let first_row = queried.rows[0]; let first_cell = first_row.cells[0]; if first_cell.kind == "int" { - let value = first_cell.int_value; + let value = first_cell.int_value.unwrap_or(-1); } ``` @@ -154,29 +168,37 @@ if first_cell.kind == "int" { | `columns` | array of string | | `rows` | array of `SqliteRow` | | `truncated` | bool | -| `next_cursor` | optional int | +| `next_cursor` | `Option` | Each `SqliteRow` has one field, `cells`, an array of `SqliteValue` in column order. Result limits are charged from the underlying column names and cell payloads, without counting the named-struct wrapper fields. `max_rows`, `max_columns`, and `max_result_bytes` can truncate a -query; already accepted rows remain in the result. `next_cursor` is the first-column integer -from the last accepted row, or `null` when no such value was accepted. +query; already accepted rows remain in the result. `next_cursor` is +`Option::Some(first-column integer)` from the last accepted row, or `Option::None` +when no such value was accepted: + +```rust +let next = match queried.next_cursor { + Option::Some(cursor) => cursor, + Option::None => -1, +}; +``` ## Transactions (`SqliteStatement` and `SqliteTransactionResult`) A transaction receives an array of named `SqliteStatement` values. Each statement has a required -`sql` string and optional typed `params`, `query`, and `limits` fields: +`sql` string and logical option `params`, `query`, and `limits` fields: ```rust let results = sqlite::transaction(&db, [ { sql: "INSERT INTO t (value) VALUES (?1)", - params: [{ kind: "int", int_value: 8 }], + params: Option::Some([{ kind: "int", int_value: Option::Some(8) }]), }, { sql: "SELECT value FROM t ORDER BY rowid", - query: true, - limits: { max_rows: 8 }, + query: Option::Some(true), + limits: Option::Some(limits), }, ]); ``` @@ -184,30 +206,38 @@ let results = sqlite::transaction(&db, [ | Field | Type | Runtime behavior | | --- | --- | --- | | `sql` | string | Required non-empty SQL | -| `params` | optional `array` | Omitted or `null` means no parameters | -| `query` | optional bool | Omitted or `null` means execute; `true` returns a query result | -| `limits` | optional `SqliteLimits` | Omitted or `null` keeps the connection ceiling | +| `params` | `Option>` | `None` means no parameters | +| `query` | `Option` | `None` means execute; `Some(true)` returns a query result | +| `limits` | `Option` | `None` keeps the connection ceiling | The return value is an `array` in statement order: ```text SqliteTransactionResult { kind: string, - execute: optional, - query: optional, + execute: Option, + query: Option, } ``` -An execute statement produces `{ kind: "execute", execute: ... }`; a query statement produces -`{ kind: "query", query: ... }`. The unselected envelope field is `null`. Discriminate with -`kind` before using `execute` or `query`. The transaction remains atomic: statement order, +An execute statement produces `{ kind: "execute", execute: Option::Some(...) }`; a query statement +produces `{ kind: "query", query: Option::Some(...) }`. The unselected envelope field is +`Option::None`. Discriminate with `kind` before matching `execute` or `query`: + +```rust +let result = results[0]; +let executed = match result.execute { + Option::Some(value) => value.rows_affected, + Option::None => -1, +}; +``` The transaction remains atomic: statement order, rollback on failure, transaction deadlines, and result limits are preserved. A SQLite progress handler interrupts a transaction after its configured deadline so the transaction rolls back. ## Resource lifecycle -`sqlite::close(db)` consumes the connection, begins adapter close before suspension, and removes -the VM resource after close completes. Cancelling a blocked close leaves its resource-owned close +`sqlite::close(db)` consumes the connection, begins adapter close before suspension, +returns the guest `()` value, and removes the VM resource after close completes. Cancelling a blocked close leaves its resource-owned close lifecycle available for a later close call to poll to completion. VM reset interrupts active SQLite work and remains pending while the resource polls `tokio-rusqlite`'s `Connection::close`; the connection permit is released only after the adapter diff --git a/docs/standard-timer-host.md b/docs/standard-timer-host.md index 94961acf..0acd3089 100644 --- a/docs/standard-timer-host.md +++ b/docs/standard-timer-host.md @@ -17,14 +17,15 @@ re-exported at the crate root, and an embedding can install different The callback parameter is typed `fn(bool) -> unknown`: the callback observes the `premature` flag and its **return value is discarded**, so the declared result -is `unknown` — any `int`/`string`/`map`/`bool`/`null` body compiles and runs, +is `unknown` — an `int`/`string`/`map`/`bool`/`()` body compiles and runs, and the value is dropped instead of accumulating. This is the one deliberate dynamic occurrence in the public timer surface, and the typed-catalog guard (`tests/typed_host_no_dynamic_contract_tests.rs`) records it as its narrow -discarded-callable-result policy exception. A callback body may therefore end -with any expression — for example `timer::at(25, |premature| null)`, -`timer::at(25, |premature| 1)`, or -`timer::at(25, |premature| if true => { work(); null } else => { null })`. +discarded-callable-result policy exception. RSS rejects a `null` literal in a +callback body; use `()` when the callback has nothing to return. A callback body +may therefore end with any accepted expression — for example +`timer::at(25, |premature| ())`, `timer::at(25, |premature| 1)`, or +`timer::at(25, |premature| if true => { work(); () } else => { () })`. ## Installation diff --git a/examples/example_complex.rss b/examples/example_complex.rss index 7c134f34..ef609358 100644 --- a/examples/example_complex.rss +++ b/examples/example_complex.rss @@ -35,13 +35,12 @@ let closure_value = add(5); let profile: Profile = {stats: {score: closure_value}}; let matched = match profile?.stats?.score { - None => 0, - Some(score) => if score == 12 => { + Option::None => 0, + Option::Some(score) => if score == 12 => { closure_value } else => { 0 }, - _ => 0, }; let a = [1, "a"]; diff --git a/examples/sqlite_named_structs.rss b/examples/sqlite_named_structs.rss index 541425fc..1c490c42 100644 --- a/examples/sqlite_named_structs.rss +++ b/examples/sqlite_named_structs.rss @@ -1,42 +1,50 @@ use sqlite; use bytes; -fn main() { - let db = sqlite::open({ - path: ":memory:", - mode: "memory", - limits: { max_rows: 32 }, - }); +let limits: SqliteLimits = { max_rows: Option::Some(32) }; +let options: SqliteOpenOptions = { + path: Option::Some(":memory:"), + mode: Option::Some("memory"), + limits: Option::Some(limits) +}; +let db = sqlite::open(options); - sqlite::execute(&db, "CREATE TABLE t (value, payload)", []); - let blob_payload = bytes::from_hex("000102"); - sqlite::execute(&db, "INSERT INTO t VALUES (?1, ?2)", [ - { kind: "int", int_value: 7 }, - { kind: "blob", blob_value: blob_payload }, - ]); +let empty: [SqliteValue] = []; +sqlite::execute(&db, "CREATE TABLE t (value, payload)", empty); +let blob_payload = bytes::from_hex("000102"); +let inserted = sqlite::execute(&db, "INSERT INTO t VALUES (?1, ?2)", [ + { kind: "int", int_value: Option::Some(7) }, + { kind: "blob", blob_value: Option::Some(blob_payload) } +]); +let affected = inserted.rows_affected; +assert(affected == 1); - let queried = sqlite::query(&db, "SELECT value, payload FROM t", [], {}); - let first = queried.rows[0].cells[0]; - assert(first.kind == "int"); - assert(first.int_value == 7); +let queried = sqlite::query(&db, "SELECT value, payload FROM t", empty, limits); +let first_row = queried.rows[0]; +let first = first_row.cells[0]; +assert(first.kind == "int"); +let first_value = first.int_value.unwrap_or(-1); +assert(first_value == 7); - let results = sqlite::transaction(&db, [ - { - sql: "INSERT INTO t VALUES (?1, ?2)", - params: [ - { kind: "text", text_value: "hello" }, - { kind: "null" }, - ], - }, - { - sql: "SELECT value, payload FROM t ORDER BY rowid", - query: true, - limits: { max_rows: 32 }, - }, - ]); - assert(results[0].kind == "execute"); - assert(results[1].kind == "query"); - assert(results[1].query.rows[0].cells[0].int_value == 7); +let insert_params: [SqliteValue] = [ + { kind: "text", text_value: Option::Some("hello") }, + { kind: "null" } +]; +let statements: [SqliteStatement] = [ + { + sql: "INSERT INTO t VALUES (?1, ?2)", + params: Option::Some(insert_params) + }, + { + sql: "SELECT value, payload FROM t ORDER BY rowid", + query: Option::Some(true), + limits: Option::Some(limits) + } +]; +let results = sqlite::transaction(&db, statements); +let execute_result = results[0]; +assert(execute_result.kind == "execute"); +let query_result = results[1]; +assert(query_result.kind == "query"); - sqlite::close(db); -} +sqlite::close(db); diff --git a/pd-host-function/src/guest_result.rs b/pd-host-function/src/guest_result.rs index d73f257a..e32f7ed6 100644 --- a/pd-host-function/src/guest_result.rs +++ b/pd-host-function/src/guest_result.rs @@ -32,6 +32,11 @@ pub(super) fn schema(shape: &GuestTypeShape<'_>) -> Result { let err = schema(err)?; quote!(#sdk::HostTypeSchema::Result { ok: Box::new(#ok), err: Box::new(#err) }) } + GuestTypeShape::Option(inner) => { + let inner = schema(inner)?; + quote!(#sdk::HostTypeSchema::Option(Box::new(#inner))) + } + GuestTypeShape::Unit => quote!(#sdk::HostTypeSchema::Unit), GuestTypeShape::Optional(inner) => { let inner = schema(inner)?; quote!(#sdk::HostTypeSchema::Optional(Box::new(#inner))) @@ -44,6 +49,17 @@ pub(super) fn schema(shape: &GuestTypeShape<'_>) -> Result { let key = super::resource_key_tokens(spec); quote!(#sdk::HostTypeSchema::Resource(#key)) } + GuestTypeShape::Callable { params, result } => { + let params = params + .iter() + .map(schema) + .collect::, Error>>()?; + let result = schema(result)?; + quote!(#sdk::HostTypeSchema::Callable { + params: vec![#(#params),*], + result: Box::new(#result), + }) + } GuestTypeShape::Scalar(label) => { super::host_type_schema_from_label(label, &syn::parse_quote!(()))? } @@ -60,6 +76,17 @@ pub(super) fn decode(shape: &GuestTypeShape<'_>, value: TokenStream) -> TokenStr quote!({ let (__pd_tag, __pd_payload) = #sdk::result::payload(#value)?; if __pd_tag { #sdk::GuestResult::Ok(#ok) } else { #sdk::GuestResult::Err(#err) } }) } + GuestTypeShape::Option(inner) => { + let inner = decode(inner, quote!(&__pd_option_value)); + quote!({ let (__pd_some, __pd_payload) = #sdk::option::payload(#value)?; + if __pd_some { + let __pd_option_value = __pd_payload.cloned().unwrap_or(#sdk::Value::Null); + #sdk::GuestOption::Some(#inner) + } else { #sdk::GuestOption::None } }) + } + GuestTypeShape::Unit => quote!({ + #sdk::FromVmValue::from_vm_value(#value, "Unit")? + }), GuestTypeShape::Optional(inner) => { let inner = decode(inner, quote!(__pd_optional)); quote!({ let __pd_optional = #value; if matches!(__pd_optional, #sdk::Value::Null) { None } else { Some(#inner) } }) @@ -85,6 +112,9 @@ pub(super) fn decode(shape: &GuestTypeShape<'_>, value: TokenStream) -> TokenStr extracted } } + GuestTypeShape::Callable { .. } => { + quote!(#sdk::FromVmValue::from_vm_value(#value, "callable")?) + } _ => quote!(#sdk::FromVmValue::from_vm_value(#value, "Result payload")?), } } @@ -111,6 +141,14 @@ pub(super) fn encode( }); quote!(match #value { #sdk::GuestResult::Ok(__pd_selected) => #ok, #sdk::GuestResult::Err(__pd_selected) => #err }) } + GuestTypeShape::Option(inner) => { + let some = walk(inner, quote!(__pd_selected), root, &|v| { + wrap(quote!(#sdk::option::some_carrier(#v))) + }); + let none = wrap(quote!(#sdk::option::none_carrier())); + quote!(match #value { #sdk::GuestOption::Some(__pd_selected) => #some, #sdk::GuestOption::None => #none }) + } + GuestTypeShape::Unit => wrap(quote!(#sdk::Value::Null)), GuestTypeShape::Optional(inner) => { let some = walk(inner, quote!(__pd_selected), root, wrap); let none = wrap(quote!(#sdk::Value::Null)); @@ -127,6 +165,9 @@ pub(super) fn encode( let wrapped = wrap(quote!(__pd_token)); quote!(#sdk::result::publish_owned::<#inner, _>(vm, #value, &#root, |__pd_token| Ok(#wrapped))?) } + GuestTypeShape::Callable { .. } => { + wrap(quote!(#sdk::IntoVmValue::into_vm_value(#value))) + } _ => wrap(quote!(#sdk::IntoVmValue::into_vm_value(#value))), } } diff --git a/pd-host-function/src/lib.rs b/pd-host-function/src/lib.rs index e6ec7b71..a3b94b6d 100644 --- a/pd-host-function/src/lib.rs +++ b/pd-host-function/src/lib.rs @@ -1515,7 +1515,7 @@ fn return_schema_tokens( let sdk = sdk_path(); let ReturnType::Type(_, ty) = output else { return Ok(( - quote!(#sdk::host_extension::HostTypeSchema::Null), + quote!(#sdk::host_extension::HostTypeSchema::Unit), None, None, )); @@ -1634,6 +1634,7 @@ fn host_type_schema_from_label(label: &str, _ty: &Type) -> Result quote!(#sdk::host_extension::HostTypeSchema::Bytes), "number" => quote!(#sdk::host_extension::HostTypeSchema::Number), "null" => quote!(#sdk::host_extension::HostTypeSchema::Null), + "()" => quote!(#sdk::host_extension::HostTypeSchema::Unit), "any" | "unknown" => quote!(#sdk::host_extension::HostTypeSchema::Unknown), "array" => quote!(#sdk::host_extension::HostTypeSchema::Array(Box::new( #sdk::host_extension::HostTypeSchema::Unknown diff --git a/pd-host-function/tests/ui/fail/guest_option_arity.rs b/pd-host-function/tests/ui/fail/guest_option_arity.rs new file mode 100644 index 00000000..4441bdc8 --- /dev/null +++ b/pd-host-function/tests/ui/fail/guest_option_arity.rs @@ -0,0 +1,9 @@ +#![allow(dead_code, unused_imports)] +use pd_host_function::pd_host_function; +use vm::{GuestOption, VmResult}; +/// Invalid typed guest return. +#[pd_host_function(name = "test::invalid")] +fn invalid() -> VmResult> { + todo!() +} +fn main() {} diff --git a/pd-host-function/tests/ui/fail/guest_option_arity.stderr b/pd-host-function/tests/ui/fail/guest_option_arity.stderr new file mode 100644 index 00000000..602f3f60 --- /dev/null +++ b/pd-host-function/tests/ui/fail/guest_option_arity.stderr @@ -0,0 +1,5 @@ +error: GuestOption requires one generic argument + --> tests/ui/fail/guest_option_arity.rs:6:17 + | +6 | fn invalid() -> VmResult> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pd-host-function/tests/ui/fail/guest_option_dynamic.rs b/pd-host-function/tests/ui/fail/guest_option_dynamic.rs new file mode 100644 index 00000000..0f970578 --- /dev/null +++ b/pd-host-function/tests/ui/fail/guest_option_dynamic.rs @@ -0,0 +1,9 @@ +#![allow(dead_code, unused_imports)] +use pd_host_function::pd_host_function; +use vm::{GuestOption, Value, VmResult}; +/// Invalid typed guest return. +#[pd_host_function(name = "test::invalid")] +fn invalid() -> VmResult> { + todo!() +} +fn main() {} diff --git a/pd-host-function/tests/ui/fail/guest_option_dynamic.stderr b/pd-host-function/tests/ui/fail/guest_option_dynamic.stderr new file mode 100644 index 00000000..1cc47078 --- /dev/null +++ b/pd-host-function/tests/ui/fail/guest_option_dynamic.stderr @@ -0,0 +1,5 @@ +error: GuestResult payload must have a fully typed public schema + --> tests/ui/fail/guest_option_dynamic.rs:6:17 + | +6 | fn invalid() -> VmResult> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pd-host-function/tests/ui/fail/guest_option_resource_array.rs b/pd-host-function/tests/ui/fail/guest_option_resource_array.rs new file mode 100644 index 00000000..c6a60bd9 --- /dev/null +++ b/pd-host-function/tests/ui/fail/guest_option_resource_array.rs @@ -0,0 +1,10 @@ +#![allow(dead_code, unused_imports)] +use pd_host_function::pd_host_function; +use vm::{GuestOption, ResourceOwned, VmResult}; +struct Counter; +/// Invalid nested resource array. +#[pd_host_function(name = "test::invalid")] +fn invalid() -> VmResult>>> { + todo!() +} +fn main() {} diff --git a/pd-host-function/tests/ui/fail/guest_option_resource_array.stderr b/pd-host-function/tests/ui/fail/guest_option_resource_array.stderr new file mode 100644 index 00000000..6c13c5e2 --- /dev/null +++ b/pd-host-function/tests/ui/fail/guest_option_resource_array.stderr @@ -0,0 +1,5 @@ +error: resource arrays are not supported by the host handle ABI + --> tests/ui/fail/guest_option_resource_array.rs:7:17 + | +7 | fn invalid() -> VmResult>>> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pd-host-function/tests/ui/pass/valid_guest_option.rs b/pd-host-function/tests/ui/pass/valid_guest_option.rs new file mode 100644 index 00000000..80223d21 --- /dev/null +++ b/pd-host-function/tests/ui/pass/valid_guest_option.rs @@ -0,0 +1,34 @@ +#![allow(dead_code, unused_imports)] +use pd_host_function::pd_host_function; +use vm::{GuestOption, Value, VmResult}; +mod generated_parent { + pub mod functions { + use super::super::*; + /// Echoes a typed guest Option with a scalar payload. + #[pd_host_function(name = "test::option_scalar")] + fn scalar(value: GuestOption) -> VmResult> { + Ok(value) + } + /// Returns every nested Option state. + #[pd_host_function(name = "test::option_nested")] + fn nested( + value: GuestOption>>, + ) -> VmResult>>> { + Ok(value) + } + /// A host function with no declared return type exposes logical Unit. + #[pd_host_function(name = "test::option_unit")] + fn unit() -> VmResult<()> { + Ok(()) + } + } +} +fn main() { + let descriptor = generated_parent::functions::scalar_descriptor(); + assert!(matches!( + descriptor.schema.return_type, + vm::HostTypeSchema::Option(_) + )); + let unit = generated_parent::functions::unit_descriptor(); + assert!(matches!(unit.schema.return_type, vm::HostTypeSchema::Unit)); +} diff --git a/pd-host-function/tests/ui/pass/valid_guest_option_callable.rs b/pd-host-function/tests/ui/pass/valid_guest_option_callable.rs new file mode 100644 index 00000000..c94a59b9 --- /dev/null +++ b/pd-host-function/tests/ui/pass/valid_guest_option_callable.rs @@ -0,0 +1,41 @@ +#![allow(dead_code, unused_imports)] +use pd_host_function::pd_host_function; +use vm::{GuestOption, Value, VmCallable, VmResult}; +mod generated_parent { + pub mod functions { + use super::super::*; + /// Echoes a callable payload nested in a logical Option. + #[pd_host_function(name = "test::option_callable")] + fn callable( + value: GuestOption i64>>, + ) -> VmResult i64>>> { + Ok(value) + } + /// Echoes a Unit-returning callable under a logical Option. + #[pd_host_function(name = "test::option_callable_unit")] + fn callable_unit( + value: GuestOption ()>>, + ) -> VmResult ()>>> { + Ok(value) + } + } +} +fn main() { + let descriptor = generated_parent::functions::callable_descriptor(); + let vm::HostTypeSchema::Option(inner) = &descriptor.schema.params[0].ty else { + panic!("callable Option parameter schema"); + }; + assert!(matches!( + inner.as_ref(), + vm::HostTypeSchema::Callable { .. } + )); + let unit = generated_parent::functions::callable_unit_descriptor(); + let vm::HostTypeSchema::Option(inner) = &unit.schema.return_type else { + panic!("Unit callable Option return schema"); + }; + let vm::HostTypeSchema::Callable { params, result } = inner.as_ref() else { + panic!("Unit callable payload must carry the Callable schema"); + }; + assert!(params.is_empty()); + assert!(matches!(result.as_ref(), vm::HostTypeSchema::Unit)); +} diff --git a/pd-vm-nostd/README.md b/pd-vm-nostd/README.md index 9647fcbe..a9590611 100644 --- a/pd-vm-nostd/README.md +++ b/pd-vm-nostd/README.md @@ -6,7 +6,7 @@ compiler, parser, CLI, debugger, JIT/AOT backends, filesystem support, and opera ## Runtime surface -- VMBC v13 decoding with environment-free `CallScript` direct script calls alongside dynamic callable calls and an explicit guest named-struct declaration section +- VMBC v14 decoding with environment-free `CallScript` direct script calls alongside dynamic callable calls and an explicit guest named-struct declaration section - stack, local, and recursive script-frame execution for direct bytecode opcodes - instruction fuel with pause/resume support - synchronous named host bindings and dynamic host dispatch diff --git a/pd-vm-nostd/src/generated_builtin_ids.rs b/pd-vm-nostd/src/generated_builtin_ids.rs index d779e5de..ef66d0b4 100644 --- a/pd-vm-nostd/src/generated_builtin_ids.rs +++ b/pd-vm-nostd/src/generated_builtin_ids.rs @@ -12,6 +12,10 @@ #![allow(dead_code)] pub const DETACH_LOCAL_CALL_INDEX: u16 = 0xFF93; +pub const JSON_DECODE_TYPED_CALL_INDEX: u16 = 0xFF8C; +pub const JSON_ENCODE_TYPED_CALL_INDEX: u16 = 0xFF8D; +pub const LOGICAL_RENDER_CALL_INDEX: u16 = 0xFF8E; +pub const MAP_EDIT_CALL_INDEX: u16 = 0xFF8F; pub const BIND_CALLABLE_CALL_INDEX: u16 = 0xFF94; pub const MAP_ITER_CLOSE_CALL_INDEX: u16 = 0xFF95; pub const MAP_ITER_TAKE_VALUE_CALL_INDEX: u16 = 0xFF96; @@ -123,6 +127,10 @@ pub const COUNT_CALL_INDEX: u16 = 0xFFFB; /// Every static builtin call index, ascending. pub const ALL_CALL_INDICES: &[u16] = &[ + JSON_DECODE_TYPED_CALL_INDEX, + JSON_ENCODE_TYPED_CALL_INDEX, + LOGICAL_RENDER_CALL_INDEX, + MAP_EDIT_CALL_INDEX, DETACH_LOCAL_CALL_INDEX, BIND_CALLABLE_CALL_INDEX, MAP_ITER_CLOSE_CALL_INDEX, diff --git a/pd-vm-nostd/src/vm.rs b/pd-vm-nostd/src/vm.rs index 05d9b302..6e68e9f4 100644 --- a/pd-vm-nostd/src/vm.rs +++ b/pd-vm-nostd/src/vm.rs @@ -812,7 +812,8 @@ impl Vm { use super::generated_builtin_ids::{ ARRAY_NEW_CALL_INDEX, ARRAY_PUSH_CALL_INDEX, BIND_CALLABLE_CALL_INDEX, CONCAT_CALL_INDEX, DETACH_LOCAL_CALL_INDEX, GET_CALL_INDEX, HAS_CALL_INDEX, - MAP_NEW_CALL_INDEX, SET_CALL_INDEX, + JSON_DECODE_TYPED_CALL_INDEX, JSON_ENCODE_TYPED_CALL_INDEX, LOGICAL_RENDER_CALL_INDEX, + MAP_EDIT_CALL_INDEX, MAP_NEW_CALL_INDEX, SET_CALL_INDEX, }; Some(match index { @@ -946,6 +947,42 @@ impl Vm { self.stack.push(Value::Map(entries)); Ok(()) } + MAP_EDIT_CALL_INDEX => { + if let Err(error) = self.require_builtin_arity("__map_edit", arity, 4) { + return Some(Err(error)); + } + let start = match self.stack.len().checked_sub(4) { + Some(start) => start, + None => return Some(Err(VmError::StackUnderflow)), + }; + let key = self.stack[start + 1].clone(); + let value = self.stack[start + 2].clone(); + let remove = match &self.stack[start + 3] { + Value::Bool(flag) => *flag, + _ => return Some(Err(VmError::TypeMismatch("bool"))), + }; + if matches!(key, Value::Callable(_)) { + return Some(Err(VmError::HostError( + "callable values are not supported as map keys", + ))); + } + let Value::Map(mut entries) = self.stack[start].clone() else { + return Some(Err(VmError::TypeMismatch("map"))); + }; + let mutable = Rc::make_mut(&mut entries); + if remove { + mutable.retain(|(current, _)| current != &key); + } else if let Some((_, current)) = + mutable.iter_mut().find(|(current, _)| current == &key) + { + *current = value; + } else { + mutable.push((key, value)); + } + self.stack.truncate(start); + self.stack.push(Value::Map(entries)); + Ok(()) + } DETACH_LOCAL_CALL_INDEX => { if let Err(error) = self.require_builtin_arity("__detach_local", arity, 1) { return Some(Err(error)); @@ -999,6 +1036,11 @@ impl Vm { self.stack.push(callable); Ok(()) } + JSON_DECODE_TYPED_CALL_INDEX + | JSON_ENCODE_TYPED_CALL_INDEX + | LOGICAL_RENDER_CALL_INDEX => { + return Some(Err(VmError::InvalidCall(index))); + } _ => return None, }) } @@ -1260,4 +1302,55 @@ mod tests { }) )); } + + #[test] + fn map_edit_keeps_null_entry() { + let program = Program::new( + vec![ + Value::map(Vec::new()), + Value::string("present"), + Value::Null, + Value::Bool(false), + ], + vec![ + OpCode::Ldc as u8, + 0, + 0, + 0, + 0, + OpCode::Ldc as u8, + 1, + 0, + 0, + 0, + OpCode::Ldc as u8, + 2, + 0, + 0, + 0, + OpCode::Ldc as u8, + 3, + 0, + 0, + 0, + OpCode::Call as u8, + 0x8F, + 0xFF, + 4, + OpCode::Ret as u8, + ], + Vec::new(), + ); + let mut vm = Vm::new(program); + assert_eq!(vm.run().expect("map edit should run"), VmStatus::Halted); + let Value::Map(entries) = vm.stack().last().expect("map edit should return the map") else { + panic!("map edit should return a map"); + }; + assert!( + entries + .iter() + .any(|(key, value)| key == &Value::string("present") && value == &Value::Null), + "no-std MapEdit must keep a null entry when remove is false" + ); + } } diff --git a/pd-vm-nostd/src/vmbc.rs b/pd-vm-nostd/src/vmbc.rs index 50172f58..1ff94e75 100644 --- a/pd-vm-nostd/src/vmbc.rs +++ b/pd-vm-nostd/src/vmbc.rs @@ -9,7 +9,7 @@ use super::{ }; const MAGIC: [u8; 4] = *b"VMBC"; -const VERSION_V13: u16 = 13; +const VERSION_V14: u16 = 14; const FLAGS: u16 = 0; const MAX_WIRE_PAYLOAD_BYTES: usize = 64 * 1024 * 1024; const MAX_WIRE_BLOB_BYTES: usize = 16 * 1024 * 1024; @@ -72,7 +72,7 @@ pub fn decode_program(bytes: &[u8]) -> Result { } let version = cursor.read_u16()?; - if version != VERSION_V13 { + if version != VERSION_V14 { return Err(WireError::UnsupportedVersion(version)); } let flags = cursor.read_u16()?; @@ -221,7 +221,8 @@ fn skip_host_schema(cursor: &mut Cursor<'_>, depth: usize) -> Result<(), WireErr } match cursor.read_u8()? { 0..=7 => Ok(()), - 8..=10 => skip_host_schema(cursor, depth + 1), + 8..=10 | 15 => skip_host_schema(cursor, depth + 1), + 16 => Ok(()), 11 => { let parameter_count = cursor.read_count_with_overhead("host callable parameters", 1, 1)?; @@ -265,7 +266,8 @@ fn skip_schema(cursor: &mut Cursor<'_>, depth: usize) -> Result<(), WireError> { } Ok(()) } - 10 | 13 | 16 => skip_schema(cursor, nested_depth), + 10 | 13 | 16 | 19 => skip_schema(cursor, nested_depth), + 20 => Ok(()), 11 => { let count = cursor.read_count("schema tuple items", 1)?; for _ in 0..count { @@ -488,6 +490,7 @@ enum WireSchema { result: Box, }, Optional(Box), + Option(Box), Resource, Result { ok: Box, @@ -567,6 +570,8 @@ fn read_cleanup_schema(cursor: &mut Cursor<'_>, depth: usize) -> Result WireSchema::Option(Box::new(read_cleanup_schema(cursor, nested)?)), + 20 => WireSchema::Scalar, value => return Err(WireError::InvalidValueType(value)), }) } @@ -599,7 +604,10 @@ fn cleanup_schema_owns( cleanup_schema_owns(¶m_args[index], decls, &[], &[], next, nodes, active) } WireSchema::Resource => Ok(true), - WireSchema::Array(inner) | WireSchema::Map(inner) | WireSchema::Optional(inner) => { + WireSchema::Array(inner) + | WireSchema::Map(inner) + | WireSchema::Optional(inner) + | WireSchema::Option(inner) => { cleanup_schema_owns(inner, decls, param_names, param_args, next, nodes, active) } WireSchema::Tuple(items) => { @@ -711,6 +719,9 @@ fn substitute_schema( WireSchema::Optional(inner) => { WireSchema::Optional(Box::new(substitute_schema(inner, param_names, param_args))) } + WireSchema::Option(inner) => { + WireSchema::Option(Box::new(substitute_schema(inner, param_names, param_args))) + } WireSchema::Map(inner) => { WireSchema::Map(Box::new(substitute_schema(inner, param_names, param_args))) } diff --git a/pd-vm-nostd/tests/call_script_tests.rs b/pd-vm-nostd/tests/call_script_tests.rs index 41381b4f..98619c5c 100644 --- a/pd-vm-nostd/tests/call_script_tests.rs +++ b/pd-vm-nostd/tests/call_script_tests.rs @@ -70,8 +70,8 @@ fn call_script_executes_direct_call() { let compiled = compile_source("fn add2(value: int) -> int { value + 2 } add2(40);") .expect("direct call source should compile"); let bytes = encode_program(&compiled.program.with_local_count(compiled.locals)) - .expect("direct call program should encode as VMBC v13"); - let program = decode_program(&bytes).expect("no-std should decode VMBC v13"); + .expect("direct call program should encode as VMBC v14"); + let program = decode_program(&bytes).expect("no-std should decode VMBC v14"); assert!( program.code().windows(2).any(|pair| pair[0] == 0x1A), "compiler output should contain CallScript" @@ -85,6 +85,26 @@ fn call_script_executes_direct_call() { assert_eq!(vm.stack(), &[EmbeddedValue::Int(42)]); } +#[test] +fn nostd_executes_std_encoded_unit_map_edit_without_source() { + let source = "let mut values = {}; values[\"present\"] = (); values.has(\"present\");"; + let compiled = compile_source(source).expect("unit map store should compile"); + let bytes = encode_program(&compiled.program.with_local_count(compiled.locals)) + .expect("unit map store should encode"); + let program = decode_program(&bytes).expect("no-std should decode unit map store"); + let edit = vm::BuiltinFunction::MapEdit.call_index().to_le_bytes(); + assert!( + program.code().windows(2).any(|pair| pair == edit), + "std-encoded MapEdit must survive the no-std decoder" + ); + let mut vm = EmbeddedVm::new(program); + assert_eq!( + vm.run().expect("no-std unit map store should run"), + EmbeddedVmStatus::Halted + ); + assert_eq!(vm.stack(), &[EmbeddedValue::Bool(true)]); +} + #[test] fn nostd_executes_std_encoded_result_ok_and_err_without_source() { let cases = [ @@ -97,20 +117,23 @@ fn nostd_executes_std_encoded_result_ok_and_err_without_source() { EmbeddedValue::Int(4), ), ( - "let value: Result, string> = Result::Ok(Result::Err(false)); match value { Result::Ok(inner) => match inner { Result::Ok(n) => true, Result::Err(flag) => flag, }, Result::Err(e) => true, };", + "let value: Result, bool>, string> = Result::Ok(Result::Err(false)); match value { Result::Ok(inner) => match inner { Result::Ok(n) => true, Result::Err(flag) => flag, }, Result::Err(e) => true, };", EmbeddedValue::Bool(false), ), ( - "let value: Result = Result::Err(null); match value { Result::Ok(s) => 9, Result::Err(n) => n, };", - EmbeddedValue::Null, + "let value: Result> = Result::Err(Option::None); match value { Result::Ok(s) => Option::Some(9), Result::Err(n) => n, };", + EmbeddedValue::map(vec![( + EmbeddedValue::string("tag"), + EmbeddedValue::Bool(false), + )]), ), ]; for (source, expected) in cases { let compiled = compile_source(source).expect("Result source should compile"); let bytes = encode_program(&compiled.program.with_local_count(compiled.locals)) .expect("Result program should encode"); - assert_eq!(u16::from_le_bytes([bytes[4], bytes[5]]), 13); - let std_program = vm::decode_program(&bytes).expect("std current v13 decode"); + assert_eq!(u16::from_le_bytes([bytes[4], bytes[5]]), 14); + let std_program = vm::decode_program(&bytes).expect("std current v14 decode"); assert_eq!(encode_program(&std_program).unwrap(), bytes); let mut std_vm = vm::Vm::new_with_jit_config( std_program, @@ -126,10 +149,11 @@ fn nostd_executes_std_encoded_result_ok_and_err_without_source() { EmbeddedValue::Int(n) => vm::Value::Int(*n), EmbeddedValue::Bool(b) => vm::Value::Bool(*b), EmbeddedValue::Null => vm::Value::Null, + EmbeddedValue::Map(_) => vm::option::none_carrier(), _ => unreachable!(), }; assert_eq!(std_vm.stack().last(), Some(&std_expected)); - let program = decode_program(&bytes).expect("no-std should decode v13 Result program"); + let program = decode_program(&bytes).expect("no-std should decode v14 Result program"); let mut vm = EmbeddedVm::new(program); assert_eq!( vm.run().expect("decoded Result program should halt"), @@ -139,6 +163,78 @@ fn nostd_executes_std_encoded_result_ok_and_err_without_source() { } } +#[test] +fn nostd_executes_std_encoded_option_and_unit_without_source() { + let cases = [ + ( + "let nested: Option> = Option::Some(Option::None); match nested { Option::Some(inner) => match inner { Option::Some(v) => v, Option::None => 0 }, Option::None => -1 };", + EmbeddedValue::Int(0), + ), + ( + "let value: Option> = Option::Some(Result::Ok(1)); match value { Option::Some(result) => match result { Result::Ok(v) => v, Result::Err(e) => -1 }, Option::None => -2 };", + EmbeddedValue::Int(1), + ), + ( + "let value: Result, string> = Result::Ok(Option::Some(2)); match value { Result::Ok(option) => match option { Option::Some(v) => v, Option::None => -4 }, Result::Err(e) => -8 };", + EmbeddedValue::Int(2), + ), + ( + "let value: Option<()> = Option::Some(()); match value { Option::Some(unit) => 7, Option::None => -1 };", + EmbeddedValue::Int(7), + ), + ( + "let value: Option = Option::Some(false); match value { Option::Some(flag) => flag, Option::None => true };", + EmbeddedValue::Bool(false), + ), + ( + "let value: Option = Option::Some(0); match value { Option::Some(v) => v, Option::None => 1 };", + EmbeddedValue::Int(0), + ), + ( + "let value: Option = Option::Some(\"\"); match value { Option::Some(text) => text, Option::None => \"missing\" };", + EmbeddedValue::string(""), + ), + ("let unit: () = (); unit;", EmbeddedValue::Null), + ( + "let value: Option = Option::None; match value { Option::Some(v) => v, Option::None => 9 };", + EmbeddedValue::Int(9), + ), + ]; + for (source, expected) in cases { + let compiled = compile_source(source).expect("Option source should compile"); + let bytes = encode_program(&compiled.program.with_local_count(compiled.locals)) + .expect("Option program should encode"); + assert_eq!(u16::from_le_bytes([bytes[4], bytes[5]]), 14); + let std_program = vm::decode_program(&bytes).expect("std current v14 decode"); + assert_eq!(encode_program(&std_program).unwrap(), bytes); + let mut std_vm = vm::Vm::new_with_jit_config( + std_program, + vm::JitConfig { + enabled: false, + hot_loop_threshold: 1, + max_trace_len: 512, + }, + ); + std_vm.set_fuel(100_000); + assert_eq!(std_vm.run().unwrap(), vm::VmStatus::Halted); + let std_expected = match &expected { + EmbeddedValue::Int(n) => vm::Value::Int(*n), + EmbeddedValue::Bool(b) => vm::Value::Bool(*b), + EmbeddedValue::Null => vm::Value::Null, + EmbeddedValue::String(text) => vm::Value::string(text.as_str()), + _ => unreachable!(), + }; + assert_eq!(std_vm.stack().last(), Some(&std_expected), "{source}"); + let program = decode_program(&bytes).expect("no-std should decode v14 Option program"); + let mut vm = EmbeddedVm::new(program); + assert_eq!( + vm.run().expect("decoded Option program should halt"), + EmbeddedVmStatus::Halted + ); + assert_eq!(vm.stack().last().cloned(), Some(expected), "{source}"); + } +} + #[test] fn call_script_executes_nested_direct_calls() { let compiled = compile_source( @@ -497,6 +593,59 @@ fn nostd_rejects_nonempty_resource_cleanup_without_executing() { ); } +#[test] +fn nostd_reads_option_cleanup_schema_and_refuses_the_resource_capability() { + use std::sync::Arc; + use vm::{ + CompileSourceFileOptions, HostApiBuilder, HostFunctionSchema, HostTypeSchema, + ResourceTypeKey, ResourceTypeSchema, SourceFlavor, compile_source_with_flavor_and_options, + }; + + let key = ResourceTypeKey::new("pr24.closure").expect("resource key"); + let mut builder = HostApiBuilder::new(); + builder.resource(ResourceTypeSchema::new(key.clone(), "closure resource")); + builder.function(HostFunctionSchema::with_return( + "test::make_local", + Vec::new(), + HostTypeSchema::Resource(key.clone()), + )); + builder.function(HostFunctionSchema::with_return( + "test::fail", + Vec::new(), + HostTypeSchema::Result { + ok: Box::new(HostTypeSchema::Int), + err: Box::new(HostTypeSchema::String), + }, + )); + let catalog = Arc::new(builder.build().expect("catalog")); + let source = r#" + fn relay() -> Result { + let local: Option> = Option::Some(test::make_local()); + let value = test::fail()?; + Result::Ok(value) + } + relay(); + "#; + let compiled = compile_source_with_flavor_and_options( + source, + SourceFlavor::RustScript, + CompileSourceFileOptions::new().with_host_api_catalog(catalog), + ) + .expect("Option resource cleanup source should compile"); + let bytes = encode_program(&compiled.program.with_local_count(compiled.locals)) + .expect("Option resource cleanup program should encode"); + assert_eq!(u16::from_le_bytes([bytes[4], bytes[5]]), 14); + let err = decode_program(&bytes) + .expect_err("no-std must decode the Option cleanup schema and refuse resources"); + assert!( + matches!( + err, + WireError::UnsupportedCapability("ret cleanup resource plans") + ), + "expected an explicit capability rejection before execution, got {err:?}" + ); +} + fn resource_cleanup_bytes() -> Vec { use std::sync::Arc; use vm::{ diff --git a/pd-vm-nostd/tests/embedded_vm.rs b/pd-vm-nostd/tests/embedded_vm.rs index fc0acff0..8a0d1475 100644 --- a/pd-vm-nostd/tests/embedded_vm.rs +++ b/pd-vm-nostd/tests/embedded_vm.rs @@ -158,3 +158,35 @@ fn invalid_jump_and_local_are_reported() { Err(VmError::InvalidLocal(9)) ); } + +#[test] +fn std_encoded_json_programs_fail_nostd_at_intended_ids() { + let cases = [ + ( + "use json; let n: int = json::decode::(\"7\"); n;", + vm::BuiltinFunction::JsonDecodeTyped.call_index(), + ), + ( + "use json; json::encode::(7);", + vm::BuiltinFunction::JsonEncodeTyped.call_index(), + ), + ( + "use json; struct Payload { value: Option<()> } let payload: Payload = {value: Option::Some(())}; json::encode(payload);", + vm::BuiltinFunction::JsonEncodeTyped.call_index(), + ), + ]; + for (source, index) in cases { + let compiled = vm::compile_source(source).expect("std json source should compile"); + let bytes = vm::encode_program(&compiled.program.with_local_count(compiled.locals)) + .expect("std encoder should emit v14"); + let program = decode_program(&bytes).expect("nostd decoder should accept std json wire"); + let mut vm = EmbeddedVm::new(program); + let error = vm + .run() + .expect_err("nostd must refuse the typed json builtin"); + assert!( + matches!(error, VmError::InvalidCall(got) if got == index), + "expected InvalidCall({index:#x}) for {source}, got {error:?}" + ); + } +} diff --git a/pd-vm-nostd/tests/embedded_vmbc.rs b/pd-vm-nostd/tests/embedded_vmbc.rs index 1466ff1d..89a908d7 100644 --- a/pd-vm-nostd/tests/embedded_vmbc.rs +++ b/pd-vm-nostd/tests/embedded_vmbc.rs @@ -31,9 +31,9 @@ fn encoded_scalar_program() -> Vec { } #[test] -fn embedded_decoder_reads_host_generated_v13() { +fn embedded_decoder_reads_host_generated_v14() { let bytes = encoded_scalar_program(); - let program = decode_program(&bytes).expect("embedded decoder should accept VMBC v13"); + let program = decode_program(&bytes).expect("embedded decoder should accept VMBC v14"); assert_eq!( program.code(), @@ -139,7 +139,7 @@ fn embedded_decoder_skips_named_host_schema_from_std_encode() { .with_host_import_schemas(vec![schema]) .expect("schema alignment"); let bytes = encode_program(&program).expect("named host schema should encode"); - assert_eq!(u16::from_le_bytes([bytes[4], bytes[5]]), 13); + assert_eq!(u16::from_le_bytes([bytes[4], bytes[5]]), 14); let decoded = decode_program(&bytes).expect("embedded decoder should skip Named host schemas"); assert_eq!(decoded.imports().len(), 1); assert_eq!(decoded.imports()[0].name, "embedded::named"); @@ -165,7 +165,7 @@ fn embedded_decoder_rejects_legacy_v11_without_schema_markers() { fn minimal_vmbc_prefix(constant_count: u32, code: &[u8], import_count: u32) -> Vec { let mut bytes = Vec::new(); bytes.extend_from_slice(b"VMBC"); - bytes.extend_from_slice(&13u16.to_le_bytes()); + bytes.extend_from_slice(&14u16.to_le_bytes()); bytes.extend_from_slice(&0u16.to_le_bytes()); bytes.extend_from_slice(&constant_count.to_le_bytes()); bytes.extend_from_slice(&(code.len() as u32).to_le_bytes()); @@ -546,7 +546,7 @@ fn current_vmbc_with_named_host_return_schema(schema: &[u8]) -> Vec { bytes.extend_from_slice(&0u32.to_le_bytes()); bytes.extend_from_slice(&0u32.to_le_bytes()); bytes.extend_from_slice(&0u32.to_le_bytes()); - // Mandatory current-v13 ret-cleanup section: empty plan count after named structs. + // Mandatory current-v14 ret-cleanup section: empty plan count after named structs. bytes.extend_from_slice(&0u32.to_le_bytes()); bytes } @@ -636,7 +636,7 @@ fn embedded_decoder_rejects_oversized_nested_named_host_depth() { } #[test] -fn embedded_decoder_reads_v13_guest_named_struct_payload() { +fn embedded_decoder_reads_v14_guest_named_struct_payload() { let compiled = compile_source( r#" struct Point { x: int, y: int } @@ -646,7 +646,7 @@ fn embedded_decoder_reads_v13_guest_named_struct_payload() { ) .expect("guest Named source should compile"); let bytes = encode_program(&compiled.program).expect("struct-bearing program should encode"); - assert_eq!(u16::from_le_bytes([bytes[4], bytes[5]]), 13); + assert_eq!(u16::from_le_bytes([bytes[4], bytes[5]]), 14); let program = decode_program(&bytes).expect("embedded decoder should skip guest named structs"); assert_eq!(program.code().last().copied(), Some(OpCode::Ret as u8)); } diff --git a/pd-vm-wasm/examples/rss-complex-example.rss b/pd-vm-wasm/examples/rss-complex-example.rss index fa631b8f..2dfbadd3 100644 --- a/pd-vm-wasm/examples/rss-complex-example.rss +++ b/pd-vm-wasm/examples/rss-complex-example.rss @@ -36,13 +36,12 @@ let mut closure_value = add(5); let profile: Profile = {stats: {score: closure_value}}; let matched = match profile?.stats?.score { - None => 0, - Some(score) => if score == 12 => { + Option::None => 0, + Option::Some(score) => if score == 12 => { closure_value } else => { 0 }, - _ => 0, }; let regex_ok = re::match("(?i)^rustscript$", "RUSTSCRIPT"); diff --git a/pd-vm-wasm/examples/rss-lrucache-example.rss b/pd-vm-wasm/examples/rss-lrucache-example.rss index 900f554b..0aad0306 100644 --- a/pd-vm-wasm/examples/rss-lrucache-example.rss +++ b/pd-vm-wasm/examples/rss-lrucache-example.rss @@ -7,8 +7,8 @@ use re; // - `lookup`: user key -> node id // - `nodes`: node id -> LruNode // -// Node ids start at 1. The cache uses `0` as the internal "no link" sentinel, -// because storing `null` into a map entry now deletes that entry. +// Node ids start at 1. The cache uses `0` as the internal "no link" sentinel. +// Missing map entries are removed explicitly; absent result payloads are Option::None. struct LruNode { key: K, value: V, @@ -34,19 +34,19 @@ struct LruCacheState { struct LruPeekResult { found: bool, - value: V + value: Option } struct LruGetResult { cache: LruCacheState, found: bool, - value: V + value: Option } struct LruEntryReport { found: bool, - key: K, - value: V + key: Option, + value: Option } struct LruEntryRow { @@ -191,8 +191,8 @@ fn evict_oldest( let evicted_key = head_node.key.copy(); let mut next_lookup = lookup; let mut next_nodes = nodes; - next_lookup[evicted_key] = null; - next_nodes[head] = null; + next_lookup = next_lookup.remove(evicted_key); + next_nodes = next_nodes.remove(head); let next_tail = if tail == head => { 0 @@ -222,9 +222,9 @@ fn evict_oldest( fn entry_report(cache: LruCacheState, id: int) -> LruEntryReport { let out: LruEntryReport = if id != 0 => { let node: LruNode = &((&cache).nodes[id]); - {found: true, key: node.key.copy(), value: node.value.copy()} + {found: true, key: Option::Some(node.key.copy()), value: Option::Some(node.value.copy())} } else => { - {found: false, key: null, value: null} + {found: false, key: Option::None, value: Option::None} }; out } @@ -266,9 +266,9 @@ pub fn peek(cache: LruCacheState, key: K) -> LruPeekResult { let out: LruPeekResult = if lookup.has(key) => { let id = (&lookup)[key]; let node: LruNode = &((&state.nodes)[id]); - {found: true, value: node.value.copy()} + {found: true, value: Option::Some(node.value.copy())} } else => { - {found: false, value: null} + {found: false, value: Option::None} }; out } @@ -311,12 +311,12 @@ pub fn get(cache: LruCacheState, key: K) -> LruGetResult { relinked.nodes ) }; - {cache: next_cache, found: true, value: value} + {cache: next_cache, found: true, value: Option::Some(value)} } else => { { cache: make_cache::(limit, size, next_id, head, tail, lookup, nodes), found: false, - value: null + value: Option::None } }; out @@ -461,10 +461,10 @@ pub fn remove(cache: LruCacheState, key: K) -> LruCacheState { let id = (&lookup)[key]; let node: LruNode = (&nodes)[id]; let mut next_lookup = lookup; - next_lookup[key] = null; + next_lookup = next_lookup.remove(key); let unlinked: LinkState = unlink_node::(nodes, head, tail, id, node); let mut next_nodes = unlinked.nodes; - next_nodes[id] = null; + next_nodes = next_nodes.remove(id); make_cache::( limit, size - 1, @@ -578,7 +578,12 @@ assert(initial_order[2] == "build-ops"); let mut world_hit = get::(cache, "world-news"); assert(world_hit.found); -let world_item: FeedCacheItem = world_hit.value.copy(); +let world_item: FeedCacheItem = world_hit.value.unwrap_or({ + day: "", + topic: "", + article_count: 0, + slug: "" +}).copy(); assert(world_item.article_count == 5); assert(re::match("^[a-z-]+$", world_item.slug)); cache = world_hit.cache; @@ -595,14 +600,20 @@ cache = put::( ); assert(has::(cache, "markets") == false); -assert(peek::(cache, "world-news").value.article_count == 5); +let world_peek = peek::(cache, "world-news"); +assert(world_peek.value.unwrap_or({ + day: "", + topic: "", + article_count: 0, + slug: "" +}).article_count == 5); cache = touch::(cache, "build-ops"); let oldest_entry = oldest::(cache); let newest_entry = newest::(cache); -let oldest_key: string = oldest_entry.key.copy(); -let newest_key: string = newest_entry.key.copy(); +let oldest_key: string = oldest_entry.key.unwrap_or("").copy(); +let newest_key: string = newest_entry.key.unwrap_or("").copy(); assert(oldest_key == "world-news"); assert(newest_key == "build-ops"); diff --git a/pd-vm-wasm/examples/rss-strings-regex-example.rss b/pd-vm-wasm/examples/rss-strings-regex-example.rss index 223394af..a88657ee 100644 --- a/pd-vm-wasm/examples/rss-strings-regex-example.rss +++ b/pd-vm-wasm/examples/rss-strings-regex-example.rss @@ -18,19 +18,25 @@ fn parse_log_line(line) { ); assert(groups.length == 5); let entry: LogEntry = { - level: groups[1], - route: groups[2], - latency_ms: groups[3], - message: groups[4] + level: string_or_empty(groups[1]), + route: string_or_empty(groups[2]), + latency_ms: string_or_empty(groups[3]), + message: string_or_empty(groups[4]) }; entry } -fn is_some_string_equal(actual: string?, expected: string) -> bool { - if type(actual) == "string" => { - actual == expected - } else => { - false +fn is_some_string_equal(actual: Option, expected: string) -> bool { + match actual { + Option::Some(value) => value == expected, + Option::None => false + } +} + +fn string_or_empty(value: Option) -> string { + match value { + Option::Some(text) => text, + Option::None => "" } } @@ -66,12 +72,13 @@ let rewritten_warn: string = string::replace(warn_message, "cache", "edge-cache" let rewritten_route = re::replace("^feed/", warn_route, "rss::"); let error_words = re::split("\\s+", &error_message); let error_word_count = error_words.length; -let id_groups: [string] = re::captures("id=([0-9]+)", error_message); -let error_id: string = if id_groups.length == 2 => { +let id_groups = re::captures("id=([0-9]+)", error_message); +let error_id: string = string_or_empty(if id_groups.length == 2 => { id_groups[1] } else => { - "none" -}; + Option::None +} +); let levels = [first_level, warn_level, error_level]; let slow_routes = [rewritten_route, string::replace(error_route, "/", "::")]; diff --git a/pd-vm-wasm/src/lib.rs b/pd-vm-wasm/src/lib.rs index 7282f6ff..122ca301 100644 --- a/pd-vm-wasm/src/lib.rs +++ b/pd-vm-wasm/src/lib.rs @@ -14,6 +14,10 @@ mod coherent_typehint; #[path = "../tests/fixtures/result_consumer.rs"] mod result_consumer; +#[cfg(test)] +#[path = "../tests/fixtures/option_consumer.rs"] +mod option_consumer; + use serde::{Deserialize, Serialize}; use vm::{ CompileSourceFileOptions, InferredLocalTypeHint, SourceFlavor, @@ -92,6 +96,7 @@ fn type_schema_to_json(schema: &vm::TypeSchema) -> serde_json::Value { match schema { TypeSchema::Unknown => json!({"kind": "unknown"}), TypeSchema::Null => json!({"kind": "null"}), + TypeSchema::Unit => json!({"kind": "unit"}), TypeSchema::Int => json!({"kind": "int"}), TypeSchema::Float => json!({"kind": "float"}), TypeSchema::Number => json!({"kind": "number"}), @@ -106,6 +111,9 @@ fn type_schema_to_json(schema: &vm::TypeSchema) -> serde_json::Value { TypeSchema::Optional(inner) => { json!({"kind": "optional", "inner": type_schema_to_json(inner)}) } + TypeSchema::Option(inner) => { + json!({"kind": "option", "inner": type_schema_to_json(inner)}) + } TypeSchema::Array(item) => json!({"kind": "array", "item": type_schema_to_json(item)}), TypeSchema::Map(item) => json!({"kind": "map", "item": type_schema_to_json(item)}), TypeSchema::Named(name, args) => { @@ -1320,8 +1328,8 @@ mod lint_tests { struct Stats { score: int } struct Profile { stats: Stats } - let payload_json = json::encode({}); - let payload_decoded: Profile = json::decode(payload_json); + let payload_json = json::encode::(json::decode::("{\"stats\":{\"score\":41}}")); + let payload_decoded: Profile = json::decode::(payload_json); payload_decoded.stats.score; "#; @@ -1507,6 +1515,20 @@ mod runtime_tests { } } + #[test] + fn playground_rss_examples_compile_with_embedded_stdlib() { + for (name, source) in rss_playground_examples() { + compile_source_with_flavor_and_options( + source, + SourceFlavor::RustScript, + embedded_stdlib_compile_options(), + ) + .unwrap_or_else(|err| { + panic!("playground example '{name}' should compile with the bundled stdlib: {err}") + }); + } + } + #[test] fn playground_rss_examples_are_formatted_lint_clean_and_runnable() { let options = embedded_stdlib_compile_options(); @@ -1782,8 +1804,8 @@ mod runtime_tests { struct Stats { score: int } struct Profile { stats: Stats } - let payload_json = json::encode({}); - let payload_decoded: Profile = json::decode(payload_json); + let payload_json = json::encode::(json::decode::("{\"stats\":{\"score\":41}}")); + let payload_decoded: Profile = json::decode::(payload_json); payload_decoded.stats.score; "#; @@ -1860,9 +1882,8 @@ mod runtime_tests { struct Data { values: [int] } let data: Data = { values: [41] }; let result = match data?.values?.[0] { - None => 0, - Some(value) => value + 1, - _ => 0, + Option::None => 0, + Option::Some(value) => value + 1, }; result; "#; @@ -2210,8 +2231,10 @@ mod runtime_tests { let source = r#" use re; use json; + struct MatchPayload { ok: bool } let matched = re::match("(?i)^rss$", "RSS"); - let payload = json::encode({ ok: matched }); + let value: MatchPayload = { ok: matched }; + let payload = json::encode::(value); payload; "#; let report = lint_source_with_flavor(source, SourceFlavor::RustScript); @@ -2227,9 +2250,10 @@ mod runtime_tests { let source = r#" use re; use json; - let matched = re::match("(?i)^rss$", "RSS"); - let payload = json::encode({ ok: matched }); struct Payload { ok: bool } + let matched = re::match("(?i)^rss$", "RSS"); + let value: Payload = { ok: matched }; + let payload = json::encode::(value); let decoded = json::decode::(payload); let ok = decoded.ok.copy(); if ok { diff --git a/pd-vm-wasm/src/runtime.rs b/pd-vm-wasm/src/runtime.rs index 7ff0d392..3043f070 100644 --- a/pd-vm-wasm/src/runtime.rs +++ b/pd-vm-wasm/src/runtime.rs @@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet}; #[cfg(not(target_family = "wasm"))] use std::sync::OnceLock; use std::sync::{Arc, Mutex}; -use std::task::{Context, Poll, Wake, Waker}; +use std::task::{Context, Poll, Waker}; #[cfg(all(not(target_family = "wasm"), test))] use std::time::Duration; #[cfg(not(target_family = "wasm"))] @@ -360,16 +360,6 @@ impl HostFunction for PlaygroundRuntimeSleepHostFunction { } } -struct NoopWake; - -impl Wake for NoopWake { - fn wake(self: Arc) {} -} - -fn noop_waker() -> Waker { - Waker::from(Arc::new(NoopWake)) -} - #[cfg(target_family = "wasm")] #[link(wasm_import_module = "env")] unsafe extern "C" { @@ -412,7 +402,7 @@ fn wait_message(op_id: HostOpId) -> String { } fn poll_waiting_host_op_once(vm: &mut Vm) -> Poll> { - let waker = noop_waker(); + let waker = Waker::noop().clone(); let mut cx = Context::from_waker(&waker); vm.poll_waiting_host_op(&mut cx) } diff --git a/pd-vm-wasm/tests/fixtures/coherent_typehint.rs b/pd-vm-wasm/tests/fixtures/coherent_typehint.rs index 0fe995ac..8b54a6e2 100644 --- a/pd-vm-wasm/tests/fixtures/coherent_typehint.rs +++ b/pd-vm-wasm/tests/fixtures/coherent_typehint.rs @@ -1,8 +1,8 @@ use super::*; #[test] fn coherent_schema_json_preserves_optional_callable_named_and_module_byte_identity() { - let source = "// 中文\r\nuse self::lib as lib;\r\nlet optional: int? = null;\r\nlet value = lib::make();\r\nfn factory() { lib::make() }\r\nlet function = factory;\r\n"; - let library = "struct Box { value: int }\npub fn make() -> Box { { value: 42 } }\nlet local: int? = null;\n"; + let source = "// 中文\r\nuse self::lib as lib;\r\nlet optional: Option = Option::None;\r\nlet value = lib::make();\r\nfn factory() { lib::make() }\r\nlet function = factory;\r\n"; + let library = "struct Box { value: int }\npub fn make() -> Box { { value: 42 } }\nlet local: Option = Option::None;\n"; let overrides = serde_json::json!([{"path": "./lib.rss", "source": library}]).to_string(); let response: serde_json::Value = serde_json::from_slice(&local_type_hints_json_with_context_response( @@ -20,7 +20,7 @@ fn coherent_schema_json_preserves_optional_callable_named_and_module_byte_identi .unwrap(); assert_eq!( optional["schema"], - serde_json::json!({"kind": "optional", "inner": {"kind": "int"}}) + serde_json::json!({"kind": "option", "inner": {"kind": "int"}}) ); assert_eq!(optional["span"]["source_id"], 0); let lo = optional["span"]["lo"].as_u64().unwrap() as usize; @@ -44,7 +44,7 @@ fn coherent_schema_json_preserves_optional_callable_named_and_module_byte_identi } #[test] fn coherent_recursive_json_keeps_distinct_module_nominals_and_instantiated_callable_contracts() { - let source = "// 中文\r\nuse self::left::lib as left;\r\nuse self::right::lib as right;\r\nlet left_value = left::make();\r\nlet right_value = right::make();\r\nfn identity(value: T) -> T { value }\r\nlet specialized = identity::;\r\nlet nested: map<[int?]> = { \"one\": [null, 42] };\r\nlet tuple: [int?, fn(int) -> int] = [null, specialized];\r\n"; + let source = "// 中文\r\nuse self::left::lib as left;\r\nuse self::right::lib as right;\r\nlet left_value = left::make();\r\nlet right_value = right::make();\r\nfn identity(value: T) -> T { value }\r\nlet specialized = identity::;\r\nlet nested: map<[Option]> = { \"one\": [Option::None, Option::Some(42)] };\r\nlet tuple: [Option, fn(int) -> int] = [Option::None, specialized];\r\n"; let left = "struct Box { value: T }\npub fn make() -> Box { { value: 42 } }\n"; let right = "struct Box { value: T }\npub fn make() -> Box { { value: \"label\" } }\n"; @@ -78,11 +78,11 @@ fn coherent_recursive_json_keeps_distinct_module_nominals_and_instantiated_calla assert_eq!(hint("specialized")["schema"], callable); assert_eq!( hint("nested")["schema"], - serde_json::json!({"kind": "map", "item": {"kind": "array", "item": {"kind": "optional", "inner": {"kind": "int"}}}}) + serde_json::json!({"kind": "map", "item": {"kind": "array", "item": {"kind": "option", "inner": {"kind": "int"}}}}) ); assert_eq!( hint("tuple")["schema"], - serde_json::json!({"kind": "tuple", "items": [{"kind": "optional", "inner": {"kind": "int"}}, callable]}) + serde_json::json!({"kind": "tuple", "items": [{"kind": "option", "inner": {"kind": "int"}}, callable]}) ); for name in [ "left_value", @@ -102,7 +102,7 @@ fn coherent_recursive_json_keeps_distinct_module_nominals_and_instantiated_calla #[test] fn coherent_inferred_callable_json_keeps_optional_results_and_per_call_generics() { - let source = "// 中文\r\nfn maybe(value: int?) { value }\r\nlet g = maybe;\r\nlet closure: fn(int?) -> int? = |value| value;\r\nfn answer() { 42 }\r\nlet f = answer;\r\nfn identity(value: T) -> T { value }\r\nlet output = identity::(41);\r\ng(null);\r\n"; + let source = "// 中文\r\nfn maybe(value: Option) { value }\r\nlet g = maybe;\r\nlet closure: fn(Option) -> Option = |value| value;\r\nfn answer() { 42 }\r\nlet f = answer;\r\nfn identity(value: T) -> T { value }\r\nlet output = identity::(41);\r\ng(Option::None);\r\n"; let response: serde_json::Value = serde_json::from_slice(&local_type_hints_json_with_context_response( source, @@ -114,7 +114,7 @@ fn coherent_inferred_callable_json_keeps_optional_results_and_per_call_generics( assert!(response.get("error").is_none(), "{response}"); let hints = response["hints"].as_array().unwrap(); let hint = |name| hints.iter().find(|hint| hint["name"] == name).unwrap(); - let optional = serde_json::json!({"kind": "optional", "inner": {"kind": "int"}}); + let optional = serde_json::json!({"kind": "option", "inner": {"kind": "int"}}); let callable = serde_json::json!({"kind": "callable", "params": [optional.clone()], "result": optional}); assert_eq!(hint("g")["schema"], callable); diff --git a/pd-vm-wasm/tests/fixtures/option_abi.cjs b/pd-vm-wasm/tests/fixtures/option_abi.cjs new file mode 100644 index 00000000..c13b23ad --- /dev/null +++ b/pd-vm-wasm/tests/fixtures/option_abi.cjs @@ -0,0 +1,117 @@ +// Actual wasm32 ABI exercise for logical Option/Unit. +// +// Build and run from the repository root: +// cargo build --locked -p pd-vm-wasm --target wasm32-unknown-unknown --features runtime +// node pd-vm-wasm/tests/fixtures/option_abi.cjs +// +// The script instantiates the real exported WebAssembly module through Node and +// asserts the run, language-service hint and format states. It is a manual ABI +// gate, not a native Rust shim and not a build-only assertion. +const fs = require('fs'); +const assert = require('assert'); + +const wasmPath = + process.argv[2] || + 'target/wasm32-unknown-unknown/debug/pd_vm_wasm.wasm'; +const wasmModule = new WebAssembly.Module(fs.readFileSync(wasmPath)); +const instance = new WebAssembly.Instance(wasmModule, { + env: { pd_playground_now_ms: () => BigInt(Date.now()) }, +}); +const wasmExports = instance.exports; +const memory = wasmExports.memory; + +function put(text) { + const bytes = Buffer.from(text, 'utf8'); + const ptr = wasmExports.wasm_alloc(bytes.length); + new Uint8Array(memory.buffer, ptr, bytes.length).set(bytes); + return [ptr, bytes.length]; +} + +function read(packed) { + const len = Number(packed >> 32n); + const ptr = Number(packed & 0xffffffffn); + const bytes = new Uint8Array(memory.buffer, ptr, len); + const text = Buffer.from(bytes).toString('utf8'); + wasmExports.wasm_dealloc(ptr, len); + return text; +} + +function runSource(source) { + const [sptr, slen] = put(source); + const [fptr, flen] = put('rss'); + const [optr, olen] = put('{"fuel":100000,"fuelCheckInterval":1}'); + return JSON.parse(read(wasmExports.run_source_json(sptr, slen, fptr, flen, optr, olen))); +} + +function hints(source) { + const [sptr, slen] = put(source); + const [fptr, flen] = put('rss'); + return JSON.parse(read(wasmExports.local_type_hints_json(sptr, slen, fptr, flen))); +} + +function format(source) { + const [sptr, slen] = put(source); + const [fptr, flen] = put('rss'); + return JSON.parse(read(wasmExports.format_source_json(sptr, slen, fptr, flen))); +} + +const runtimeSource = ` +fn relay(value: Option) -> Option { match value { Option::Some(v) => Option::Some(v + 1), Option::None => Option::None } } +let some = relay(Option::Some(7)); +let none = relay(Option::None); +match some { Option::Some(value) => print("some:" + value), Option::None => print("some:none"), }; +match none { Option::Some(value) => print("none:some"), Option::None => print("none:none"), }; +let unit: () = (); +print("unit"); +`; + +const runtime = runSource(runtimeSource); +console.log('WASM_OPTION_RUNTIME', JSON.stringify(runtime)); +assert.strictEqual(runtime.ok, true, JSON.stringify(runtime)); +assert.strictEqual(runtime.error, null); +assert.strictEqual(runtime.halted, true); +assert.deepStrictEqual(runtime.output, ['some:8', 'none:none', 'unit']); + +const hintSource = ` +let nested: Option> = Option::Some(Option::None); +let unit: () = (); +let empty: Option = Option::Some(""); +let flag: Option = Option::Some(false); +let zero: Option = Option::Some(0); +`; +const hintPayload = hints(hintSource); +console.log('WASM_OPTION_HINTS', JSON.stringify(hintPayload)); +assert.ok(!hintPayload.error, JSON.stringify(hintPayload)); +const byName = new Map(hintPayload.hints.map((hint) => [hint.name, hint])); +assert.deepStrictEqual(byName.get('nested').schema, { + kind: 'option', + inner: { kind: 'option', inner: { kind: 'int' } }, +}); +assert.deepStrictEqual(byName.get('unit').schema, { kind: 'unit' }); +assert.deepStrictEqual(byName.get('empty').schema, { + kind: 'option', + inner: { kind: 'string' }, +}); +assert.deepStrictEqual(byName.get('flag').schema, { + kind: 'option', + inner: { kind: 'bool' }, +}); +assert.deepStrictEqual(byName.get('zero').schema, { + kind: 'option', + inner: { kind: 'int' }, +}); + +const formatSourceText = + 'fn relay(value:Option)->Option{match value{Option::Some(v)=>Option::Some(v+1),Option::None=>Option::None}}\n' + + 'let value:Option<()> =Option::Some(());\nlet result=relay(Option::Some(1));\n'; +const first = format(formatSourceText); +console.log('WASM_OPTION_FORMAT', JSON.stringify(first)); +assert.strictEqual(first.ok, true, JSON.stringify(first)); +assert.ok(first.formatted.includes('Option'), first.formatted); +assert.ok(first.formatted.includes('Option<()>'), first.formatted); +const second = format(first.formatted); +assert.deepStrictEqual(second, first); +const formattedRun = runSource(first.formatted); +assert.strictEqual(formattedRun.ok, true, JSON.stringify(formattedRun)); + +console.log('WASM_OPTION_ABI_OK'); diff --git a/pd-vm-wasm/tests/fixtures/option_consumer.rs b/pd-vm-wasm/tests/fixtures/option_consumer.rs new file mode 100644 index 00000000..63d183c3 --- /dev/null +++ b/pd-vm-wasm/tests/fixtures/option_consumer.rs @@ -0,0 +1,128 @@ +use super::*; +use serde_json::{Value, json}; + +fn option_schema(inner: Value) -> Value { + json!({"kind": "option", "inner": inner}) +} + +fn response(bytes: Vec) -> Value { + serde_json::from_slice(&bytes).expect("service response must be JSON") +} + +fn hint<'a>(response: &'a Value, name: &str) -> &'a Value { + assert!(response.get("error").is_none(), "{response}"); + response["hints"] + .as_array() + .unwrap() + .iter() + .find(|hint| hint["name"] == name) + .unwrap_or_else(|| panic!("missing hint {name}: {response}")) +} + +#[test] +fn option_public_basic_hints_keep_option_and_unit_schemas() { + let source = "fn relay(value: Option) -> Option { match value { Option::Some(v) => Option::Some(v + 1), Option::None => Option::None } }\n\ + let some: Option = Option::Some(3);\n\ + let none: Option = Option::None;\n\ + let unit: () = ();\n\ + let relay_fn = relay;\n"; + let payload = response(local_type_hints_json_response(source, "rss")); + println!("OPTION_OBSERVATION basic-hints={payload}"); + let int = json!({"kind":"int"}); + let option_int = option_schema(int.clone()); + assert_eq!(hint(&payload, "some")["schema"], option_int); + assert_eq!(hint(&payload, "none")["schema"], option_int); + assert_eq!(hint(&payload, "unit")["schema"], json!({"kind":"unit"})); + assert_eq!(hint(&payload, "some")["inferred_type"], "Option"); + assert_eq!(hint(&payload, "unit")["inferred_type"], "()"); + assert_eq!( + hint(&payload, "relay_fn")["schema"], + json!({"kind":"callable", "params":[option_int.clone()], "result":option_int}) + ); +} + +#[test] +fn option_public_hints_preserve_nested_result_and_resource_free_units() { + let source = "let nested: Option> = Option::Some(Result::Ok(1));\n\ + let inner_unit: Option<()> = Option::Some(());\n\ + let empty: Option = Option::Some(\"\");\n\ + let value: int = match nested { Option::Some(result) => match result { Result::Ok(v) => v, Result::Err(e) => -1 }, Option::None => -1 };\n"; + let payload = response(local_type_hints_json_response(source, "rss")); + println!("OPTION_OBSERVATION nested-hints={payload}"); + let int = json!({"kind":"int"}); + let string = json!({"kind":"string"}); + let result = json!({"kind":"result", "ok":int, "err":string}); + assert_eq!(hint(&payload, "nested")["schema"], option_schema(result)); + assert_eq!( + hint(&payload, "inner_unit")["schema"], + option_schema(json!({"kind":"unit"})) + ); + assert_eq!(hint(&payload, "empty")["schema"], option_schema(string)); + assert_eq!(hint(&payload, "value")["schema"], int); +} + +#[test] +fn option_public_format_is_idempotent_and_preserves_unit_spelling() { + let source = "fn relay(value:Option)->Option{match value{Option::Some(v)=>Option::Some(v+1),Option::None=>Option::None}}\n\ + let value:Option<()> =Option::Some(());\nlet result=relay(Option::Some(1));\n"; + let formatted = response(format_source_json_response(source, "rss")); + println!("OPTION_OBSERVATION format={formatted}"); + assert_eq!(formatted["ok"], true); + let text = formatted["formatted"].as_str().unwrap(); + assert!(text.contains("Option"), "{text}"); + assert!(text.contains("Option<()>"), "{text}"); + assert!(text.contains("Option::None"), "{text}"); + assert_eq!( + response(format_source_json_response(text, "rss")), + formatted + ); +} + +#[cfg(feature = "runtime")] +#[test] +fn option_native_runtime_response_json_keeps_all_states() { + let source = r#" +fn relay(value: Option) -> Option { match value { Option::Some(v) => Option::Some(v + 1), Option::None => Option::None } } +let some = relay(Option::Some(7)); +let none = relay(Option::None); +match some { Option::Some(value) => print("some:" + value), Option::None => print("some:none"), }; +match none { Option::Some(value) => print("none:some"), Option::None => print("none:none"), }; +let unit: () = (); +print("unit"); +"#; + let payload = response(run_source_json_response( + source, + "rss", + Some(r#"{"fuel":100000,"fuelCheckInterval":1}"#), + )); + println!("OPTION_OBSERVATION native-runtime={payload}"); + assert_eq!(payload["ok"], true, "{payload}"); + assert!(payload["error"].is_null(), "{payload}"); + assert_eq!(payload["halted"], true); + assert_eq!(payload["output"], json!(["some:8", "none:none", "unit"])); +} + +#[test] +fn option_schema_json_recurses_through_option_nested_in_containers() { + use vm::TypeSchema as S; + let int = json!({"kind":"int"}); + let option = S::Option(Box::new(S::Int)); + let expected = option_schema(int.clone()); + assert_eq!(type_schema_to_json(&option), expected); + assert_eq!(type_schema_to_json(&S::Unit), json!({"kind":"unit"})); + let nested = S::Option(Box::new(S::Result { + ok: Box::new(S::Array(Box::new(option.clone()))), + err: Box::new(S::Callable { + params: vec![S::Unit], + result: Box::new(option.clone()), + }), + })); + assert_eq!( + type_schema_to_json(&nested), + option_schema(json!({ + "kind":"result", + "ok":{"kind":"array", "item":expected}, + "err":{"kind":"callable", "params":[{"kind":"unit"}], "result":expected} + })) + ); +} diff --git a/pd-vm-wasm/tests/fixtures/result_consumer.rs b/pd-vm-wasm/tests/fixtures/result_consumer.rs index ee572270..15169e3c 100644 --- a/pd-vm-wasm/tests/fixtures/result_consumer.rs +++ b/pd-vm-wasm/tests/fixtures/result_consumer.rs @@ -39,7 +39,7 @@ fn result_public_basic_hints_keep_ok_err_and_callable_payload_schemas() { #[test] fn result_public_hints_preserve_recursive_types_module_identity_and_arm_bindings() { - let source = "// 中文\r\nuse self::lib as lib;\r\nlet returned = lib::make();\r\nlet matched = match returned { Result::Ok(value) => value.value, Result::Err(error) => 0 };\r\nlet nested: Result<[Result?], Result> = Result::Ok([null]);\r\nlet optional: Result? = null;\r\nfn relay(input: Result) -> Result { let number = input?; Result::Ok(\"done\") }\r\nlet relay_fn = relay;\r\nlet propagated = relay(Result::Err(\"bad\"));\r\n"; + let source = "// 中文\r\nuse self::lib as lib;\r\nlet returned = lib::make();\r\nlet matched = match returned { Result::Ok(value) => value.value, Result::Err(error) => 0 };\r\nlet nested: Result<[Option>], Result, int>> = Result::Ok([Option::None]);\r\nlet optional: Option> = Option::None;\r\nfn relay(input: Result) -> Result { let number = input?; Result::Ok(\"done\") }\r\nlet relay_fn = relay;\r\nlet propagated = relay(Result::Err(\"bad\"));\r\n"; let library = "struct Box { value: T }\npub fn make() -> Result, Box> { Result::Ok({ value: 42 }) }\nlet failure: Result = Result::Err(\"bad\");\n"; let overrides = json!([{"path":"./lib.rss", "source":library}]).to_string(); let payload = response(local_type_hints_json_with_context_response( @@ -63,13 +63,13 @@ fn result_public_hints_preserve_recursive_types_module_identity_and_arm_bindings assert_eq!( hint(&payload, "nested")["schema"], result_schema( - json!({"kind":"array", "item":{"kind":"optional", "inner":basic}}), - result_schema(json!({"kind":"optional", "inner":string}), int.clone()) + json!({"kind":"array", "item":{"kind":"option", "inner":basic}}), + result_schema(json!({"kind":"option", "inner":string}), int.clone()) ) ); assert_eq!( hint(&payload, "optional")["schema"], - json!({"kind":"optional", "inner":basic}) + json!({"kind":"option", "inner":basic}) ); assert_eq!(hint(&payload, "number")["schema"], int); let outer = result_schema(string.clone(), string.clone()); diff --git a/src/builtins/catalog.rs b/src/builtins/catalog.rs index 0426e250..6e4fa4ce 100644 --- a/src/builtins/catalog.rs +++ b/src/builtins/catalog.rs @@ -12,8 +12,8 @@ // // | Block | Range | Purpose | // |---|---|---| -// | extension | 0x0000 ..= 0xFF8F | reserved for future builtins and host imports | -// | special-call | 0xFF90 ..= 0xFFA1 | special-call builtins (incl. internal lowering builtins) | +// | extension | 0x0000 ..= 0xFF8B | reserved for future builtins and host imports | +// | special-call | 0xFF8C ..= 0xFFA1 | special-call builtins (incl. internal lowering builtins) | // | ordinary | 0xFFA2 ..= 0xFFFF | ordinary builtins (language + namespaced) | // // # Rules @@ -145,3 +145,7 @@ builtin_id!(0xFF96, "__map_iter_take_value", MapIterTakeValue, Internal, none); builtin_id!(0xFF95, "__map_iter_close", MapIterClose, Internal, none); builtin_id!(0xFF94, "__bind_callable", BindCallable, Internal, none); builtin_id!(0xFF93, "__detach_local", DetachLocal, Internal, none); +builtin_id!(0xFF8C, "__json_decode_typed", JsonDecodeTyped, Internal, none); +builtin_id!(0xFF8D, "__json_encode_typed", JsonEncodeTyped, Internal, none); +builtin_id!(0xFF8E, "__logical_render", LogicalRender, Internal, none); +builtin_id!(0xFF8F, "__map_edit", MapEdit, Internal, none); diff --git a/src/builtins/metadata.rs b/src/builtins/metadata.rs index 0ae7f518..95c44c51 100644 --- a/src/builtins/metadata.rs +++ b/src/builtins/metadata.rs @@ -2,6 +2,8 @@ pub enum CallableParamType { Any, Null, + /// Logical guest `()`; the physical carrier is the internal Null value. + Unit, Int, Float, Bool, @@ -25,6 +27,7 @@ impl CallableParamType { match self { Self::Any => "any", Self::Null => "null", + Self::Unit => "()", Self::Int => "int", Self::Float => "float", Self::Bool => "bool", diff --git a/src/builtins/runtime/core.rs b/src/builtins/runtime/core.rs index c07d5163..314eb975 100644 --- a/src/builtins/runtime/core.rs +++ b/src/builtins/runtime/core.rs @@ -440,6 +440,44 @@ pub(super) fn builtin_type_of_impl(value: VmValueRef<'_>) -> String { .to_string() } +/// Compiler-private typed JSON decode. Not language-visible. +#[pd_host_function(name = "__json_decode_typed")] +pub(crate) fn builtin_json_decode_typed_impl( + vm: &mut crate::vm::Vm, + text: &str, + descriptor: super::typed::VmBytesRef<'_>, +) -> VmResult { + let raw = super::json::decode_json_text(text)?; + super::logical_json::project_decoded(&raw, descriptor, json_names(vm)) +} + +/// Compiler-private typed JSON encode. Not language-visible. +#[pd_host_function(name = "__json_encode_typed")] +pub(crate) fn builtin_json_encode_typed_impl( + vm: &mut crate::vm::Vm, + value: VmValueRef<'_>, + descriptor: super::typed::VmBytesRef<'_>, +) -> VmResult { + super::logical_json::encode_typed(value, descriptor, json_names(vm)) +} + +/// Compiler-private logical display. Not language-visible. +#[pd_host_function(name = "__logical_render")] +pub(crate) fn builtin_logical_render_impl( + vm: &mut crate::vm::Vm, + value: VmValueRef<'_>, + descriptor: super::typed::VmBytesRef<'_>, +) -> VmResult { + super::logical_json::render_typed(value, descriptor, json_names(vm)) +} + +fn json_names(vm: &crate::vm::Vm) -> super::logical_json::JsonNames<'_> { + super::logical_json::JsonNames { + host: vm.host.named_struct_schemas.as_ref(), + guest: &vm.program().named_struct_decls, + } +} + /// Convert a value into a display string. #[pd_host_function(name = "__to_string")] pub(crate) fn builtin_to_string_impl(value: VmValueRef<'_>) -> String { @@ -742,6 +780,52 @@ pub(super) fn builtin_set(args: &mut [Value]) -> VmResult { builtin_set_owned(container, key, value).map(return_one) } +/// Insert or remove one map entry and return the updated map. +/// +/// `remove` is the only deletion signal. A null value is stored when +/// `remove` is false, so Unit data is not mistaken for a missing key. +pub(crate) fn builtin_map_edit_owned( + mut entries: SharedMap, + key: AnyValue, + value: AnyValue, + remove: bool, +) -> VmResult { + ensure_supported_map_key(&key)?; + let entries_mut = Arc::make_mut(&mut entries); + if remove { + entries_mut.remove(&key); + } else { + entries_mut.insert(key, value); + } + Ok(entries) +} + +/// Compiler-private map insert or removal. Not language-visible. +#[pd_host_function(name = "__map_edit")] +pub(crate) fn builtin_map_edit_impl( + entries: VmMapHandle, + key: VmValueOwned, + value: VmValueOwned, + remove: bool, +) -> VmResult { + builtin_map_edit_owned(entries, key, value, remove) +} + +pub(super) fn execute_map_edit(args: &mut [Value]) -> VmResult { + let container = take_arg(args, 0, "map edit container")?; + let key = take_arg(args, 1, "map edit key")?; + let value = take_arg(args, 2, "map edit value")?; + let remove = take_arg(args, 3, "map edit remove")?; + let Value::Map(entries) = container else { + return Err(VmError::TypeMismatch("map")); + }; + let Value::Bool(remove) = remove else { + return Err(VmError::TypeMismatch("bool")); + }; + builtin_map_edit_owned(entries, key, value, remove) + .map(|entries| return_one(Value::Map(entries))) +} + /// Return an array of container keys or indices. #[pd_host_function(name = "keys")] pub(super) fn builtin_keys_array_impl(items: VmArrayRef<'_>) -> VmArray { @@ -921,6 +1005,10 @@ pub(super) fn core_host_module() -> super::host_modules::StandardHostModule { builtin_format_template_descriptor, builtin_set_array_descriptor, builtin_set_map_descriptor, + builtin_map_edit_descriptor, + builtin_json_decode_typed_descriptor, + builtin_json_encode_typed_descriptor, + builtin_logical_render_descriptor, builtin_keys_array_descriptor, builtin_keys_map_descriptor, builtin_count_array_descriptor, @@ -955,6 +1043,7 @@ mod tests { (BuiltinFunction::MapIterClose, 0xFF95), (BuiltinFunction::BindCallable, 0xFF94), (BuiltinFunction::DetachLocal, 0xFF93), + (BuiltinFunction::MapEdit, 0xFF8F), ]; for (builtin, index) in reserved { assert_eq!(builtin.call_index(), index); diff --git a/src/builtins/runtime/io/async_io.rs b/src/builtins/runtime/io/async_io.rs index 817e4a4e..b407ec37 100644 --- a/src/builtins/runtime/io/async_io.rs +++ b/src/builtins/runtime/io/async_io.rs @@ -279,6 +279,13 @@ pub(crate) fn io_file_resource() -> crate::host_extension::HostResourceTypeMeta } impl HostResource for IoResource { + fn resource_type_key() -> Option { + Some( + crate::host_api::ResourceTypeKey::new(super::IO_FILE_KEY) + .expect("the io.file resource key is a valid static key"), + ) + } + fn begin_close(&mut self, _reason: ResourceCloseReason) -> ResourceResult { self.begin_close_after_operations_quiesce() } diff --git a/src/builtins/runtime/io/blocking.rs b/src/builtins/runtime/io/blocking.rs index d895d0d5..52ceed40 100644 --- a/src/builtins/runtime/io/blocking.rs +++ b/src/builtins/runtime/io/blocking.rs @@ -97,6 +97,13 @@ pub(crate) fn io_file_resource() -> crate::host_extension::HostResourceTypeMeta } impl HostResource for IoResource { + fn resource_type_key() -> Option { + Some( + crate::host_api::ResourceTypeKey::new(super::IO_FILE_KEY) + .expect("the io.file resource key is a valid static key"), + ) + } + fn begin_close(&mut self, _reason: ResourceCloseReason) -> ResourceResult { self.close_explicit().map_err(|error| { ResourceError::new( diff --git a/src/builtins/runtime/jit.rs b/src/builtins/runtime/jit.rs index 2b7c9522..2d49b597 100644 --- a/src/builtins/runtime/jit.rs +++ b/src/builtins/runtime/jit.rs @@ -253,7 +253,7 @@ pub(super) fn jit_host_module() -> super::host_modules::StandardHostModule { } } -/// Registers `jit::get_config` / `jit::set_config` from [`standard_host_catalog`]. +/// Registers `jit::get_config` / `jit::set_config` from `standard_host_catalog`. pub fn register_jit_builtin_module(registry: &mut HostFunctionRegistry) -> VmResult<()> { let catalog = crate::builtins::runtime::standard_host_catalog(); register_jit_builtin_module_from_catalog(registry, catalog.as_ref()) diff --git a/src/builtins/runtime/json.rs b/src/builtins/runtime/json.rs index d40a5179..e9c5b252 100644 --- a/src/builtins/runtime/json.rs +++ b/src/builtins/runtime/json.rs @@ -15,14 +15,18 @@ pub(super) fn builtin_json_encode(value: &AnyValue) -> VmResult { .map_err(|err| VmError::HostError(format!("json_encode failed: {err}"))) } +pub(super) fn decode_json_text(text: &str) -> VmResult { + let json_value = serde_json::from_str::(text) + .map_err(|err| VmError::HostError(format!("json_decode failed: {err}")))?; + Ok(json_value.0) +} + /// Decodes a JSON string into a `Value`. /// Note: This function enforces a strict unique-keys contract for JSON objects. /// If the JSON string contains duplicate keys, it will return an error instead of letting the last key win. #[pd_host_function(name = "json::decode")] pub(super) fn builtin_json_decode(text: &str) -> VmResult { - let json_value = serde_json::from_str::(text) - .map_err(|err| VmError::HostError(format!("json_decode failed: {err}")))?; - Ok(json_value.0) + decode_json_text(text) } fn vm_to_json_value(value: &Value) -> VmResult { diff --git a/src/builtins/runtime/logical_json.rs b/src/builtins/runtime/logical_json.rs new file mode 100644 index 00000000..7d9815a5 --- /dev/null +++ b/src/builtins/runtime/logical_json.rs @@ -0,0 +1,932 @@ +//! Schema-directed JSON projection for compiler-private typed decode/encode. +//! +//! The public parser stays the strict unique-key visitor. This module only +//! projects that raw value onto a lossless `TypeSchema`, and encodes a typed +//! value back to natural JSON. + +use serde_json::{Map as JsonMap, Number as JsonNumber, Value as JsonValue}; + +use crate::compiler::ir::{StructDecl, TypeSchema}; +use crate::option::{none_carrier, payload, some_carrier}; +use crate::vm::{Value, VmError, VmResult}; + +const MAX_JSON_DEPTH: usize = 64; +const MAX_JSON_NODES: usize = crate::host_api::MAX_HOST_SCHEMA_NODES; + +pub(super) struct JsonNames<'a> { + pub host: &'a std::collections::HashMap, + pub guest: &'a std::collections::HashMap, +} + +pub(super) fn project_decoded( + raw: &Value, + descriptor: &[u8], + names: JsonNames<'_>, +) -> VmResult { + let schema = prepare_json_schema(descriptor, &names)?; + charge_value(raw, &mut 0)?; + project_value(raw, &schema, Position::Value, 0, &names) +} + +pub(super) fn encode_typed( + value: &Value, + descriptor: &[u8], + names: JsonNames<'_>, +) -> VmResult { + let schema = prepare_json_schema(descriptor, &names)?; + charge_value(value, &mut 0)?; + let json = encode_value(value, &schema, Position::Value, 0, &names)?; + serde_json::to_string(&json) + .map_err(|err| VmError::HostError(format!("typed json encode: {err}"))) +} + +fn decode_schema(descriptor: &[u8]) -> VmResult { + crate::vmbc::decode_type_schema_bytes(descriptor) + .map_err(|err| VmError::HostError(format!("typed json descriptor: {err}"))) +} + +fn prepare_json_schema(descriptor: &[u8], names: &JsonNames<'_>) -> VmResult { + let schema = decode_schema(descriptor)?; + let mut nodes = 0; + let mut stack = Vec::new(); + let expanded = expand_named(&schema, names, &mut stack, &mut nodes)?; + admit_json_schema(&expanded, &mut 0)?; + Ok(expanded) +} + +fn expand_named( + schema: &TypeSchema, + names: &JsonNames<'_>, + stack: &mut Vec, + nodes: &mut usize, +) -> VmResult { + charge_node(nodes)?; + match schema { + TypeSchema::Named(name, args) => { + let key = format!("{name}/{}", args.len()); + let mut rendered_args = String::new(); + for arg in args { + rendered_args.push_str(&format!("/{arg:?}")); + } + let key = format!("{key}{rendered_args}"); + if stack.iter().any(|seen| seen == &key) { + return Ok(schema.clone()); + } + let reentries = stack + .iter() + .filter(|seen| seen.starts_with(&format!("{name}/"))) + .count(); + if reentries >= 2 { + return Ok(schema.clone()); + } + let Some(body) = + crate::vm::resolve_named_struct_body(names.host, names.guest, name, args) + else { + return Err(VmError::HostError(format!( + "typed json named schema '{name}' is not in the host catalog or guest program" + ))); + }; + stack.push(key); + let expanded = expand_named(&body, names, stack, nodes)?; + stack.pop(); + Ok(expanded) + } + TypeSchema::Option(inner) => Ok(TypeSchema::Option(Box::new(expand_named( + inner, names, stack, nodes, + )?))), + TypeSchema::Array(inner) => Ok(TypeSchema::Array(Box::new(expand_named( + inner, names, stack, nodes, + )?))), + TypeSchema::Map(inner) => Ok(TypeSchema::Map(Box::new(expand_named( + inner, names, stack, nodes, + )?))), + TypeSchema::Optional(inner) => Ok(TypeSchema::Optional(Box::new(expand_named( + inner, names, stack, nodes, + )?))), + TypeSchema::ArrayTuple(items) => Ok(TypeSchema::ArrayTuple( + items + .iter() + .map(|item| expand_named(item, names, stack, nodes)) + .collect::>>()?, + )), + TypeSchema::Object(fields) => { + let mut expanded = std::collections::HashMap::new(); + for (name, field) in fields { + expanded.insert(name.clone(), expand_named(field, names, stack, nodes)?); + } + Ok(TypeSchema::Object(expanded)) + } + TypeSchema::Result { ok, err } => Ok(TypeSchema::Result { + ok: Box::new(expand_named(ok, names, stack, nodes)?), + err: Box::new(expand_named(err, names, stack, nodes)?), + }), + TypeSchema::Callable { params, result } => Ok(TypeSchema::Callable { + params: params + .iter() + .map(|param| expand_named(param, names, stack, nodes)) + .collect::>>()?, + result: Box::new(expand_named(result, names, stack, nodes)?), + }), + TypeSchema::ArrayTupleRest { prefix, rest } => Ok(TypeSchema::ArrayTupleRest { + prefix: prefix + .iter() + .map(|item| expand_named(item, names, stack, nodes)) + .collect::>>()?, + rest: Box::new(expand_named(rest, names, stack, nodes)?), + }), + other => Ok(other.clone()), + } +} + +fn admit_json_schema(schema: &TypeSchema, nodes: &mut usize) -> VmResult<()> { + charge_node(nodes)?; + match schema { + TypeSchema::Result { .. } + | TypeSchema::Bytes + | TypeSchema::Callable { .. } + | TypeSchema::Resource(_) + | TypeSchema::Null + | TypeSchema::Optional(_) + | TypeSchema::Unknown + | TypeSchema::GenericParam(_) => Err(unsupported(schema)), + TypeSchema::Named(_, _) => Ok(()), + TypeSchema::Option(inner) | TypeSchema::Array(inner) | TypeSchema::Map(inner) => { + admit_json_schema(inner, nodes) + } + TypeSchema::ArrayTuple(items) => { + for item in items { + admit_json_schema(item, nodes)?; + } + Ok(()) + } + TypeSchema::ArrayTupleRest { prefix, rest } => { + for item in prefix { + admit_json_schema(item, nodes)?; + } + admit_json_schema(rest, nodes) + } + TypeSchema::Object(fields) => { + for field in fields.values() { + admit_json_schema(field, nodes)?; + } + Ok(()) + } + TypeSchema::Int + | TypeSchema::Bool + | TypeSchema::Float + | TypeSchema::Number + | TypeSchema::String + | TypeSchema::Unit => Ok(()), + } +} + +fn charge_node(nodes: &mut usize) -> VmResult<()> { + *nodes += 1; + if *nodes > MAX_JSON_NODES { + Err(VmError::HostError( + "typed json schema exceeds the node budget".to_string(), + )) + } else { + Ok(()) + } +} + +fn charge_value(value: &Value, nodes: &mut usize) -> VmResult<()> { + charge_node(nodes)?; + match value { + Value::Array(values) => { + for value in values.iter() { + charge_value(value, nodes)?; + } + Ok(()) + } + Value::Map(entries) => { + for (key, value) in entries.iter() { + charge_value(key, nodes)?; + charge_value(value, nodes)?; + } + Ok(()) + } + _ => Ok(()), + } +} + +#[derive(Clone, Copy, PartialEq, Eq)] +enum Position { + Value, + Field, +} + +fn project_value( + raw: &Value, + schema: &TypeSchema, + position: Position, + depth: usize, + names: &JsonNames<'_>, +) -> VmResult { + let depth = next_depth(depth)?; + match schema { + TypeSchema::Named(name, args) => { + let Some(body) = + crate::vm::resolve_named_struct_body(names.host, names.guest, name, args) + else { + return Err(unsupported(schema)); + }; + project_value(raw, &body, position, depth, names) + } + TypeSchema::Option(inner) => project_option(raw, inner, position, depth, names), + TypeSchema::Int => project_int(raw), + TypeSchema::Bool => match raw { + Value::Bool(value) => Ok(Value::Bool(*value)), + _ => Err(VmError::TypeMismatch("json bool")), + }, + TypeSchema::Float | TypeSchema::Number => project_float(raw), + TypeSchema::String => match raw { + Value::String(value) => Ok(Value::String(value.clone())), + _ => Err(VmError::TypeMismatch("json string")), + }, + TypeSchema::Unit => match raw { + Value::Null => Ok(Value::Null), + _ => Err(VmError::TypeMismatch("json unit")), + }, + TypeSchema::Array(inner) => project_array(raw, inner, depth, names), + TypeSchema::ArrayTuple(items) => project_tuple(raw, items, depth, names), + TypeSchema::Map(inner) => project_map(raw, inner, depth, names), + TypeSchema::Object(fields) => project_object(raw, fields, depth, names), + other => Err(unsupported(other)), + } +} + +fn project_option( + raw: &Value, + inner: &TypeSchema, + position: Position, + depth: usize, + names: &JsonNames<'_>, +) -> VmResult { + if position == Position::Value && null_ambiguous(inner) { + return Err(ambiguous_option()); + } + if matches!(raw, Value::Null) { + return Ok(none_carrier()); + } + Ok(some_carrier(project_value( + raw, + inner, + Position::Value, + depth, + names, + )?)) +} + +fn project_int(raw: &Value) -> VmResult { + match raw { + Value::Int(value) => Ok(Value::Int(*value)), + Value::Float(value) => exact_i64(*value) + .map(Value::Int) + .ok_or(VmError::TypeMismatch("json int")), + _ => Err(VmError::TypeMismatch("json int")), + } +} + +fn exact_i64(value: f64) -> Option { + if !value.is_finite() || value.fract() != 0.0 { + return None; + } + // i64::MAX is not an exact f64. Values at or above 2^63 are not i64. + if value < i64::MIN as f64 || value >= 9223372036854775808.0 { + return None; + } + let converted = value as i64; + (converted as f64 == value).then_some(converted) +} + +fn project_float(raw: &Value) -> VmResult { + match raw { + Value::Float(value) if value.is_finite() => Ok(Value::Float(*value)), + Value::Int(value) => Ok(Value::Float(*value as f64)), + _ => Err(VmError::TypeMismatch("json number")), + } +} + +fn project_array( + raw: &Value, + inner: &TypeSchema, + depth: usize, + names: &JsonNames<'_>, +) -> VmResult { + let Value::Array(values) = raw else { + return Err(VmError::TypeMismatch("json array")); + }; + let mut out = Vec::with_capacity(values.len()); + for value in values.iter() { + out.push(project_value(value, inner, Position::Value, depth, names)?); + } + Ok(Value::array(out)) +} + +fn project_tuple( + raw: &Value, + items: &[TypeSchema], + depth: usize, + names: &JsonNames<'_>, +) -> VmResult { + let Value::Array(values) = raw else { + return Err(VmError::TypeMismatch("json tuple")); + }; + if values.len() != items.len() { + return Err(VmError::HostError(format!( + "typed json tuple length {}, schema expects {}", + values.len(), + items.len() + ))); + } + let mut out = Vec::with_capacity(items.len()); + for (value, schema) in values.iter().zip(items) { + out.push(project_value(value, schema, Position::Value, depth, names)?); + } + Ok(Value::array(out)) +} + +fn project_map( + raw: &Value, + inner: &TypeSchema, + depth: usize, + names: &JsonNames<'_>, +) -> VmResult { + if map_value_ambiguous(inner) { + return Err(VmError::HostError( + "typed json rejects Map> and other null-ambiguous map values".to_string(), + )); + } + let Value::Map(entries) = raw else { + return Err(VmError::TypeMismatch("json map")); + }; + let mut out = Vec::with_capacity(entries.len()); + for (key, value) in entries.iter() { + let Value::String(_) = key else { + return Err(VmError::HostError( + "typed json map keys must be strings".to_string(), + )); + }; + out.push(( + key.clone(), + project_value(value, inner, Position::Value, depth, names)?, + )); + } + Ok(Value::map(out)) +} + +fn project_object( + raw: &Value, + fields: &std::collections::HashMap, + depth: usize, + names: &JsonNames<'_>, +) -> VmResult { + let Value::Map(entries) = raw else { + return Err(VmError::TypeMismatch("json object")); + }; + let mut present = std::collections::HashMap::new(); + for (key, value) in entries.iter() { + let Value::String(name) = key else { + return Err(VmError::HostError( + "typed json object keys must be strings".to_string(), + )); + }; + if present.insert(name.as_str().to_string(), value).is_some() { + return Err(VmError::HostError(format!( + "typed json object has a duplicate key '{}'", + name.as_str() + ))); + } + } + let mut out = Vec::new(); + for (name, schema) in fields { + match present.get(name) { + Some(value) => { + out.push(( + Value::string(name.clone()), + project_field(value, schema, depth, names)?, + )); + } + None => out.push((Value::string(name.clone()), missing_field(schema)?)), + } + } + Ok(Value::map(out)) +} + +fn project_field( + raw: &Value, + schema: &TypeSchema, + depth: usize, + names: &JsonNames<'_>, +) -> VmResult { + match field_form(schema)? { + FieldForm::OrdinaryOption(inner) => { + if matches!(raw, Value::Null) { + Ok(none_carrier()) + } else { + Ok(some_carrier(project_value( + raw, + inner, + Position::Value, + depth, + names, + )?)) + } + } + FieldForm::OptionUnit => match raw { + Value::Null => Ok(some_carrier(Value::Null)), + _ => Err(VmError::TypeMismatch("json unit")), + }, + FieldForm::OptionOption(inner) => { + if matches!(raw, Value::Null) { + Ok(some_carrier(none_carrier())) + } else { + Ok(some_carrier(some_carrier(project_value( + raw, + inner, + Position::Value, + depth, + names, + )?))) + } + } + FieldForm::Value => project_value(raw, schema, Position::Field, depth, names), + } +} + +fn missing_field(schema: &TypeSchema) -> VmResult { + match field_form(schema)? { + FieldForm::OrdinaryOption(_) | FieldForm::OptionUnit | FieldForm::OptionOption(_) => { + Ok(none_carrier()) + } + FieldForm::Value => Err(VmError::HostError( + "typed json object is missing a required field".to_string(), + )), + } +} + +enum FieldForm<'a> { + OrdinaryOption(&'a TypeSchema), + OptionUnit, + OptionOption(&'a TypeSchema), + Value, +} + +fn field_form(schema: &TypeSchema) -> VmResult> { + match schema { + TypeSchema::Option(inner) => match inner.as_ref() { + TypeSchema::Unit => Ok(FieldForm::OptionUnit), + TypeSchema::Option(nested) => { + if null_ambiguous(nested) { + Err(ambiguous_option()) + } else { + Ok(FieldForm::OptionOption(nested)) + } + } + other if null_ambiguous(other) => Err(ambiguous_option()), + other => Ok(FieldForm::OrdinaryOption(other)), + }, + other + if matches!( + other, + TypeSchema::Null + | TypeSchema::Optional(_) + | TypeSchema::Unknown + | TypeSchema::GenericParam(_) + | TypeSchema::Bytes + | TypeSchema::Callable { .. } + | TypeSchema::Resource(_) + | TypeSchema::Result { .. } + ) => + { + Err(unsupported(other)) + } + _ => Ok(FieldForm::Value), + } +} + +fn encode_value( + value: &Value, + schema: &TypeSchema, + position: Position, + depth: usize, + names: &JsonNames<'_>, +) -> VmResult { + let depth = next_depth(depth)?; + match schema { + TypeSchema::Named(name, args) => { + let Some(body) = + crate::vm::resolve_named_struct_body(names.host, names.guest, name, args) + else { + return Err(unsupported(schema)); + }; + encode_value(value, &body, position, depth, names) + } + TypeSchema::Option(inner) => encode_option(value, inner, position, depth, names), + TypeSchema::Int => match value { + Value::Int(number) => Ok(JsonValue::Number((*number).into())), + _ => Err(VmError::TypeMismatch("json int")), + }, + TypeSchema::Bool => match value { + Value::Bool(flag) => Ok(JsonValue::Bool(*flag)), + _ => Err(VmError::TypeMismatch("json bool")), + }, + TypeSchema::Float | TypeSchema::Number => encode_float(value), + TypeSchema::String => match value { + Value::String(text) => Ok(JsonValue::String(text.as_str().to_string())), + _ => Err(VmError::TypeMismatch("json string")), + }, + TypeSchema::Unit => match value { + Value::Null => Ok(JsonValue::Null), + _ => Err(VmError::TypeMismatch("json unit")), + }, + TypeSchema::Array(inner) => encode_array( + value, + std::slice::from_ref(inner.as_ref()), + true, + depth, + names, + ), + TypeSchema::ArrayTuple(items) => encode_array(value, items, false, depth, names), + TypeSchema::Map(inner) => encode_map(value, inner, depth, names), + TypeSchema::Object(fields) => encode_object(value, fields, depth, names), + other => Err(unsupported(other)), + } +} + +fn encode_option( + value: &Value, + inner: &TypeSchema, + position: Position, + depth: usize, + names: &JsonNames<'_>, +) -> VmResult { + if position == Position::Value && null_ambiguous(inner) { + return Err(ambiguous_option()); + } + let (some, payload_value) = payload(value)?; + if !some { + return Ok(JsonValue::Null); + } + let Some(payload_value) = payload_value else { + return Err(VmError::TypeMismatch("Option Some payload")); + }; + encode_value(payload_value, inner, Position::Value, depth, names) +} + +fn encode_float(value: &Value) -> VmResult { + let number = match value { + Value::Float(number) if number.is_finite() => *number, + Value::Int(number) => *number as f64, + _ => return Err(VmError::TypeMismatch("json number")), + }; + Ok(JsonValue::Number(JsonNumber::from_f64(number).ok_or_else( + || VmError::HostError("typed json encode does not support NaN or infinity".to_string()), + )?)) +} + +fn encode_array( + value: &Value, + items: &[TypeSchema], + repeat: bool, + depth: usize, + names: &JsonNames<'_>, +) -> VmResult { + let Value::Array(values) = value else { + return Err(VmError::TypeMismatch("json array")); + }; + if !repeat && values.len() != items.len() { + return Err(VmError::HostError(format!( + "typed json tuple length {}, schema expects {}", + values.len(), + items.len() + ))); + } + let mut out = Vec::with_capacity(values.len()); + for (index, value) in values.iter().enumerate() { + let schema = if repeat { &items[0] } else { &items[index] }; + out.push(encode_value(value, schema, Position::Value, depth, names)?); + } + Ok(JsonValue::Array(out)) +} + +fn encode_map( + value: &Value, + inner: &TypeSchema, + depth: usize, + names: &JsonNames<'_>, +) -> VmResult { + if map_value_ambiguous(inner) { + return Err(VmError::HostError( + "typed json rejects Map> and other null-ambiguous map values".to_string(), + )); + } + let Value::Map(entries) = value else { + return Err(VmError::TypeMismatch("json map")); + }; + let mut out = JsonMap::new(); + for (key, value) in entries.iter() { + let Value::String(key) = key else { + return Err(VmError::HostError( + "typed json map keys must be strings".to_string(), + )); + }; + let encoded = encode_value(value, inner, Position::Value, depth, names)?; + out.insert(key.as_str().to_string(), encoded); + } + Ok(JsonValue::Object(out)) +} + +fn encode_object( + value: &Value, + fields: &std::collections::HashMap, + depth: usize, + names: &JsonNames<'_>, +) -> VmResult { + let Value::Map(entries) = value else { + return Err(VmError::TypeMismatch("json object")); + }; + let mut present = std::collections::HashMap::new(); + for (key, value) in entries.iter() { + let Value::String(name) = key else { + return Err(VmError::HostError( + "typed json object keys must be strings".to_string(), + )); + }; + present.insert(name.as_str().to_string(), value); + } + let mut out = JsonMap::new(); + for (name, schema) in fields { + let encoded = match present.get(name) { + Some(value) => encode_field(value, schema, depth, names)?, + None => match field_form(schema)? { + FieldForm::OrdinaryOption(_) + | FieldForm::OptionUnit + | FieldForm::OptionOption(_) => None, + FieldForm::Value => { + return Err(VmError::HostError( + "typed json object is missing a required field".to_string(), + )); + } + }, + }; + if let Some(encoded) = encoded { + out.insert(name.clone(), encoded); + } + } + Ok(JsonValue::Object(out)) +} + +fn encode_field( + value: &Value, + schema: &TypeSchema, + depth: usize, + names: &JsonNames<'_>, +) -> VmResult> { + match field_form(schema)? { + FieldForm::OrdinaryOption(inner) => { + let (some, payload_value) = payload(value)?; + if !some { + return Ok(None); + } + let Some(payload_value) = payload_value else { + return Err(VmError::TypeMismatch("Option Some payload")); + }; + Ok(Some(encode_value( + payload_value, + inner, + Position::Value, + depth, + names, + )?)) + } + FieldForm::OptionUnit => { + let (some, payload_value) = payload(value)?; + if !some { + return Ok(None); + } + match payload_value { + Some(Value::Null) => Ok(Some(JsonValue::Null)), + Some(_) => Err(VmError::TypeMismatch("json unit")), + // A canonical writer may omit the Null-valued Unit payload. + None => Ok(Some(JsonValue::Null)), + } + } + FieldForm::OptionOption(inner) => { + let (outer_some, outer_payload) = payload(value)?; + if !outer_some { + return Ok(None); + } + let Some(outer_payload) = outer_payload else { + return Err(VmError::TypeMismatch("Option Some payload")); + }; + let (inner_some, inner_payload) = payload(outer_payload)?; + if !inner_some { + return Ok(Some(JsonValue::Null)); + } + let Some(inner_payload) = inner_payload else { + return Err(VmError::TypeMismatch("Option Some payload")); + }; + Ok(Some(encode_value( + inner_payload, + inner, + Position::Value, + depth, + names, + )?)) + } + FieldForm::Value => Ok(Some(encode_value( + value, + schema, + Position::Field, + depth, + names, + )?)), + } +} + +fn next_depth(depth: usize) -> VmResult { + if depth >= MAX_JSON_DEPTH { + return Err(VmError::HostError( + "typed json schema exceeds the nesting budget".to_string(), + )); + } + Ok(depth + 1) +} + +fn null_ambiguous(schema: &TypeSchema) -> bool { + matches!( + schema, + TypeSchema::Unit + | TypeSchema::Option(_) + | TypeSchema::Null + | TypeSchema::Optional(_) + | TypeSchema::Unknown + | TypeSchema::GenericParam(_) + ) +} + +fn map_value_ambiguous(schema: &TypeSchema) -> bool { + match schema { + TypeSchema::Option(inner) => null_ambiguous(inner), + TypeSchema::Null + | TypeSchema::Optional(_) + | TypeSchema::Unknown + | TypeSchema::GenericParam(_) => true, + _ => false, + } +} + +fn ambiguous_option() -> VmError { + VmError::HostError( + "typed json rejects value-position Option or nested Option".to_string(), + ) +} + +fn unsupported(schema: &TypeSchema) -> VmError { + VmError::HostError(format!("typed json has no natural encoding for {schema:?}")) +} + +pub(super) fn render_typed( + value: &Value, + descriptor: &[u8], + names: JsonNames<'_>, +) -> VmResult { + let schema = decode_schema(descriptor)?; + let mut nodes = 0; + let mut stack = Vec::new(); + let schema = expand_named(&schema, &names, &mut stack, &mut nodes)?; + render_value(value, &schema, 0, &names) +} + +fn render_value( + value: &Value, + schema: &TypeSchema, + depth: usize, + names: &JsonNames<'_>, +) -> VmResult { + let depth = next_depth(depth)?; + match schema { + TypeSchema::Named(name, args) => { + let Some(body) = + crate::vm::resolve_named_struct_body(names.host, names.guest, name, args) + else { + return Err(unsupported(schema)); + }; + render_value(value, &body, depth, names) + } + TypeSchema::Option(inner) => { + let (some, payload_value) = payload(value)?; + if !some { + return Ok("None".to_string()); + } + let Some(payload_value) = payload_value else { + // Only the authoritative Unit schema may restore an omitted + // Null-valued payload; every other payload stays malformed. + if matches!(inner.as_ref(), TypeSchema::Unit) { + return Ok("Some(())".to_string()); + } + return Err(VmError::TypeMismatch("Option Some payload")); + }; + let rendered = render_value(payload_value, inner, depth, names)?; + Ok(format!("Some({rendered})")) + } + TypeSchema::Unit => match value { + Value::Null => Ok("()".to_string()), + _ => Err(VmError::TypeMismatch("json unit")), + }, + TypeSchema::Int => match value { + Value::Int(number) => Ok(number.to_string()), + _ => Err(VmError::TypeMismatch("json int")), + }, + TypeSchema::Bool => match value { + Value::Bool(flag) => Ok(flag.to_string()), + _ => Err(VmError::TypeMismatch("json bool")), + }, + TypeSchema::String => match value { + Value::String(text) => Ok(text.as_str().to_string()), + _ => Err(VmError::TypeMismatch("json string")), + }, + TypeSchema::Float | TypeSchema::Number => match value { + Value::Float(number) if number.is_finite() => Ok(number.to_string()), + Value::Int(number) => Ok((*number as f64).to_string()), + _ => Err(VmError::TypeMismatch("json number")), + }, + TypeSchema::Bytes => match value { + Value::Bytes(bytes) => Ok(render_bytes(bytes.as_ref())), + _ => Err(VmError::TypeMismatch("bytes")), + }, + TypeSchema::Resource(_) => match value { + Value::Int(token) => Ok(format!("resource<{token}>")), + _ => Err(VmError::TypeMismatch("resource")), + }, + TypeSchema::Callable { .. } => match value { + Value::Callable(callable) => Ok(format!("", callable.prototype_id)), + _ => Err(VmError::TypeMismatch("callable")), + }, + TypeSchema::Result { ok, err } => { + let (is_ok, payload_value) = crate::result::payload(value)?; + let rendered = render_value(payload_value, if is_ok { ok } else { err }, depth, names)?; + Ok(if is_ok { + format!("Ok({rendered})") + } else { + format!("Err({rendered})") + }) + } + TypeSchema::Array(inner) => match value { + Value::Array(values) => { + let mut parts = Vec::with_capacity(values.len()); + for value in values.iter() { + parts.push(render_value(value, inner, depth, names)?); + } + Ok(format!("[{}]", parts.join(", "))) + } + _ => Err(VmError::TypeMismatch("array")), + }, + TypeSchema::Map(inner) => match value { + Value::Map(entries) => { + let mut parts = Vec::with_capacity(entries.len()); + for (key, value) in entries.iter() { + let Value::String(key) = key else { + return Err(VmError::TypeMismatch("map key")); + }; + parts.push(format!( + "{}: {}", + key.as_str(), + render_value(value, inner, depth, names)? + )); + } + Ok(format!("{{{}}}", parts.join(", "))) + } + _ => Err(VmError::TypeMismatch("map")), + }, + TypeSchema::Object(fields) => match value { + Value::Map(entries) => { + let mut parts = Vec::new(); + for (name, field) in fields { + let Some(field_value) = entries.get(&Value::string(name)) else { + continue; + }; + parts.push(format!( + "{name}: {}", + render_value(field_value, field, depth, names)? + )); + } + Ok(format!("{{{}}}", parts.join(", "))) + } + _ => Err(VmError::TypeMismatch("object")), + }, + other => Err(unsupported(other)), + } +} + +fn render_bytes(bytes: &[u8]) -> String { + let parts = bytes + .iter() + .map(|byte| format!("{byte:02x}")) + .collect::>() + .join(" "); + format!("b[{parts}]") +} diff --git a/src/builtins/runtime/mod.rs b/src/builtins/runtime/mod.rs index bfb1b13b..9f6046ce 100644 --- a/src/builtins/runtime/mod.rs +++ b/src/builtins/runtime/mod.rs @@ -26,6 +26,7 @@ pub(crate) mod http; mod io; mod jit; mod json; +mod logical_json; mod map_iter; mod math; pub(crate) mod print; @@ -99,7 +100,7 @@ pub fn standard_host_catalog_fingerprint() -> HostApiFingerprint { standard_host_catalog().fingerprint() } -/// The standard host modules for this build (see [`host_modules`]). +/// The standard host modules for this build (see `host_modules`). pub fn standard_host_modules() -> &'static [StandardHostModule] { host_modules::standard_host_modules() } @@ -122,8 +123,8 @@ pub use typed::HostCallResult; use typed::{AnyValue, IntoBuiltinCallOutcome, NumberValue, UnknownValue, VmArray, VmBytes, VmMap}; #[allow(unused_imports)] pub use typed::{ - BorrowVmValue, FromVmValue, IntoHostCallOutcome, IntoVmValue, TakeVmValue, arg, borrow_arg, - return_none, return_one, take_arg, + BorrowVmValue, FromVmValue, IntoHostCallOutcome, IntoVmValue, TakeVmValue, VmCallable, arg, + borrow_arg, return_none, return_one, take_arg, }; pub(crate) enum BuiltinCallOutcome { @@ -159,6 +160,7 @@ pub(crate) fn execute_builtin_call( BuiltinFunction::Get => core::builtin_get(args).map(BuiltinCallOutcome::Return), BuiltinFunction::Has => core::builtin_has(args).map(BuiltinCallOutcome::Return), BuiltinFunction::Set => core::builtin_set(args).map(BuiltinCallOutcome::Return), + BuiltinFunction::MapEdit => core::execute_map_edit(args).map(BuiltinCallOutcome::Return), BuiltinFunction::Keys => core::builtin_keys(args).map(BuiltinCallOutcome::Return), BuiltinFunction::Count => core::builtin_count(args).map(BuiltinCallOutcome::Return), BuiltinFunction::MapIterInit => map_iter::init(vm, args).map(BuiltinCallOutcome::Return), @@ -208,6 +210,12 @@ pub(crate) fn execute_builtin_call( BuiltinFunction::ToString => { core::builtin_to_string(args).map(IntoBuiltinCallOutcome::into_builtin_call_outcome) } + BuiltinFunction::JsonDecodeTyped => core::builtin_json_decode_typed(vm, args) + .map(IntoBuiltinCallOutcome::into_builtin_call_outcome), + BuiltinFunction::JsonEncodeTyped => core::builtin_json_encode_typed(vm, args) + .map(IntoBuiltinCallOutcome::into_builtin_call_outcome), + BuiltinFunction::LogicalRender => core::builtin_logical_render(vm, args) + .map(IntoBuiltinCallOutcome::into_builtin_call_outcome), BuiltinFunction::TypeOf => { core::builtin_type_of(args).map(IntoBuiltinCallOutcome::into_builtin_call_outcome) } diff --git a/src/builtins/runtime/regex.rs b/src/builtins/runtime/regex.rs index 8437fd8c..6b58977b 100644 --- a/src/builtins/runtime/regex.rs +++ b/src/builtins/runtime/regex.rs @@ -16,6 +16,7 @@ use std::sync::Arc; use regex::Regex; use super::VmArray; +use crate::IntoVmValue; use crate::vm::{HostStateMut, Value, Vm, VmError, VmResult}; use pd_host_function::pd_host_function; @@ -55,14 +56,20 @@ pub(crate) fn native_re_match(vm: &mut Vm, pattern: &str, text: &str) -> VmResul } /// Returns the first substring matched by a regular expression. +/// +/// The guest result is a logical `Option`: a missing match is +/// `Option::None`, while an empty match stays `Option::Some("")`. #[pd_host_function(name = "re::find")] pub(super) fn builtin_re_find( mut cache: HostStateMut<'_, RegexCache>, pattern: &str, text: &str, -) -> VmResult> { +) -> VmResult> { let regex = cached_regex(&mut cache, "re_find", pattern)?; - Ok(regex.find(text).map(|matched| matched.as_str().to_string())) + Ok(regex + .find(text) + .map(|matched| matched.as_str().to_string()) + .into()) } /// Replaces all regular-expression matches in a string. @@ -88,8 +95,25 @@ pub(crate) fn native_re_replace( }) } +/// Guest contract for `re::split`. +/// +/// The native producer returns string elements; the declared contract carries +/// that element type into the installed descriptor because a raw `VmArray` +/// signature alone can only advertise an unknown element. +fn re_split_contract() -> crate::host_api::HostFunctionSchema { + use crate::host_api::{HostFunctionSchema, HostParamSchema, HostTypeSchema}; + HostFunctionSchema::with_return( + "re::split", + vec![ + HostParamSchema::value("pattern", HostTypeSchema::String), + HostParamSchema::value("text", HostTypeSchema::String), + ], + HostTypeSchema::Array(Box::new(HostTypeSchema::String)), + ) +} + /// Splits a string on regular-expression matches. -#[pd_host_function(name = "re::split")] +#[pd_host_function(name = "re::split", contract = re_split_contract)] pub(super) fn builtin_re_split( mut cache: HostStateMut<'_, RegexCache>, pattern: &str, @@ -102,8 +126,30 @@ pub(super) fn builtin_re_split( .collect::>()) } +/// Guest contract for `re::captures`. +/// +/// Group 0 and every present group are `Some`, unmatched groups are `None`. +/// A missing match stays an empty array rather than an Option of the array. +fn re_captures_contract() -> crate::host_api::HostFunctionSchema { + use crate::host_api::{HostFunctionSchema, HostParamSchema, HostTypeSchema}; + HostFunctionSchema::with_return( + "re::captures", + vec![ + HostParamSchema::value("pattern", HostTypeSchema::String), + HostParamSchema::value("text", HostTypeSchema::String), + ], + HostTypeSchema::Array(Box::new(HostTypeSchema::Option(Box::new( + HostTypeSchema::String, + )))), + ) +} + /// Returns the capture groups produced by the first regular-expression match. -#[pd_host_function(name = "re::captures")] +/// +/// Each entry is a logical `Option`: a present group (including an +/// empty one) is `Some`, an unmatched optional group is `None`. A missing +/// match returns an empty array. +#[pd_host_function(name = "re::captures", contract = re_captures_contract)] pub(super) fn builtin_re_captures( mut cache: HostStateMut<'_, RegexCache>, pattern: &str, @@ -117,8 +163,8 @@ pub(super) fn builtin_re_captures( let mut groups = Vec::with_capacity(captures.len()); for index in 0..captures.len() { let group_value = match captures.get(index) { - Some(group) => Value::string(group.as_str().to_string()), - None => Value::Null, + Some(group) => crate::GuestOption::Some(group.as_str().to_string()).into_vm_value(), + None => crate::GuestOption::::None.into_vm_value(), }; groups.push(group_value); } @@ -165,7 +211,7 @@ mod tests { "FoO" )) .expect("find should work"), - Some("FoO".to_string()) + crate::GuestOption::Some("FoO".to_string()) ); assert_eq!(vm.regex_cache_entry_count(), 1); diff --git a/src/builtins/runtime/sqlite.rs b/src/builtins/runtime/sqlite.rs index 5c9aa147..8c49505d 100644 --- a/src/builtins/runtime/sqlite.rs +++ b/src/builtins/runtime/sqlite.rs @@ -185,7 +185,7 @@ fn reserve_connection( limit: usize, ) -> VmResult { open_connections - .fetch_update(Ordering::AcqRel, Ordering::Acquire, |count| { + .try_update(Ordering::AcqRel, Ordering::Acquire, |count| { (count < limit).then_some(count + 1) }) .map_err(|_| VmError::HostError(format!("SQLite connection limit {limit} reached")))?; @@ -267,7 +267,7 @@ impl SqliteCloseLifecycle { #[cfg(test)] let inject_failure = self .injected_failures - .fetch_update(Ordering::AcqRel, Ordering::Acquire, |remaining| { + .try_update(Ordering::AcqRel, Ordering::Acquire, |remaining| { remaining.checked_sub(1) }) .is_ok(); @@ -483,7 +483,7 @@ impl SqliteConnectionContext { self.ensure_open()?; let limit = self.limits.max_pending_operations; self.in_flight - .fetch_update(Ordering::AcqRel, Ordering::Acquire, |count| { + .try_update(Ordering::AcqRel, Ordering::Acquire, |count| { (count < limit).then_some(count + 1) }) .map_err(|_| { @@ -708,11 +708,41 @@ fn required_string(map: &VmMap, key: &str) -> VmResult { } } -fn optional_string(map: &VmMap, key: &str) -> VmResult> { +/// Decodes one canonical logical `Option` carrier. A present `Null` scalar is +/// not a second reading of an optional public field. +fn logical_option_value(value: &Value) -> VmResult> { + let (some, payload) = crate::option::payload(value)?; + if !some { + return Ok(None); + } + payload + .map(Some) + .ok_or(VmError::TypeMismatch("SQLite Option payload")) +} + +fn logical_option_field<'a>(map: &'a VmMap, key: &str) -> VmResult> { match map_value(map, key) { + Some(value) => logical_option_value(value), + None => Ok(None), + } +} + +fn required_option_string(map: &VmMap, key: &str) -> VmResult { + match logical_option_field(map, key)? { + Some(Value::String(value)) if !value.is_empty() => Ok(value.as_ref().clone()), + Some(Value::String(_)) => Err(VmError::HostError(format!( + "SQLite {key} must not be empty" + ))), + Some(_) => Err(VmError::TypeMismatch("SQLite option string")), + None => Err(VmError::HostError(format!("missing SQLite {key}"))), + } +} + +fn optional_option_string(map: &VmMap, key: &str) -> VmResult> { + match logical_option_field(map, key)? { Some(Value::String(value)) => Ok(Some(value.as_ref().clone())), - Some(Value::Null) | None => Ok(None), Some(_) => Err(VmError::TypeMismatch("SQLite option string")), + None => Ok(None), } } @@ -740,24 +770,15 @@ fn parse_positive_u64(value: &Value, label: &str) -> VmResult { u64::try_from(*value).map_err(|_| VmError::HostError(format!("SQLite {label} is too large"))) } -fn parse_limits(value: Option<&Value>, ceiling: SqliteLimits) -> VmResult { - let Some(value) = value else { - return Ok(ceiling); - }; - if matches!(value, Value::Null) { - return Ok(ceiling); - } - let Value::Map(map) = value else { - return Err(VmError::TypeMismatch("SQLite limits map")); - }; +fn parse_limits(map: &VmMap, ceiling: SqliteLimits) -> VmResult { let mut limits = ceiling; for (key, value) in map.iter() { let Value::String(key) = key else { return Err(VmError::TypeMismatch("SQLite limit name")); }; - if matches!(value, Value::Null) { + let Some(value) = logical_option_value(value)? else { continue; - } + }; match key.as_str() { "max_connections" => { limits.max_connections = @@ -808,7 +829,7 @@ fn parse_limits(value: Option<&Value>, ceiling: SqliteLimits) -> VmResult VmResult { - parse_limits(Some(&Value::Map(Arc::new(value.clone()))), ceiling) + parse_limits(value, ceiling) } fn validate_relative_path(path: &Path) -> VmResult<()> { @@ -1103,13 +1124,6 @@ fn reject_unexpected_fields(map: &VmMap, allowed: &[&str], context: &'static str Ok(()) } -fn present_payload<'a>(map: &'a VmMap, key: &str) -> Option<&'a Value> { - match map_value(map, key) { - Some(Value::Null) | None => None, - Some(value) => Some(value), - } -} - fn sqlite_parameter_value( value: &Value, parameter_bytes: &mut usize, @@ -1136,38 +1150,43 @@ fn sqlite_parameter_value( } Some(_) => return Err(VmError::TypeMismatch("SQLite value kind")), }; - let payload_count = ["int_value", "float_value", "text_value", "blob_value"] + let int_value = logical_option_field(map, "int_value")?; + let float_value = logical_option_field(map, "float_value")?; + let text_value = logical_option_field(map, "text_value")?; + let blob_value = logical_option_field(map, "blob_value")?; + let payload_count = [int_value, float_value, text_value, blob_value] .into_iter() - .filter(|key| present_payload(map, key).is_some()) + .filter(Option::is_some) .count(); if payload_count > 1 { return Err(VmError::HostError( "SQLite value has multiple non-null payloads".to_string(), )); } - let selected_payload = match kind { - "null" => { - if payload_count != 0 { - return Err(VmError::HostError( - "SQLite null value must not have a payload".to_string(), - )); + let selected_payload = + match kind { + "null" => { + if payload_count != 0 { + return Err(VmError::HostError( + "SQLite null value must not have a payload".to_string(), + )); + } + return Ok(SqlValue::Null); } - return Ok(SqlValue::Null); - } - "int" => present_payload(map, "int_value") - .ok_or_else(|| VmError::HostError("missing SQLite int_value".to_string()))?, - "float" => present_payload(map, "float_value") - .ok_or_else(|| VmError::HostError("missing SQLite float_value".to_string()))?, - "text" => present_payload(map, "text_value") - .ok_or_else(|| VmError::HostError("missing SQLite text_value".to_string()))?, - "blob" => present_payload(map, "blob_value") - .ok_or_else(|| VmError::HostError("missing SQLite blob_value".to_string()))?, - _ => { - return Err(VmError::HostError(format!( - "unknown SQLite value kind '{kind}'" - ))); - } - }; + "int" => int_value + .ok_or_else(|| VmError::HostError("missing SQLite int_value".to_string()))?, + "float" => float_value + .ok_or_else(|| VmError::HostError("missing SQLite float_value".to_string()))?, + "text" => text_value + .ok_or_else(|| VmError::HostError("missing SQLite text_value".to_string()))?, + "blob" => blob_value + .ok_or_else(|| VmError::HostError("missing SQLite blob_value".to_string()))?, + _ => { + return Err(VmError::HostError(format!( + "unknown SQLite value kind '{kind}'" + ))); + } + }; match kind { "int" => match selected_payload { Value::Int(value) => Ok(SqlValue::Integer(*value)), @@ -1240,6 +1259,13 @@ fn estimate_value_bytes(value: &Value) -> usize { } } +fn option_payload_carrier(value: Option) -> Value { + match value { + Some(value) => crate::option::some_carrier(value), + None => crate::option::none_carrier(), + } +} + fn sqlite_value_map( kind: &str, int_value: Option, @@ -1251,19 +1277,19 @@ fn sqlite_value_map( (Value::string("kind"), Value::string(kind)), ( Value::string("int_value"), - int_value.map_or(Value::Null, Value::Int), + option_payload_carrier(int_value.map(Value::Int)), ), ( Value::string("float_value"), - float_value.map_or(Value::Null, Value::Float), + option_payload_carrier(float_value.map(Value::Float)), ), ( Value::string("text_value"), - text_value.map_or(Value::Null, Value::string), + option_payload_carrier(text_value.map(Value::string)), ), ( Value::string("blob_value"), - blob_value.map_or(Value::Null, Value::bytes), + option_payload_carrier(blob_value.map(Value::bytes)), ), ]))) } @@ -1372,8 +1398,8 @@ fn query_with_connection( entries.push(( Value::string("next_cursor"), match next_cursor { - Some(next_cursor) => Value::Int(next_cursor), - None => Value::Null, + Some(next_cursor) => crate::option::some_carrier(Value::Int(next_cursor)), + None => crate::option::none_carrier(), }, )); Ok(VmMap::from_entries(entries)) @@ -1400,22 +1426,23 @@ fn execute_with_connection( } fn transaction_result_value(kind: &str, execute: Option, query: Option) -> Value { + fn opt_map(value: Option) -> Value { + match value { + Some(value) => crate::option::some_carrier(Value::Map(Arc::new(value))), + None => crate::option::none_carrier(), + } + } + Value::Map(Arc::new(VmMap::from_entries(vec![ (Value::string("kind"), Value::string(kind)), - ( - Value::string("execute"), - execute.map_or(Value::Null, |value| Value::Map(Arc::new(value))), - ), - ( - Value::string("query"), - query.map_or(Value::Null, |value| Value::Map(Arc::new(value))), - ), + (Value::string("execute"), opt_map(execute)), + (Value::string("query"), opt_map(query)), ]))) } fn parse_open_options(policy: &SqlitePolicy, options: &VmMap) -> VmResult { - let path = required_string(options, "path")?; - let mode = match optional_string(options, "mode")?.as_deref() { + let path = required_option_string(options, "path")?; + let mode = match optional_option_string(options, "mode")?.as_deref() { Some("memory") => OpenMode::Memory, Some("read_only") => OpenMode::ReadOnly, Some("read_write") => OpenMode::ReadWrite, @@ -1427,7 +1454,7 @@ fn parse_open_options(policy: &SqlitePolicy, options: &VmMap) -> VmResult VmResult parse_limits(limits.as_ref(), policy.limits)?, + Some(_) => return Err(VmError::TypeMismatch("SQLite limits map")), + None => policy.limits, + }; Ok(OpenOptions { path, mode, @@ -1576,20 +1607,22 @@ fn parse_transaction_statements( }; let sql = required_string(statement, "sql")?; validate_sql(&sql, limits, allow_unsafe_sql)?; - let params = match map_value(statement, "params") { + let params = match logical_option_field(statement, "params")? { Some(Value::Array(params)) => sqlite_params(params, limits)?, - Some(Value::Null) | None => Vec::new(), Some(_) => return Err(VmError::TypeMismatch("SQLite parameter array")), + None => Vec::new(), }; - let query = match map_value(statement, "query") { + let query = match logical_option_field(statement, "query")? { Some(Value::Bool(query)) => *query, - Some(Value::Null) | None => false, Some(_) => return Err(VmError::TypeMismatch("SQLite query flag")), + None => false, }; - let statement_limits = match map_value(statement, "limits") { - Some(Value::Map(statement_limits)) => parse_query_limits(statement_limits, limits)?, - Some(Value::Null) | None => limits, + let statement_limits = match logical_option_field(statement, "limits")? { + Some(Value::Map(statement_limits)) => { + parse_query_limits(statement_limits.as_ref(), limits)? + } Some(_) => return Err(VmError::TypeMismatch("SQLite limits map")), + None => limits, }; Ok(TransactionStatement { sql, @@ -1609,6 +1642,37 @@ fn transaction_deadline_error(max_transaction_ms: u64) -> VmError { )) } +fn is_deadline_lock_wait(error: &VmError) -> bool { + let VmError::HostError(message) = error else { + return false; + }; + message.starts_with("SQLite error DatabaseBusy (") + || message.starts_with("SQLite error DatabaseLocked (") +} + +fn should_surface_transaction_deadline(deadline: Instant, now: Instant, error: &VmError) -> bool { + if now >= deadline { + return true; + } + // sqlite3_busy_timeout is an integer millisecond API. A deadline-bounded + // wait can therefore return SQLITE_BUSY up to one millisecond before the + // Instant deadline. That is still the configured deadline, not a new budget. + is_deadline_lock_wait(error) + && deadline.saturating_duration_since(now) <= Duration::from_millis(1) +} + +fn busy_wait_covering_deadline(remaining: Duration, busy_timeout_ms: u64) -> Option { + // A sub-millisecond Duration truncates to 0, and sqlite3_busy_timeout(0) + // clears the handler instead of waiting. Treat that remainder as expired. + if remaining < Duration::from_millis(1) { + return None; + } + let fractional_millis = !remaining.subsec_nanos().is_multiple_of(1_000_000); + let wait_ms = remaining.as_millis() + u128::from(fractional_millis); + let capped = wait_ms.min(u128::from(busy_timeout_ms)); + Some(Duration::from_millis(capped as u64)) +} + fn set_transaction_busy_timeout( connection: &Connection, deadline: Instant, @@ -1616,12 +1680,10 @@ fn set_transaction_busy_timeout( busy_timeout_ms: u64, ) -> VmResult<()> { let remaining = deadline.saturating_duration_since(Instant::now()); - if remaining.is_zero() { + let Some(wait) = busy_wait_covering_deadline(remaining, busy_timeout_ms) else { return Err(transaction_deadline_error(max_transaction_ms)); - } - connection - .busy_timeout(remaining.min(Duration::from_millis(busy_timeout_ms))) - .map_err(sqlite_error) + }; + connection.busy_timeout(wait).map_err(sqlite_error) } fn transaction_with_connection( @@ -1742,7 +1804,9 @@ fn transaction_with_connection_until( commit_hook_cleanup?; progress_cleanup?; busy_timeout_cleanup?; - if Instant::now() >= deadline && result.is_err() { + if let Err(error) = &result + && should_surface_transaction_deadline(deadline, Instant::now(), error) + { return Err(transaction_deadline_error(max_transaction_ms)); } result @@ -2087,6 +2151,52 @@ mod tests { ); } + #[test] + fn deadline_busy_wait_covers_fractional_milliseconds_without_widening_the_cap() { + let fractional = Duration::from_nanos(97_200_000); + let wait = busy_wait_covering_deadline(fractional, 2_000) + .expect("a multi-millisecond remainder must still wait"); + assert!(wait >= fractional); + assert_eq!(wait, Duration::from_millis(98)); + assert_eq!( + busy_wait_covering_deadline(Duration::from_millis(100), 2_000), + Some(Duration::from_millis(100)) + ); + assert_eq!( + busy_wait_covering_deadline(Duration::from_millis(2_500), 2_000), + Some(Duration::from_millis(2_000)) + ); + assert!(busy_wait_covering_deadline(Duration::from_micros(400), 2_000).is_none()); + } + + #[test] + fn near_deadline_lock_wait_surfaces_as_the_configured_deadline() { + let deadline = Instant::now(); + let busy = + VmError::HostError("SQLite error DatabaseBusy (5): database is locked".to_string()); + let other = VmError::HostError("SQLite error ConstraintViolation (19): veto".to_string()); + assert!(should_surface_transaction_deadline( + deadline, + deadline - Duration::from_micros(400), + &busy + )); + assert!(!should_surface_transaction_deadline( + deadline, + deadline - Duration::from_millis(50), + &busy + )); + assert!(should_surface_transaction_deadline( + deadline, + deadline + Duration::from_millis(1), + &other + )); + assert!(!should_surface_transaction_deadline( + deadline, + deadline - Duration::from_micros(400), + &other + )); + } + #[test] fn transaction_deadline_rolls_back_when_commit_waits_for_reader() { let nonce = SystemTime::now() @@ -2281,7 +2391,7 @@ mod tests { open_connections: Arc::clone(&open_connections), }, }; - let program = crate::compile_source("null;") + let program = crate::compile_source("();") .expect("test program should compile") .program; let mut vm = Vm::new(program); @@ -2376,7 +2486,7 @@ mod tests { open_connections: Arc::clone(&open_connections), }, }; - let program = crate::compile_source("null;") + let program = crate::compile_source("();") .expect("test program should compile") .program; let mut vm = Vm::new(program); diff --git a/src/builtins/runtime/sqlite_schema.rs b/src/builtins/runtime/sqlite_schema.rs index 9bd52136..aaa11a07 100644 --- a/src/builtins/runtime/sqlite_schema.rs +++ b/src/builtins/runtime/sqlite_schema.rs @@ -25,8 +25,9 @@ fn sqlite_connection_key() -> crate::host_api::ResourceTypeKey { sqlite_connection_resource().schema.key } -fn sqlite_optional_type(inner: HostTypeSchema) -> HostTypeSchema { - HostTypeSchema::Optional(Box::new(inner)) +/// A public field whose absence is logical, not a tagged SQL NULL cell. +fn sqlite_option_type(inner: HostTypeSchema) -> HostTypeSchema { + HostTypeSchema::Option(Box::new(inner)) } fn sqlite_limits_struct() -> HostStructSchema { @@ -46,7 +47,7 @@ fn sqlite_limits_struct() -> HostStructSchema { "busy_timeout_ms", ] .into_iter() - .map(|name| HostStructField::new(name, sqlite_optional_type(HostTypeSchema::Int))) + .map(|name| HostStructField::new(name, sqlite_option_type(HostTypeSchema::Int))) .collect(), ) .with_description("Effective SQLite host limits. Omitted keys keep the embedding ceiling.") @@ -56,10 +57,10 @@ fn sqlite_open_options_struct(limits: &HostStructSchema) -> HostStructSchema { HostStructSchema::new( "SqliteOpenOptions", vec![ - HostStructField::new("path", sqlite_optional_type(HostTypeSchema::String)), - HostStructField::new("mode", sqlite_optional_type(HostTypeSchema::String)), - HostStructField::new("root", sqlite_optional_type(HostTypeSchema::String)), - HostStructField::new("limits", sqlite_optional_type(limits.as_type())), + HostStructField::new("path", sqlite_option_type(HostTypeSchema::String)), + HostStructField::new("mode", sqlite_option_type(HostTypeSchema::String)), + HostStructField::new("root", sqlite_option_type(HostTypeSchema::String)), + HostStructField::new("limits", sqlite_option_type(limits.as_type())), ], ) .with_description("SQLite open options. Runtime still requires a non-empty path.") @@ -81,10 +82,10 @@ fn sqlite_value_struct() -> HostStructSchema { "SqliteValue", vec![ HostStructField::new("kind", HostTypeSchema::String), - HostStructField::new("int_value", sqlite_optional_type(HostTypeSchema::Int)), - HostStructField::new("float_value", sqlite_optional_type(HostTypeSchema::Float)), - HostStructField::new("text_value", sqlite_optional_type(HostTypeSchema::String)), - HostStructField::new("blob_value", sqlite_optional_type(HostTypeSchema::Bytes)), + HostStructField::new("int_value", sqlite_option_type(HostTypeSchema::Int)), + HostStructField::new("float_value", sqlite_option_type(HostTypeSchema::Float)), + HostStructField::new("text_value", sqlite_option_type(HostTypeSchema::String)), + HostStructField::new("blob_value", sqlite_option_type(HostTypeSchema::Bytes)), ], ) .with_description( @@ -110,7 +111,7 @@ fn sqlite_query_result_struct(row: &HostStructSchema) -> HostStructSchema { HostStructField::new("columns", sqlite_array_type(HostTypeSchema::String)), HostStructField::new("rows", sqlite_array_type(row.as_type())), HostStructField::new("truncated", HostTypeSchema::Bool), - HostStructField::new("next_cursor", sqlite_optional_type(HostTypeSchema::Int)), + HostStructField::new("next_cursor", sqlite_option_type(HostTypeSchema::Int)), ], ) .with_description("Query result envelope with typed rows; next_cursor is omitted when absent.") @@ -126,10 +127,10 @@ fn sqlite_statement_struct( HostStructField::new("sql", HostTypeSchema::String), HostStructField::new( "params", - sqlite_optional_type(sqlite_array_type(value.as_type())), + sqlite_option_type(sqlite_array_type(value.as_type())), ), - HostStructField::new("query", sqlite_optional_type(HostTypeSchema::Bool)), - HostStructField::new("limits", sqlite_optional_type(limits.as_type())), + HostStructField::new("query", sqlite_option_type(HostTypeSchema::Bool)), + HostStructField::new("limits", sqlite_option_type(limits.as_type())), ], ) .with_description("One sqlite::transaction statement with optional typed parameters.") @@ -143,8 +144,8 @@ fn sqlite_transaction_result_struct( "SqliteTransactionResult", vec![ HostStructField::new("kind", HostTypeSchema::String), - HostStructField::new("execute", sqlite_optional_type(execute.as_type())), - HostStructField::new("query", sqlite_optional_type(query.as_type())), + HostStructField::new("execute", sqlite_option_type(execute.as_type())), + HostStructField::new("query", sqlite_option_type(query.as_type())), ], ) .with_description("A tagged ordered SQLite transaction result envelope.") @@ -257,7 +258,7 @@ pub(super) fn sqlite_close_contract() -> HostFunctionSchema { HostTypeSchema::Resource(sqlite_connection_key()), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, ) } diff --git a/src/builtins/runtime/timer.rs b/src/builtins/runtime/timer.rs index d0e31577..5f24df41 100644 --- a/src/builtins/runtime/timer.rs +++ b/src/builtins/runtime/timer.rs @@ -806,12 +806,6 @@ mod tests { use crate::vm::{HostImportParam, HostImportSchema}; use crate::{BytecodeBuilder, HostAsyncBridge, HostFunction, HostOpId}; - struct NoopWake; - - impl std::task::Wake for NoopWake { - fn wake(self: Arc) {} - } - #[derive(Default)] struct RecordingBackend { registrations: Mutex, @@ -919,7 +913,7 @@ mod tests { ); callback.vm.add_fuel(16).expect("recharge fuel"); - let waker = Waker::from(Arc::new(NoopWake)); + let waker = Waker::noop().clone(); let mut cx = Context::from_waker(&waker); assert!(matches!( callback.poll(&mut cx), @@ -1465,7 +1459,7 @@ mod tests { TimerCallbackStatus::Waiting(_) )); - let waker = Waker::from(Arc::new(NoopWake)); + let waker = Waker::noop().clone(); let mut cx = Context::from_waker(&waker); assert!(matches!(first.poll(&mut cx), Poll::Pending)); assert!(matches!(second.poll(&mut cx), Poll::Pending)); @@ -1526,7 +1520,7 @@ mod tests { )); state.lock().expect("bridge state").complete = true; - let waker = Waker::from(Arc::new(NoopWake)); + let waker = Waker::noop().clone(); let mut cx = Context::from_waker(&waker); let result = callback.poll(&mut cx); assert!(matches!(result, Poll::Ready(Err(_)))); @@ -1550,7 +1544,7 @@ mod tests { )); state.lock().expect("bridge state").poll_error = true; - let waker = Waker::from(Arc::new(NoopWake)); + let waker = Waker::noop().clone(); let mut cx = Context::from_waker(&waker); assert!(matches!(callback.poll(&mut cx), Poll::Ready(Err(_)))); assert_eq!(callback.state(), TimerCallbackStatus::Complete); @@ -1584,7 +1578,7 @@ mod tests { TimerCallbackStatus::Waiting(_) )); state.lock().expect("bridge state").complete = true; - let waker = Waker::from(Arc::new(NoopWake)); + let waker = Waker::noop().clone(); let mut cx = Context::from_waker(&waker); assert!(matches!( callback.poll(&mut cx), @@ -1610,7 +1604,7 @@ mod tests { )); state.lock().expect("bridge state").poll_error = true; - let waker = Waker::from(Arc::new(NoopWake)); + let waker = Waker::noop().clone(); let mut cx = Context::from_waker(&waker); assert!(matches!( callback.poll_reporting(&mut cx, &backend), @@ -1661,7 +1655,7 @@ mod tests { )); state.lock().expect("bridge state").panic_poll = true; - let waker = Waker::from(Arc::new(NoopWake)); + let waker = Waker::noop().clone(); let mut cx = Context::from_waker(&waker); assert!(matches!( callback.poll_reporting(&mut cx, &backend), @@ -1724,7 +1718,7 @@ mod tests { )); state.lock().expect("bridge state").complete = true; - let waker = Waker::from(Arc::new(NoopWake)); + let waker = Waker::noop().clone(); let mut cx = Context::from_waker(&waker); assert!(matches!( callback.poll_reporting(&mut cx, &backend), @@ -1762,7 +1756,7 @@ mod tests { )); state.lock().expect("bridge state").complete = true; - let waker = Waker::from(Arc::new(NoopWake)); + let waker = Waker::noop().clone(); let mut cx = Context::from_waker(&waker); assert!(matches!( callback.poll_reporting(&mut cx, &backend), diff --git a/src/builtins/runtime/typed.rs b/src/builtins/runtime/typed.rs index 692853fb..9407e008 100644 --- a/src/builtins/runtime/typed.rs +++ b/src/builtins/runtime/typed.rs @@ -23,14 +23,14 @@ pub(super) type VmMapHandle = SharedMap; #[allow(dead_code)] #[derive(Clone, Debug)] -pub(super) struct VmCallable { +pub struct VmCallable { value: Value, marker: PhantomData Signature>, } impl VmCallable { #[allow(dead_code)] - pub(super) fn into_value(self) -> Value { + pub fn into_value(self) -> Value { self.value } } @@ -59,6 +59,12 @@ impl TakeVmValue for VmCallable { } } +impl IntoVmValue for VmCallable { + fn into_vm_value(self) -> Value { + self.value + } +} + #[derive(Clone, Copy, Debug, PartialEq)] pub(super) enum NumberValue { Int(i64), @@ -483,6 +489,50 @@ impl IntoVmValue for crate::GuestResult { } } +impl<'a, T> FromVmValue<'a> for crate::GuestOption +where + T: FromVmValue<'a>, +{ + fn from_vm_value(value: &'a Value, label: &str) -> VmResult { + let (some, payload) = crate::option::payload(value)?; + if some { + let payload = payload.unwrap_or(&Value::Null); + T::from_vm_value(payload, label).map(Self::Some) + } else { + Ok(Self::None) + } + } +} + +impl TakeVmValue for crate::GuestOption +where + T: TakeVmValue, +{ + fn take_vm_value(slot: &mut Value, label: &str) -> VmResult { + let (some, _) = crate::option::payload(slot)?; + if !some { + let _ = std::mem::replace(slot, Value::Null); + return Ok(Self::None); + } + let Value::Map(mut entries) = std::mem::replace(slot, Value::Null) else { + unreachable!(); + }; + let mut payload = std::sync::Arc::make_mut(&mut entries) + .remove(&Value::string("payload")) + .unwrap_or(Value::Null); + T::take_vm_value(&mut payload, label).map(Self::Some) + } +} + +impl IntoVmValue for crate::GuestOption { + fn into_vm_value(self) -> Value { + match self { + Self::Some(value) => crate::option::some_carrier(value.into_vm_value()), + Self::None => crate::option::none_carrier(), + } + } +} + pub trait IntoVmValue { fn into_vm_value(self) -> Value; } diff --git a/src/bytecode.rs b/src/bytecode.rs index 3483b7ee..1366f0ca 100644 --- a/src/bytecode.rs +++ b/src/bytecode.rs @@ -8,8 +8,8 @@ use crate::host_api::HostImportSchema; /// Bytecode ABI version used for VM-internal cache identity (JIT trace cache, /// program cache keys). The VMBC wire format version lives in `src/vmbc.rs`; -/// both current formats use version 13 and catalog fingerprint v3. -pub const BYTECODE_ABI_VERSION: u16 = 13; +/// both current formats use version 14 and catalog fingerprint v3. +pub const BYTECODE_ABI_VERSION: u16 = 14; pub type SharedString = Arc; pub type SharedBytes = Arc>; diff --git a/src/cli.rs b/src/cli.rs index aa7093e4..9740cf54 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1224,7 +1224,10 @@ pub fn run_repl_with_options( &mut session, ); if let Some(value) = vm.stack().last() { - println!("=> {}", format_value(value)); + println!( + "=> {}", + format_repl_result(value, compiled.result_schema.as_ref()) + ); } else { println!("=> "); } @@ -1756,6 +1759,36 @@ fn echo_host_function(_vm: &mut Vm, args: &[Value]) -> Result) -> String { + match result_schema { + Some(schema) => format_logical_value(value, schema), + None => format_value(value), + } +} + +fn format_logical_value(value: &Value, schema: &crate::TypeSchema) -> String { + match schema { + crate::TypeSchema::Option(inner) => match crate::option::payload(value) { + Ok((false, _)) => "None".to_string(), + Ok((true, Some(payload))) => { + format!("Some({})", format_logical_value(payload, inner)) + } + // A canonical host writer may omit the Null-valued Unit payload. + Ok((true, None)) if matches!(inner.as_ref(), crate::TypeSchema::Unit) => { + "Some(())".to_string() + } + _ => format_value(value), + }, + crate::TypeSchema::Unit if matches!(value, Value::Null) => "()".to_string(), + crate::TypeSchema::Result { ok, err } => match crate::result::payload(value) { + Ok((true, payload)) => format!("Ok({})", format_logical_value(payload, ok)), + Ok((false, payload)) => format!("Err({})", format_logical_value(payload, err)), + Err(_) => format_value(value), + }, + _ => format_value(value), + } +} + fn format_value(value: &Value) -> String { match value { Value::Null => "null".to_string(), @@ -1897,29 +1930,64 @@ mod tests { vm } + #[test] + fn logical_display_renders_option_none_from_schema() { + let rendered = super::format_logical_value( + &crate::option::none_carrier(), + &crate::TypeSchema::Option(Box::new(crate::TypeSchema::Int)), + ); + assert_eq!(rendered, "None"); + } + + #[test] + fn logical_display_restores_canonical_tag_only_some_unit() { + let tag_only = crate::Value::map(vec![( + crate::Value::string("tag"), + crate::Value::Bool(true), + )]); + let rendered = super::format_logical_value( + &tag_only, + &crate::TypeSchema::Option(Box::new(crate::TypeSchema::Unit)), + ); + assert_eq!(rendered, "Some(())"); + let non_unit = super::format_logical_value( + &tag_only, + &crate::TypeSchema::Option(Box::new(crate::TypeSchema::Int)), + ); + assert!( + !non_unit.contains("Some("), + "tag-only Some must not satisfy a non-Unit schema: {non_unit}" + ); + } + #[test] fn root_result_join_float_production_sync_and_nullable_effects() { for (source, value, effect) in [ ( - "let maybe: float? = 2.5; let y = if true => { maybe } else => { null };", - Value::Float(2.5), + "let maybe: Option = Option::Some(2.5); let y = if true => { maybe } else => { Option::None };", + crate::option::some_carrier(Value::Float(2.5)), false, ), ( - "let flag = true; let mut x = 1; let maybe: float? = 3.5; let y = if flag => { x = 2.5; maybe } else => { x = 2.5; null };", - Value::Float(3.5), + "let flag = true; let mut x = 1; let maybe: Option = Option::Some(3.5); let y = if flag => { x = 2.5; maybe } else => { x = 2.5; Option::None };", + crate::option::some_carrier(Value::Float(3.5)), true, ), ( - "let flag = false; let mut x = 1; let maybe: float? = 3.5; let y = if flag => { x = 2.5; maybe } else => { x = 2.5; null };", - Value::Null, + "let flag = false; let mut x = 1; let maybe: Option = Option::Some(3.5); let y = if flag => { x = 2.5; maybe } else => { x = 2.5; Option::None };", + crate::option::none_carrier(), true, ), ] { let mut session = super::ReplSession::default(); run_repl_snippet_and_sync(&mut session, source); - assert_eq!(session.locals["y"].schema, Some(crate::TypeSchema::Float)); - assert!(session.locals["y"].optional); + assert_eq!( + session.locals["y"].schema, + Some(crate::TypeSchema::Option(Box::new( + crate::TypeSchema::Float + ))) + ); + assert!(!session.locals["y"].optional); assert_eq!(session.locals["y"].value, value); if effect { assert_eq!(session.locals["x"].schema, Some(crate::TypeSchema::Float)); @@ -1932,9 +2000,11 @@ mod tests { assert_eq!(next.stack(), &[value]); assert_eq!( session.locals["following"].schema, - Some(crate::TypeSchema::Float) + Some(crate::TypeSchema::Option(Box::new( + crate::TypeSchema::Float + ))) ); - assert!(session.locals["following"].optional); + assert!(!session.locals["following"].optional); } } @@ -1952,15 +2022,18 @@ mod tests { builder.function(crate::HostFunctionSchema::with_return( "resultjoin::origin", vec![], - crate::HostTypeSchema::Optional(Box::new(named.as_type())), + crate::HostTypeSchema::Option(Box::new(named.as_type())), )); let catalog = std::sync::Arc::new(builder.build().unwrap()); let options = CompileSourceFileOptions::default() .with_host_api_catalog(std::sync::Arc::clone(&catalog)); fn origin(_: &mut Vm, _: &[Value]) -> crate::VmResult { - Ok(crate::CallOutcome::Return(crate::return_one(Value::map( - vec![(Value::string("value"), Value::Int(42))], - )))) + Ok(crate::CallOutcome::Return(crate::return_one( + crate::option::some_carrier(Value::map(vec![( + Value::string("value"), + Value::Int(42), + )])), + ))) } let mut registry = crate::HostFunctionRegistry::new(); registry @@ -1970,18 +2043,33 @@ mod tests { ) .unwrap(); for (expression, flag, is_null) in [ - ("if flag => { maybe } else => { null }", true, false), - ("if flag => { null } else => { maybe }", false, false), - ("if flag => { maybe } else => { null }", false, true), - ("if flag => { null } else => { maybe }", true, true), - ("match choice { 1 => maybe, _ => null }", true, false), - ("match choice { 1 => null, _ => maybe }", false, false), + ("if flag => { maybe } else => { Option::None }", true, false), + ( + "if flag => { Option::None } else => { maybe }", + false, + false, + ), + ("if flag => { maybe } else => { Option::None }", false, true), + ("if flag => { Option::None } else => { maybe }", true, true), + ( + "match choice { 1 => maybe, _ => Option::None }", + true, + false, + ), + ( + "match choice { 1 => Option::None, _ => maybe }", + false, + false, + ), ] { let source = format!( - "let flag = {flag}; let choice = if flag => {{ 1 }} else => {{ 2 }}; let maybe: ResultJoinRoot? = resultjoin::origin(); let y = {expression};" + "let flag = {flag}; let choice = if flag => {{ 1 }} else => {{ 2 }}; let maybe: Option = resultjoin::origin(); let y = {expression};" ); let mut session = super::ReplSession::default(); - for snippet in [&source, "let following = y; following == null;"] { + for snippet in [ + source.as_str(), + "let following = y; match following { Option::None => true, Option::Some(_) => false };", + ] { let compiled = super::compile_repl_snippet_with_options(snippet, &session.locals, &options) .unwrap(); @@ -1997,10 +2085,22 @@ mod tests { let name = if snippet == source { "y" } else { "following" }; assert_eq!( session.locals[name].schema, - Some(crate::TypeSchema::Named("ResultJoinRoot".into(), vec![])) + Some(crate::TypeSchema::Option(Box::new( + crate::TypeSchema::Named("ResultJoinRoot".into(), vec![]) + ))) ); - assert!(session.locals[name].optional); - assert_eq!(session.locals[name].value == Value::Null, is_null); + assert!(!session.locals[name].optional); + let absent = session.locals[name].value == crate::option::none_carrier(); + assert_eq!(absent, is_null, "{expression}"); + if !is_null { + let (some, payload) = crate::option::payload(&session.locals[name].value) + .expect("named option carrier"); + assert!(some); + let Value::Map(fields) = payload.expect("some payload") else { + panic!("named payload"); + }; + assert_eq!(fields.get(&Value::string("value")), Some(&Value::Int(42))); + } if name == "following" { assert_eq!(machine.stack(), &[Value::Bool(is_null)]); } @@ -2154,32 +2254,44 @@ mod tests { #[test] fn root_expression_effects_optional_named_alternatives_remain_checked() { for assign in [ - "let ignored = if true => { x = 2.5; 0 } else => { x = 2.5; 0 };", - "let ignored = if true => { x = 2.5; 0 } else => { 0 };", - "let ignored = if false => { x = 2.5; 0 } else => { 0 };", + "let ignored = if true => { x = Option::Some(2.5); 0 } else => { x = Option::Some(2.5); 0 };", + "let ignored = if true => { x = Option::Some(2.5); 0 } else => { 0 };", + "let ignored = if false => { x = Option::Some(2.5); 0 } else => { 0 };", ] { - let source = format!("let mut x: float? = null; {assign}"); + let source = format!("let mut x: Option = Option::None; {assign}"); let mut session = super::ReplSession::default(); let compiled = super::compile_repl_snippet(&source, &session.locals).unwrap(); let x = compiled.bindings.iter().find(|b| b.name == "x").unwrap(); - assert_eq!(x.schema, Some(crate::TypeSchema::Float)); - assert!(x.optional); + assert_eq!( + x.schema, + Some(crate::TypeSchema::Option(Box::new( + crate::TypeSchema::Float + ))) + ); + assert!(!x.optional); run_repl_snippet_and_sync(&mut session, &source); - assert_eq!(session.locals["x"].schema, Some(crate::TypeSchema::Float)); - assert!(session.locals["x"].optional); + assert_eq!( + session.locals["x"].schema, + Some(crate::TypeSchema::Option(Box::new( + crate::TypeSchema::Float + ))) + ); + assert!(!session.locals["x"].optional); let value = session.locals["x"].value.clone(); - run_repl_snippet_and_sync(&mut session, "let y: float? = x;"); + run_repl_snippet_and_sync(&mut session, "let y: Option = x;"); assert_eq!(session.locals["y"].value, value); + assert_eq!(session.locals["y"].schema, x.schema); + assert!(!session.locals["y"].optional); } for condition in ["true", "false"] { let source = format!( - "struct EffectRoot {{ value: int }} let mut x: EffectRoot? = null; let ignored = if {condition} => {{ x = {{ value: 41 }}; 0 }} else => {{ 0 }};" + "struct EffectRoot {{ value: int }} let mut x: Option = Option::None; let ignored = if {condition} => {{ x = Option::Some({{ value: 41 }}); 0 }} else => {{ 0 }};" ); let mut session = super::ReplSession::default(); let compiled = super::compile_repl_snippet(&source, &session.locals).unwrap(); let x = compiled.bindings.iter().find(|b| b.name == "x").unwrap(); let control = super::compile_repl_snippet( - "struct EffectRoot { value: int } let mut x: EffectRoot? = null;", + "struct EffectRoot { value: int } let mut x: Option = Option::None;", &BTreeMap::new(), ) .unwrap(); @@ -2190,15 +2302,20 @@ mod tests { .unwrap() .schema .clone(); - assert!( - matches!(&checked_named, Some(crate::TypeSchema::Named(name, args)) if !name.is_empty() && args.is_empty()) - ); + assert!(matches!( + &checked_named, + Some(crate::TypeSchema::Option(inner)) + if matches!(inner.as_ref(), crate::TypeSchema::Named(name, args) if !name.is_empty() && args.is_empty()) + )); assert_eq!(x.schema, checked_named); - assert!(x.optional); + assert!(!x.optional); run_repl_snippet_and_sync(&mut session, &source); assert_eq!(session.locals["x"].schema, x.schema); - assert!(session.locals["x"].optional); - run_repl_snippet_and_sync(&mut session, "x == null;"); + assert!(!session.locals["x"].optional); + run_repl_snippet_and_sync( + &mut session, + "match x { Option::None => true, Option::Some(_) => false };", + ); } } @@ -2213,15 +2330,18 @@ mod tests { .find(|b| b.name == "field") .unwrap(); assert_eq!( - field.schema.as_ref().map(|s| s.split_optional().0), - Some(crate::TypeSchema::Int) + field.schema, + Some(crate::TypeSchema::Option(Box::new(crate::TypeSchema::Int))) ); - assert!(field.optional); + assert!(!field.optional); run_repl_snippet_and_sync(&mut session, source); - assert_eq!(session.locals["field"].value, Value::Int(41)); - assert!(session.locals["field"].optional); - let machine = - run_repl_snippet_and_sync(&mut session, "let y: int? = field; y.unwrap_or(0);"); + assert!(matches!(session.locals["field"].value, Value::Map(_))); + assert_eq!( + session.locals["field"].schema, + Some(crate::TypeSchema::Option(Box::new(crate::TypeSchema::Int))) + ); + assert!(!session.locals["field"].optional); + let machine = run_repl_snippet_and_sync(&mut session, "field.unwrap_or(0);"); assert_eq!(machine.stack(), &[Value::Int(41)]); } @@ -2286,13 +2406,21 @@ mod tests { builder.function(HostFunctionSchema::with_return( "layout::maybe", vec![], - HostTypeSchema::Optional(Box::new(named.as_type())), + HostTypeSchema::Option(Box::new(named.as_type())), )); builder.function(HostFunctionSchema::with_return( "layout::read", vec![crate::HostParamSchema::value("root", named.as_type())], HostTypeSchema::Int, )); + builder.function(HostFunctionSchema::with_return( + "layout::observe", + vec![crate::HostParamSchema::value( + "value", + HostTypeSchema::Option(Box::new(named.as_type())), + )], + HostTypeSchema::Bool, + )); let catalog = std::sync::Arc::new(builder.build().unwrap()); let options = CompileSourceFileOptions::default().with_host_api_catalog(std::sync::Arc::clone(&catalog)) .with_module_override_source("calibration.rss", "let a = 101; let b = 102; let c = 103; let d = 104; let e = 105; let f = 106; let g = 107; let h = 108; let i = 109; pub fn value() -> int { 1 }"); @@ -2302,7 +2430,9 @@ mod tests { )))) } fn maybe(_: &mut Vm, _: &[Value]) -> crate::VmResult { - Ok(crate::CallOutcome::Return(crate::return_one(Value::Null))) + Ok(crate::CallOutcome::Return(crate::return_one( + crate::option::none_carrier(), + ))) } fn read(_: &mut Vm, args: &[Value]) -> crate::VmResult { let Value::Map(fields) = &args[0] else { @@ -2312,11 +2442,20 @@ mod tests { fields.get(&Value::string("value")).unwrap().clone(), ))) } + fn observe(_: &mut Vm, args: &[Value]) -> crate::VmResult { + if !matches!(args.first(), Some(Value::Map(_))) { + return Err(crate::VmError::TypeMismatch("Option")); + } + Ok(crate::CallOutcome::Return(crate::return_one(Value::Bool( + true, + )))) + } let mut registry = HostFunctionRegistry::new(); for (name, handler) in [ ("layout::root", root as crate::StaticHostFunction), ("layout::maybe", maybe), ("layout::read", read), + ("layout::observe", observe), ] { registry .register_catalog_static( @@ -2352,18 +2491,17 @@ mod tests { let mut session = super::ReplSession::default(); for source in [ initial, - "use calibration; layout::read(root); maybe == null; calibration::value();", + "use calibration; layout::read(root); layout::observe(maybe); calibration::value();", "use calibration; calibration::value();", large_callee.as_str(), recursive_capture.as_str(), if mutable { "root = layout::root();" } else { - "layout::read(root); maybe == null;" + "layout::read(root); layout::observe(maybe);" }, - "layout::read(root); maybe == null;", + "layout::read(root); layout::observe(maybe);", ] { - let first_snippet = session.locals.is_empty(); let compiled = super::compile_repl_snippet_with_options(source, &session.locals, &options) .unwrap(); @@ -2399,16 +2537,14 @@ mod tests { false, Value::map(vec![(Value::string("value"), Value::Int(41))]), ), - ("maybe", true, Value::Null), + ("maybe", false, crate::option::none_carrier()), ] { let slot = machine.debug_info().unwrap().local_index(name).unwrap() as usize; - let schema = crate::TypeSchema::Named("RootNamedOptional".into(), vec![]); - // New optional host results retain their Optional wrapper; - // entry reseeding uses the base schema plus the optional bit. - let schema = if optional && first_snippet { - crate::TypeSchema::Optional(Box::new(schema)) + let named = crate::TypeSchema::Named("RootNamedOptional".into(), vec![]); + let schema = if name == "maybe" { + crate::TypeSchema::Option(Box::new(named)) } else { - schema + named }; let binding = compiled .bindings @@ -2427,8 +2563,10 @@ mod tests { if source == large_callee && name == "maybe" { // The same physical index also summarizes an Int callee // local. Backend observations remain frame-aggregate. + // Option is a schema, not the legacy optional bit, so + // that aggregate slot is not optional. assert_eq!(map.local_schemas[slot], Some(crate::TypeSchema::Int)); - assert!(map.optional_slots[slot]); + assert!(!map.optional_slots[slot]); } assert_eq!(machine.locals()[slot], value, "{source}: {name} at {slot}"); println!( @@ -2980,10 +3118,20 @@ mod tests { #[test] fn repl_session_preserves_optional_string_move_state() { let mut session = super::ReplSession::default(); - let _ = run_repl_snippet_and_sync(&mut session, "let a: string? = \"payload\";"); + let _ = run_repl_snippet_and_sync( + &mut session, + "let a: Option = Option::Some(\"payload\");", + ); let _ = run_repl_snippet_and_sync(&mut session, "let b = a;"); assert_eq!(session.locals.get("a").map(|local| local.moved), Some(true)); + assert_eq!( + session.locals["a"].schema, + Some(crate::TypeSchema::Option(Box::new( + crate::TypeSchema::String + ))) + ); + assert!(!session.locals["a"].optional); match super::compile_repl_snippet("a", &session.locals) { Err(vm::SourceError::Parse(parse)) => { assert_eq!(parse.code.as_deref(), Some("E_LOCAL_MOVED")); @@ -3012,7 +3160,7 @@ mod tests { #[test] fn repl_session_marks_all_value_types_moved_after_binding() { let cases = [ - ("null", "let a = null;"), + ("unit", "let a = ();"), ("int", "let a = 1;"), ("float", "let a = 1.5;"), ("bool", "let a = true;"), @@ -3079,14 +3227,44 @@ mod tests { #[test] fn repl_session_persists_null_between_entries() { let mut session = super::ReplSession::default(); - let _ = run_repl_snippet_and_sync(&mut session, "let x = null;"); + let _ = run_repl_snippet_and_sync(&mut session, "let x = ();"); + let local = session.locals.get("x").expect("unit binding"); + assert_eq!(local.value, Value::Null); + assert_eq!(local.schema, Some(crate::TypeSchema::Unit)); + assert!(!local.optional); assert_eq!( - session.locals.get("x").map(|local| &local.value), - Some(&Value::Null) + super::format_repl_result(&local.value, local.schema.as_ref()), + "()" ); let vm = run_repl_snippet_and_sync(&mut session, "x"); assert_eq!(vm.stack().last(), Some(&Value::Null)); + assert_eq!(session.locals["x"].schema, Some(crate::TypeSchema::Unit)); + } + + #[test] + fn repl_session_persists_option_none_between_entries() { + let mut session = super::ReplSession::default(); + let _ = run_repl_snippet_and_sync(&mut session, "let x: Option = Option::None;"); + let local = session.locals.get("x").expect("option binding"); + assert_eq!(local.value, crate::option::none_carrier()); + assert_eq!( + local.schema, + Some(crate::TypeSchema::Option(Box::new(crate::TypeSchema::Int))) + ); + assert!(!local.optional); + assert_eq!( + super::format_repl_result(&local.value, local.schema.as_ref()), + "None" + ); + + let vm = run_repl_snippet_and_sync(&mut session, "x"); + assert_eq!(vm.stack().last(), Some(&crate::option::none_carrier())); + assert_eq!( + session.locals["x"].schema, + Some(crate::TypeSchema::Option(Box::new(crate::TypeSchema::Int))) + ); + assert!(!session.locals["x"].optional); } #[test] diff --git a/src/cli_capture_certification_tests.rs b/src/cli_capture_certification_tests.rs index 84cbc98c..8f69479f 100644 --- a/src/cli_capture_certification_tests.rs +++ b/src/cli_capture_certification_tests.rs @@ -79,7 +79,7 @@ fn catalog() -> Arc { HostTypeSchema::Resource(key()), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, )); Arc::new(builder.build().unwrap()) } diff --git a/src/compiler/codegen.rs b/src/compiler/codegen.rs index 386bf72c..af7486e4 100644 --- a/src/compiler/codegen.rs +++ b/src/compiler/codegen.rs @@ -89,6 +89,13 @@ impl Default for Compiler { } } +#[derive(Clone, Copy)] +struct OptionalProjection { + logical: bool, + receiver: bool, + unit_field: bool, +} + impl Compiler { pub fn new() -> Self { Self { @@ -817,6 +824,9 @@ impl Compiler { Expr::Null => { self.assembler.push_const(Value::Null); } + Expr::Unit => { + self.assembler.push_const(Value::Null); + } Expr::Int(value) => { self.assembler.push_const(Value::Int(*value)); } @@ -834,13 +844,27 @@ impl Compiler { } Expr::OptionalGet { field_projection: _, + logical_projection, + option_receiver, + logical_unit_field, container, key, container_slot, key_slot, semantic_id: _, } => { - self.compile_optional_get_expr(container, key, *container_slot, *key_slot)?; + self.compile_optional_get_expr( + container, + key, + *container_slot, + *key_slot, + OptionalProjection { + logical: *logical_projection, + receiver: *option_receiver, + unit_field: *logical_unit_field, + }, + crate::compiler::source_map::Span::new(0, 0, 0), + )?; } Expr::OptionUnwrapOr { value, @@ -1127,7 +1151,9 @@ impl Compiler { self.assembler .push_const(Value::string(super::result::RESULT_PAYLOAD_KEY)); self.compile_expr(payload)?; - self.assembler.call(BuiltinFunction::Set.call_index(), 3); + self.assembler.push_const(Value::Bool(false)); + self.assembler + .call(BuiltinFunction::MapEdit.call_index(), 4); } Expr::ResultQuery { value, ok, .. } => { self.compile_expr(value)?; @@ -1173,10 +1199,29 @@ impl Compiler { value, value_slot, payload_slot, + operand_schema, exit_cleanup, span, .. - } => self.compile_result_try(value, *value_slot, *payload_slot, exit_cleanup, *span)?, + } => { + if matches!(operand_schema, crate::compiler::TypeSchema::Option(_)) { + self.compile_option_try( + value, + *value_slot, + *payload_slot, + exit_cleanup, + *span, + )?; + } else { + self.compile_result_try( + value, + *value_slot, + *payload_slot, + exit_cleanup, + *span, + )?; + } + } Expr::ResultMethod { span, .. } => { return Err(CompileError::StrictTypingRequired { line: None, @@ -1186,6 +1231,67 @@ impl Compiler { span: Some(*span), }); } + Expr::OptionMethod { span, .. } => { + return Err(CompileError::StrictTypingRequired { + line: None, + source_name: None, + detail: "unresolved Option combinator method reached code generation" + .to_string(), + span: Some(*span), + }); + } + Expr::ResultConvert { span, .. } => { + return Err(CompileError::StrictTypingRequired { + line: None, + source_name: None, + detail: "unresolved Result conversion reached code generation".to_string(), + span: Some(*span), + }); + } + Expr::OptionConstruct { some, payload, .. } => { + self.assembler.call(BuiltinFunction::MapNew.call_index(), 0); + self.assembler + .push_const(Value::string(super::option::OPTION_TAG_KEY)); + self.assembler.push_const(Value::Bool(*some)); + self.assembler.call(BuiltinFunction::Set.call_index(), 3); + if let Some(payload) = payload { + self.assembler + .push_const(Value::string(super::option::OPTION_PAYLOAD_KEY)); + self.compile_expr(payload)?; + self.assembler.push_const(Value::Bool(false)); + self.assembler + .call(BuiltinFunction::MapEdit.call_index(), 4); + } + } + Expr::OptionMatch { + value, + value_slot, + some_binding, + some_expr, + none_expr, + .. + } => { + self.compile_expr(value)?; + self.emit_stloc(*value_slot)?; + self.emit_copy_ldloc(*value_slot)?; + self.assembler + .push_const(Value::string(super::option::OPTION_TAG_KEY)); + self.assembler.call(BuiltinFunction::Get.call_index(), 2); + let none_label = self.fresh_label("option_none"); + let end_label = self.fresh_label("option_end"); + self.record_unary_operand_type(ValueType::Bool); + self.assembler.brfalse_label(&none_label); + self.compile_result_binding(*value_slot, *some_binding)?; + self.compile_expr(some_expr)?; + self.assembler.br_label(&end_label); + self.assembler + .label(&none_label) + .map_err(CompileError::Assembler)?; + self.compile_expr(none_expr)?; + self.assembler + .label(&end_label) + .map_err(CompileError::Assembler)?; + } } Ok(()) } @@ -1307,18 +1413,103 @@ impl Compiler { Ok(()) } + fn compile_option_try( + &mut self, + value: &Expr, + value_slot: LocalSlot, + payload_slot: LocalSlot, + exit_cleanup: &[(LocalSlot, crate::compiler::TypeSchema)], + span: crate::compiler::source_map::Span, + ) -> Result<(), CompileError> { + if !self.in_callable { + return Err(CompileError::StrictTypingRequired { + line: None, + source_name: None, + detail: "? requires the enclosing function or closure to return Option" + .to_string(), + span: Some(span), + }); + } + self.compile_expr(value)?; + self.emit_stloc(value_slot)?; + self.emit_copy_ldloc(value_slot)?; + self.assembler + .push_const(Value::string(super::option::OPTION_TAG_KEY)); + self.record_operand_types(ValueType::Map, ValueType::String); + self.assembler.call(BuiltinFunction::Get.call_index(), 2); + let none_label = self.fresh_label("option_try_none"); + let some_label = self.fresh_label("option_try_some"); + self.record_unary_operand_type(ValueType::Bool); + self.assembler.brfalse_label(&none_label); + self.compile_result_binding(value_slot, Some(payload_slot))?; + self.emit_move_ldloc(payload_slot)?; + self.assembler.br_label(&some_label); + self.assembler + .label(&none_label) + .map_err(CompileError::Assembler)?; + self.emit_move_ldloc(value_slot)?; + self.emit_err_ret(exit_cleanup)?; + self.assembler + .label(&some_label) + .map_err(CompileError::Assembler)?; + Ok(()) + } + fn compile_optional_get_expr( &mut self, container: &Expr, key: &Expr, container_slot: LocalSlot, key_slot: LocalSlot, + projection: OptionalProjection, + span: crate::compiler::source_map::Span, ) -> Result<(), CompileError> { self.compile_scalar_expr(container)?; self.emit_stloc(container_slot)?; self.compile_scalar_expr(key)?; self.emit_stloc(key_slot)?; + if projection.logical { + let none_label = self.fresh_label("option_projection_none"); + let done_label = self.fresh_label("option_projection_done"); + if projection.receiver { + // Consume exactly one receiver Option layer: None projects to + // None; Some(payload) projects the payload's child. The + // payload's own shape/schema (including a nested Option) is + // preserved by the lookup below. + self.emit_copy_ldloc(container_slot)?; + self.assembler + .push_const(Value::string(super::option::OPTION_TAG_KEY)); + self.record_operand_types(ValueType::Map, ValueType::String); + self.assembler.call(BuiltinFunction::Get.call_index(), 2); + self.record_unary_operand_type(ValueType::Bool); + self.assembler.brfalse_label(&none_label); + self.compile_result_binding(container_slot, Some(container_slot))?; + } + let lookup = self.logical_projection_lookup( + container_slot, + key_slot, + projection.unit_field, + span, + ); + self.compile_expr(&lookup)?; + self.assembler.br_label(&done_label); + self.assembler + .label(&none_label) + .map_err(CompileError::Assembler)?; + self.compile_expr(&Expr::OptionConstruct { + some: false, + payload: None, + option_schema: crate::compiler::TypeSchema::Unknown, + span, + semantic_id: None, + })?; + self.assembler + .label(&done_label) + .map_err(CompileError::Assembler)?; + return Ok(()); + } + let map_lookup = Expr::IfElse { condition: Box::new(Expr::Call( BuiltinFunction::Has.call_index(), @@ -1433,6 +1624,113 @@ impl Compiler { self.compile_expr(&lowered) } + /// Logical `?.` lookup: the projected child is wrapped as `Some` only when + /// the key/index is present. A missing key/out-of-range index yields + /// `None`; a present value is never collapsed to absence, including a + /// present Unit/Null payload or a nested Option map. + fn logical_projection_lookup( + &mut self, + container_slot: LocalSlot, + key_slot: LocalSlot, + logical_unit_field: bool, + span: crate::compiler::source_map::Span, + ) -> Expr { + let call = |index: u16, args: Vec| Expr::Call(index, Vec::new(), args, None, None); + let none = || Expr::OptionConstruct { + some: false, + payload: None, + option_schema: crate::compiler::TypeSchema::Unknown, + span, + semantic_id: None, + }; + let some = |value: Expr| Expr::OptionConstruct { + some: true, + payload: Some(Box::new(value)), + option_schema: crate::compiler::TypeSchema::Unknown, + span, + semantic_id: None, + }; + let type_of = + |slot: LocalSlot| call(BuiltinFunction::TypeOf.call_index(), vec![Expr::Var(slot)]); + let get = || { + Expr::Call( + BuiltinFunction::Get.call_index(), + Vec::new(), + vec![Expr::Var(container_slot), Expr::Var(key_slot)], + None, + None, + ) + }; + let map_has_key = Expr::Call( + BuiltinFunction::Has.call_index(), + Vec::new(), + vec![Expr::Var(container_slot), Expr::Var(key_slot)], + None, + None, + ); + let map_lookup = Expr::IfElse { + condition: Box::new(map_has_key), + then_expr: Box::new(some(get())), + else_expr: Box::new(if logical_unit_field { + // A required Unit field's canonical payload is Null and may be + // omitted from the physical map; its declared schema still + // makes the field present with an internal Null payload. + some(Expr::Null) + } else { + none() + }), + }; + let key_is_int = Expr::Eq( + Box::new(type_of(key_slot)), + Box::new(Expr::String("int".to_string())), + ); + let key_non_negative = Expr::Not(Box::new(Expr::Lt( + Box::new(Expr::Var(key_slot)), + Box::new(Expr::Int(0)), + ))); + let key_in_bounds = Expr::Lt( + Box::new(Expr::Var(key_slot)), + Box::new(call( + BuiltinFunction::Len.call_index(), + vec![Expr::Var(container_slot)], + )), + ); + let index_value = Expr::IfElse { + condition: Box::new(Expr::And( + Box::new(key_is_int), + Box::new(Expr::And( + Box::new(key_non_negative), + Box::new(key_in_bounds), + )), + )), + then_expr: Box::new(some(get())), + else_expr: Box::new(none()), + }; + let container_is_map = Expr::Eq( + Box::new(type_of(container_slot)), + Box::new(Expr::String("map".to_string())), + ); + let container_is_sequence = Expr::Or( + Box::new(Expr::Eq( + Box::new(type_of(container_slot)), + Box::new(Expr::String("array".to_string())), + )), + Box::new(Expr::Eq( + Box::new(type_of(container_slot)), + Box::new(Expr::String("string".to_string())), + )), + ); + Expr::IfElse { + condition: Box::new(container_is_map), + then_expr: Box::new(map_lookup), + else_expr: Box::new(Expr::IfElse { + condition: Box::new(container_is_sequence), + then_expr: Box::new(index_value), + else_expr: Box::new(none()), + }), + } + } + fn compile_option_unwrap_or_expr( &mut self, value: &Expr, @@ -1630,6 +1928,7 @@ impl Compiler { }; let expected_arity = match builtin { BuiltinFunction::Set => 3, + BuiltinFunction::MapEdit => 4, BuiltinFunction::ArrayPush => 2, _ => return Ok(false), }; diff --git a/src/compiler/frontends/legacy_rss.rs b/src/compiler/frontends/legacy_rss.rs new file mode 100644 index 00000000..f0095c81 --- /dev/null +++ b/src/compiler/frontends/legacy_rss.rs @@ -0,0 +1,147 @@ +//! Test-only parser dialect for fixtures that still exercise the pre-Option +//! nullable VM contract. This is not a product language mode: RustScript +//! itself continues to reject legacy null. + +use std::path::Path; +use std::sync::Arc; + +use crate::compiler::ir::FrontendIr; +use crate::compiler::parser::ParserDialect; +use crate::compiler::source_loader::test_module_imports_from_uses; +use crate::compiler::{ + CompileSourceFileOptions, FrontendId, FrontendTypingPolicy, ModuleImport, ParseError, + SharedParserOptions, SourceError, SourceFlavor, SourceFrontendContext, + SourceFrontendDescriptor, SourcePathError, SourcePlugin, +}; + +struct LegacyRssDialect; + +impl ParserDialect for LegacyRssDialect { + fn is_import_keyword(&self, ident: &str) -> bool { + ident == "import" + } + + fn allow_let_mut_binding(&self) -> bool { + true + } + + fn allow_macro_calls(&self) -> bool { + true + } + + fn allow_plus_equal_operator(&self) -> bool { + true + } + + fn allow_for_in_loop(&self) -> bool { + true + } + + fn allow_result_syntax(&self) -> bool { + true + } + + fn allow_option_syntax(&self) -> bool { + true + } + + fn allow_result_try(&self) -> bool { + true + } + + fn reject_legacy_null(&self) -> bool { + false + } +} + +static LEGACY_RSS_DIALECT: LegacyRssDialect = LegacyRssDialect; +static LEGACY_RSS_PLUGIN: LegacyRssPlugin = LegacyRssPlugin; + +struct LegacyRssPlugin; + +impl SourcePlugin for LegacyRssPlugin { + fn descriptor(&self) -> SourceFrontendDescriptor { + SourceFrontendDescriptor { + id: FrontendId::new("legacy-rss-test"), + extensions: &["rss"], + typing_policy: FrontendTypingPolicy::Strict, + supports_ownership_checking: true, + } + } + + fn parser_dialect(&self) -> Option<&'static dyn ParserDialect> { + Some(dialect()) + } + + fn parse_source( + &self, + source: &str, + context: &SourceFrontendContext<'_>, + ) -> Result { + parse_with_catalog(source, context, false) + } + + fn parse_module_imports( + &self, + source: &str, + path: &Path, + context: &SourceFrontendContext<'_>, + ) -> Result, SourcePathError> { + let ir = parse_with_catalog(source, context, true) + .map_err(|error| SourcePathError::Source(SourceError::Parse(error)))?; + test_module_imports_from_uses(path, &ir.use_declarations) + } +} + +fn parse_with_catalog( + source: &str, + context: &SourceFrontendContext<'_>, + import_scan_mode: bool, +) -> Result { + let catalog = context + .host_api_catalog + .map(|catalog| Arc::new(catalog.clone())); + // Module compilation parses with implicit externs so imported names and + // catalog host calls survive until the loader or catalog binder remaps them. + super::parse_with_parser( + source, + context.source_id, + true, + false, + true, + import_scan_mode, + dialect(), + catalog, + ) +} + +pub(crate) fn dialect() -> &'static dyn ParserDialect { + &LEGACY_RSS_DIALECT +} + +pub(crate) fn plugin() -> &'static dyn SourcePlugin { + &LEGACY_RSS_PLUGIN +} + +pub(crate) fn flavor() -> SourceFlavor { + SourceFlavor::Plugin(plugin().descriptor().id) +} + +pub(crate) fn parse(source: &str) -> Result { + super::parse_source_with_dialect( + source, + dialect(), + SharedParserOptions { + source_id: 0, + allow_implicit_externs: false, + allow_implicit_semicolons: false, + enforce_mutable_bindings: true, + import_scan_mode: false, + }, + ) +} + +pub(crate) fn options_with(mut options: CompileSourceFileOptions) -> CompileSourceFileOptions { + options.add_source_plugin(plugin()); + options +} diff --git a/src/compiler/frontends/mod.rs b/src/compiler/frontends/mod.rs index abc38081..c4427cae 100644 --- a/src/compiler/frontends/mod.rs +++ b/src/compiler/frontends/mod.rs @@ -1,5 +1,8 @@ mod rustscript; +#[cfg(test)] +pub(crate) mod legacy_rss; + use std::collections::{HashMap, HashSet}; use std::path::Path; use std::sync::Arc; @@ -255,7 +258,8 @@ fn collect_named_host_types( } HostTypeSchema::Array(inner) | HostTypeSchema::Map(inner) - | HostTypeSchema::Optional(inner) => { + | HostTypeSchema::Optional(inner) + | HostTypeSchema::Option(inner) => { collect_named_host_types(inner, catalog, visited, pending) } HostTypeSchema::Callable { params, result } => { @@ -270,6 +274,7 @@ fn collect_named_host_types( } HostTypeSchema::Unknown | HostTypeSchema::Null + | HostTypeSchema::Unit | HostTypeSchema::Int | HostTypeSchema::Float | HostTypeSchema::Number @@ -479,6 +484,18 @@ pub fn parse_source_with_dialect( source: &str, dialect: &'static dyn ParserDialect, options: SharedParserOptions, +) -> Result { + parse_source_with_dialect_and_catalog(source, dialect, options, None) +} + +/// Same parser as [`parse_source_with_dialect`], with the caller catalog +/// installed so namespaced host calls bind to that catalog instead of a +/// name-only fallback. +pub fn parse_source_with_dialect_and_catalog( + source: &str, + dialect: &'static dyn ParserDialect, + options: SharedParserOptions, + host_catalog: Option>, ) -> Result { parse_with_parser( source, @@ -488,7 +505,7 @@ pub fn parse_source_with_dialect( options.enforce_mutable_bindings, options.import_scan_mode, dialect, - None, + host_catalog, ) } @@ -950,6 +967,10 @@ fn remap_result_expr_spans(expr: &mut super::ir::Expr, map: &impl Fn(&mut Span)) map(span); remap_result_expr_spans(value, map); } + Expr::ResultConvert { value, span, .. } => { + map(span); + remap_result_expr_spans(value, map); + } Expr::ResultMatch { value, ok_expr, @@ -972,6 +993,34 @@ fn remap_result_expr_spans(expr: &mut super::ir::Expr, map: &impl Fn(&mut Span)) remap_result_expr_spans(receiver, map); remap_result_expr_spans(argument, map); } + Expr::OptionMethod { + receiver, + argument, + span, + .. + } => { + map(span); + remap_result_expr_spans(receiver, map); + remap_result_expr_spans(argument, map); + } + Expr::OptionConstruct { payload, span, .. } => { + map(span); + if let Some(payload) = payload { + remap_result_expr_spans(payload, map); + } + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + span, + .. + } => { + map(span); + remap_result_expr_spans(value, map); + remap_result_expr_spans(some_expr, map); + remap_result_expr_spans(none_expr, map); + } Expr::OptionalGet { container, key, .. } => { remap_result_expr_spans(container, map); remap_result_expr_spans(key, map); @@ -1042,6 +1091,7 @@ fn remap_result_expr_spans(expr: &mut super::ir::Expr, map: &impl Fn(&mut Span)) remap_result_expr_spans(expr, map); } Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -2626,7 +2676,7 @@ mod postfix_provenance_tests { #[test] fn unwrap_or_records_member_callee_and_node_id() { let source = "let o = null;\no.unwrap_or(5);\n"; - let ir = parse(source); + let ir = super::legacy_rss::parse(source).expect("legacy nullable fixture must parse"); let index = ir.parsed_semantic_index.as_ref().expect("index present"); let unwrap = site(index, "unwrap_or"); @@ -2660,7 +2710,7 @@ mod postfix_provenance_tests { #[test] fn optional_access_records_member_callee_and_node_id() { let source = "let x = null; let k = 1;\nx?.y;\nx?.[k];\n"; - let ir = parse(source); + let ir = super::legacy_rss::parse(source).expect("legacy nullable fixture must parse"); let index = ir.parsed_semantic_index.as_ref().expect("index present"); let member_sites = index diff --git a/src/compiler/frontends/rustscript.rs b/src/compiler/frontends/rustscript.rs index 19ff581e..b9a568eb 100644 --- a/src/compiler/frontends/rustscript.rs +++ b/src/compiler/frontends/rustscript.rs @@ -29,9 +29,17 @@ impl ParserDialect for RustScriptDialect { true } + fn allow_option_syntax(&self) -> bool { + true + } + fn allow_result_try(&self) -> bool { true } + + fn reject_legacy_null(&self) -> bool { + true + } } static RUSTSCRIPT_DIALECT: RustScriptDialect = RustScriptDialect; diff --git a/src/compiler/host_call_resolve.rs b/src/compiler/host_call_resolve.rs index 4204ee9b..f2a8d975 100644 --- a/src/compiler/host_call_resolve.rs +++ b/src/compiler/host_call_resolve.rs @@ -635,13 +635,14 @@ fn score_pair(expected: &TypeSchema, actual: &TypeSchema) -> MatchScore { Mismatch, } // Classify the pair, then apply the recursive rule unless it's structural. - fn classify(e: &TypeSchema, a: &TypeSchema) -> Option { + fn classify(e: &TypeSchema, a: &TypeSchema) -> std::option::Option { match (e, a) { // Unknown branch first: deferred/dynamic, never a concrete match. (Unknown, _) | (_, Unknown) => Some(Kind::Deferred), // Numeric compatibility second. (Number, Int | Float) | (Int | Float, Number) => Some(Kind::Numeric), (Null, Null) + | (Unit, Unit) | (Int, Int) | (Float, Float) | (Number, Number) @@ -682,6 +683,9 @@ fn score_pair(expected: &TypeSchema, actual: &TypeSchema) -> MatchScore { (Optional(e), Optional(a)) => MatchScore::default() .plus_exact() .combined(score_pair(e, a)), + (Option(e), Option(a)) => MatchScore::default() + .plus_exact() + .combined(score_pair(e, a)), (Optional(_), Null) => MatchScore::default().plus_exact(), (Optional(e), a) => score_pair(e, a), (Array(e), Array(a)) | (Map(e), Map(a)) => MatchScore::default() @@ -907,6 +911,7 @@ fn tf_schema_label(schema: &TypeSchema) -> String { match schema { TypeSchema::Unknown => "unknown".to_string(), TypeSchema::Null => "null".to_string(), + TypeSchema::Unit => "()".to_string(), TypeSchema::Int => "int".to_string(), TypeSchema::Float => "float".to_string(), TypeSchema::Number => "number".to_string(), @@ -914,6 +919,7 @@ fn tf_schema_label(schema: &TypeSchema) -> String { TypeSchema::String => "string".to_string(), TypeSchema::Bytes => "bytes".to_string(), TypeSchema::Optional(inner) => format!("optional<{}>", tf_schema_label(inner)), + TypeSchema::Option(inner) => format!("Option<{}>", tf_schema_label(inner)), TypeSchema::Array(inner) => format!("array<{}>", tf_schema_label(inner)), TypeSchema::ArrayTuple(items) => format!( "({})", @@ -1022,6 +1028,7 @@ fn schema_label(schema: &crate::host_api::HostTypeSchema) -> String { match schema { HostTypeSchema::Unknown => "unknown".to_string(), HostTypeSchema::Null => "null".to_string(), + HostTypeSchema::Unit => "()".to_string(), HostTypeSchema::Int => "int".to_string(), HostTypeSchema::Float => "float".to_string(), HostTypeSchema::Number => "number".to_string(), @@ -1031,6 +1038,7 @@ fn schema_label(schema: &crate::host_api::HostTypeSchema) -> String { HostTypeSchema::Array(inner) => format!("array<{}>", schema_label(inner)), HostTypeSchema::Map(inner) => format!("map<{}>", schema_label(inner)), HostTypeSchema::Optional(inner) => format!("optional<{}>", schema_label(inner)), + HostTypeSchema::Option(inner) => format!("Option<{}>", schema_label(inner)), HostTypeSchema::Callable { params, result } => { let params = params .iter() @@ -1061,6 +1069,9 @@ fn expand_actual_named( (TypeSchema::Optional(inner), HostTypeSchema::Optional(expected_inner)) => { TypeSchema::Optional(Box::new(expand_actual_named(inner, expected_inner))) } + (TypeSchema::Option(inner), HostTypeSchema::Option(expected_inner)) => { + TypeSchema::Option(Box::new(expand_actual_named(inner, expected_inner))) + } (TypeSchema::Array(inner), HostTypeSchema::Array(expected_inner)) => { TypeSchema::Array(Box::new(expand_actual_named(inner, expected_inner))) } diff --git a/src/compiler/host_conversion.rs b/src/compiler/host_conversion.rs index 7609b292..f9cc8a19 100644 --- a/src/compiler/host_conversion.rs +++ b/src/compiler/host_conversion.rs @@ -38,12 +38,13 @@ impl HostTypeSchema { /// integration calls when it needs the compiler's semantic view of a /// host signature. Every [`HostTypeSchema::Resource`] becomes the /// distinct nominal [`TypeSchema::Resource`] carrying the same shared - /// [`ResourceTypeKey`]. Named structs become [`TypeSchema::Named`]; + /// `ResourceTypeKey`. Named structs become [`TypeSchema::Named`]; /// dynamic maps stay maps. pub fn to_compiler_schema(&self) -> TypeSchema { match self { HostTypeSchema::Unknown => TypeSchema::Unknown, HostTypeSchema::Null => TypeSchema::Null, + HostTypeSchema::Unit => TypeSchema::Unit, HostTypeSchema::Int => TypeSchema::Int, HostTypeSchema::Float => TypeSchema::Float, HostTypeSchema::Number => TypeSchema::Number, @@ -55,6 +56,9 @@ impl HostTypeSchema { HostTypeSchema::Optional(inner) => { TypeSchema::Optional(Box::new(inner.to_compiler_schema())) } + HostTypeSchema::Option(inner) => { + TypeSchema::Option(Box::new(inner.to_compiler_schema())) + } HostTypeSchema::Callable { params, result } => TypeSchema::Callable { params: params.iter().map(Self::to_compiler_schema).collect(), result: Box::new(result.to_compiler_schema()), @@ -83,6 +87,9 @@ impl HostTypeSchema { HostTypeSchema::Optional(inner) => { TypeSchema::Optional(Box::new(inner.to_compiler_object_schema())) } + HostTypeSchema::Option(inner) => { + TypeSchema::Option(Box::new(inner.to_compiler_object_schema())) + } HostTypeSchema::Callable { params, result } => TypeSchema::Callable { params: params.iter().map(Self::to_compiler_object_schema).collect(), result: Box::new(result.to_compiler_object_schema()), @@ -138,6 +145,7 @@ pub(crate) fn to_host_schema(schema: &TypeSchema) -> HostTypeSchema { match schema { TypeSchema::Unknown | TypeSchema::GenericParam(_) => HostTypeSchema::Unknown, TypeSchema::Null => HostTypeSchema::Null, + TypeSchema::Unit => HostTypeSchema::Unit, TypeSchema::Int => HostTypeSchema::Int, TypeSchema::Float => HostTypeSchema::Float, TypeSchema::Number => HostTypeSchema::Number, @@ -145,6 +153,7 @@ pub(crate) fn to_host_schema(schema: &TypeSchema) -> HostTypeSchema { TypeSchema::String => HostTypeSchema::String, TypeSchema::Bytes => HostTypeSchema::Bytes, TypeSchema::Optional(inner) => HostTypeSchema::Optional(Box::new(to_host_schema(inner))), + TypeSchema::Option(inner) => HostTypeSchema::Option(Box::new(to_host_schema(inner))), TypeSchema::Array(inner) => HostTypeSchema::Array(Box::new(to_host_schema(inner))), TypeSchema::ArrayTuple(items) => HostTypeSchema::Array(Box::new( items diff --git a/src/compiler/ir.rs b/src/compiler/ir.rs index e04f17fc..81c2fe20 100644 --- a/src/compiler/ir.rs +++ b/src/compiler/ir.rs @@ -26,6 +26,10 @@ pub struct SemanticNodeId(pub u32); pub enum TypeSchema { Unknown, Null, + /// Guest `()`. The physical carrier is the internal Null value; the + /// logical type is distinct from [`TypeSchema::Null`] and + /// [`TypeSchema::Optional`]. + Unit, Int, Float, Number, @@ -33,6 +37,10 @@ pub enum TypeSchema { String, Bytes, Optional(Box), + /// Guest `Option`. Runtime values are maps with a Boolean `tag` + /// (`true` = Some) and the active `payload`; this variant is not a + /// [`TypeSchema::Named`] or [`TypeSchema::Map`]. + Option(Box), GenericParam(String), Named(String, Vec), Array(Box), @@ -70,13 +78,17 @@ impl Hash for TypeSchema { match self { TypeSchema::Unknown | TypeSchema::Null + | TypeSchema::Unit | TypeSchema::Int | TypeSchema::Float | TypeSchema::Number | TypeSchema::Bool | TypeSchema::String | TypeSchema::Bytes => {} - TypeSchema::Optional(inner) | TypeSchema::Array(inner) | TypeSchema::Map(inner) => { + TypeSchema::Optional(inner) + | TypeSchema::Option(inner) + | TypeSchema::Array(inner) + | TypeSchema::Map(inner) => { inner.hash(state); } TypeSchema::GenericParam(name) => name.hash(state), @@ -92,7 +104,7 @@ impl Hash for TypeSchema { TypeSchema::Object(fields) => { fields.len().hash(state); let mut fields = fields.iter().collect::>(); - fields.sort_unstable_by(|(lhs, _), (rhs, _)| lhs.cmp(rhs)); + fields.sort_unstable_by_key(|(name, _)| *name); for (name, schema) in fields { name.hash(state); schema.hash(state); @@ -137,6 +149,7 @@ impl TypeSchema { } TypeSchema::Callable { .. } => ValueType::Callable, TypeSchema::Null => ValueType::Null, + TypeSchema::Unit => ValueType::Null, TypeSchema::Int => ValueType::Int, TypeSchema::Float => ValueType::Float, TypeSchema::Bool => ValueType::Bool, @@ -144,6 +157,7 @@ impl TypeSchema { TypeSchema::Bytes => ValueType::Bytes, TypeSchema::Optional(inner) => inner.coarse_value_type(), TypeSchema::Named(_, _) + | TypeSchema::Option(_) | TypeSchema::Map(_) | TypeSchema::Object(_) | TypeSchema::Result { .. } => ValueType::Map, @@ -216,7 +230,7 @@ impl TypeSchema { pub(crate) fn contains_resource(&self) -> bool { match self { TypeSchema::Resource(_) => true, - TypeSchema::Optional(inner) => inner.contains_resource(), + TypeSchema::Optional(inner) | TypeSchema::Option(inner) => inner.contains_resource(), TypeSchema::Named(_, type_args) => type_args.iter().any(|arg| arg.contains_resource()), TypeSchema::Array(element) => element.contains_resource(), TypeSchema::ArrayTuple(items) => items.iter().any(|item| item.contains_resource()), @@ -231,6 +245,7 @@ impl TypeSchema { TypeSchema::Result { ok, err } => ok.contains_resource() || err.contains_resource(), TypeSchema::Unknown | TypeSchema::Null + | TypeSchema::Unit | TypeSchema::Int | TypeSchema::Float | TypeSchema::Number @@ -254,6 +269,9 @@ impl TypeSchema { TypeSchema::Optional(inner) => { TypeSchema::Optional(Box::new(substitute(inner, bindings))) } + TypeSchema::Option(inner) => { + TypeSchema::Option(Box::new(substitute(inner, bindings))) + } TypeSchema::Named(name, args) => TypeSchema::Named( name.clone(), args.iter().map(|arg| substitute(arg, bindings)).collect(), @@ -310,9 +328,10 @@ impl TypeSchema { ) -> bool { match schema { TypeSchema::Resource(_) => true, - TypeSchema::Optional(inner) | TypeSchema::Array(inner) | TypeSchema::Map(inner) => { - visit(inner, structs, active, reentries) - } + TypeSchema::Optional(inner) + | TypeSchema::Option(inner) + | TypeSchema::Array(inner) + | TypeSchema::Map(inner) => visit(inner, structs, active, reentries), TypeSchema::ArrayTuple(items) => items .iter() .any(|item| visit(item, structs, active, reentries)), @@ -370,6 +389,7 @@ impl TypeSchema { } TypeSchema::Unknown | TypeSchema::Null + | TypeSchema::Unit | TypeSchema::Int | TypeSchema::Float | TypeSchema::Number @@ -389,7 +409,7 @@ impl TypeSchema { /// schema and the eventual runtime handle/token ABI. A later scope that /// wires a resource table / handle transport resolves a [`Self::Resource`] /// schema to an integral token here. It is deliberately NOT used by - /// [`Self::coarse_value_type`], which keeps resources semantically opaque + /// `Self::coarse_value_type`, which keeps resources semantically opaque /// (`ValueType::Unknown`) so inferred schemas and diagnostics never reveal /// the integer backing. // Test-only boundary surface (see compiler::typing::helpers); non-test @@ -569,13 +589,64 @@ impl ResultMethod { } } + /// The shared combinator names also apply to a logical `Option` receiver. + /// `map_err` stays Result-only; Option-only conversions are represented by + /// [`OptionMethod`]. + pub(crate) fn as_option_method(self) -> Option { + match self { + Self::Map => Some(OptionMethod::Map), + Self::AndThen => Some(OptionMethod::AndThen), + Self::OrElse => Some(OptionMethod::OrElse), + Self::UnwrapOr => Some(OptionMethod::UnwrapOr), + Self::UnwrapOrElse => Some(OptionMethod::UnwrapOrElse), + Self::MapErr => None, + } + } + /// `unwrap_or` keeps the pre-existing nullable lowering when the receiver - /// is not a `Result`. + /// is neither a `Result` nor a logical `Option`. pub(crate) fn retains_option_lowering_on_non_result(self) -> bool { matches!(self, Self::UnwrapOr) } } +/// Option-specific combinator and conversion methods. The first five names are +/// also parsed as [`ResultMethod`] because the receiver schema decides their +/// owner; `map`, `and_then`, `or_else`, `unwrap_or` and `unwrap_or_else` are +/// resolved here for logical `Option` receivers. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum OptionMethod { + Map, + AndThen, + OrElse, + UnwrapOr, + UnwrapOrElse, + OkOr, + OkOrElse, +} + +impl OptionMethod { + pub(crate) fn name(self) -> &'static str { + match self { + Self::Map => "map", + Self::AndThen => "and_then", + Self::OrElse => "or_else", + Self::UnwrapOr => "unwrap_or", + Self::UnwrapOrElse => "unwrap_or_else", + Self::OkOr => "ok_or", + Self::OkOrElse => "ok_or_else", + } + } + + pub(crate) fn from_member(member: &str) -> Option { + match member { + "ok_or" => Some(Self::OkOr), + "ok_or_else" => Some(Self::OkOrElse), + _ => None, + } + } +} + /// Parser-assigned evaluation slots for a [`Expr::ResultMethod`]. /// /// `receiver` and `argument` receive the receiver/callback evaluation results @@ -591,9 +662,24 @@ pub struct ResultMethodSlots { pub err_binding: Option, } +/// Parser-assigned evaluation slots for an [`Expr::OptionMethod`]. +/// +/// `receiver` and `argument` receive the receiver/argument evaluation results +/// in source order; `some_binding` receives the selected `Some` payload in the +/// lowered Option match. +#[derive(Clone, Copy, Debug)] +pub struct OptionMethodSlots { + pub receiver: LocalSlot, + pub argument: LocalSlot, + pub some_binding: LocalSlot, +} + #[derive(Clone, Debug)] pub enum Expr { Null, + /// Guest `()` value. The physical carrier is the internal Null constant; + /// this node keeps the logical type distinct from an explicit null. + Unit, Int(i64), Float(f64), Bool(bool), @@ -627,6 +713,18 @@ pub enum Expr { /// literal String/Int key; borrowed, temporary and dynamic-key /// receivers must use value mode. Native `?.` remains value mode. field_projection: bool, + /// RustScript `?.` produces a logical `Option` of the projected value. + /// Other frontends retain their established nullable value contract. + logical_projection: bool, + /// Set by the Option resolver when the receiver schema is a logical + /// `Option`: the projection consumes one and_then-equivalent layer + /// before reading the child. A payload `Option` is never flattened + /// beyond that explicit receiver layer. + option_receiver: bool, + /// The projected leaf is a required declared `Unit` field. Its + /// canonical physical payload may be omitted from the map, so a + /// missing physical key still represents a present Some(Unit). + logical_unit_field: bool, container: Box, key: Box, container_slot: LocalSlot, @@ -767,13 +865,27 @@ pub enum Expr { span: Span, semantic_id: Option, }, - /// A postfix Result combinator method preserved from source until the - /// Result type owner resolves the receiver's actual schema. + /// `Result.ok()` / `.err()`. The Result resolver selects the retained + /// branch as `Option` / `Option` and lowers the conversion into an + /// ordinary typed Result match; the discarded branch must be resource-free + /// or the composition is rejected statically. + ResultConvert { + ok: bool, + value: Box, + value_slot: LocalSlot, + ok_binding: LocalSlot, + err_binding: LocalSlot, + span: Span, + semantic_id: Option, + }, + /// A postfix combinator method preserved from source until the receiver + /// schema decides its owner. /// /// The parser keeps the receiver and the callback/fallback argument as /// structured children plus their evaluation slots; only the Result /// resolver decides whether the receiver is a `Result` (and lowers - /// the call into an ordinary typed `ResultMatch`) or whether + /// the call into an ordinary typed `ResultMatch`), whether the shared + /// method name belongs to a logical `Option` receiver, or whether /// `.unwrap_or(...)` keeps the pre-existing nullable lowering. A /// non-Result receiver never has its ordinary member-call semantics /// stolen by the method name at parse time. @@ -785,6 +897,17 @@ pub enum Expr { span: Span, semantic_id: Option, }, + /// Option-specific method (`ok_or`, `ok_or_else`). Shared combinator names + /// keep the [`Expr::ResultMethod`] carrier because the receiver schema + /// decides whether Result or Option owns them. + OptionMethod { + method: OptionMethod, + receiver: Box, + argument: Box, + slots: OptionMethodSlots, + span: Span, + semantic_id: Option, + }, /// Postfix `?`. Evaluates `value` once. Ok continues as `T`; Err returns /// the active carrier from the lexical function through the existing `Ret`. ResultTry { @@ -800,6 +923,27 @@ pub enum Expr { span: Span, semantic_id: Option, }, + /// `Option::Some(payload)` or `Option::None`. `some` selects the branch; + /// None has no payload. `option_schema` is filled by the Option resolver. + OptionConstruct { + some: bool, + payload: Option>, + option_schema: TypeSchema, + span: Span, + semantic_id: Option, + }, + /// Exhaustive two-arm Option match. The Some binding is `None` for a + /// wildcard Some arm; the None arm never binds a payload. + OptionMatch { + value: Box, + value_slot: LocalSlot, + some_binding: Option, + some_expr: Box, + none_expr: Box, + scrutinee_schema: TypeSchema, + span: Span, + semantic_id: Option, + }, } impl Expr { @@ -818,6 +962,7 @@ impl Expr { visit(err_expr); } Expr::ResultTry { value, .. } => visit(value), + Expr::ResultConvert { value, .. } => visit(value), Expr::ResultMethod { receiver, argument, .. } => { @@ -828,6 +973,33 @@ impl Expr { } } + pub(crate) fn visit_option_children(&self, mut visit: impl FnMut(&Expr)) { + match self { + Expr::OptionConstruct { + payload: Some(payload), + .. + } => visit(payload), + Expr::OptionConstruct { payload: None, .. } => {} + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + visit(value); + visit(some_expr); + visit(none_expr); + } + Expr::OptionMethod { + receiver, argument, .. + } => { + visit(receiver); + visit(argument); + } + _ => {} + } + } + /// The exact per-call host-call catalog resolution carried by this node, /// if it is a catalog-resolved [`Expr::Call`]. /// @@ -1555,12 +1727,18 @@ fn collect_resolved_calls_in_expr( } other @ (Expr::ResultConstruct { .. } | Expr::ResultQuery { .. } + | Expr::ResultConvert { .. } | Expr::ResultMatch { .. } | Expr::ResultTry { .. }) => { other.visit_result_children(|child| { collect_resolved_calls_in_expr(child, by_id, typed, slot_schemas); }); } + other @ Expr::OptionMethod { .. } => { + other.visit_option_children(|child| { + collect_resolved_calls_in_expr(child, by_id, typed, slot_schemas); + }); + } _ => {} } } @@ -1789,7 +1967,7 @@ pub struct CatalogVisibility { /// cursor-position queries (completion prefix derivation, token-at-offset /// resolution). The parser's full token stream is preserved verbatim so the /// language service never re-lexes or scans source text; spans carry their -/// owning [`SourceId`] and survive unit merge unchanged. +/// owning `SourceId` and survive unit merge unchanged. #[derive(Clone, Debug, PartialEq, Eq)] pub struct LexerToken { /// The lexer token kind, as a stable string tag (e.g. `Ident`, `Colon`, diff --git a/src/compiler/lifetime/availability.rs b/src/compiler/lifetime/availability.rs index 4926fc96..3005bb09 100644 --- a/src/compiler/lifetime/availability.rs +++ b/src/compiler/lifetime/availability.rs @@ -1015,6 +1015,7 @@ impl AvailabilityAnalyzer { } match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -1123,12 +1124,14 @@ impl AvailabilityAnalyzer { Ok(out) } else if let Some(root_slot) = self.extract_collection_mutation_root(*index, args) { - let mut out = - if BuiltinFunction::from_call_index(*index) == Some(BuiltinFunction::Set) { - self.analyze_projection_args(args, state, line)? - } else { - self.analyze_args(args, state, line)? - }; + let mut out = if matches!( + BuiltinFunction::from_call_index(*index), + Some(BuiltinFunction::Set | BuiltinFunction::MapEdit) + ) { + self.analyze_projection_args(args, state, line)? + } else { + self.analyze_args(args, state, line)? + }; self.apply_interprocedural_consumed_call_effects(*index, args, &mut out); // Inserting an owned local into an aggregate transfers // ownership of the handle into the collection/field. @@ -1307,6 +1310,35 @@ impl AvailabilityAnalyzer { } Ok(out) } + Expr::OptionMethod { + receiver, + argument, + slots, + .. + } => { + let mut out = self.analyze_expr(receiver, state, line)?; + self.mark_owned_value_reads_moved(receiver, &mut out); + self.mark_available(&mut out, slots.receiver, line)?; + out = self.analyze_expr(argument, &out, line)?; + self.mark_owned_value_reads_moved(argument, &mut out); + self.mark_available(&mut out, slots.argument, line)?; + self.mark_available(&mut out, slots.some_binding, line)?; + Ok(out) + } + Expr::ResultConvert { + value, + value_slot, + ok_binding, + err_binding, + .. + } => { + let mut out = self.analyze_expr(value, state, line)?; + self.mark_owned_value_reads_moved(value, &mut out); + self.mark_available(&mut out, *value_slot, line)?; + self.mark_available(&mut out, *ok_binding, line)?; + self.mark_available(&mut out, *err_binding, line)?; + Ok(out) + } Expr::ResultTry { value, value_slot, .. } => { @@ -1339,6 +1371,34 @@ impl AvailabilityAnalyzer { self.mark_owned_value_reads_moved(err_expr, &mut out); Ok(out) } + Expr::OptionConstruct { payload, .. } => { + let mut out = if let Some(payload) = payload { + self.analyze_expr(payload, state, line)? + } else { + state.clone() + }; + if let Some(payload) = payload { + self.mark_owned_value_reads_moved(payload, &mut out); + } + Ok(out) + } + Expr::OptionMatch { + value, + value_slot, + some_binding, + some_expr, + none_expr, + .. + } => { + let (some_in, none_in) = + self.option_match_arm_states(value, *value_slot, *some_binding, state, line)?; + let some_state = self.analyze_expr(some_expr, &some_in, line)?; + let none_state = self.analyze_expr(none_expr, &none_in, line)?; + let mut out = self.merge_states(some_state, none_state); + self.mark_owned_value_reads_moved(some_expr, &mut out); + self.mark_owned_value_reads_moved(none_expr, &mut out); + Ok(out) + } Expr::ToOwned(inner) => { // `.copy()` on a resource-containing value would duplicate the // underlying handle; the core has no generic resource copy, so @@ -1512,6 +1572,27 @@ impl AvailabilityAnalyzer { Ok((ok_in, err_in)) } + fn option_match_arm_states( + &self, + value: &Expr, + value_slot: LocalSlot, + some_binding: Option, + state: &FlowState, + line: u32, + ) -> Result<(FlowState, FlowState), ParseError> { + let mut value_state = self.analyze_expr(value, state, line)?; + self.mark_owned_value_reads_moved(value, &mut value_state); + self.mark_available(&mut value_state, value_slot, line)?; + let mut some_in = value_state.clone(); + if let Some(slot) = some_binding { + self.mark_available(&mut some_in, slot, line)?; + self.clear_local_moved_state(&mut some_in, slot); + self.note_result_payload_owner(&mut some_in, slot, value); + } + let none_in = value_state; + Ok((some_in, none_in)) + } + fn note_result_payload_owner( &self, state: &mut FlowState, @@ -1689,6 +1770,11 @@ impl AvailabilityAnalyzer { } let value_position = match BuiltinFunction::from_call_index(call_index) { Some(BuiltinFunction::Set) if args.len() == 3 => Some(2), + Some(BuiltinFunction::MapEdit) + if args.len() == 4 && !matches!(args.get(3), Some(Expr::Bool(true))) => + { + Some(2) + } Some(BuiltinFunction::ArrayPush) if args.len() == 2 => Some(1), _ => None, }; @@ -1781,7 +1867,7 @@ impl AvailabilityAnalyzer { key, container_slot, key_slot, - semantic_id: _, + .. } => { self.is_owned_slot(*container_slot) || self.is_owned_slot(*key_slot) @@ -1857,6 +1943,7 @@ impl AvailabilityAnalyzer { } other @ (Expr::ResultConstruct { .. } | Expr::ResultQuery { .. } + | Expr::ResultConvert { .. } | Expr::ResultMatch { .. } | Expr::ResultTry { .. } | Expr::ResultMethod { .. }) => { @@ -1864,6 +1951,13 @@ impl AvailabilityAnalyzer { other.visit_result_children(|child| found |= self.expr_contains_owned_local(child)); found } + other @ (Expr::OptionConstruct { .. } + | Expr::OptionMatch { .. } + | Expr::OptionMethod { .. }) => { + let mut found = false; + other.visit_option_children(|child| found |= self.expr_contains_owned_local(child)); + found + } Expr::Block { stmts, expr } => { stmts .iter() @@ -1871,6 +1965,7 @@ impl AvailabilityAnalyzer { || self.expr_contains_owned_local(expr) } Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -2115,7 +2210,11 @@ impl AvailabilityAnalyzer { && position == 0 && matches!( BuiltinFunction::from_call_index(*index), - Some(BuiltinFunction::Get | BuiltinFunction::Set) + Some( + BuiltinFunction::Get + | BuiltinFunction::Set + | BuiltinFunction::MapEdit + ) ) { // A projection may read an available remaining child // of a partially moved root; it is not a whole read. @@ -2139,6 +2238,12 @@ impl AvailabilityAnalyzer { if let Some(builtin) = BuiltinFunction::from_call_index(*index) { let value_position = match builtin { BuiltinFunction::Set if args.len() == 3 => Some(2), + BuiltinFunction::MapEdit + if args.len() == 4 + && !matches!(args.get(3), Some(Expr::Bool(true))) => + { + Some(2) + } BuiltinFunction::ArrayPush if args.len() == 2 => Some(1), _ => None, }; @@ -2176,6 +2281,9 @@ impl AvailabilityAnalyzer { } => Ok(self.rewrite_runtime_field_move_expr(expr, state)), Expr::OptionalGet { field_projection, + logical_projection, + option_receiver, + logical_unit_field, container, key, container_slot, @@ -2187,6 +2295,9 @@ impl AvailabilityAnalyzer { self.mark_owned_value_reads_moved(&container, &mut container_state); Ok(Expr::OptionalGet { field_projection: *field_projection, + logical_projection: *logical_projection, + option_receiver: *option_receiver, + logical_unit_field: *logical_unit_field, container: Box::new(container), key: Box::new(self.rewrite_expr_ownership_inner( key, @@ -2472,8 +2583,102 @@ impl AvailabilityAnalyzer { semantic_id: *semantic_id, }) } + Expr::OptionMethod { + method, + receiver, + argument, + slots, + span, + semantic_id, + } => { + let (receiver, mut receiver_state) = + self.rewrite_expr_and_state(receiver, false, state, 1)?; + self.mark_owned_value_reads_moved(&receiver, &mut receiver_state); + self.mark_available(&mut receiver_state, slots.receiver, 1)?; + let argument = + self.rewrite_expr_ownership_inner(argument, false, &receiver_state)?; + Ok(Expr::OptionMethod { + method: *method, + receiver: Box::new(receiver), + argument: Box::new(argument), + slots: *slots, + span: *span, + semantic_id: *semantic_id, + }) + } + Expr::ResultConvert { + ok, + value, + value_slot, + ok_binding, + err_binding, + span, + semantic_id, + } => { + let (value, mut receiver_state) = + self.rewrite_expr_and_state(value, false, state, 1)?; + self.mark_owned_value_reads_moved(&value, &mut receiver_state); + self.mark_available(&mut receiver_state, *value_slot, 1)?; + Ok(Expr::ResultConvert { + ok: *ok, + value: Box::new(value), + value_slot: *value_slot, + ok_binding: *ok_binding, + err_binding: *err_binding, + span: *span, + semantic_id: *semantic_id, + }) + } + Expr::OptionConstruct { + some, + payload, + option_schema, + span, + semantic_id, + } => Ok(Expr::OptionConstruct { + some: *some, + payload: match payload { + Some(payload) => Some(Box::new( + self.rewrite_expr_ownership_inner(payload, false, state)?, + )), + None => None, + }, + option_schema: option_schema.clone(), + span: *span, + semantic_id: *semantic_id, + }), + Expr::OptionMatch { + value, + value_slot, + some_binding, + some_expr, + none_expr, + scrutinee_schema, + span, + semantic_id, + } => { + let line = 1u32; + let value = self.rewrite_expr_ownership_inner(value, false, state)?; + let (some_in, none_in) = + self.option_match_arm_states(&value, *value_slot, *some_binding, state, line)?; + Ok(Expr::OptionMatch { + value: Box::new(value), + value_slot: *value_slot, + some_binding: *some_binding, + some_expr: Box::new( + self.rewrite_expr_ownership_inner(some_expr, false, &some_in)?, + ), + none_expr: Box::new( + self.rewrite_expr_ownership_inner(none_expr, false, &none_in)?, + ), + scrutinee_schema: scrutinee_schema.clone(), + span: *span, + semantic_id: *semantic_id, + }) + } Expr::Block { .. } => self.rewrite_expr_for_ownership_with_state(expr, state, 0), Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -3164,6 +3369,9 @@ mod tests { } let optional = Expr::OptionalGet { field_projection: false, + logical_projection: false, + option_receiver: false, + logical_unit_field: false, container: Box::new(take_call(Expr::Var(0), &key)), key: Box::new(polex_try_expr()), container_slot: 4, diff --git a/src/compiler/lifetime/availability/captures.rs b/src/compiler/lifetime/availability/captures.rs index ca21f7e1..b74ce9e5 100644 --- a/src/compiler/lifetime/availability/captures.rs +++ b/src/compiler/lifetime/availability/captures.rs @@ -430,6 +430,7 @@ impl AvailabilityAnalyzer { ) { match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -624,6 +625,7 @@ impl AvailabilityAnalyzer { } other @ (Expr::ResultConstruct { .. } | Expr::ResultQuery { .. } + | Expr::ResultConvert { .. } | Expr::ResultMatch { .. } | Expr::ResultTry { .. } | Expr::ResultMethod { .. }) => { @@ -631,6 +633,37 @@ impl AvailabilityAnalyzer { self.capture_mode_for_expr(child, captured_slot, context, implicit, scan); }); } + other @ Expr::OptionMethod { .. } => { + other.visit_option_children(|child| { + self.capture_mode_for_expr(child, captured_slot, context, implicit, scan); + }); + } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + self.capture_mode_for_expr(payload, captured_slot, context, implicit, scan); + } + } + Expr::OptionMatch { + value, + value_slot, + some_binding, + some_expr, + none_expr, + .. + } => { + if *value_slot == captured_slot + || some_binding.is_some_and(|binding| binding == captured_slot) + { + scan.seen = true; + scan.mode = scan.mode.max(context); + if implicit && context == CaptureBindingMode::Copy { + scan.implicit_read = true; + } + } + self.capture_mode_for_expr(value, captured_slot, context, implicit, scan); + self.capture_mode_for_expr(some_expr, captured_slot, context, implicit, scan); + self.capture_mode_for_expr(none_expr, captured_slot, context, implicit, scan); + } Expr::Block { stmts, expr } => { self.capture_mode_for_stmts(stmts, captured_slot, context, implicit, scan); self.capture_mode_for_expr(expr, captured_slot, context, implicit, scan); diff --git a/src/compiler/lifetime/availability/consumption.rs b/src/compiler/lifetime/availability/consumption.rs index 923349d7..b5ba2236 100644 --- a/src/compiler/lifetime/availability/consumption.rs +++ b/src/compiler/lifetime/availability/consumption.rs @@ -74,6 +74,10 @@ pub(super) fn collect_direct_return_consumed_positions( Expr::Var(slot) | Expr::MoveVar(slot) => vec![*slot], _ => Vec::new(), }, + Expr::OptionConstruct { payload, .. } => match payload.as_deref() { + Some(Expr::Var(slot) | Expr::MoveVar(slot)) => vec![*slot], + _ => Vec::new(), + }, _ => Vec::new(), }; for slot in slots { @@ -191,6 +195,7 @@ pub(super) fn stmt_uses_slot(stmt: &Stmt, slot: LocalSlot) -> bool { pub(super) fn expr_uses_slot(expr: &Expr, slot: LocalSlot) -> bool { match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -201,13 +206,30 @@ pub(super) fn expr_uses_slot(expr: &Expr, slot: LocalSlot) -> bool { | Expr::UnresolvedFunctionRef { .. } => false, Expr::Var(index) | Expr::MoveVar(index) => *index == slot, Expr::MoveField { root, .. } | Expr::MoveIndex { root, .. } => *root == slot, + Expr::OptionConstruct { payload, .. } => payload + .as_deref() + .is_some_and(|payload| expr_uses_slot(payload, slot)), + Expr::OptionMatch { + value, + value_slot, + some_binding, + some_expr, + none_expr, + .. + } => { + *value_slot == slot + || some_binding.is_some_and(|binding| binding == slot) + || expr_uses_slot(value, slot) + || expr_uses_slot(some_expr, slot) + || expr_uses_slot(none_expr, slot) + } Expr::OptionalGet { field_projection: _, container, key, container_slot, key_slot, - semantic_id: _, + .. } => { *container_slot == slot || *key_slot == slot @@ -276,6 +298,7 @@ pub(super) fn expr_uses_slot(expr: &Expr, slot: LocalSlot) -> bool { } other @ (Expr::ResultConstruct { .. } | Expr::ResultQuery { .. } + | Expr::ResultConvert { .. } | Expr::ResultMatch { .. } | Expr::ResultTry { .. } | Expr::ResultMethod { .. }) => { @@ -292,6 +315,24 @@ pub(super) fn expr_uses_slot(expr: &Expr, slot: LocalSlot) -> bool { || slots.ok_binding == slot || slots.err_binding == Some(slot); } + if let Expr::ResultConvert { + value_slot, + ok_binding, + err_binding, + .. + } = other + { + used |= *value_slot == slot || *ok_binding == slot || *err_binding == slot; + } + used + } + other @ Expr::OptionMethod { .. } => { + let mut used = false; + other.visit_option_children(|child| used |= expr_uses_slot(child, slot)); + if let Expr::OptionMethod { slots, .. } = other { + used |= + slots.receiver == slot || slots.argument == slot || slots.some_binding == slot; + } used } Expr::Block { stmts, expr } => { @@ -420,6 +461,7 @@ pub(super) fn collect_consumed_positions_from_expr( ) { match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -447,6 +489,41 @@ pub(super) fn collect_consumed_positions_from_expr( out.insert(position); } } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + collect_consumed_positions_from_expr( + payload, + function_impl, + known_consumed_positions, + out, + ); + } + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + collect_consumed_positions_from_expr( + value, + function_impl, + known_consumed_positions, + out, + ); + collect_consumed_positions_from_expr( + some_expr, + function_impl, + known_consumed_positions, + out, + ); + collect_consumed_positions_from_expr( + none_expr, + function_impl, + known_consumed_positions, + out, + ); + } Expr::OptionalGet { container, key, .. } => { collect_consumed_positions_from_expr( container, @@ -646,6 +723,7 @@ pub(super) fn collect_consumed_positions_from_expr( } other @ (Expr::ResultConstruct { .. } | Expr::ResultQuery { .. } + | Expr::ResultConvert { .. } | Expr::ResultMatch { .. } | Expr::ResultTry { .. } | Expr::ResultMethod { .. }) => { @@ -658,6 +736,16 @@ pub(super) fn collect_consumed_positions_from_expr( ); }); } + other @ Expr::OptionMethod { .. } => { + other.visit_option_children(|child| { + collect_consumed_positions_from_expr( + child, + function_impl, + known_consumed_positions, + out, + ); + }); + } Expr::Block { stmts, expr } => { for nested in stmts { collect_consumed_positions_from_stmt( diff --git a/src/compiler/lifetime/availability/field_moves.rs b/src/compiler/lifetime/availability/field_moves.rs index 3897ad47..0064489b 100644 --- a/src/compiler/lifetime/availability/field_moves.rs +++ b/src/compiler/lifetime/availability/field_moves.rs @@ -78,6 +78,7 @@ impl AvailabilityAnalyzer { let builtin = BuiltinFunction::from_call_index(call_index)?; let expected_arity = match builtin { BuiltinFunction::Set => 3, + BuiltinFunction::MapEdit => 4, BuiltinFunction::ArrayPush => 2, _ => return None, }; @@ -535,6 +536,18 @@ impl AvailabilityAnalyzer { } Some(keys) } + BuiltinFunction::MapEdit if args.len() == 4 => { + let mut keys = self.collect_copyable_fields_for_expr(&args[0], state)?; + let key = self.extract_literal_moved_field_key(&args[1])?; + if matches!(args.get(3), Some(Expr::Bool(true))) + || !self.is_definitely_copyable_expr(&args[2], state) + { + keys.remove(&key); + } else { + keys.insert(key); + } + Some(keys) + } _ => None, } } @@ -543,7 +556,7 @@ impl AvailabilityAnalyzer { match expr { // RustScript models string values as move-by-default. String-specific ergonomics // (for example `p.a + p.a`) are handled by `analyze_expr_to_owned`. - Expr::Null | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) => true, + Expr::Null | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) => true, Expr::Neg(inner) | Expr::ToOwned(inner) | Expr::Borrow(inner) @@ -607,6 +620,9 @@ impl AvailabilityAnalyzer { Some(BuiltinFunction::Set) if args.len() == 3 => { self.is_definitely_collection_expr(&args[0], state) } + Some(BuiltinFunction::MapEdit) if args.len() == 4 => { + self.is_definitely_collection_expr(&args[0], state) + } Some(BuiltinFunction::ArrayPush) if args.len() == 2 => { self.is_definitely_collection_expr(&args[0], state) } diff --git a/src/compiler/lifetime/liveness.rs b/src/compiler/lifetime/liveness.rs index a73d692a..d3b19d14 100644 --- a/src/compiler/lifetime/liveness.rs +++ b/src/compiler/lifetime/liveness.rs @@ -552,6 +552,7 @@ impl LivenessRewriter { fn add_expr_uses_impl(&self, expr: &Expr, live: &mut LiveSet, conservative: bool) { match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -570,7 +571,7 @@ impl LivenessRewriter { key, container_slot, key_slot, - semantic_id: _, + .. } => { self.mark_live(live, *container_slot); self.mark_live(live, *key_slot); @@ -677,6 +678,7 @@ impl LivenessRewriter { } other @ (Expr::ResultConstruct { .. } | Expr::ResultQuery { .. } + | Expr::ResultConvert { .. } | Expr::ResultMatch { .. } | Expr::ResultTry { .. } | Expr::ResultMethod { .. }) => { @@ -692,6 +694,48 @@ impl LivenessRewriter { self.mark_live(live, slots.receiver); self.mark_live(live, slots.argument); } + if let Expr::ResultConvert { + value_slot, + ok_binding, + err_binding, + .. + } = other + { + self.mark_live(live, *value_slot); + self.mark_live(live, *ok_binding); + self.mark_live(live, *err_binding); + } + } + other @ Expr::OptionMethod { .. } => { + other.visit_option_children(|child| { + self.add_expr_uses_impl(child, live, conservative) + }); + if let Expr::OptionMethod { slots, .. } = other { + self.mark_live(live, slots.receiver); + self.mark_live(live, slots.argument); + self.mark_live(live, slots.some_binding); + } + } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + self.add_expr_uses_impl(payload, live, conservative); + } + } + Expr::OptionMatch { + value, + value_slot, + some_binding, + some_expr, + none_expr, + .. + } => { + self.mark_live(live, *value_slot); + if let Some(slot) = *some_binding { + self.mark_live(live, slot); + } + self.add_expr_uses_impl(value, live, conservative); + self.add_expr_uses_impl(some_expr, live, conservative); + self.add_expr_uses_impl(none_expr, live, conservative); } Expr::Block { stmts, expr } => { let live_out = if conservative { @@ -1129,6 +1173,7 @@ impl LocalSlotAllocator { .union_inplace(&mut live_during, &self.liveness.uses_expr_precise(expr)); match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -1149,7 +1194,7 @@ impl LocalSlotAllocator { key, container_slot, key_slot, - semantic_id: _, + .. } => { self.add_slot_live_edges(*container_slot, &live_during); self.add_slot_live_edges(*key_slot, &live_during); @@ -1300,6 +1345,30 @@ impl LocalSlotAllocator { self.collect_expr_constraints(receiver, &live_during, protected_slots)?; self.collect_expr_constraints(argument, &live_during, protected_slots)?; } + Expr::OptionMethod { + receiver, + argument, + slots, + .. + } => { + self.add_slot_live_edges(slots.receiver, &live_during); + self.add_slot_live_edges(slots.argument, &live_during); + self.add_slot_live_edges(slots.some_binding, &live_during); + self.collect_expr_constraints(receiver, &live_during, protected_slots)?; + self.collect_expr_constraints(argument, &live_during, protected_slots)?; + } + Expr::ResultConvert { + value, + value_slot, + ok_binding, + err_binding, + .. + } => { + self.add_slot_live_edges(*value_slot, &live_during); + self.add_slot_live_edges(*ok_binding, &live_during); + self.add_slot_live_edges(*err_binding, &live_during); + self.collect_expr_constraints(value, &live_during, protected_slots)?; + } Expr::ResultMatch { value, value_slot, @@ -1320,6 +1389,27 @@ impl LocalSlotAllocator { self.collect_expr_constraints(ok_expr, &live_during, protected_slots)?; self.collect_expr_constraints(err_expr, &live_during, protected_slots)?; } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + self.collect_expr_constraints(payload, &live_during, protected_slots)?; + } + } + Expr::OptionMatch { + value, + value_slot, + some_binding, + some_expr, + none_expr, + .. + } => { + self.add_slot_live_edges(*value_slot, &live_during); + if let Some(slot) = *some_binding { + self.add_slot_live_edges(slot, &live_during); + } + self.collect_expr_constraints(value, &live_during, protected_slots)?; + self.collect_expr_constraints(some_expr, &live_during, protected_slots)?; + self.collect_expr_constraints(none_expr, &live_during, protected_slots)?; + } Expr::Block { stmts, expr } => { self.collect_expr_constraints(expr, &live_during, protected_slots)?; let mut block_live_out = live_during.clone(); @@ -1460,6 +1550,7 @@ impl LocalSlotAllocator { fn collect_expr_footprint(&mut self, expr: &Expr, set: &mut LiveSet, stack: &mut Vec) { match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -1480,7 +1571,7 @@ impl LocalSlotAllocator { key, container_slot, key_slot, - semantic_id: _, + .. } => { self.mark_set_slot(set, *container_slot); self.mark_set_slot(set, *key_slot); @@ -1608,6 +1699,30 @@ impl LocalSlotAllocator { self.collect_expr_footprint(receiver, set, stack); self.collect_expr_footprint(argument, set, stack); } + Expr::OptionMethod { + receiver, + argument, + slots, + .. + } => { + self.mark_set_slot(set, slots.receiver); + self.mark_set_slot(set, slots.argument); + self.mark_set_slot(set, slots.some_binding); + self.collect_expr_footprint(receiver, set, stack); + self.collect_expr_footprint(argument, set, stack); + } + Expr::ResultConvert { + value, + value_slot, + ok_binding, + err_binding, + .. + } => { + self.mark_set_slot(set, *value_slot); + self.mark_set_slot(set, *ok_binding); + self.mark_set_slot(set, *err_binding); + self.collect_expr_footprint(value, set, stack); + } Expr::ResultMatch { value, value_slot, @@ -1628,6 +1743,27 @@ impl LocalSlotAllocator { self.collect_expr_footprint(ok_expr, set, stack); self.collect_expr_footprint(err_expr, set, stack); } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + self.collect_expr_footprint(payload, set, stack); + } + } + Expr::OptionMatch { + value, + value_slot, + some_binding, + some_expr, + none_expr, + .. + } => { + self.mark_set_slot(set, *value_slot); + if let Some(slot) = *some_binding { + self.mark_set_slot(set, slot); + } + self.collect_expr_footprint(value, set, stack); + self.collect_expr_footprint(some_expr, set, stack); + self.collect_expr_footprint(none_expr, set, stack); + } Expr::Block { stmts, expr } => { for nested in stmts { self.collect_stmt_footprint(nested, set, stack); @@ -1905,6 +2041,7 @@ fn collect_persistent_closure_sources_from_stmts(stmts: &[Stmt], slots: &mut BTr fn collect_persistent_closure_sources_from_expr(expr: &Expr, slots: &mut BTreeSet) { match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -1984,6 +2121,7 @@ fn collect_persistent_closure_sources_from_expr(expr: &Expr, slots: &mut BTreeSe } other @ (Expr::ResultConstruct { .. } | Expr::ResultQuery { .. } + | Expr::ResultConvert { .. } | Expr::ResultMatch { .. } | Expr::ResultTry { .. } | Expr::ResultMethod { .. }) => { @@ -1991,6 +2129,13 @@ fn collect_persistent_closure_sources_from_expr(expr: &Expr, slots: &mut BTreeSe collect_persistent_closure_sources_from_expr(child, slots) }); } + other @ (Expr::OptionConstruct { .. } + | Expr::OptionMatch { .. } + | Expr::OptionMethod { .. }) => { + other.visit_option_children(|child| { + collect_persistent_closure_sources_from_expr(child, slots) + }); + } Expr::Block { stmts, expr } => { collect_persistent_closure_sources_from_stmts(stmts, slots); collect_persistent_closure_sources_from_expr(expr, slots); @@ -2071,6 +2216,7 @@ fn remap_stmt_slots(stmt: &mut Stmt, mapping: &[LocalSlot]) -> Result<(), ParseE fn remap_expr_slots(expr: &mut Expr, mapping: &[LocalSlot]) -> Result<(), ParseError> { match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -2137,7 +2283,7 @@ fn remap_expr_slots(expr: &mut Expr, mapping: &[LocalSlot]) -> Result<(), ParseE key, container_slot, key_slot, - semantic_id: _, + .. } => { *container_slot = remap_slot(*container_slot, mapping)?; *key_slot = remap_slot(*key_slot, mapping)?; @@ -2217,6 +2363,27 @@ fn remap_expr_slots(expr: &mut Expr, mapping: &[LocalSlot]) -> Result<(), ParseE remap_expr_slots(ok_expr, mapping)?; remap_expr_slots(err_expr, mapping)?; } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + remap_expr_slots(payload, mapping)?; + } + } + Expr::OptionMatch { + value, + value_slot, + some_binding, + some_expr, + none_expr, + .. + } => { + *value_slot = remap_slot(*value_slot, mapping)?; + if let Some(slot) = some_binding { + *slot = remap_slot(*slot, mapping)?; + } + remap_expr_slots(value, mapping)?; + remap_expr_slots(some_expr, mapping)?; + remap_expr_slots(none_expr, mapping)?; + } Expr::ResultMethod { receiver, argument, @@ -2232,6 +2399,30 @@ fn remap_expr_slots(expr: &mut Expr, mapping: &[LocalSlot]) -> Result<(), ParseE remap_expr_slots(receiver, mapping)?; remap_expr_slots(argument, mapping)?; } + Expr::OptionMethod { + receiver, + argument, + slots, + .. + } => { + slots.receiver = remap_slot(slots.receiver, mapping)?; + slots.argument = remap_slot(slots.argument, mapping)?; + slots.some_binding = remap_slot(slots.some_binding, mapping)?; + remap_expr_slots(receiver, mapping)?; + remap_expr_slots(argument, mapping)?; + } + Expr::ResultConvert { + value, + value_slot, + ok_binding, + err_binding, + .. + } => { + *value_slot = remap_slot(*value_slot, mapping)?; + *ok_binding = remap_slot(*ok_binding, mapping)?; + *err_binding = remap_slot(*err_binding, mapping)?; + remap_expr_slots(value, mapping)?; + } Expr::Block { stmts, expr } => { for nested in stmts { remap_stmt_slots(nested, mapping)?; diff --git a/src/compiler/linker.rs b/src/compiler/linker.rs index 1ce18a49..bdc07fa3 100644 --- a/src/compiler/linker.rs +++ b/src/compiler/linker.rs @@ -154,7 +154,10 @@ pub(super) fn remap_guest_type_schemas(parsed: &mut FrontendIr, names: &HashMap< pub(super) fn remap_type_schema_names(schema: &mut TypeSchema, names: &HashMap) { match schema { - TypeSchema::Optional(inner) | TypeSchema::Array(inner) | TypeSchema::Map(inner) => { + TypeSchema::Optional(inner) + | TypeSchema::Option(inner) + | TypeSchema::Array(inner) + | TypeSchema::Map(inner) => { remap_type_schema_names(inner, names); } TypeSchema::Named(name, args) => { @@ -193,6 +196,7 @@ pub(super) fn remap_type_schema_names(schema: &mut TypeSchema, names: &HashMap { + remap_expression_type_names(value, names); + } + Expr::OptionMethod { + receiver, argument, .. + } => { + remap_expression_type_names(receiver, names); + remap_expression_type_names(argument, names); + } + Expr::OptionConstruct { + payload, + option_schema, + .. + } => { + remap_type_schema_names(option_schema, names); + if let Some(payload) = payload { + remap_expression_type_names(payload, names); + } + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + scrutinee_schema, + .. + } => { + remap_type_schema_names(scrutinee_schema, names); + remap_expression_type_names(value, names); + remap_expression_type_names(some_expr, names); + remap_expression_type_names(none_expr, names); + } Expr::Block { stmts, expr } => { for statement in stmts { remap_statement_type_names(statement, names); @@ -391,6 +426,7 @@ fn remap_expression_type_names(expression: &mut Expr, names: &HashMap Result<(), SourcePathError> { match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -1431,6 +1468,7 @@ fn remap_expr_indices( container_slot, key_slot, semantic_id, + .. } => { *container_slot = remap_local_index(*container_slot, local_base)?; *key_slot = remap_local_index(*key_slot, local_base)?; @@ -1640,6 +1678,104 @@ fn remap_expr_indices( flat_index_by_symbol, )?; } + Expr::OptionMethod { + receiver, + argument, + slots, + semantic_id, + .. + } => { + slots.receiver = remap_local_index(slots.receiver, local_base)?; + slots.argument = remap_local_index(slots.argument, local_base)?; + slots.some_binding = remap_local_index(slots.some_binding, local_base)?; + rebase_semantic_id(semantic_id, node_offset); + remap_expr_indices( + receiver, + local_base, + node_offset, + function_map, + flat_index_by_symbol, + )?; + remap_expr_indices( + argument, + local_base, + node_offset, + function_map, + flat_index_by_symbol, + )?; + } + Expr::ResultConvert { + value, + value_slot, + ok_binding, + err_binding, + semantic_id, + .. + } => { + *value_slot = remap_local_index(*value_slot, local_base)?; + *ok_binding = remap_local_index(*ok_binding, local_base)?; + *err_binding = remap_local_index(*err_binding, local_base)?; + rebase_semantic_id(semantic_id, node_offset); + remap_expr_indices( + value, + local_base, + node_offset, + function_map, + flat_index_by_symbol, + )?; + } + Expr::OptionConstruct { + payload, + semantic_id, + .. + } => { + rebase_semantic_id(semantic_id, node_offset); + if let Some(payload) = payload { + remap_expr_indices( + payload, + local_base, + node_offset, + function_map, + flat_index_by_symbol, + )?; + } + } + Expr::OptionMatch { + value, + value_slot, + some_binding, + some_expr, + none_expr, + semantic_id, + .. + } => { + *value_slot = remap_local_index(*value_slot, local_base)?; + if let Some(slot) = some_binding { + *slot = remap_local_index(*slot, local_base)?; + } + rebase_semantic_id(semantic_id, node_offset); + remap_expr_indices( + value, + local_base, + node_offset, + function_map, + flat_index_by_symbol, + )?; + remap_expr_indices( + some_expr, + local_base, + node_offset, + function_map, + flat_index_by_symbol, + )?; + remap_expr_indices( + none_expr, + local_base, + node_offset, + function_map, + flat_index_by_symbol, + )?; + } Expr::Add(lhs, rhs) | Expr::Sub(lhs, rhs) | Expr::Mul(lhs, rhs) @@ -1966,7 +2102,7 @@ fn rebase_parsed_semantic_index( let mut site = site.clone(); site.call_id = remap_node(site.call_id); for arg in &mut site.arguments { - arg.local = arg.local.map(&remap_slot).transpose()?; + arg.local = arg.local.map(remap_slot).transpose()?; } Ok(site) }) diff --git a/src/compiler/logical_boundary.rs b/src/compiler/logical_boundary.rs new file mode 100644 index 00000000..6100b373 --- /dev/null +++ b/src/compiler/logical_boundary.rs @@ -0,0 +1,1851 @@ +//! Compiler-private lowering of schema-directed JSON calls. +//! +//! Public `json::decode` / `json::encode` arities stay unchanged. A typed +//! decode call is rewritten to the fixed two-argument internal builtin and a +//! lossless schema constant before ownership planning. + +use crate::builtins::BuiltinFunction; +use crate::compiler::CompileError; +use crate::compiler::ir::{ + Expr, FrontendIr, FunctionImpl, LocalSlot, SemanticNodeId, Stmt, TypeSchema, +}; +use std::collections::HashMap; + +/// Which source producers receive the RustScript logical/JSON policy. +/// +/// `All` is a single-producer IR compiled by an already-selected RustScript +/// frontend. `Sources` maps RustScript producer names to their linked source +/// ids, so a merged compilation can keep other frontend capabilities. +#[derive(Clone, Debug)] +pub(super) enum SourceScope { + All, + Sources(HashMap), + None, +} + +impl SourceScope { + pub(super) fn all() -> Self { + Self::All + } + + pub(super) fn none() -> Self { + Self::None + } + + pub(super) fn sources(names: HashMap) -> Self { + if names.is_empty() { + Self::None + } else { + Self::Sources(names) + } + } +} + +/// Per-compilation site attribution for one lowering pass. +struct LoweringScope<'a> { + scope: &'a SourceScope, + call_sources: HashMap, + call_spans: HashMap, +} + +impl<'a> LoweringScope<'a> { + fn new(ir: &FrontendIr, scope: &'a SourceScope) -> Self { + let mut call_sources = HashMap::new(); + let mut call_spans = HashMap::new(); + if let Some(index) = ir.parsed_semantic_index.as_ref() { + for site in &index.call_sites { + call_sources.insert(site.id, site.callee_span.source_id); + call_spans.insert(site.id, site.callee_span); + } + } + Self { + scope, + call_sources, + call_spans, + } + } + + fn allows(&self, id: Option, default_source: Option<&str>) -> bool { + let SourceScope::Sources(names) = self.scope else { + return matches!(self.scope, SourceScope::All); + }; + if let Some(id) = id + && self + .call_sources + .get(&id) + .is_some_and(|source_id| names.values().any(|candidate| candidate == source_id)) + { + return true; + } + default_source.is_some_and(|name| names.contains_key(name)) + } +} + +fn call_site_id(expr: &Expr) -> Option { + match expr { + Expr::Call(_, _, _, _, id) => *id, + Expr::ModuleCall(_, _, _, id) => *id, + Expr::LocalCall(_, _, _, id) => *id, + _ => None, + } +} + +/// Value expressions of a presentation site, in the order their schemas are +/// recorded. `None` means this expression is not a site. Trusted print is the +/// language binding: no user impl and no catalog resolution. A same-name +/// concrete host or user function is not a site. +pub(super) fn presentation_inputs<'a>( + function_impls: &HashMap, + function_names: &HashMap, + expr: &'a Expr, +) -> Option> { + let Expr::Call(index, _, args, resolution, _) = expr else { + return None; + }; + if resolution.is_none() + && !function_impls.contains_key(index) + && function_names.get(index).map(String::as_str) == Some("print") + && args.len() == 1 + { + return Some(vec![print_input_expr(&args[0])]); + } + if BuiltinFunction::from_call_index(*index) == Some(BuiltinFunction::TypeOf) && args.len() == 1 + { + return Some(vec![&args[0]]); + } + if BuiltinFunction::from_call_index(*index) == Some(BuiltinFunction::FormatTemplate) + && args.len() == 2 + { + return Some(format_payload_exprs(&args[1])); + } + None +} + +pub(super) fn is_language_print( + function_impls: &HashMap, + function_names: &HashMap, + index: u16, +) -> bool { + !function_impls.contains_key(&index) + && function_names.get(&index).map(String::as_str) == Some("print") +} + +fn print_input_expr(arg: &Expr) -> &Expr { + let (inner, _) = split_print_newline(arg); + to_string_input(inner).unwrap_or(inner) +} + +fn split_print_newline(arg: &Expr) -> (&Expr, bool) { + match arg { + Expr::Add(left, right) if matches!(right.as_ref(), Expr::String(text) if text == "\n") => { + (left.as_ref(), true) + } + other => (other, false), + } +} + +fn to_string_input(expr: &Expr) -> Option<&Expr> { + let Expr::Call(index, _, args, _, _) = expr else { + return None; + }; + if BuiltinFunction::from_call_index(*index) == Some(BuiltinFunction::ToString) + && args.len() == 1 + { + Some(&args[0]) + } else { + None + } +} + +fn format_payload_exprs(expr: &Expr) -> Vec<&Expr> { + let mut values = Vec::new(); + collect_format_payload(expr, &mut values); + values +} + +fn collect_format_payload<'a>(expr: &'a Expr, values: &mut Vec<&'a Expr>) { + let Expr::Call(index, _, args, _, _) = expr else { + return; + }; + if BuiltinFunction::from_call_index(*index) != Some(BuiltinFunction::ArrayPush) + || args.len() != 2 + { + return; + } + collect_format_payload(&args[0], values); + values.push(&args[1]); +} + +pub(super) fn contains_logical_display(schema: &TypeSchema) -> bool { + match schema { + TypeSchema::Option(_) | TypeSchema::Unit | TypeSchema::Result { .. } => true, + TypeSchema::Array(inner) | TypeSchema::Map(inner) | TypeSchema::Optional(inner) => { + contains_logical_display(inner) + } + TypeSchema::ArrayTuple(items) => items.iter().any(contains_logical_display), + TypeSchema::ArrayTupleRest { prefix, rest } => { + prefix.iter().any(contains_logical_display) || contains_logical_display(rest) + } + TypeSchema::Object(fields) => fields.values().any(contains_logical_display), + TypeSchema::Named(_, args) => args.iter().any(contains_logical_display), + TypeSchema::Callable { params, result } => { + params.iter().any(contains_logical_display) || contains_logical_display(result) + } + _ => false, + } +} + +pub(super) fn lower_typed_json( + ir: &mut FrontendIr, + local_schemas: &[Option], + scope: &SourceScope, +) -> Result<(), CompileError> { + let lowering = LoweringScope::new(ir, scope); + attach_inferred_encode_schemas(ir, local_schemas, &lowering)?; + for (index, stmt) in ir.stmts.iter_mut().enumerate() { + let default_source = ir.stmt_sources.get(index).and_then(Option::as_deref); + lower_stmt(stmt, &lowering, default_source)?; + } + let indexes = ir.function_impls.keys().copied().collect::>(); + for index in indexes { + let default_source = ir.function_sources.get(&index).map(String::as_str).or(None); + let Some(function) = ir.function_impls.get_mut(&index) else { + continue; + }; + lower_function(function, &lowering, default_source)?; + } + Ok(()) +} + +fn lower_function( + function: &mut FunctionImpl, + lowering: &LoweringScope<'_>, + default_source: Option<&str>, +) -> Result<(), CompileError> { + for stmt in &mut function.body_stmts { + lower_stmt(stmt, lowering, default_source)?; + } + lower_expr(&mut function.body_expr, lowering, default_source) +} + +fn lower_stmt( + stmt: &mut Stmt, + lowering: &LoweringScope<'_>, + default_source: Option<&str>, +) -> Result<(), CompileError> { + match stmt { + Stmt::Let { expr, .. } | Stmt::Assign { expr, .. } | Stmt::Expr { expr, .. } => { + lower_expr(expr, lowering, default_source) + } + Stmt::ClosureLet { closure, .. } => lower_expr(&mut closure.body, lowering, default_source), + Stmt::IfElse { + condition, + then_branch, + else_branch, + .. + } => { + lower_expr(condition, lowering, default_source)?; + for stmt in then_branch.iter_mut().chain(else_branch.iter_mut()) { + lower_stmt(stmt, lowering, default_source)?; + } + Ok(()) + } + Stmt::For { + init, + condition, + post, + body, + .. + } => { + lower_stmt(init, lowering, default_source)?; + lower_expr(condition, lowering, default_source)?; + lower_stmt(post, lowering, default_source)?; + for stmt in body { + lower_stmt(stmt, lowering, default_source)?; + } + Ok(()) + } + Stmt::While { + condition, body, .. + } => { + lower_expr(condition, lowering, default_source)?; + for stmt in body { + lower_stmt(stmt, lowering, default_source)?; + } + Ok(()) + } + Stmt::Noop { .. } + | Stmt::FuncDecl { .. } + | Stmt::Break { .. } + | Stmt::Continue { .. } + | Stmt::Drop { .. } => Ok(()), + } +} + +fn lower_expr( + expr: &mut Expr, + lowering: &LoweringScope<'_>, + default_source: Option<&str>, +) -> Result<(), CompileError> { + match expr { + Expr::ResultConstruct { payload, .. } => lower_expr(payload, lowering, default_source), + Expr::ResultQuery { value, .. } + | Expr::ResultTry { value, .. } + | Expr::ResultConvert { value, .. } => lower_expr(value, lowering, default_source), + Expr::ResultMatch { + value, + ok_expr, + err_expr, + .. + } => { + lower_expr(value, lowering, default_source)?; + lower_expr(ok_expr, lowering, default_source)?; + lower_expr(err_expr, lowering, default_source) + } + Expr::ResultMethod { + receiver, argument, .. + } + | Expr::OptionMethod { + receiver, argument, .. + } => { + lower_expr(receiver, lowering, default_source)?; + lower_expr(argument, lowering, default_source) + } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + lower_expr(payload, lowering, default_source)?; + } + Ok(()) + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + lower_expr(value, lowering, default_source)?; + lower_expr(some_expr, lowering, default_source)?; + lower_expr(none_expr, lowering, default_source) + } + Expr::OptionalGet { container, key, .. } => { + lower_expr(container, lowering, default_source)?; + lower_expr(key, lowering, default_source) + } + Expr::OptionUnwrapOr { + value, fallback, .. + } => { + lower_expr(value, lowering, default_source)?; + lower_expr(fallback, lowering, default_source) + } + Expr::Call(index, type_args, args, _, semantic_id) => { + for arg in args.iter_mut() { + lower_expr(arg, lowering, default_source)?; + } + let builtin = BuiltinFunction::from_call_index(*index); + let allowed = lowering.allows(*semantic_id, default_source); + if builtin == Some(BuiltinFunction::JsonDecode) + && type_args.len() == 1 + && args.len() == 1 + && allowed + { + let schema = type_args[0].clone(); + rewrite_typed_json(expr, schema, BuiltinFunction::JsonDecodeTyped)?; + } else if builtin == Some(BuiltinFunction::JsonEncode) + && type_args.len() == 1 + && args.len() == 1 + && allowed + { + let schema = type_args[0].clone(); + rewrite_typed_json(expr, schema, BuiltinFunction::JsonEncodeTyped)?; + } else if allowed + && matches!( + builtin, + Some(BuiltinFunction::JsonDecode | BuiltinFunction::JsonEncode) + ) + { + let builtin = builtin.expect("json builtin"); + return Err(CompileError::StrictTypingRequired { + line: None, + source_name: None, + detail: format!( + "RustScript source JSON calls require a concrete typed JSON target; untyped {} is not part of the RustScript boundary", + builtin.name() + ), + span: semantic_id.and_then(|id| lowering.call_spans.get(&id).copied()), + }); + } + Ok(()) + } + Expr::ModuleCall(_, _, args, _) | Expr::LocalCall(_, _, args, _) => { + for arg in args { + lower_expr(arg, lowering, default_source)?; + } + Ok(()) + } + Expr::Closure(closure) => lower_expr(&mut closure.body, lowering, default_source), + Expr::ClosureCall(closure, args) => { + lower_expr(&mut closure.body, lowering, default_source)?; + for arg in args { + lower_expr(arg, lowering, default_source)?; + } + Ok(()) + } + Expr::Add(lhs, rhs) + | Expr::Sub(lhs, rhs) + | Expr::Mul(lhs, rhs) + | Expr::Div(lhs, rhs) + | Expr::Mod(lhs, rhs) + | Expr::And(lhs, rhs) + | Expr::Or(lhs, rhs) + | Expr::Eq(lhs, rhs) + | Expr::Lt(lhs, rhs) + | Expr::Gt(lhs, rhs) => { + lower_expr(lhs, lowering, default_source)?; + lower_expr(rhs, lowering, default_source) + } + Expr::Neg(inner) + | Expr::Not(inner) + | Expr::ToOwned(inner) + | Expr::Borrow(inner) + | Expr::BorrowMut(inner) => lower_expr(inner, lowering, default_source), + Expr::IfElse { + condition, + then_expr, + else_expr, + } => { + lower_expr(condition, lowering, default_source)?; + lower_expr(then_expr, lowering, default_source)?; + lower_expr(else_expr, lowering, default_source) + } + Expr::Match { + value, + arms, + default, + .. + } => { + lower_expr(value, lowering, default_source)?; + for (_, arm) in arms { + lower_expr(arm, lowering, default_source)?; + } + lower_expr(default, lowering, default_source) + } + Expr::Block { stmts, expr } => { + for stmt in stmts { + lower_stmt(stmt, lowering, default_source)?; + } + lower_expr(expr, lowering, default_source) + } + Expr::Null + | Expr::Unit + | Expr::Int(_) + | Expr::Float(_) + | Expr::Bool(_) + | Expr::String(_) + | Expr::Bytes(_) + | Expr::FunctionRef(..) + | Expr::ModuleFunctionRef(..) + | Expr::UnresolvedFunctionRef { .. } + | Expr::Var(_) + | Expr::MoveVar(_) + | Expr::MoveField { .. } + | Expr::MoveIndex { .. } => Ok(()), + } +} + +fn rewrite_typed_json( + expr: &mut Expr, + schema: crate::compiler::ir::TypeSchema, + builtin: BuiltinFunction, +) -> Result<(), CompileError> { + let descriptor = crate::compiler::type_schema_codec::encode_type_schema_bytes(&schema) + .map_err(|err| CompileError::HostCallResolve { + line: None, + source_name: None, + detail: format!("typed JSON descriptor: {err}"), + span: None, + })?; + let Expr::Call(_, _, args, _, semantic_id) = expr else { + unreachable!("typed json rewrite requires a call"); + }; + let value = args.remove(0); + let id = semantic_id.take(); + *expr = Expr::Call( + builtin.call_index(), + vec![schema], + vec![value, Expr::Bytes(descriptor)], + None, + id, + ); + Ok(()) +} + +fn attach_inferred_encode_schemas( + ir: &mut FrontendIr, + local_schemas: &[Option], + lowering: &LoweringScope<'_>, +) -> Result<(), CompileError> { + let mut facts = Vec::new(); + { + let typed = super::FrontendTypedIr { + source_id: None, + ir, + local_schemas, + host_api_catalog: None, + source_map: None, + }; + for stmt in &ir.stmts { + collect_encode_stmt(stmt, &typed, &mut facts); + } + for function in ir.function_impls.values() { + for stmt in &function.body_stmts { + collect_encode_stmt(stmt, &typed, &mut facts); + } + collect_encode_expr(&function.body_expr, &typed, &mut facts); + } + } + let mut facts = facts.into_iter(); + for (index, stmt) in ir.stmts.iter_mut().enumerate() { + let default_source = ir.stmt_sources.get(index).and_then(Option::as_deref); + apply_encode_stmt(stmt, lowering, default_source, &mut facts)?; + } + for (index, function) in ir.function_impls.iter_mut() { + let default_source = ir.function_sources.get(index).map(String::as_str).or(None); + for stmt in &mut function.body_stmts { + apply_encode_stmt(stmt, lowering, default_source, &mut facts)?; + } + apply_encode_expr( + &mut function.body_expr, + lowering, + default_source, + &mut facts, + )?; + } + Ok(()) +} + +fn collect_encode_stmt( + stmt: &Stmt, + typed: &super::FrontendTypedIr<'_>, + facts: &mut Vec>, +) { + match stmt { + Stmt::Let { expr, .. } | Stmt::Assign { expr, .. } | Stmt::Expr { expr, .. } => { + collect_encode_expr(expr, typed, facts); + } + Stmt::ClosureLet { closure, .. } => collect_encode_expr(&closure.body, typed, facts), + Stmt::IfElse { + condition, + then_branch, + else_branch, + .. + } => { + collect_encode_expr(condition, typed, facts); + for nested in then_branch.iter().chain(else_branch) { + collect_encode_stmt(nested, typed, facts); + } + } + Stmt::While { + condition, body, .. + } => { + collect_encode_expr(condition, typed, facts); + for nested in body { + collect_encode_stmt(nested, typed, facts); + } + } + Stmt::For { + init, + condition, + post, + body, + .. + } => { + collect_encode_stmt(init, typed, facts); + collect_encode_expr(condition, typed, facts); + collect_encode_stmt(post, typed, facts); + for nested in body { + collect_encode_stmt(nested, typed, facts); + } + } + Stmt::Noop { .. } + | Stmt::FuncDecl { .. } + | Stmt::Break { .. } + | Stmt::Continue { .. } + | Stmt::Drop { .. } => {} + } +} + +fn collect_encode_expr( + expr: &Expr, + typed: &super::FrontendTypedIr<'_>, + facts: &mut Vec>, +) { + match expr { + Expr::Call(index, type_args, args, _, _) => { + for arg in args { + collect_encode_expr(arg, typed, facts); + } + if BuiltinFunction::from_call_index(*index) == Some(BuiltinFunction::JsonEncode) + && type_args.is_empty() + && args.len() == 1 + { + facts.push(typed.schema_of_expr(&args[0])); + } + } + Expr::Block { stmts, expr } => { + for stmt in stmts { + collect_encode_stmt(stmt, typed, facts); + } + collect_encode_expr(expr, typed, facts); + } + Expr::Add(lhs, rhs) + | Expr::Sub(lhs, rhs) + | Expr::Mul(lhs, rhs) + | Expr::Div(lhs, rhs) + | Expr::Mod(lhs, rhs) + | Expr::And(lhs, rhs) + | Expr::Or(lhs, rhs) + | Expr::Eq(lhs, rhs) + | Expr::Lt(lhs, rhs) + | Expr::Gt(lhs, rhs) => { + collect_encode_expr(lhs, typed, facts); + collect_encode_expr(rhs, typed, facts); + } + Expr::IfElse { + condition, + then_expr, + else_expr, + } => { + collect_encode_expr(condition, typed, facts); + collect_encode_expr(then_expr, typed, facts); + collect_encode_expr(else_expr, typed, facts); + } + other => visit_encode_children(other, typed, facts), + } +} + +fn visit_encode_children( + expr: &Expr, + typed: &super::FrontendTypedIr<'_>, + facts: &mut Vec>, +) { + match expr { + Expr::ResultConstruct { payload, .. } + | Expr::ResultQuery { value: payload, .. } + | Expr::ResultTry { value: payload, .. } + | Expr::ResultConvert { value: payload, .. } + | Expr::Neg(payload) + | Expr::Not(payload) + | Expr::ToOwned(payload) + | Expr::Borrow(payload) + | Expr::BorrowMut(payload) => collect_encode_expr(payload, typed, facts), + Expr::ResultMatch { + value, + ok_expr, + err_expr, + .. + } => { + collect_encode_expr(value, typed, facts); + collect_encode_expr(ok_expr, typed, facts); + collect_encode_expr(err_expr, typed, facts); + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + collect_encode_expr(value, typed, facts); + collect_encode_expr(some_expr, typed, facts); + collect_encode_expr(none_expr, typed, facts); + } + Expr::OptionConstruct { + payload: Some(payload), + .. + } => { + collect_encode_expr(payload, typed, facts); + } + Expr::ModuleCall(_, _, args, _) | Expr::LocalCall(_, _, args, _) => { + for arg in args { + collect_encode_expr(arg, typed, facts); + } + } + Expr::ClosureCall(closure, args) => { + collect_encode_expr(&closure.body, typed, facts); + for arg in args { + collect_encode_expr(arg, typed, facts); + } + } + Expr::ResultMethod { + receiver, argument, .. + } + | Expr::OptionMethod { + receiver, argument, .. + } => { + collect_encode_expr(receiver, typed, facts); + collect_encode_expr(argument, typed, facts); + } + Expr::OptionUnwrapOr { + value, fallback, .. + } => { + collect_encode_expr(value, typed, facts); + collect_encode_expr(fallback, typed, facts); + } + Expr::OptionalGet { container, key, .. } => { + collect_encode_expr(container, typed, facts); + collect_encode_expr(key, typed, facts); + } + Expr::Closure(closure) => collect_encode_expr(&closure.body, typed, facts), + Expr::Match { + value, + arms, + default, + .. + } => { + collect_encode_expr(value, typed, facts); + for (_, arm) in arms { + collect_encode_expr(arm, typed, facts); + } + collect_encode_expr(default, typed, facts); + } + _ => {} + } +} + +fn apply_encode_stmt( + stmt: &mut Stmt, + lowering: &LoweringScope<'_>, + default_source: Option<&str>, + facts: &mut std::vec::IntoIter>, +) -> Result<(), CompileError> { + match stmt { + Stmt::Let { expr, .. } | Stmt::Assign { expr, .. } | Stmt::Expr { expr, .. } => { + apply_encode_expr(expr, lowering, default_source, facts) + } + Stmt::ClosureLet { closure, .. } => { + apply_encode_expr(&mut closure.body, lowering, default_source, facts) + } + Stmt::IfElse { + condition, + then_branch, + else_branch, + .. + } => { + apply_encode_expr(condition, lowering, default_source, facts)?; + for nested in then_branch.iter_mut().chain(else_branch.iter_mut()) { + apply_encode_stmt(nested, lowering, default_source, facts)?; + } + Ok(()) + } + Stmt::While { + condition, body, .. + } => { + apply_encode_expr(condition, lowering, default_source, facts)?; + for nested in body { + apply_encode_stmt(nested, lowering, default_source, facts)?; + } + Ok(()) + } + Stmt::For { + init, + condition, + post, + body, + .. + } => { + apply_encode_stmt(init, lowering, default_source, facts)?; + apply_encode_expr(condition, lowering, default_source, facts)?; + apply_encode_stmt(post, lowering, default_source, facts)?; + for nested in body { + apply_encode_stmt(nested, lowering, default_source, facts)?; + } + Ok(()) + } + Stmt::Noop { .. } + | Stmt::FuncDecl { .. } + | Stmt::Break { .. } + | Stmt::Continue { .. } + | Stmt::Drop { .. } => Ok(()), + } +} + +fn typed_json_schema_is_concrete(schema: &TypeSchema) -> bool { + match schema { + TypeSchema::Int + | TypeSchema::Float + | TypeSchema::Number + | TypeSchema::Bool + | TypeSchema::String + | TypeSchema::Unit => true, + TypeSchema::Array(inner) | TypeSchema::Map(inner) | TypeSchema::Option(inner) => { + typed_json_schema_is_concrete(inner) + } + TypeSchema::ArrayTuple(items) => items.iter().all(typed_json_schema_is_concrete), + TypeSchema::Object(fields) => fields.values().all(typed_json_schema_is_concrete), + TypeSchema::Named(_, args) => args.iter().all(typed_json_schema_is_concrete), + TypeSchema::Result { ok, err } => { + typed_json_schema_is_concrete(ok) && typed_json_schema_is_concrete(err) + } + TypeSchema::Unknown + | TypeSchema::Null + | TypeSchema::Optional(_) + | TypeSchema::GenericParam(_) + | TypeSchema::Bytes + | TypeSchema::Callable { .. } + | TypeSchema::Resource(_) + | TypeSchema::ArrayTupleRest { .. } => false, + } +} + +fn apply_encode_expr( + expr: &mut Expr, + lowering: &LoweringScope<'_>, + default_source: Option<&str>, + facts: &mut std::vec::IntoIter>, +) -> Result<(), CompileError> { + match expr { + Expr::Call(index, type_args, args, _, semantic_id) => { + for arg in args.iter_mut() { + apply_encode_expr(arg, lowering, default_source, facts)?; + } + if BuiltinFunction::from_call_index(*index) == Some(BuiltinFunction::JsonEncode) + && type_args.is_empty() + && args.len() == 1 + { + let fact = facts.next(); + if lowering.allows(*semantic_id, default_source) + && let Some(Some(schema)) = fact + && typed_json_schema_is_concrete(&schema) + { + type_args.push(schema); + } + } + Ok(()) + } + Expr::Block { stmts, expr } => { + for stmt in stmts { + apply_encode_stmt(stmt, lowering, default_source, facts)?; + } + apply_encode_expr(expr, lowering, default_source, facts) + } + Expr::Add(lhs, rhs) + | Expr::Sub(lhs, rhs) + | Expr::Mul(lhs, rhs) + | Expr::Div(lhs, rhs) + | Expr::Mod(lhs, rhs) + | Expr::And(lhs, rhs) + | Expr::Or(lhs, rhs) + | Expr::Eq(lhs, rhs) + | Expr::Lt(lhs, rhs) + | Expr::Gt(lhs, rhs) => { + apply_encode_expr(lhs, lowering, default_source, facts)?; + apply_encode_expr(rhs, lowering, default_source, facts) + } + Expr::IfElse { + condition, + then_expr, + else_expr, + } => { + apply_encode_expr(condition, lowering, default_source, facts)?; + apply_encode_expr(then_expr, lowering, default_source, facts)?; + apply_encode_expr(else_expr, lowering, default_source, facts) + } + other => apply_encode_children(other, lowering, default_source, facts), + } +} + +fn apply_encode_children( + expr: &mut Expr, + lowering: &LoweringScope<'_>, + default_source: Option<&str>, + facts: &mut std::vec::IntoIter>, +) -> Result<(), CompileError> { + match expr { + Expr::ResultConstruct { payload, .. } + | Expr::ResultQuery { value: payload, .. } + | Expr::ResultTry { value: payload, .. } + | Expr::ResultConvert { value: payload, .. } + | Expr::Neg(payload) + | Expr::Not(payload) + | Expr::ToOwned(payload) + | Expr::Borrow(payload) + | Expr::BorrowMut(payload) => apply_encode_expr(payload, lowering, default_source, facts), + Expr::ResultMatch { + value, + ok_expr, + err_expr, + .. + } => { + apply_encode_expr(value, lowering, default_source, facts)?; + apply_encode_expr(ok_expr, lowering, default_source, facts)?; + apply_encode_expr(err_expr, lowering, default_source, facts) + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + apply_encode_expr(value, lowering, default_source, facts)?; + apply_encode_expr(some_expr, lowering, default_source, facts)?; + apply_encode_expr(none_expr, lowering, default_source, facts) + } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + apply_encode_expr(payload, lowering, default_source, facts)?; + } + Ok(()) + } + Expr::ModuleCall(_, _, args, _) | Expr::LocalCall(_, _, args, _) => { + for arg in args { + apply_encode_expr(arg, lowering, default_source, facts)?; + } + Ok(()) + } + Expr::ClosureCall(closure, args) => { + apply_encode_expr(&mut closure.body, lowering, default_source, facts)?; + for arg in args { + apply_encode_expr(arg, lowering, default_source, facts)?; + } + Ok(()) + } + Expr::ResultMethod { + receiver, argument, .. + } + | Expr::OptionMethod { + receiver, argument, .. + } => { + apply_encode_expr(receiver, lowering, default_source, facts)?; + apply_encode_expr(argument, lowering, default_source, facts) + } + Expr::OptionUnwrapOr { + value, fallback, .. + } => { + apply_encode_expr(value, lowering, default_source, facts)?; + apply_encode_expr(fallback, lowering, default_source, facts) + } + Expr::OptionalGet { container, key, .. } => { + apply_encode_expr(container, lowering, default_source, facts)?; + apply_encode_expr(key, lowering, default_source, facts) + } + Expr::Closure(closure) => { + apply_encode_expr(&mut closure.body, lowering, default_source, facts) + } + Expr::Match { + value, + arms, + default, + .. + } => { + apply_encode_expr(value, lowering, default_source, facts)?; + for (_, arm) in arms { + apply_encode_expr(arm, lowering, default_source, facts)?; + } + apply_encode_expr(default, lowering, default_source, facts) + } + _ => Ok(()), + } +} + +struct RejectContext<'a, 'b> { + lowering: &'b LoweringScope<'b>, + default_source: Option<&'a str>, +} + +impl RejectContext<'_, '_> { + fn allows(&self, expr: &Expr) -> bool { + self.lowering + .allows(call_site_id(expr), self.default_source) + } +} + +pub(super) fn reject_dynamic_host_output( + ir: &FrontendIr, + scope: &SourceScope, +) -> Result<(), CompileError> { + let lowering = LoweringScope::new(ir, scope); + for (index, stmt) in ir.stmts.iter().enumerate() { + let default_source = ir.stmt_sources.get(index).and_then(Option::as_deref); + let ctx = RejectContext { + lowering: &lowering, + default_source, + }; + reject_dynamic_host_stmt(&ctx, stmt)?; + } + for (index, function) in ir.function_impls.iter() { + let default_source = ir.function_sources.get(index).map(String::as_str).or(None); + let ctx = RejectContext { + lowering: &lowering, + default_source, + }; + for stmt in &function.body_stmts { + reject_dynamic_host_stmt(&ctx, stmt)?; + } + reject_dynamic_host_expr(&ctx, &function.body_expr)?; + } + Ok(()) +} + +fn reject_dynamic_host_stmt(ctx: &RejectContext<'_, '_>, stmt: &Stmt) -> Result<(), CompileError> { + match stmt { + Stmt::Let { expr, .. } | Stmt::Assign { expr, .. } | Stmt::Expr { expr, .. } => { + reject_dynamic_host_expr(ctx, expr) + } + Stmt::ClosureLet { closure, .. } => reject_dynamic_host_expr(ctx, &closure.body), + Stmt::IfElse { + condition, + then_branch, + else_branch, + .. + } => { + reject_dynamic_host_expr(ctx, condition)?; + for nested in then_branch.iter().chain(else_branch) { + reject_dynamic_host_stmt(ctx, nested)?; + } + Ok(()) + } + Stmt::While { + condition, body, .. + } => { + reject_dynamic_host_expr(ctx, condition)?; + for nested in body { + reject_dynamic_host_stmt(ctx, nested)?; + } + Ok(()) + } + Stmt::For { + init, + condition, + post, + body, + .. + } => { + reject_dynamic_host_stmt(ctx, init)?; + reject_dynamic_host_expr(ctx, condition)?; + reject_dynamic_host_stmt(ctx, post)?; + for nested in body { + reject_dynamic_host_stmt(ctx, nested)?; + } + Ok(()) + } + Stmt::Noop { .. } + | Stmt::FuncDecl { .. } + | Stmt::Break { .. } + | Stmt::Continue { .. } + | Stmt::Drop { .. } => Ok(()), + } +} + +fn reject_dynamic_host_expr(ctx: &RejectContext<'_, '_>, expr: &Expr) -> Result<(), CompileError> { + if ctx.allows(expr) + && let Some(resolved) = expr.host_call_resolution() + && host_output_forbidden(&resolved.host_return_type) + { + return Err(CompileError::StrictTypingRequired { + line: None, + source_name: None, + detail: format!( + "host function '{}' returns a dynamic or legacy-null schema, which RustScript does not admit", + resolved.name + ), + span: None, + }); + } + // Exhaustive child traversal: every structured position that can hold a + // value expression must be visited, including match/option/result + // carriers, projection and method nodes, and a closure-call body. Guest + // consumption (host arguments) and the timer callback result exception + // stay outside this host-to-guest output check. + match expr { + Expr::Call(_, _, args, _, _) + | Expr::ModuleCall(_, _, args, _) + | Expr::LocalCall(_, _, args, _) => { + for arg in args { + reject_dynamic_host_expr(ctx, arg)?; + } + } + Expr::ClosureCall(closure, args) => { + reject_dynamic_host_expr(ctx, &closure.body)?; + for arg in args { + reject_dynamic_host_expr(ctx, arg)?; + } + } + Expr::Block { stmts, expr } => { + for stmt in stmts { + reject_dynamic_host_stmt(ctx, stmt)?; + } + reject_dynamic_host_expr(ctx, expr)?; + } + Expr::Add(lhs, rhs) + | Expr::Sub(lhs, rhs) + | Expr::Mul(lhs, rhs) + | Expr::Div(lhs, rhs) + | Expr::Mod(lhs, rhs) + | Expr::And(lhs, rhs) + | Expr::Or(lhs, rhs) + | Expr::Eq(lhs, rhs) + | Expr::Lt(lhs, rhs) + | Expr::Gt(lhs, rhs) => { + reject_dynamic_host_expr(ctx, lhs)?; + reject_dynamic_host_expr(ctx, rhs)?; + } + Expr::Neg(inner) + | Expr::Not(inner) + | Expr::ToOwned(inner) + | Expr::Borrow(inner) + | Expr::BorrowMut(inner) => reject_dynamic_host_expr(ctx, inner)?, + Expr::IfElse { + condition, + then_expr, + else_expr, + } => { + reject_dynamic_host_expr(ctx, condition)?; + reject_dynamic_host_expr(ctx, then_expr)?; + reject_dynamic_host_expr(ctx, else_expr)?; + } + Expr::Closure(closure) => reject_dynamic_host_expr(ctx, &closure.body)?, + Expr::Match { + value, + arms, + default, + .. + } => { + reject_dynamic_host_expr(ctx, value)?; + for (_, arm) in arms { + reject_dynamic_host_expr(ctx, arm)?; + } + reject_dynamic_host_expr(ctx, default)?; + } + Expr::ResultConstruct { payload, .. } => reject_dynamic_host_expr(ctx, payload)?, + Expr::ResultMatch { + value, + ok_expr, + err_expr, + .. + } => { + reject_dynamic_host_expr(ctx, value)?; + reject_dynamic_host_expr(ctx, ok_expr)?; + reject_dynamic_host_expr(ctx, err_expr)?; + } + Expr::ResultQuery { value, .. } + | Expr::ResultTry { value, .. } + | Expr::ResultConvert { value, .. } => reject_dynamic_host_expr(ctx, value)?, + Expr::ResultMethod { + receiver, argument, .. + } + | Expr::OptionMethod { + receiver, argument, .. + } => { + reject_dynamic_host_expr(ctx, receiver)?; + reject_dynamic_host_expr(ctx, argument)?; + } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + reject_dynamic_host_expr(ctx, payload)?; + } + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + reject_dynamic_host_expr(ctx, value)?; + reject_dynamic_host_expr(ctx, some_expr)?; + reject_dynamic_host_expr(ctx, none_expr)?; + } + Expr::OptionalGet { container, key, .. } => { + reject_dynamic_host_expr(ctx, container)?; + reject_dynamic_host_expr(ctx, key)?; + } + Expr::OptionUnwrapOr { + value, fallback, .. + } => { + reject_dynamic_host_expr(ctx, value)?; + reject_dynamic_host_expr(ctx, fallback)?; + } + Expr::Null + | Expr::Unit + | Expr::Int(_) + | Expr::Float(_) + | Expr::Bool(_) + | Expr::String(_) + | Expr::Bytes(_) + | Expr::FunctionRef(..) + | Expr::ModuleFunctionRef(..) + | Expr::UnresolvedFunctionRef { .. } + | Expr::Var(_) + | Expr::MoveVar(_) + | Expr::MoveField { .. } + | Expr::MoveIndex { .. } => {} + } + Ok(()) +} + +fn host_output_forbidden(schema: &crate::host_api::HostTypeSchema) -> bool { + use crate::host_api::HostTypeSchema; + match schema { + HostTypeSchema::Unknown | HostTypeSchema::Null | HostTypeSchema::Optional(_) => true, + HostTypeSchema::Array(inner) + | HostTypeSchema::Map(inner) + | HostTypeSchema::Option(inner) => host_output_forbidden(inner), + HostTypeSchema::Result { ok, err } => { + host_output_forbidden(ok) || host_output_forbidden(err) + } + HostTypeSchema::Callable { result, .. } => host_output_forbidden(result), + HostTypeSchema::Named { fields, .. } => { + fields.iter().any(|field| host_output_forbidden(&field.ty)) + } + HostTypeSchema::Unit + | HostTypeSchema::Int + | HostTypeSchema::Float + | HostTypeSchema::Number + | HostTypeSchema::Bool + | HostTypeSchema::String + | HostTypeSchema::Bytes + | HostTypeSchema::Resource(_) => false, + } +} + +/// Rewrite context shared by the presentation walk. +struct RewriteContext<'a, 'b> { + names: &'a HashMap, + impls: &'a HashMap, + lowering: &'b LoweringScope<'b>, + default_source: Option<&'a str>, +} + +impl RewriteContext<'_, '_> { + fn allows(&self, id: Option) -> bool { + self.lowering.allows(id, self.default_source) + } +} + +pub(super) fn lower_logical_print( + ir: &mut FrontendIr, + typing_mode: super::TypingMode, + entry_local_types: &[super::typing::EntryLocalType], + scope: &SourceScope, +) -> Result<(), CompileError> { + let facts = super::typing::presentation_site_schemas(ir, typing_mode, entry_local_types); + let mut facts = facts.into_iter(); + let names: HashMap = ir + .functions + .iter() + .map(|function| (function.index, function.name.clone())) + .collect(); + let impls: HashMap = ir + .function_impls + .keys() + .copied() + .map(|index| (index, ())) + .collect(); + let lowering = LoweringScope::new(ir, scope); + let mut aliases = HashMap::new(); + for (index, stmt) in ir.stmts.iter_mut().enumerate() { + let default_source = ir.stmt_sources.get(index).and_then(Option::as_deref); + let ctx = RewriteContext { + names: &names, + impls: &impls, + lowering: &lowering, + default_source, + }; + rewrite_stmt(stmt, &ctx, &mut facts, &mut aliases, &mut ir.locals)?; + } + let mut indexes = ir.function_impls.keys().copied().collect::>(); + indexes.sort_unstable(); + for index in indexes { + let default_source = ir.function_sources.get(&index).map(String::as_str).or(None); + let Some(function) = ir.function_impls.get_mut(&index) else { + continue; + }; + let ctx = RewriteContext { + names: &names, + impls: &impls, + lowering: &lowering, + default_source, + }; + let mut function_aliases = HashMap::new(); + rewrite_stmts( + &mut function.body_stmts, + &ctx, + &mut facts, + &mut function_aliases, + &mut ir.locals, + )?; + rewrite_expr( + &mut function.body_expr, + &ctx, + &mut facts, + &mut function_aliases, + &mut ir.locals, + )?; + } + if facts.next().is_some() { + return Err(presentation_error("unused presentation fact")); + } + Ok(()) +} + +fn rewrite_stmts( + stmts: &mut [Stmt], + ctx: &RewriteContext<'_, '_>, + facts: &mut std::vec::IntoIter>, + aliases: &mut HashMap, + locals: &mut usize, +) -> Result<(), CompileError> { + for stmt in stmts { + rewrite_stmt(stmt, ctx, facts, aliases, locals)?; + } + Ok(()) +} + +fn rewrite_stmt( + stmt: &mut Stmt, + ctx: &RewriteContext<'_, '_>, + facts: &mut std::vec::IntoIter>, + aliases: &mut HashMap, + locals: &mut usize, +) -> Result<(), CompileError> { + match stmt { + Stmt::Let { index, expr, .. } | Stmt::Assign { index, expr, .. } => { + rewrite_expr(expr, ctx, facts, aliases, locals)?; + note_print_alias(*index, expr, ctx.names, ctx.impls, aliases); + } + Stmt::Expr { expr, .. } => rewrite_expr(expr, ctx, facts, aliases, locals)?, + Stmt::IfElse { + condition, + then_branch, + else_branch, + .. + } => { + rewrite_expr(condition, ctx, facts, aliases, locals)?; + let mut then_aliases = aliases.clone(); + let mut else_aliases = aliases.clone(); + rewrite_stmts(then_branch, ctx, facts, &mut then_aliases, locals)?; + rewrite_stmts(else_branch, ctx, facts, &mut else_aliases, locals)?; + } + Stmt::While { + condition, body, .. + } => { + rewrite_expr(condition, ctx, facts, aliases, locals)?; + rewrite_stmts(body, ctx, facts, &mut aliases.clone(), locals)?; + } + Stmt::For { + init, + condition, + post, + body, + .. + } => { + rewrite_stmt(init, ctx, facts, aliases, locals)?; + rewrite_expr(condition, ctx, facts, aliases, locals)?; + rewrite_stmt(post, ctx, facts, aliases, locals)?; + rewrite_stmts(body, ctx, facts, aliases, locals)?; + } + Stmt::ClosureLet { closure, .. } => { + rewrite_closure(closure, ctx, facts, aliases, locals)?; + } + Stmt::Noop { .. } + | Stmt::FuncDecl { .. } + | Stmt::Break { .. } + | Stmt::Continue { .. } + | Stmt::Drop { .. } => {} + } + Ok(()) +} + +fn rewrite_expr( + expr: &mut Expr, + ctx: &RewriteContext<'_, '_>, + facts: &mut std::vec::IntoIter>, + aliases: &mut HashMap, + locals: &mut usize, +) -> Result<(), CompileError> { + if let Expr::Closure(closure) = expr { + return rewrite_closure(closure, ctx, facts, aliases, locals); + } + if let Expr::Block { stmts, expr } = expr { + let mut nested = aliases.clone(); + rewrite_stmts(stmts, ctx, facts, &mut nested, locals)?; + return rewrite_expr(expr, ctx, facts, &mut nested, locals); + } + if let Expr::LocalCall(slot, _, args, semantic_id) = expr + && args.len() == 1 + && aliases.contains_key(slot) + { + let allowed = ctx.allows(*semantic_id); + let schema = next_fact(facts)?; + rewrite_expr(&mut args[0], ctx, facts, aliases, locals)?; + if allowed && let Some(schema) = schema { + return rewrite_print_call(expr, schema, false, locals); + } + return Ok(()); + } + if presentation_inputs_mut(ctx.names, ctx.impls, expr).is_some() { + return rewrite_presentation_site(expr, ctx, facts, aliases, locals); + } + match expr { + Expr::IfElse { + condition, + then_expr, + else_expr, + } => { + rewrite_expr(condition, ctx, facts, aliases, locals)?; + rewrite_expr(then_expr, ctx, facts, aliases, locals)?; + rewrite_expr(else_expr, ctx, facts, aliases, locals)?; + } + Expr::Call(_, _, args, _, _) + | Expr::LocalCall(_, _, args, _) + | Expr::ModuleCall(_, _, args, _) + | Expr::ClosureCall(_, args) => { + for arg in args { + rewrite_expr(arg, ctx, facts, aliases, locals)?; + } + } + Expr::Add(lhs, rhs) + | Expr::Sub(lhs, rhs) + | Expr::Mul(lhs, rhs) + | Expr::Div(lhs, rhs) + | Expr::Mod(lhs, rhs) + | Expr::Eq(lhs, rhs) + | Expr::Lt(lhs, rhs) + | Expr::Gt(lhs, rhs) + | Expr::And(lhs, rhs) + | Expr::Or(lhs, rhs) => { + rewrite_expr(lhs, ctx, facts, aliases, locals)?; + rewrite_expr(rhs, ctx, facts, aliases, locals)?; + } + Expr::Neg(inner) + | Expr::Not(inner) + | Expr::ToOwned(inner) + | Expr::Borrow(inner) + | Expr::BorrowMut(inner) => { + rewrite_expr(inner, ctx, facts, aliases, locals)?; + } + Expr::OptionConstruct { + payload: Some(payload), + .. + } => { + rewrite_expr(payload, ctx, facts, aliases, locals)?; + } + Expr::ResultConstruct { payload, .. } + | Expr::ResultQuery { value: payload, .. } + | Expr::ResultTry { value: payload, .. } => { + rewrite_expr(payload, ctx, facts, aliases, locals)?; + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + rewrite_expr(value, ctx, facts, aliases, locals)?; + rewrite_expr(some_expr, ctx, facts, aliases, locals)?; + rewrite_expr(none_expr, ctx, facts, aliases, locals)?; + } + _ => {} + } + Ok(()) +} + +fn rewrite_closure( + closure: &mut crate::compiler::ir::ClosureExpr, + ctx: &RewriteContext<'_, '_>, + facts: &mut std::vec::IntoIter>, + aliases: &HashMap, + locals: &mut usize, +) -> Result<(), CompileError> { + let mut inner = HashMap::new(); + for (source, capture) in &closure.capture_copies { + if aliases.contains_key(source) { + inner.insert(*capture, ()); + } + } + rewrite_expr(&mut closure.body, ctx, facts, &mut inner, locals) +} + +fn rewrite_presentation_site( + expr: &mut Expr, + ctx: &RewriteContext<'_, '_>, + facts: &mut std::vec::IntoIter>, + aliases: &mut HashMap, + locals: &mut usize, +) -> Result<(), CompileError> { + let kind = presentation_kind(ctx.names, ctx.impls, expr); + let allowed = ctx.allows(call_site_id(expr)); + let count = match &kind { + PresentationKind::Print | PresentationKind::Type => 1, + PresentationKind::Format(count) => *count, + PresentationKind::None => 0, + }; + let mut schemas = Vec::with_capacity(count); + for _ in 0..count { + schemas.push(next_fact(facts)?); + } + match kind { + PresentationKind::Print => { + let newline = print_newline(expr); + if let Expr::Call(_, _, args, _, _) = expr + && let Some(arg) = args.get_mut(0) + { + if allowed { + rewrite_print_input(arg, ctx, facts, aliases, locals)?; + } else { + rewrite_expr(arg, ctx, facts, aliases, locals)?; + } + } + if allowed && let Some(schema) = schemas.pop().flatten() { + rewrite_print_call(expr, schema, newline, locals)?; + } + } + PresentationKind::Type => { + if let Expr::Call(_, _, args, _, _) = expr + && let Some(arg) = args.get_mut(0) + { + rewrite_expr(arg, ctx, facts, aliases, locals)?; + } + if allowed + && let Some(schema) = schemas.pop().flatten() + && let Some(label) = type_label(&schema) + { + rewrite_type_call(expr, schema, label, locals)?; + } + } + PresentationKind::Format(_) => { + schemas.reverse(); + if let Expr::Call(_, _, args, _, _) = expr + && args.len() == 2 + { + rewrite_format_values( + &mut args[1], + &mut schemas, + ctx, + allowed, + facts, + aliases, + locals, + )?; + } + } + PresentationKind::None => {} + } + Ok(()) +} + +enum PresentationKind { + None, + Print, + Type, + Format(usize), +} + +fn presentation_kind( + names: &HashMap, + impls: &HashMap, + expr: &Expr, +) -> PresentationKind { + let Expr::Call(index, _, args, resolution, _) = expr else { + return PresentationKind::None; + }; + if resolution.is_none() + && !impls.contains_key(index) + && names.get(index).map(String::as_str) == Some("print") + && args.len() == 1 + { + return PresentationKind::Print; + } + if BuiltinFunction::from_call_index(*index) == Some(BuiltinFunction::TypeOf) && args.len() == 1 + { + return PresentationKind::Type; + } + if BuiltinFunction::from_call_index(*index) == Some(BuiltinFunction::FormatTemplate) + && args.len() == 2 + { + return PresentationKind::Format(format_payload_exprs(&args[1]).len()); + } + PresentationKind::None +} + +fn presentation_inputs_mut( + names: &HashMap, + impls: &HashMap, + expr: &Expr, +) -> Option<()> { + match presentation_kind(names, impls, expr) { + PresentationKind::None => None, + _ => Some(()), + } +} + +fn print_newline(expr: &Expr) -> bool { + let Expr::Call(_, _, args, _, _) = expr else { + return false; + }; + let Some(arg) = args.first() else { + return false; + }; + let (inner, newline) = split_print_newline(arg); + newline && to_string_input(inner).is_some() +} + +enum PrintInputLoc { + ToStringNewline, + PlainToString, + NewlineLeft, + Whole, +} + +fn print_input_loc(arg: &Expr) -> PrintInputLoc { + let (inner, newline) = split_print_newline(arg); + if to_string_input(inner).is_some() { + if newline { + PrintInputLoc::ToStringNewline + } else { + PrintInputLoc::PlainToString + } + } else if newline { + PrintInputLoc::NewlineLeft + } else { + PrintInputLoc::Whole + } +} + +fn rewrite_print_input( + arg: &mut Expr, + ctx: &RewriteContext<'_, '_>, + facts: &mut std::vec::IntoIter>, + aliases: &mut HashMap, + locals: &mut usize, +) -> Result<(), CompileError> { + match print_input_loc(arg) { + PrintInputLoc::ToStringNewline => { + if let Expr::Add(left, _) = arg + && let Expr::Call(_, _, args, _, _) = left.as_mut() + && let Some(input) = args.get_mut(0) + { + return rewrite_expr(input, ctx, facts, aliases, locals); + } + Ok(()) + } + PrintInputLoc::PlainToString => { + if let Expr::Call(_, _, args, _, _) = arg + && let Some(input) = args.get_mut(0) + { + return rewrite_expr(input, ctx, facts, aliases, locals); + } + Ok(()) + } + PrintInputLoc::NewlineLeft => { + if let Expr::Add(left, _) = arg { + return rewrite_expr(left, ctx, facts, aliases, locals); + } + Ok(()) + } + PrintInputLoc::Whole => rewrite_expr(arg, ctx, facts, aliases, locals), + } +} + +fn take_print_input(arg: &mut Expr) -> Expr { + match print_input_loc(arg) { + PrintInputLoc::ToStringNewline => { + if let Expr::Add(left, _) = arg + && let Expr::Call(_, _, args, _, _) = left.as_mut() + && !args.is_empty() + { + return std::mem::replace(&mut args[0], Expr::Null); + } + Expr::Null + } + PrintInputLoc::PlainToString => { + if let Expr::Call(_, _, args, _, _) = arg + && !args.is_empty() + { + return std::mem::replace(&mut args[0], Expr::Null); + } + Expr::Null + } + PrintInputLoc::NewlineLeft => { + if let Expr::Add(left, _) = arg { + return std::mem::replace(left.as_mut(), Expr::Null); + } + Expr::Null + } + PrintInputLoc::Whole => std::mem::replace(arg, Expr::Null), + } +} + +fn rewrite_print_call( + expr: &mut Expr, + schema: TypeSchema, + newline: bool, + locals: &mut usize, +) -> Result<(), CompileError> { + let Expr::Call(index, _, args, _, _) = expr else { + return Ok(()); + }; + let print_index = *index; + let input = args.get_mut(0).map(take_print_input).unwrap_or(Expr::Null); + let descriptor = encode_descriptor(&schema)?; + let hidden = alloc_local(locals)?; + let mut rendered = Expr::Call( + BuiltinFunction::LogicalRender.call_index(), + Vec::new(), + vec![Expr::Var(hidden), Expr::Bytes(descriptor)], + None, + None, + ); + if newline { + rendered = Expr::Add(Box::new(rendered), Box::new(Expr::String("\n".to_string()))); + } + let print = Expr::Call(print_index, Vec::new(), vec![rendered], None, None); + *expr = Expr::Block { + stmts: vec![ + Stmt::Let { + index: hidden, + declared_schema: Some(schema), + expr: input, + line: 0, + }, + Stmt::Expr { + expr: print, + line: 0, + }, + ], + expr: Box::new(Expr::Var(hidden)), + }; + Ok(()) +} + +fn rewrite_type_call( + expr: &mut Expr, + schema: TypeSchema, + label: &'static str, + locals: &mut usize, +) -> Result<(), CompileError> { + let Expr::Call(_, _, args, _, _) = expr else { + return Ok(()); + }; + let input = args + .get_mut(0) + .map(|arg| std::mem::replace(arg, Expr::Null)) + .unwrap_or(Expr::Null); + let hidden = alloc_local(locals)?; + *expr = Expr::Block { + stmts: vec![Stmt::Let { + index: hidden, + declared_schema: Some(schema), + expr: input, + line: 0, + }], + expr: Box::new(Expr::String(label.to_string())), + }; + Ok(()) +} + +fn rewrite_format_values( + expr: &mut Expr, + schemas: &mut Vec>, + ctx: &RewriteContext<'_, '_>, + allowed: bool, + facts: &mut std::vec::IntoIter>, + aliases: &mut HashMap, + locals: &mut usize, +) -> Result<(), CompileError> { + let Expr::Call(index, _, args, _, _) = expr else { + return Ok(()); + }; + if BuiltinFunction::from_call_index(*index) != Some(BuiltinFunction::ArrayPush) + || args.len() != 2 + { + return Ok(()); + } + rewrite_format_values(&mut args[0], schemas, ctx, allowed, facts, aliases, locals)?; + rewrite_expr(&mut args[1], ctx, facts, aliases, locals)?; + if !allowed { + let _ = schemas.pop(); + return Ok(()); + } + let Some(schema) = schemas.pop().flatten() else { + return Ok(()); + }; + if !contains_logical_display(&schema) { + return Ok(()); + } + let descriptor = encode_descriptor(&schema)?; + let value = std::mem::replace(&mut args[1], Expr::Null); + args[1] = Expr::Call( + BuiltinFunction::LogicalRender.call_index(), + Vec::new(), + vec![value, Expr::Bytes(descriptor)], + None, + None, + ); + let _ = locals; + Ok(()) +} + +fn note_print_alias( + slot: LocalSlot, + expr: &Expr, + names: &HashMap, + impls: &HashMap, + aliases: &mut HashMap, +) { + let trusted = match expr { + Expr::FunctionRef(index, _) => is_language_print_index(*index, names, impls), + Expr::Var(source) | Expr::MoveVar(source) => aliases.contains_key(source), + _ => false, + }; + if trusted { + aliases.insert(slot, ()); + } else { + aliases.remove(&slot); + } +} + +fn is_language_print_index( + index: u16, + names: &HashMap, + impls: &HashMap, +) -> bool { + !impls.contains_key(&index) && names.get(&index).map(String::as_str) == Some("print") +} + +fn type_label(schema: &TypeSchema) -> Option<&'static str> { + match schema { + TypeSchema::Option(_) => Some("Option"), + TypeSchema::Result { .. } => Some("Result"), + TypeSchema::Unit => Some("()"), + _ => None, + } +} + +fn next_fact( + facts: &mut std::vec::IntoIter>, +) -> Result, CompileError> { + facts + .next() + .ok_or_else(|| presentation_error("missing presentation fact")) +} + +fn encode_descriptor(schema: &TypeSchema) -> Result, CompileError> { + crate::compiler::type_schema_codec::encode_type_schema_bytes(schema) + .map_err(|err| presentation_error(&format!("logical render descriptor: {err}"))) +} + +fn alloc_local(locals: &mut usize) -> Result { + let hidden = (*locals) + .try_into() + .map_err(|_| presentation_error("logical presentation local overflow"))?; + *locals += 1; + Ok(hidden) +} + +fn presentation_error(detail: &str) -> CompileError { + CompileError::HostCallResolve { + line: None, + source_name: None, + detail: detail.to_string(), + span: None, + } +} diff --git a/src/compiler/materialization.rs b/src/compiler/materialization.rs index 2b257c54..ab3ce98d 100644 --- a/src/compiler/materialization.rs +++ b/src/compiler/materialization.rs @@ -371,6 +371,7 @@ impl Classifier { fn expr(&mut self, frame: usize, expr: &Expr) { match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -494,12 +495,22 @@ impl Classifier { self.expr(frame, receiver); self.expr(frame, argument); } + Expr::ResultConvert { value, .. } => self.expr(frame, value), + Expr::OptionMethod { + receiver, argument, .. + } => { + self.expr(frame, receiver); + self.expr(frame, argument); + } other @ (Expr::ResultConstruct { .. } | Expr::ResultQuery { .. } | Expr::ResultMatch { .. } | Expr::ResultTry { .. }) => { other.visit_result_children(|child| self.expr(frame, child)); } + other @ (Expr::OptionConstruct { .. } | Expr::OptionMatch { .. }) => { + other.visit_option_children(|child| self.expr(frame, child)); + } Expr::Block { stmts, expr } => { for stmt in stmts { self.stmt(frame, stmt); diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index b5cedde2..23fb3299 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -14,13 +14,18 @@ mod codegen; pub mod diagnostics; mod format; mod frontends; + +#[cfg(test)] +pub(crate) use frontends::legacy_rss; mod host_call_resolve; mod host_conversion; pub mod ir; mod lifetime; mod linker; +mod logical_boundary; mod materialization; mod modules; +mod option; mod parser; mod pipeline; mod result; @@ -28,6 +33,7 @@ mod semantic_model; mod source_loader; pub mod source_map; mod try_anf; +pub(crate) mod type_schema_codec; mod typing; #[cfg(test)] @@ -38,7 +44,7 @@ pub use self::codegen::Compiler; pub use self::format::{ FormatError, format_source, format_source_with_flavor, format_source_with_flavor_and_options, }; -pub use self::frontends::parse_source_with_dialect; +pub use self::frontends::{parse_source_with_dialect, parse_source_with_dialect_and_catalog}; pub use self::host_call_resolve::{HostCallResolveError, HostCallResolver}; pub use self::ir::{ AssignmentKind, ClosureExpr, Expr, FrontendIr, FunctionDecl, FunctionImpl, FunctionParam, @@ -682,6 +688,9 @@ impl CompiledProgram { pub struct CompiledReplProgram { pub compiled: CompiledProgram, pub bindings: Vec, + /// Checked schema of the snippet's root expression result. + /// `None` is absence, not Unit and not a value-equality guess. + pub result_schema: Option, } #[derive(Clone, Default)] diff --git a/src/compiler/option.rs b/src/compiler/option.rs new file mode 100644 index 00000000..6438de18 --- /dev/null +++ b/src/compiler/option.rs @@ -0,0 +1,1830 @@ +//! Guest `Option` resolution and Unit typing owner. +//! +//! Runtime Option values are ordinary maps with a Boolean `tag` (`true` = +//! Some) and an active `payload` only on Some. This pass fills constructor +//! schemas from annotations and rejects non-exhaustive or mistyped Option +//! matches before bytecode emission. It never treats an arbitrary map, an +//! explicit null or the legacy nullable Optional schema as an Option. + +use std::collections::HashMap; + +use super::CompileError; +use super::ir::{ + AssignmentKind, ClosureExpr, Expr, FrontendIr, FunctionDecl, FunctionImpl, HostApiIrMetadata, + LocalSlot, Stmt, StructDecl, TypeSchema, +}; +use super::source_map::Span; + +pub(crate) const OPTION_TAG_KEY: &str = "tag"; +pub(crate) const OPTION_PAYLOAD_KEY: &str = "payload"; + +struct Env { + slots: HashMap, + /// Schemas frozen at the declaring Let or parameter. Later assignments + /// update `slots` only, so a capture does not observe a future reassignment. + declaration_schemas: HashMap, + functions: HashMap, + function_list: Vec, + function_impls: HashMap, + struct_schemas: HashMap, + lexical_return: Option, +} + +pub(crate) fn resolve_option_types( + ir: &mut FrontendIr, + entry_local_types: &[super::typing::EntryLocalType], +) -> Result<(), CompileError> { + let function_list = ir.functions.clone(); + let functions = function_list + .iter() + .map(|function| (function.index, function.clone())) + .collect(); + let mut slots = HashMap::new(); + for entry_local in entry_local_types { + if let Some(schema) = super::typing::logical_entry_schema(entry_local) { + slots.insert(entry_local.slot, schema); + } + } + let mut env = Env { + declaration_schemas: slots.clone(), + slots, + functions, + function_list, + function_impls: ir.function_impls.clone(), + struct_schemas: ir.struct_schemas.clone(), + lexical_return: None, + }; + resolve_stmts(&mut ir.stmts, None, &mut env)?; + let mut impls = std::mem::take(&mut ir.function_impls); + resolve_function_impls(&mut impls, &mut env)?; + ir.function_impls = impls; + Ok(()) +} + +fn resolve_function( + index: u16, + function_impl: &mut FunctionImpl, + env: &mut Env, +) -> Result<(), CompileError> { + let saved = env.slots.clone(); + let saved_return = env.lexical_return.clone(); + if let Some(function) = env.functions.get(&index).cloned() { + env.lexical_return = function.return_schema.clone(); + for (slot, schema) in function_impl + .param_slots + .iter() + .zip(function.arg_schemas.iter()) + { + if let Some(schema) = schema { + env.slots.insert(*slot, schema.clone()); + env.declaration_schemas + .entry(*slot) + .or_insert(schema.clone()); + } + } + seed_captured_schemas(function_impl, env); + resolve_stmts( + &mut function_impl.body_stmts, + function.return_schema.clone(), + env, + )?; + resolve_expr( + &mut function_impl.body_expr, + function.return_schema.as_ref(), + env, + )?; + } else { + env.lexical_return = None; + seed_captured_schemas(function_impl, env); + resolve_stmts(&mut function_impl.body_stmts, None, env)?; + resolve_expr(&mut function_impl.body_expr, None, env)?; + } + env.slots = saved; + env.lexical_return = saved_return; + Ok(()) +} + +/// Copy declaration-position schemas onto the function's captured slots. +/// +/// The payload checker reads `Var(captured)`, not the outer source slot. +/// Seeding uses the schema already recorded for that source; it does not +/// invent a type or consult a later aggregate slot. +fn seed_captured_schemas(function_impl: &FunctionImpl, env: &mut Env) { + for (source, captured) in &function_impl.capture_copies { + let Some(schema) = env + .declaration_schemas + .get(source) + .cloned() + .or_else(|| env.slots.get(source).cloned()) + else { + continue; + }; + env.slots.insert(*captured, schema.clone()); + env.declaration_schemas.entry(*captured).or_insert(schema); + } +} + +fn resolve_function_impls( + impls: &mut HashMap, + env: &mut Env, +) -> Result<(), CompileError> { + let owners = local_slot_owners(impls); + let mut pending: Vec = impls.keys().copied().collect(); + while !pending.is_empty() { + let ready = pending.iter().position(|index| { + capture_sources_ready( + *index, + &impls[index], + &pending, + &owners, + &env.declaration_schemas, + ) + }); + let index = match ready { + Some(position) => pending.remove(position), + None => pending.remove(0), + }; + let function_impl = impls.get_mut(&index).expect("pending function impl"); + resolve_function(index, function_impl, env)?; + } + Ok(()) +} + +fn capture_sources_ready( + index: u16, + function_impl: &FunctionImpl, + pending: &[u16], + owners: &HashMap, + declarations: &HashMap, +) -> bool { + function_impl.capture_copies.iter().all(|(source, _)| { + if declarations.contains_key(source) { + return true; + } + !matches!( + owners.get(source), + Some(owner) if *owner != index && pending.contains(owner) + ) + }) +} + +pub(super) fn local_slot_owners(impls: &HashMap) -> HashMap { + let mut owners = HashMap::new(); + for (index, function_impl) in impls { + for slot in &function_impl.param_slots { + owners.insert(*slot, *index); + } + let mut slots = Vec::new(); + collect_declared_slots(&function_impl.body_stmts, &mut slots); + collect_declared_slots_expr(&function_impl.body_expr, &mut slots); + for slot in slots { + owners.insert(slot, *index); + } + } + owners +} + +fn collect_declared_slots(stmts: &[Stmt], slots: &mut Vec) { + for stmt in stmts { + match stmt { + Stmt::Let { index, expr, .. } => { + slots.push(*index); + collect_declared_slots_expr(expr, slots); + } + Stmt::Assign { expr, .. } | Stmt::Expr { expr, .. } => { + collect_declared_slots_expr(expr, slots); + } + Stmt::IfElse { + condition, + then_branch, + else_branch, + .. + } => { + collect_declared_slots_expr(condition, slots); + collect_declared_slots(then_branch, slots); + collect_declared_slots(else_branch, slots); + } + Stmt::For { + init, + condition, + post, + body, + .. + } => { + collect_declared_slots(std::slice::from_ref(init.as_ref()), slots); + collect_declared_slots_expr(condition, slots); + collect_declared_slots(std::slice::from_ref(post.as_ref()), slots); + collect_declared_slots(body, slots); + } + Stmt::While { + condition, body, .. + } => { + collect_declared_slots_expr(condition, slots); + collect_declared_slots(body, slots); + } + Stmt::ClosureLet { closure, .. } => collect_declared_slots_expr(&closure.body, slots), + Stmt::Noop { .. } + | Stmt::FuncDecl { .. } + | Stmt::Break { .. } + | Stmt::Continue { .. } + | Stmt::Drop { .. } => {} + } + } +} + +fn collect_declared_slots_expr(expr: &Expr, slots: &mut Vec) { + match expr { + Expr::Block { stmts, expr } => { + collect_declared_slots(stmts, slots); + collect_declared_slots_expr(expr, slots); + } + Expr::IfElse { + condition, + then_expr, + else_expr, + } => { + collect_declared_slots_expr(condition, slots); + collect_declared_slots_expr(then_expr, slots); + collect_declared_slots_expr(else_expr, slots); + } + Expr::Closure(closure) => collect_declared_slots_expr(&closure.body, slots), + Expr::ClosureCall(closure, args) => { + collect_declared_slots_expr(&closure.body, slots); + for arg in args { + collect_declared_slots_expr(arg, slots); + } + } + Expr::Match { + value, + arms, + default, + .. + } => { + collect_declared_slots_expr(value, slots); + for (_, arm) in arms { + collect_declared_slots_expr(arm, slots); + } + collect_declared_slots_expr(default, slots); + } + _ => {} + } +} + +fn resolve_stmts( + stmts: &mut [Stmt], + expected_tail: Option, + env: &mut Env, +) -> Result<(), CompileError> { + for stmt in stmts { + resolve_stmt(stmt, expected_tail.as_ref(), env)?; + } + Ok(()) +} + +fn resolve_stmt( + stmt: &mut Stmt, + expected_tail: Option<&TypeSchema>, + env: &mut Env, +) -> Result<(), CompileError> { + match stmt { + Stmt::Noop { .. } | Stmt::Break { .. } | Stmt::Continue { .. } | Stmt::FuncDecl { .. } => { + Ok(()) + } + Stmt::Let { + index, + declared_schema, + expr, + .. + } => { + resolve_expr(expr, declared_schema.as_ref(), env)?; + let inferred = declared_schema + .clone() + .or_else(|| infer_schema(expr, env)) + .or_else(|| { + super::typing::infer_value_schema( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + expr, + &env.slots, + ) + }); + if let Some(schema) = inferred.filter(|schema| !matches!(schema, TypeSchema::Unknown)) { + env.declaration_schemas + .entry(*index) + .or_insert_with(|| schema.clone()); + env.slots.insert(*index, schema); + } + Ok(()) + } + Stmt::Assign { + kind, + index, + expr, + line, + } => { + if !matches!(kind, AssignmentKind::Set) { + let _ = (expected_tail, line); + } + let expected = env.slots.get(index).cloned(); + resolve_expr(expr, expected.as_ref(), env)?; + if let Some(schema) = infer_schema(expr, env) { + env.slots.insert(*index, schema); + } + Ok(()) + } + Stmt::ClosureLet { + closure, + expected_schema, + .. + } => resolve_closure(closure, expected_schema.as_ref(), env), + Stmt::Expr { expr, .. } => resolve_expr(expr, None, env), + Stmt::IfElse { + condition, + then_branch, + else_branch, + .. + } => { + resolve_expr(condition, Some(&TypeSchema::Bool), env)?; + resolve_stmts(then_branch, expected_tail.cloned(), env)?; + resolve_stmts(else_branch, expected_tail.cloned(), env) + } + Stmt::For { + init, + condition, + post, + body, + .. + } => { + resolve_stmt(init, None, env)?; + resolve_expr(condition, Some(&TypeSchema::Bool), env)?; + resolve_stmt(post, None, env)?; + resolve_stmts(body, None, env) + } + Stmt::While { + condition, body, .. + } => { + resolve_expr(condition, Some(&TypeSchema::Bool), env)?; + resolve_stmts(body, None, env) + } + Stmt::Drop { index, .. } => { + env.slots.remove(index); + Ok(()) + } + } +} + +fn resolve_expr( + expr: &mut Expr, + expected: Option<&TypeSchema>, + env: &mut Env, +) -> Result<(), CompileError> { + match expr { + Expr::OptionConstruct { + some, + payload, + option_schema, + span, + .. + } => resolve_construct( + *some, + payload.as_deref_mut(), + option_schema, + *span, + expected, + env, + ), + Expr::OptionMatch { + value, + some_binding, + some_expr, + none_expr, + scrutinee_schema, + span, + .. + } => resolve_match( + value, + *some_binding, + some_expr, + none_expr, + scrutinee_schema, + *span, + expected, + env, + ), + Expr::ResultConstruct { + ok, + result_schema, + payload, + .. + } => { + // The Result owner ran first and filled both branches. Resolve the + // selected payload with its own schema so Option constructors + // nested under Result::Ok/Err keep their context. + let active = match result_schema { + TypeSchema::Result { + ok: ok_branch, + err: err_branch, + } => Some(if *ok { + ok_branch.as_ref().clone() + } else { + err_branch.as_ref().clone() + }), + _ => None, + }; + resolve_expr(payload, active.as_ref(), env) + } + Expr::ResultMatch { + value, + ok_binding, + ok_expr, + err_binding, + err_expr, + scrutinee_schema, + .. + } => { + resolve_expr(value, None, env)?; + let saved = env.slots.clone(); + if let TypeSchema::Result { ok, .. } = scrutinee_schema.clone() + && let Some(slot) = ok_binding.as_ref() + { + env.slots.insert(*slot, ok.as_ref().clone()); + } + resolve_expr(ok_expr, expected, env)?; + env.slots = saved.clone(); + if let TypeSchema::Result { err, .. } = scrutinee_schema.clone() + && let Some(slot) = err_binding.as_ref() + { + env.slots.insert(*slot, err.as_ref().clone()); + } + resolve_expr(err_expr, expected, env)?; + env.slots = saved; + Ok(()) + } + Expr::ResultQuery { value, .. } => resolve_expr(value, None, env), + Expr::ResultMethod { + receiver, argument, .. + } => { + // The Result pass owns lowering for every local method node. A + // residual node is unresolved structural input; keep its children + // available to the Option constructor/match resolver. + resolve_expr(receiver, None, env)?; + resolve_expr(argument, None, env) + } + Expr::OptionMethod { + receiver, argument, .. + } => { + resolve_expr(receiver, None, env)?; + resolve_expr(argument, None, env) + } + Expr::ResultTry { + value, + operand_schema, + return_schema, + span, + .. + } => resolve_option_try(value, operand_schema, return_schema, *span, env), + Expr::ResultConvert { value, .. } => resolve_expr(value, None, env), + Expr::Block { stmts, expr } => { + resolve_stmts(stmts, expected.cloned(), env)?; + resolve_expr(expr, expected, env) + } + Expr::IfElse { + condition, + then_expr, + else_expr, + } => { + resolve_expr(condition, Some(&TypeSchema::Bool), env)?; + let hint = agree_option_schemas([ + branch_option_hint(then_expr, env), + branch_option_hint(else_expr, env), + ]); + resolve_expr( + then_expr, + branch_expected(then_expr, expected, hint.as_ref()), + env, + )?; + resolve_expr( + else_expr, + branch_expected(else_expr, expected, hint.as_ref()), + env, + ) + } + Expr::Match { + value, + arms, + default, + .. + } => { + resolve_expr(value, None, env)?; + let hint = agree_option_schemas( + arms.iter() + .map(|(_, arm)| branch_option_hint(arm, env)) + .chain(std::iter::once(branch_option_hint(default, env))), + ); + for (_, arm) in arms.iter_mut() { + resolve_expr(arm, branch_expected(arm, expected, hint.as_ref()), env)?; + } + resolve_expr( + default, + branch_expected(default, expected, hint.as_ref()), + env, + ) + } + Expr::OptionalGet { + logical_projection, + option_receiver, + logical_unit_field, + container, + key, + .. + } => { + resolve_expr(container, None, env)?; + resolve_expr(key, None, env)?; + if *logical_projection { + match authoritative_schema(container, env) { + Some(TypeSchema::Option(_)) => *option_receiver = true, + Some(_) => *option_receiver = false, + None => {} + } + let container_schema = match authoritative_schema(container, env) { + Some(TypeSchema::Option(inner)) | Some(TypeSchema::Optional(inner)) + if *logical_projection => + { + Some(inner.as_ref().clone()) + } + other => other, + }; + if matches!(key.as_ref(), Expr::String(_)) + && matches!( + container_schema, + Some(TypeSchema::Object(_)) | Some(TypeSchema::Named(_, _)) + ) + && let Some(container_schema) = container_schema + && let Some(access) = super::typing::infer_access_value_schema( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + &container_schema, + key.as_ref(), + &env.slots, + ) + { + *logical_unit_field = matches!(access, TypeSchema::Unit); + } + } + Ok(()) + } + Expr::OptionUnwrapOr { + value, fallback, .. + } => { + resolve_expr(value, None, env)?; + resolve_expr(fallback, expected, env) + } + Expr::Call(index, type_args, args, resolution, _) => { + let expected_args = resolution.as_ref().map(|resolved| { + resolved + .params + .iter() + .map(|param| Some(param.schema.clone())) + .collect::>() + }); + let expected_args = expected_args + .or_else(|| { + super::typing::argument_schemas_for_call( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + *index, + type_args, + ) + }) + .or_else(|| { + super::typing::expected_container_argument_schemas( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + *index, + args, + expected, + ) + }); + for (offset, arg) in args.iter_mut().enumerate() { + let expected_arg = expected_args + .as_ref() + .and_then(|schemas| schemas.get(offset)) + .and_then(|schema| schema.as_ref()); + resolve_expr(arg, expected_arg, env)?; + } + Ok(()) + } + Expr::LocalCall(slot, _, args, _) => { + let params = env.slots.get(slot).and_then(|schema| match schema { + TypeSchema::Callable { params, .. } => Some(params.clone()), + _ => None, + }); + for (offset, arg) in args.iter_mut().enumerate() { + let expected_arg = params.as_ref().and_then(|schemas| schemas.get(offset)); + resolve_expr(arg, expected_arg, env)?; + } + Ok(()) + } + Expr::ModuleCall(symbol, type_args, args, _) => { + let expected_args = env + .function_list + .iter() + .find(|function| function.symbol.as_ref() == Some(symbol)) + .and_then(|function| { + super::typing::argument_schemas_for_call( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + function.index, + type_args, + ) + }); + for (offset, arg) in args.iter_mut().enumerate() { + let expected_arg = expected_args + .as_ref() + .and_then(|schemas| schemas.get(offset)) + .and_then(|schema| schema.as_ref()); + resolve_expr(arg, expected_arg, env)?; + } + Ok(()) + } + Expr::Closure(closure) => resolve_closure(closure, expected, env), + Expr::ClosureCall(closure, args) => { + resolve_closure(closure, expected, env)?; + for arg in args.iter_mut() { + resolve_expr(arg, None, env)?; + } + Ok(()) + } + Expr::Add(lhs, rhs) + | Expr::Sub(lhs, rhs) + | Expr::Mul(lhs, rhs) + | Expr::Div(lhs, rhs) + | Expr::Mod(lhs, rhs) + | Expr::And(lhs, rhs) + | Expr::Or(lhs, rhs) + | Expr::Eq(lhs, rhs) + | Expr::Lt(lhs, rhs) + | Expr::Gt(lhs, rhs) => { + resolve_expr(lhs, None, env)?; + resolve_expr(rhs, None, env) + } + Expr::Neg(inner) + | Expr::Not(inner) + | Expr::ToOwned(inner) + | Expr::Borrow(inner) + | Expr::BorrowMut(inner) => resolve_expr(inner, None, env), + Expr::Unit + | Expr::Null + | Expr::Int(_) + | Expr::Float(_) + | Expr::Bool(_) + | Expr::String(_) + | Expr::Bytes(_) + | Expr::FunctionRef(_, _) + | Expr::ModuleFunctionRef(_, _) + | Expr::UnresolvedFunctionRef { .. } + | Expr::Var(_) + | Expr::MoveVar(_) + | Expr::MoveField { .. } + | Expr::MoveIndex { .. } => Ok(()), + } +} + +fn resolve_construct( + some: bool, + payload: Option<&mut Expr>, + option_schema: &mut TypeSchema, + span: Span, + expected: Option<&TypeSchema>, + env: &mut Env, +) -> Result<(), CompileError> { + let explicit = match option_schema { + TypeSchema::Option(_) => Some(option_schema.clone()), + _ => None, + }; + let expected_schema = match (expected, explicit.as_ref()) { + (Some(expected @ TypeSchema::Option(expected_inner)), Some(explicit_schema)) => { + let TypeSchema::Option(explicit_inner) = explicit_schema else { + unreachable!("matched Option"); + }; + if !matches!(expected_inner.as_ref(), TypeSchema::Unknown) + && !matches!(explicit_inner.as_ref(), TypeSchema::Unknown) + && expected_inner.as_ref() != explicit_inner.as_ref() + { + return Err(option_error( + span, + "Option constructor explicit schema does not match the expected Option schema" + .to_string(), + )); + } + expected.clone() + } + (Some(expected @ TypeSchema::Option(_)), None) => expected.clone(), + (Some(other), _) => { + return Err(option_error( + span, + format!("Option::{} is not a value of type {other}", ctor_name(some)), + )); + } + (None, Some(explicit_schema)) => explicit_schema.clone(), + (None, None) if some => { + // Some may infer its payload when that payload has a fully known + // schema; None can never invent its missing type argument. + let Some(payload) = payload else { + unreachable!("Some has a payload"); + }; + resolve_expr(payload, None, env)?; + let actual = infer_schema(payload, env) + .or_else(|| { + super::typing::infer_value_schema( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + payload, + &env.slots, + ) + }) + .ok_or_else(|| { + option_error( + span, + "Option::Some payload type is unresolved; annotate Option".to_string(), + ) + })?; + if schema_contains_unknown(&actual) || matches!(actual, TypeSchema::GenericParam(_)) { + return Err(option_error( + span, + "Option::Some payload type is unresolved; annotate Option".to_string(), + )); + } + *option_schema = TypeSchema::Option(Box::new(actual)); + return Ok(()); + } + (None, None) => { + return Err(option_error( + span, + "Option::None needs an Option annotation".to_string(), + )); + } + }; + let TypeSchema::Option(inner) = &expected_schema else { + unreachable!("driver matched Option"); + }; + let inner = inner.as_ref().clone(); + if !some { + if matches!(inner, TypeSchema::Unknown) { + return Err(option_error( + span, + "Option::None needs a resolved Option annotation".to_string(), + )); + } + *option_schema = expected_schema; + return Ok(()); + } + let Some(payload) = payload else { + unreachable!("Some has a payload"); + }; + let active = if matches!(inner, TypeSchema::Unknown) { + resolve_expr(payload, None, env)?; + let actual = infer_schema(payload, env) + .or_else(|| { + super::typing::infer_value_schema( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + payload, + &env.slots, + ) + }) + .ok_or_else(|| { + option_error(span, "Option::Some payload type is unresolved".to_string()) + })?; + if schema_contains_unknown(&actual) || matches!(actual, TypeSchema::GenericParam(_)) { + return Err(option_error( + span, + "Option::Some payload type is unresolved".to_string(), + )); + } + actual + } else { + resolve_expr(payload, Some(&inner), env)?; + if !empty_container_payload_matches(payload, &inner) + && let Err(detail) = super::typing::check_result_payload( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + payload, + &inner, + &env.slots, + ) + { + return Err(option_error( + span, + format!("Option::Some payload: {detail}"), + )); + } + inner + }; + *option_schema = TypeSchema::Option(Box::new(active)); + Ok(()) +} + +/// An empty array/map literal has no element whose type could contradict the +/// contextual element schema, so its inferred `Array`/`Map` +/// shape is compatible with the expected container payload. +fn empty_container_payload_matches(payload: &Expr, expected: &TypeSchema) -> bool { + let Expr::Call(index, _, args, _, _) = payload else { + return false; + }; + if !args.is_empty() { + return false; + } + matches!( + (crate::BuiltinFunction::from_call_index(*index), expected), + (Some(crate::BuiltinFunction::ArrayNew), TypeSchema::Array(_)) + | (Some(crate::BuiltinFunction::MapNew), TypeSchema::Map(_)) + ) +} + +#[allow(clippy::too_many_arguments)] +fn resolve_match( + value: &mut Expr, + some_binding: Option, + some_expr: &mut Expr, + none_expr: &mut Expr, + scrutinee_schema: &mut TypeSchema, + span: Span, + expected: Option<&TypeSchema>, + env: &mut Env, +) -> Result<(), CompileError> { + resolve_expr(value, None, env)?; + let schema = authoritative_schema(value, env) + .ok_or_else(|| option_error(span, "match scrutinee must be an Option".to_string()))?; + let TypeSchema::Option(inner) = &schema else { + return Err(option_error( + span, + format!("match scrutinee has type {schema}, expected Option"), + )); + }; + *scrutinee_schema = schema.clone(); + let saved = env.slots.clone(); + if let Some(slot) = some_binding { + env.slots.insert(slot, inner.as_ref().clone()); + } + resolve_expr(some_expr, expected, env)?; + env.slots = saved; + resolve_expr(none_expr, expected, env) +} + +fn resolve_option_try( + value: &mut Expr, + operand_schema: &mut TypeSchema, + return_schema: &mut TypeSchema, + span: Span, + env: &mut Env, +) -> Result<(), CompileError> { + if matches!(operand_schema, TypeSchema::Result { .. }) { + // The Result pass already accepted and typed this operand. + return Ok(()); + } + let lexical_option = match env.lexical_return.clone() { + Some(schema @ TypeSchema::Option(_)) => Some(schema), + _ => None, + }; + resolve_expr(value, lexical_option.as_ref(), env)?; + let operand = authoritative_schema(value, env).ok_or_else(|| { + option_error( + span, + "? operand must be a logical Option when the enclosing callable returns Option" + .to_string(), + ) + })?; + let TypeSchema::Option(_) = &operand else { + let lexical_label = env + .lexical_return + .as_ref() + .map(super::result::render_schema) + .unwrap_or_else(|| "a non-Option callable".to_string()); + return Err(option_error( + span, + format!( + "? operand must be a logical Option in an Option-returning callable, found {} with enclosing return {}", + super::result::render_schema(&operand), + lexical_label + ), + )); + }; + let Some(return_schema_value) = lexical_option else { + let found = env + .lexical_return + .as_ref() + .map(super::result::render_schema) + .unwrap_or_else(|| "a non-Option callable".to_string()); + return Err(option_error( + span, + format!( + "? requires the enclosing function or closure to return Option, found {}; convert with .ok()? before Option propagation", + found + ), + )); + }; + *operand_schema = operand; + *return_schema = return_schema_value; + Ok(()) +} + +fn resolve_closure( + closure: &mut ClosureExpr, + expected: Option<&TypeSchema>, + env: &mut Env, +) -> Result<(), CompileError> { + let (param_schemas, expected_return) = match expected { + Some(TypeSchema::Callable { params, result }) => { + (Some(params.as_slice()), Some(result.as_ref())) + } + other => (None, other), + }; + let nested = super::typing::contextual_callable_slot_schemas( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + &env.slots, + &closure.param_slots, + param_schemas, + &closure.capture_copies, + ); + let saved = std::mem::replace(&mut env.slots, nested); + let saved_return = env.lexical_return.clone(); + env.lexical_return = expected_return.cloned(); + let resolved = resolve_expr(&mut closure.body, expected_return, env); + env.slots = saved; + env.lexical_return = saved_return; + resolved +} + +fn concrete_option_schema(schema: &TypeSchema) -> Option { + match schema { + TypeSchema::Option(inner) + if !matches!( + inner.as_ref(), + TypeSchema::Unknown | TypeSchema::GenericParam(_) + ) && !schema_contains_unknown(inner) => + { + Some(schema.clone()) + } + _ => None, + } +} + +fn branch_option_hint(expr: &Expr, env: &Env) -> Option { + if is_bare_option_none(expr) { + return None; + } + match expr { + Expr::Block { expr, .. } => branch_option_hint(expr, env), + Expr::IfElse { + then_expr, + else_expr, + .. + } => agree_option_schemas([ + branch_option_hint(then_expr, env), + branch_option_hint(else_expr, env), + ]), + Expr::OptionConstruct { + some: true, + option_schema, + payload, + .. + } => concrete_option_schema(option_schema).or_else(|| { + payload.as_deref().and_then(|payload| { + infer_schema(payload, env) + .filter(|schema| { + !schema_contains_unknown(schema) + && !matches!(schema, TypeSchema::GenericParam(_)) + }) + .map(|inner| TypeSchema::Option(Box::new(inner))) + }) + }), + other => { + authoritative_schema(other, env).and_then(|schema| concrete_option_schema(&schema)) + } + } +} + +fn is_bare_option_none(expr: &Expr) -> bool { + match expr { + Expr::OptionConstruct { + some: false, + option_schema, + .. + } => !matches!(option_schema, TypeSchema::Option(_)), + Expr::Block { expr, .. } => is_bare_option_none(expr), + Expr::IfElse { + then_expr, + else_expr, + .. + } => is_bare_option_none(then_expr) && is_bare_option_none(else_expr), + Expr::Match { arms, default, .. } => { + arms.iter().all(|(_, arm)| is_bare_option_none(arm)) && is_bare_option_none(default) + } + _ => false, + } +} + +fn agree_option_schemas(hints: impl IntoIterator>) -> Option { + let mut agreed = None; + for hint in hints.into_iter().flatten() { + match &agreed { + None => agreed = Some(hint), + Some(current) if current == &hint => {} + Some(_) => return None, + } + } + agreed +} + +fn branch_expected<'a>( + branch: &Expr, + expected: Option<&'a TypeSchema>, + hint: Option<&'a TypeSchema>, +) -> Option<&'a TypeSchema> { + if expected.is_some() || !is_bare_option_none(branch) { + expected + } else { + hint + } +} + +fn infer_schema(expr: &Expr, env: &Env) -> Option { + match expr { + Expr::Unit => Some(TypeSchema::Unit), + Expr::Null => Some(TypeSchema::Null), + Expr::Int(_) => Some(TypeSchema::Int), + Expr::Float(_) => Some(TypeSchema::Float), + Expr::Bool(_) => Some(TypeSchema::Bool), + Expr::String(_) => Some(TypeSchema::String), + Expr::Bytes(_) => Some(TypeSchema::Bytes), + Expr::Var(slot) | Expr::MoveVar(slot) => env.slots.get(slot).cloned(), + Expr::OptionConstruct { option_schema, .. } => Some(option_schema.clone()), + Expr::ResultConstruct { result_schema, .. } => Some(result_schema.clone()), + Expr::ResultTry { operand_schema, .. } => match operand_schema { + TypeSchema::Option(inner) => Some(inner.as_ref().clone()), + TypeSchema::Result { ok, .. } => Some(ok.as_ref().clone()), + _ => None, + }, + Expr::Add(lhs, rhs) | Expr::Sub(lhs, rhs) | Expr::Mul(lhs, rhs) => { + match (infer_schema(lhs, env), infer_schema(rhs, env)) { + (Some(TypeSchema::Int), Some(TypeSchema::Int)) => Some(TypeSchema::Int), + (Some(TypeSchema::Float), Some(TypeSchema::Float | TypeSchema::Int)) + | (Some(TypeSchema::Int), Some(TypeSchema::Float)) => Some(TypeSchema::Float), + _ => None, + } + } + Expr::Block { expr, .. } => infer_schema(expr, env), + Expr::IfElse { + then_expr, + else_expr, + .. + } => { + let then_schema = infer_schema(then_expr, env)?; + let else_schema = infer_schema(else_expr, env)?; + if then_schema == else_schema { + Some(then_schema) + } else { + None + } + } + _ => None, + } +} + +fn authoritative_schema(expr: &Expr, env: &Env) -> Option { + infer_schema(expr, env) + .or_else(|| { + super::typing::infer_value_schema( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + expr, + &env.slots, + ) + }) + .filter(|schema| !matches!(schema, TypeSchema::Unknown)) +} + +fn schema_contains_unknown(schema: &TypeSchema) -> bool { + match schema { + TypeSchema::Unknown => true, + TypeSchema::Optional(inner) + | TypeSchema::Option(inner) + | TypeSchema::Array(inner) + | TypeSchema::Map(inner) => schema_contains_unknown(inner), + TypeSchema::Named(_, args) | TypeSchema::ArrayTuple(args) => { + args.iter().any(schema_contains_unknown) + } + TypeSchema::ArrayTupleRest { prefix, rest } => { + prefix.iter().any(schema_contains_unknown) || schema_contains_unknown(rest) + } + TypeSchema::Object(fields) => fields.values().any(schema_contains_unknown), + TypeSchema::Callable { params, result } => { + params.iter().any(schema_contains_unknown) || schema_contains_unknown(result) + } + TypeSchema::Result { ok, err } => { + schema_contains_unknown(ok) || schema_contains_unknown(err) + } + TypeSchema::Null + | TypeSchema::Unit + | TypeSchema::Int + | TypeSchema::Float + | TypeSchema::Number + | TypeSchema::Bool + | TypeSchema::String + | TypeSchema::Bytes + | TypeSchema::Resource(_) + | TypeSchema::GenericParam(_) => false, + } +} + +struct NormalizeEnv<'a> { + function_list: &'a [FunctionDecl], + function_impls: &'a HashMap, + struct_schemas: &'a HashMap, + host: Option<&'a HostApiIrMetadata>, + slots: HashMap, +} + +/// Materialize permitted omitted logical Option fields as explicit None map +/// entries at construction sites where the surrounding typed context supplies +/// the expected record schema. Generic named fields are instantiated through +/// the typing owner's existing substitution rules before an entry is added. +pub(crate) fn normalize_omitted_option_fields(ir: &mut FrontendIr) { + let functions = ir.functions.clone(); + let structs = ir.struct_schemas.clone(); + let impls = ir.function_impls.clone(); + let host = ir.host_api_metadata.clone(); + let mut env = NormalizeEnv { + function_list: &functions, + function_impls: &impls, + struct_schemas: &structs, + host: host.as_ref(), + slots: HashMap::new(), + }; + for stmt in &mut ir.stmts { + normalize_stmt_omitted(stmt, None, &mut env); + } + let root_slots = env.slots.clone(); + let mut targets = std::mem::take(&mut ir.function_impls); + let owners = local_slot_owners(&targets); + let mut pending = targets.keys().copied().collect::>(); + pending.sort_unstable(); + let mut resolved = root_slots.clone(); + while !pending.is_empty() { + // A capture is ready when its source schema is already resolved, or + // its owner is this function or a function that is no longer pending. + // Declarations keep their first checked schema; later assignments do + // not replace the captured fact. + let ready = pending.iter().position(|index| { + targets[index].capture_copies.iter().all(|(source, _)| { + resolved.contains_key(source) + || owners + .get(source) + .is_none_or(|owner| *owner == *index || !pending.contains(owner)) + }) + }); + let index = pending.remove(ready.unwrap_or(0)); + let Some(function) = functions.iter().find(|function| function.index == index) else { + continue; + }; + let expected = function.return_schema.clone(); + let Some(implementation) = targets.get_mut(&index) else { + continue; + }; + env.slots = root_slots.clone(); + for (slot, schema) in implementation + .param_slots + .iter() + .zip(function.arg_schemas.iter()) + { + if let Some(schema) = schema { + env.slots.insert(*slot, schema.clone()); + } + } + for (source, captured) in &implementation.capture_copies { + // The source may be owned by an enclosing function that has + // already been normalized in this pass; its declaration schema is + // then in `resolved`, not in the root/parameter environment. + let schema = env + .slots + .get(source) + .cloned() + .or_else(|| resolved.get(source).cloned()); + if let Some(schema) = schema { + env.slots.insert(*captured, schema); + } + } + for stmt in &mut implementation.body_stmts { + normalize_stmt_omitted(stmt, expected.clone(), &mut env); + } + normalize_expr_omitted(&mut implementation.body_expr, expected.as_ref(), &mut env); + for (slot, schema) in &env.slots { + resolved.entry(*slot).or_insert_with(|| schema.clone()); + } + } + ir.function_impls = targets; +} + +fn infer_omitted_schema(expr: &Expr, env: &NormalizeEnv<'_>) -> Option { + super::typing::infer_value_schema( + env.function_list, + env.function_impls, + env.struct_schemas, + expr, + &env.slots, + ) + .filter(|schema| !matches!(schema, TypeSchema::Unknown)) +} + +fn normalize_stmt_omitted( + stmt: &mut Stmt, + expected_tail: Option, + env: &mut NormalizeEnv<'_>, +) { + match stmt { + Stmt::Let { + index, + declared_schema, + expr, + .. + } => { + normalize_expr_omitted(expr, declared_schema.as_ref(), env); + let inferred = declared_schema + .clone() + .or_else(|| infer_omitted_schema(expr, env)); + if let Some(schema) = inferred.filter(|schema| !matches!(schema, TypeSchema::Unknown)) { + env.slots.insert(*index, schema); + } + } + Stmt::Assign { index, expr, .. } => { + let expected = env.slots.get(index).cloned(); + normalize_expr_omitted(expr, expected.as_ref(), env); + if !env.slots.contains_key(index) + && let Some(schema) = infer_omitted_schema(expr, env) + { + env.slots.insert(*index, schema); + } + } + Stmt::Expr { expr, .. } => normalize_expr_omitted(expr, None, env), + Stmt::ClosureLet { + closure, + expected_schema, + .. + } => normalize_closure_omitted(closure, expected_schema.as_ref(), env), + Stmt::IfElse { + condition, + then_branch, + else_branch, + .. + } => { + normalize_expr_omitted(condition, Some(&TypeSchema::Bool), env); + for nested in then_branch { + normalize_stmt_omitted(nested, expected_tail.clone(), env); + } + for nested in else_branch { + normalize_stmt_omitted(nested, expected_tail.clone(), env); + } + } + Stmt::For { + init, + condition, + post, + body, + .. + } => { + normalize_stmt_omitted(init, None, env); + normalize_expr_omitted(condition, Some(&TypeSchema::Bool), env); + normalize_stmt_omitted(post, None, env); + for nested in body { + normalize_stmt_omitted(nested, None, env); + } + } + Stmt::While { + condition, body, .. + } => { + normalize_expr_omitted(condition, Some(&TypeSchema::Bool), env); + for nested in body { + normalize_stmt_omitted(nested, None, env); + } + } + Stmt::Noop { .. } | Stmt::FuncDecl { .. } | Stmt::Break { .. } | Stmt::Continue { .. } => {} + Stmt::Drop { index, .. } => { + env.slots.remove(index); + } + } +} + +fn normalize_expr_omitted( + expr: &mut Expr, + expected: Option<&TypeSchema>, + env: &mut NormalizeEnv<'_>, +) { + if let Some(expected) = expected + && normalize_map_literal_omitted(expr, expected, env) + { + return; + } + match expr { + Expr::Block { stmts, expr } => { + for stmt in stmts { + normalize_stmt_omitted(stmt, expected.cloned(), env); + } + normalize_expr_omitted(expr, expected, env); + } + Expr::IfElse { + condition, + then_expr, + else_expr, + } => { + normalize_expr_omitted(condition, Some(&TypeSchema::Bool), env); + normalize_expr_omitted(then_expr, expected, env); + normalize_expr_omitted(else_expr, expected, env); + } + Expr::Match { + value, + arms, + default, + .. + } => { + normalize_expr_omitted(value, None, env); + for (_, arm) in arms { + normalize_expr_omitted(arm, expected, env); + } + normalize_expr_omitted(default, expected, env); + } + Expr::Call(index, type_args, args, resolution, _) => { + let expected_args = resolution.as_ref().map(|resolved| { + resolved + .params + .iter() + .map(|param| Some(param.schema.clone())) + .collect::>() + }); + let expected_args = expected_args + .or_else(|| { + super::typing::argument_schemas_for_call( + env.function_list, + env.function_impls, + env.struct_schemas, + *index, + type_args, + ) + }) + .or_else(|| { + super::typing::expected_container_argument_schemas( + env.function_list, + env.function_impls, + env.struct_schemas, + *index, + args, + expected, + ) + }); + let expected_args = overlay_host_argument_schemas(expected_args, env.host, *index); + for (offset, arg) in args.iter_mut().enumerate() { + let expected_arg = expected_args + .as_ref() + .and_then(|schemas| schemas.get(offset)) + .and_then(|schema| schema.as_ref()); + normalize_expr_omitted(arg, expected_arg, env); + } + } + Expr::ModuleCall(symbol, type_args, args, _) => { + let expected_args = env + .function_list + .iter() + .find(|function| function.symbol.as_ref() == Some(symbol)) + .and_then(|function| { + super::typing::argument_schemas_for_call( + env.function_list, + env.function_impls, + env.struct_schemas, + function.index, + type_args, + ) + }); + for (offset, arg) in args.iter_mut().enumerate() { + let expected_arg = expected_args + .as_ref() + .and_then(|schemas| schemas.get(offset)) + .and_then(|schema| schema.as_ref()); + normalize_expr_omitted(arg, expected_arg, env); + } + } + Expr::LocalCall(slot, _, args, _) => { + let params = env.slots.get(slot).and_then(|schema| match schema { + TypeSchema::Callable { params, .. } => Some(params.clone()), + _ => None, + }); + for (offset, arg) in args.iter_mut().enumerate() { + let expected_arg = params.as_ref().and_then(|schemas| schemas.get(offset)); + normalize_expr_omitted(arg, expected_arg, env); + } + } + Expr::ClosureCall(closure, args) => { + normalize_closure_omitted(closure, expected, env); + for arg in args.iter_mut() { + normalize_expr_omitted(arg, None, env); + } + } + Expr::Closure(closure) => normalize_closure_omitted(closure, expected, env), + Expr::ToOwned(inner) + | Expr::Borrow(inner) + | Expr::BorrowMut(inner) + | Expr::Neg(inner) + | Expr::Not(inner) => normalize_expr_omitted(inner, expected, env), + Expr::Add(lhs, rhs) + | Expr::Sub(lhs, rhs) + | Expr::Mul(lhs, rhs) + | Expr::Div(lhs, rhs) + | Expr::Mod(lhs, rhs) + | Expr::And(lhs, rhs) + | Expr::Or(lhs, rhs) + | Expr::Eq(lhs, rhs) + | Expr::Lt(lhs, rhs) + | Expr::Gt(lhs, rhs) => { + normalize_expr_omitted(lhs, None, env); + normalize_expr_omitted(rhs, None, env); + } + Expr::ResultConstruct { + ok, + payload, + result_schema, + .. + } => { + let active = match result_schema { + TypeSchema::Result { ok: ok_branch, err } => Some(if *ok { + ok_branch.as_ref().clone() + } else { + err.as_ref().clone() + }), + _ => expected.and_then(|schema| match schema { + TypeSchema::Result { ok: ok_branch, err } => Some(if *ok { + ok_branch.as_ref().clone() + } else { + err.as_ref().clone() + }), + _ => None, + }), + }; + normalize_expr_omitted(payload, active.as_ref(), env); + } + Expr::OptionConstruct { + payload: Some(payload), + option_schema, + .. + } => { + let active = match option_schema { + TypeSchema::Option(inner) => Some(inner.as_ref().clone()), + _ => match expected { + Some(TypeSchema::Option(inner)) => Some(inner.as_ref().clone()), + _ => None, + }, + }; + normalize_expr_omitted(payload, active.as_ref(), env); + } + Expr::OptionConstruct { payload: None, .. } => {} + Expr::ResultMatch { + value, + ok_binding, + ok_expr, + err_binding, + err_expr, + scrutinee_schema, + .. + } => { + normalize_expr_omitted(value, None, env); + let saved = env.slots.clone(); + if let TypeSchema::Result { ok, .. } = scrutinee_schema.clone() + && let Some(slot) = *ok_binding + { + env.slots.insert(slot, ok.as_ref().clone()); + } + normalize_expr_omitted(ok_expr, expected, env); + env.slots = saved.clone(); + if let TypeSchema::Result { err, .. } = scrutinee_schema.clone() + && let Some(slot) = *err_binding + { + env.slots.insert(slot, err.as_ref().clone()); + } + normalize_expr_omitted(err_expr, expected, env); + env.slots = saved; + } + Expr::OptionMatch { + value, + some_binding, + some_expr, + none_expr, + scrutinee_schema, + .. + } => { + normalize_expr_omitted(value, None, env); + let saved = env.slots.clone(); + if let TypeSchema::Option(inner) = scrutinee_schema.clone() + && let Some(slot) = *some_binding + { + env.slots.insert(slot, inner.as_ref().clone()); + } + normalize_expr_omitted(some_expr, expected, env); + env.slots = saved; + normalize_expr_omitted(none_expr, expected, env); + } + Expr::OptionalGet { container, key, .. } => { + normalize_expr_omitted(container, None, env); + normalize_expr_omitted(key, None, env); + } + Expr::OptionUnwrapOr { + value, fallback, .. + } => { + normalize_expr_omitted(value, None, env); + normalize_expr_omitted(fallback, expected, env); + } + Expr::ResultMethod { + receiver, argument, .. + } + | Expr::OptionMethod { + receiver, argument, .. + } => { + normalize_expr_omitted(receiver, None, env); + normalize_expr_omitted(argument, None, env); + } + Expr::ResultConvert { value, .. } | Expr::ResultTry { value, .. } => { + normalize_expr_omitted(value, None, env); + } + Expr::ResultQuery { value, .. } => normalize_expr_omitted(value, None, env), + Expr::Null + | Expr::Unit + | Expr::Int(_) + | Expr::Float(_) + | Expr::Bool(_) + | Expr::String(_) + | Expr::Bytes(_) + | Expr::FunctionRef(_, _) + | Expr::ModuleFunctionRef(_, _) + | Expr::UnresolvedFunctionRef { .. } + | Expr::Var(_) + | Expr::MoveVar(_) + | Expr::MoveField { .. } + | Expr::MoveIndex { .. } => {} + } +} + +fn normalize_closure_omitted( + closure: &mut ClosureExpr, + expected: Option<&TypeSchema>, + env: &mut NormalizeEnv<'_>, +) { + let (param_schemas, expected_return) = match expected { + Some(TypeSchema::Callable { params, result }) => { + (Some(params.as_slice()), Some(result.as_ref())) + } + other => (None, other), + }; + let nested = super::typing::contextual_callable_slot_schemas( + env.function_list, + env.function_impls, + env.struct_schemas, + &env.slots, + &closure.param_slots, + param_schemas, + &closure.capture_copies, + ); + let saved = std::mem::replace(&mut env.slots, nested); + normalize_expr_omitted(&mut closure.body, expected_return, env); + env.slots = saved; +} + +/// Catalog host decls store `arg_schemas: None`. Fill from the candidate's +/// compiler schema (`Named(name, [])` included) so struct lookup uses the +/// same declaration body later Option checking expects. Do not expand to an +/// object here: that body does not match `Named` field identity. +fn overlay_host_argument_schemas( + existing: Option>>, + host: Option<&HostApiIrMetadata>, + index: u16, +) -> Option>> { + let Some(host_args) = + host.and_then(|metadata| host_candidate_argument_schemas(metadata, index)) + else { + return existing; + }; + let mut existing = existing.unwrap_or_else(|| vec![None; host_args.len()]); + if existing.len() < host_args.len() { + existing.resize(host_args.len(), None); + } + for (slot, host_schema) in existing.iter_mut().zip(host_args) { + if slot.is_none() { + *slot = host_schema; + } + } + Some(existing) +} + +fn host_candidate_argument_schemas( + metadata: &HostApiIrMetadata, + index: u16, +) -> Option>> { + let candidates = metadata.candidates(index)?; + let arity = candidates.first()?.params.len(); + if candidates + .iter() + .any(|candidate| candidate.params.len() != arity) + { + return None; + } + let mut schemas = Vec::with_capacity(arity); + for offset in 0..arity { + let objects = candidates + .iter() + .map(|candidate| candidate.params[offset].ty.to_compiler_schema()) + .collect::>(); + schemas.push(agreed_host_object_schema(&objects)); + } + Some(schemas) +} + +fn agreed_host_object_schema(schemas: &[TypeSchema]) -> Option { + let first = schemas.first()?; + if schemas.iter().all(|schema| schema == first) { + return Some(first.clone()); + } + let TypeSchema::Object(first_fields) = first else { + return None; + }; + let mut fields = HashMap::new(); + for (name, schema) in first_fields { + if !matches!(schema, TypeSchema::Option(_)) { + continue; + } + if schemas.iter().all(|other| match other { + TypeSchema::Object(map) => map.get(name) == Some(schema), + _ => false, + }) { + fields.insert(name.clone(), schema.clone()); + } + } + if fields.is_empty() { + None + } else { + Some(TypeSchema::Object(fields)) + } +} + +fn object_fields_for_normalization( + expected: &TypeSchema, + env: &NormalizeEnv<'_>, +) -> Option> { + super::typing::object_fields_for_schema( + env.function_list, + env.function_impls, + env.struct_schemas, + expected, + ) +} + +fn normalize_map_literal_omitted( + expr: &mut Expr, + expected: &TypeSchema, + env: &mut NormalizeEnv<'_>, +) -> bool { + let Some(fields) = object_fields_for_normalization(expected, env) else { + return false; + }; + let mut keys = Vec::new(); + if !collect_map_literal_keys(expr, &mut keys) { + return false; + } + normalize_map_literal_values(expr, &fields, env); + let mut additions = Vec::new(); + for (name, schema) in &fields { + if keys.iter().any(|key| key == name) { + continue; + } + let TypeSchema::Option(_) = schema else { + continue; + }; + additions.push((name.clone(), schema.clone())); + } + if additions.is_empty() { + return true; + } + additions.sort_unstable_by(|(lhs, _), (rhs, _)| lhs.cmp(rhs)); + let original = std::mem::replace(expr, Expr::Null); + let mut out = original; + for (name, schema) in additions { + out = Expr::Call( + crate::BuiltinFunction::Set.call_index(), + Vec::new(), + vec![ + out, + Expr::String(name), + Expr::OptionConstruct { + some: false, + payload: None, + option_schema: schema, + span: super::source_map::Span::new(0, 0, 0), + semantic_id: None, + }, + ], + None, + None, + ); + } + *expr = out; + true +} + +/// Recurse into the value positions of a recovered `MapNew` + `Set` literal +/// chain with each field's expected schema. The container positions are not +/// revisited with the record expectation: the chain has already been matched +/// at the construction root, and nested records are handled at their value. +fn normalize_map_literal_values( + expr: &mut Expr, + fields: &HashMap, + env: &mut NormalizeEnv<'_>, +) { + match expr { + Expr::Call(index, _, args, _, _) + if crate::BuiltinFunction::from_call_index(*index) + == Some(crate::BuiltinFunction::MapNew) => + { + if !args.is_empty() { + for arg in args { + normalize_expr_omitted(arg, None, env); + } + } + } + Expr::Call(index, _, args, _, _) + if matches!( + crate::BuiltinFunction::from_call_index(*index), + Some(crate::BuiltinFunction::Set) + ) && args.len() == 3 + || matches!( + crate::BuiltinFunction::from_call_index(*index), + Some(crate::BuiltinFunction::MapEdit) + ) && args.len() == 4 => + { + normalize_map_literal_values(&mut args[0], fields, env); + let expected_value = match &args[1] { + Expr::String(key) => fields.get(key).cloned(), + _ => None, + }; + normalize_expr_omitted(&mut args[2], expected_value.as_ref(), env); + } + _ => {} + } +} + +/// Recover the string keys of a pure `MapNew` + `Set` literal chain. A map +/// variable or any non-literal root is not normalized. +fn collect_map_literal_keys(expr: &Expr, keys: &mut Vec) -> bool { + match expr { + Expr::Call(index, _, args, _, _) + if crate::BuiltinFunction::from_call_index(*index) + == Some(crate::BuiltinFunction::MapNew) => + { + args.is_empty() + } + Expr::Call(index, _, args, _, _) + if matches!( + crate::BuiltinFunction::from_call_index(*index), + Some(crate::BuiltinFunction::Set) + ) && args.len() == 3 + || matches!( + crate::BuiltinFunction::from_call_index(*index), + Some(crate::BuiltinFunction::MapEdit) + ) && args.len() == 4 => + { + if let Expr::String(key) = &args[1] { + keys.push(key.clone()); + } + collect_map_literal_keys(&args[0], keys) + } + _ => false, + } +} + +fn ctor_name(some: bool) -> &'static str { + if some { "Some" } else { "None" } +} + +fn option_error(span: Span, detail: String) -> CompileError { + CompileError::StrictTypingRequired { + line: None, + source_name: None, + detail, + span: Some(span), + } +} diff --git a/src/compiler/parser/expressions.rs b/src/compiler/parser/expressions.rs index 4a29b8ce..d6ac9d84 100644 --- a/src/compiler/parser/expressions.rs +++ b/src/compiler/parser/expressions.rs @@ -327,6 +327,12 @@ impl Parser { if self.match_kind(&TokenKind::False) { return Ok(Expr::Bool(false)); } + if self.check(&TokenKind::Null) && self.dialect.reject_legacy_null() { + return Err(super::legacy_null_error( + self, + "RSS rejects null literals; use Option::None or ()", + )); + } if self.match_kind(&TokenKind::Null) { return Ok(Expr::Null); } @@ -367,6 +373,11 @@ impl Parser { let expr = self.parse_result_constructor(name_span)?; return self.parse_postfix_access(expr, name_span); } + if self.dialect.allow_option_syntax() && name == "Option" && self.check_path_separator() + { + let expr = self.parse_option_constructor(name_span)?; + return self.parse_postfix_access(expr, name_span); + } if self.dialect.allow_dotted_call() && let Some(expr) = self.try_parse_js_dotted_call(&name, name_span)? { @@ -552,12 +563,22 @@ impl Parser { let semantic_id = self.alloc_local_call_id(name_span, rparen_span, local, name.clone()); Expr::LocalCall(local, Vec::new(), args, semantic_id) + } else if let Some(selected) = + self.selected_catalog_print_target(&name, args.len()) + { + // A same-named catalog overload, an explicit direct + // import, or a matching wildcard member is the + // selected callee. Language print sugar must not run + // before that identity. + self.build_host_call_expr_with_type_args(&selected, args, type_args)? } else if self.functions.contains_key(&name) { let builtin_alias_call = if matches!(name.as_str(), "print" | "println") { - self.functions - .get(&name) - .map(|decl| !self.function_impls.contains_key(&decl.index)) - .unwrap_or(false) + self.functions.get(&name).is_some_and(|decl| { + !self.function_impls.contains_key(&decl.index) + && !self.parsed_function_decls.contains(&decl.index) + && decl.return_schema.is_none() + && decl.arg_schemas.iter().all(|schema| schema.is_none()) + }) } else { false }; @@ -701,6 +722,9 @@ impl Parser { .get(self.pos.saturating_sub(1)) .map(|t| t.span) .unwrap_or_else(|| self.current_span()); + if self.dialect.allow_option_syntax() && self.match_kind(&TokenKind::RParen) { + return Ok(Expr::Unit); + } let mut expr = self.parse_expr()?; self.expect(&TokenKind::RParen, "expected ')' after expression")?; expr = self.parse_postfix_access(expr, open_span)?; @@ -804,23 +828,24 @@ impl Parser { let expr = if let Some(expr) = trailing_expr { expr } else { - let Some(last_stmt) = stmts.pop() else { - return Err(ParseError { - span: None, - code: None, - line: parser.current_line(), - message: "if expression branch must end with an expression".to_string(), - }); - }; - if let Stmt::Expr { expr, .. } = last_stmt { - expr - } else { - return Err(ParseError { - span: None, - code: None, - line: parser.current_line(), - message: "if expression branch must end with an expression".to_string(), - }); + match stmts.last() { + Some(Stmt::Expr { .. }) => { + let last_stmt = stmts.pop().expect("checked last statement"); + let Stmt::Expr { expr, .. } = last_stmt else { + unreachable!("checked expression statement"); + }; + expr + } + Some(_) if parser.dialect.allow_option_syntax() => Expr::Unit, + None if parser.dialect.allow_option_syntax() => Expr::Unit, + _ => { + return Err(ParseError { + span: None, + code: None, + line: parser.current_line(), + message: "if expression branch must end with an expression".to_string(), + }); + } } }; @@ -885,6 +910,117 @@ impl Parser { && self.check_kind_at(self.pos + 2, &TokenKind::Colon) } + pub(super) fn parse_option_constructor(&mut self, start: Span) -> Result { + if !self.match_path_separator() { + return Err(ParseError { + span: Some(start), + code: None, + line: self.current_line(), + message: "expected '::' after Option".to_string(), + }); + } + let ctor = self.expect_ident("expected Option constructor 'Some' or 'None'")?; + let (some, payload) = match ctor.as_str() { + "Some" => { + self.expect(&TokenKind::LParen, "expected '(' after Option::Some")?; + let payload = self.parse_expr()?; + self.expect( + &TokenKind::RParen, + "expected ')' after Option::Some payload", + )?; + (true, Some(Box::new(payload))) + } + "None" => { + if self.check(&TokenKind::LParen) { + return Err(ParseError { + span: Some(start), + code: None, + line: self.current_line(), + message: "Option::None has no payload".to_string(), + }); + } + (false, None) + } + other => { + return Err(ParseError { + span: Some(start), + code: None, + line: self.current_line(), + message: format!("unknown Option constructor '{other}'"), + }); + } + }; + let hi = self + .tokens + .get(self.pos.saturating_sub(1)) + .map(|token| token.span.hi) + .unwrap_or(start.hi); + Ok(Expr::OptionConstruct { + some, + payload, + option_schema: TypeSchema::Unknown, + span: Span::new(start.source_id, start.lo, hi), + semantic_id: None, + }) + } + + fn peek_option_constructor_pattern(&self) -> bool { + self.dialect.allow_option_syntax() + && self.check_ident_literal("Option") + && self.check_kind_at(self.pos + 1, &TokenKind::Colon) + && self.check_kind_at(self.pos + 2, &TokenKind::Colon) + } + + fn parse_option_constructor_pattern(&mut self) -> Result { + let start = self.current_span(); + let name = self.expect_ident("expected Option")?; + if name != "Option" || !self.match_path_separator() { + return Err(ParseError { + span: Some(start), + code: None, + line: self.current_line(), + message: "expected Option::Some or Option::None match pattern".to_string(), + }); + } + let ctor = self.expect_ident("expected Option constructor in match pattern")?; + match ctor.as_str() { + "None" => { + if self.check(&TokenKind::LParen) { + return Err(ParseError { + span: Some(start), + code: None, + line: self.current_line(), + message: "Option::None match pattern has no payload".to_string(), + }); + } + Ok((false, None)) + } + "Some" => { + self.expect(&TokenKind::LParen, "expected '(' in Option match pattern")?; + let binding = if self.match_ident_literal("_") { + None + } else { + let (binding_name, binding_span) = self + .expect_ident_with_span("expected binding name in Option match pattern")?; + let slot = self.allocate_hidden_local()?; + self.set_local_slot_mutable(slot, false); + Some((binding_name, binding_span, slot)) + }; + self.expect( + &TokenKind::RParen, + "expected ')' after Option match pattern", + )?; + Ok((true, binding)) + } + other => Err(ParseError { + span: Some(start), + code: None, + line: self.current_line(), + message: format!("unknown Option match constructor '{other}'"), + }), + } + } + fn parse_result_constructor_pattern(&mut self) -> Result { let start = self.current_span(); let name = self.expect_ident("expected Result")?; @@ -937,6 +1073,9 @@ impl Parser { let mut result_ok: Option<(Option, Expr)> = None; let mut result_err: Option<(Option, Expr)> = None; let mut saw_result_arm = false; + let mut option_some: Option<(Option, Expr)> = None; + let mut option_none: Option = None; + let mut saw_option_arm = false; while !self.check(&TokenKind::RBrace) { if self.check(&TokenKind::Eof) { @@ -949,7 +1088,47 @@ impl Parser { } let pattern_token_line = self.current_line(); - if self.peek_result_constructor_pattern() { + if self.peek_option_constructor_pattern() { + let (some, binding) = self.parse_option_constructor_pattern()?; + self.expect(&TokenKind::FatArrow, "expected '=>' in match arm")?; + let arm_expr = if let Some((name, ident_span, slot)) = binding.clone() { + let mut scope = HashMap::new(); + scope.insert(name.clone(), slot); + self.closure_scopes.push(scope); + let arm_open = self.current_span(); + let arm_result = self.with_scope(arm_open, |parser| { + parser.record_local_decl(ident_span, ident_span, slot, name); + parser.parse_expr() + }); + self.closure_scopes.pop(); + arm_result? + } else { + let arm_open = self.current_span(); + self.with_scope(arm_open, |parser| parser.parse_expr())? + }; + saw_option_arm = true; + if some { + if option_some.is_some() { + return Err(ParseError { + span: None, + code: None, + line: pattern_token_line, + message: "duplicate Option::Some match arm".to_string(), + }); + } + option_some = Some((binding.map(|(_, _, slot)| slot), arm_expr)); + } else { + if option_none.is_some() { + return Err(ParseError { + span: None, + code: None, + line: pattern_token_line, + message: "duplicate Option::None match arm".to_string(), + }); + } + option_none = Some(arm_expr); + } + } else if self.peek_result_constructor_pattern() { let (ok, binding) = self.parse_result_constructor_pattern()?; self.expect(&TokenKind::FatArrow, "expected '=>' in match arm")?; let arm_expr = if let Some((name, ident_span, slot)) = binding.clone() { @@ -1053,6 +1232,72 @@ impl Parser { } self.expect(&TokenKind::RBrace, "expected '}' after match expression")?; + if saw_option_arm { + if saw_result_arm { + return Err(ParseError { + span: None, + code: None, + line: self.current_line(), + message: "Option match cannot mix constructor arms with Result arms" + .to_string(), + }); + } + if !arms.is_empty() { + return Err(ParseError { + span: None, + code: None, + line: self.current_line(), + message: "Option match cannot mix constructor arms with value patterns" + .to_string(), + }); + } + let (some_binding, some_expr) = if let Some((slot, expr)) = option_some { + if option_none.is_some() && default.is_some() { + return Err(ParseError { + span: None, + code: None, + line: self.current_line(), + message: + "Option match with both constructor arms cannot also have a wildcard" + .to_string(), + }); + } + (slot, expr) + } else if let Some(expr) = default.clone() { + (None, expr) + } else { + return Err(ParseError { + span: None, + code: None, + line: self.current_line(), + message: "Option match is missing Option::Some arm".to_string(), + }); + }; + let none_expr = if let Some(expr) = option_none { + expr + } else if let Some(expr) = default { + expr + } else { + return Err(ParseError { + span: None, + code: None, + line: self.current_line(), + message: "Option match is missing Option::None arm".to_string(), + }); + }; + let span = self.current_span(); + return Ok(Expr::OptionMatch { + value: Box::new(value), + value_slot, + some_binding, + some_expr: Box::new(some_expr), + none_expr: Box::new(none_expr), + scrutinee_schema: TypeSchema::Unknown, + span, + semantic_id: None, + }); + } + if saw_result_arm { if !arms.is_empty() { return Err(ParseError { @@ -1149,6 +1394,12 @@ impl Parser { if let Some(value) = self.match_bytes() { return Ok((Some(MatchPattern::Bytes(value)), None)); } + if self.check(&TokenKind::Null) && self.dialect.reject_legacy_null() { + return Err(super::legacy_null_error( + self, + "RSS rejects null patterns; use Option::None or ()", + )); + } if self.match_kind(&TokenKind::Null) { return Ok((Some(MatchPattern::Null), None)); } @@ -1379,6 +1630,28 @@ impl Parser { member_span, expr_span, )?; + } else if self.dialect.allow_result_syntax() + && self.check(&TokenKind::LParen) + && let Some(method) = OptionMethod::from_member(&member) + { + self.expect(&TokenKind::LParen, "expected '(' after Option method")?; + let argument = self.parse_expr()?; + self.expect( + &TokenKind::RParen, + "expected ')' after Option method argument", + )?; + let expr_span = Span::new( + chain_start.source_id, + chain_start.lo, + self.tokens[self.pos - 1].span.hi, + ); + expr = self.build_option_method_expr( + method, + expr, + argument, + member_span, + expr_span, + )?; } else if self.dialect.allow_result_syntax() && (member == "is_ok" || member == "is_err") && self.check(&TokenKind::LParen) @@ -1396,6 +1669,33 @@ impl Parser { span: expr_span, semantic_id: None, }; + } else if self.dialect.allow_result_syntax() + && (member == "ok" || member == "err") + && self.check(&TokenKind::LParen) + { + self.expect(&TokenKind::LParen, "expected '(' after Result conversion")?; + self.expect(&TokenKind::RParen, "Result conversion takes no arguments")?; + let expr_span = Span::new( + chain_start.source_id, + chain_start.lo, + self.tokens[self.pos - 1].span.hi, + ); + let semantic_id = self.alloc_call_id( + member_span, + expr_span, + ParsedCallTarget::Unresolved, + member.clone(), + false, + ); + expr = Expr::ResultConvert { + ok: member == "ok", + value: Box::new(expr), + value_slot: self.allocate_hidden_local()?, + ok_binding: self.allocate_hidden_local()?, + err_binding: self.allocate_hidden_local()?, + span: expr_span, + semantic_id, + }; } else if member == "length" { let expr_span = Span::new(chain_start.source_id, chain_start.lo, member_span.hi); @@ -1422,6 +1722,26 @@ impl Parser { expr_span, "has".to_string(), )?; + } else if self.dialect.reject_legacy_null() + && member == "remove" + && self.check(&TokenKind::LParen) + { + self.reject_record_map_removal(&expr)?; + self.expect(&TokenKind::LParen, "expected '(' after '.remove'")?; + let key = self.parse_expr()?; + self.expect(&TokenKind::RParen, "expected ')' after map remove key")?; + let expr_span = Span::new( + chain_start.source_id, + chain_start.lo, + self.tokens[self.pos - 1].span.hi, + ); + expr = self.build_postfix_builtin_call( + BuiltinFunction::MapEdit, + vec![expr, key, Expr::Unit, Expr::Bool(true)], + member_span, + expr_span, + "remove".to_string(), + )?; } else if member == "keys" { let expr_span = Span::new(chain_start.source_id, chain_start.lo, member_span.hi); @@ -1523,6 +1843,135 @@ impl Parser { Ok(expr) } + fn map_store_call( + &mut self, + container: Expr, + key: Expr, + value: Expr, + ) -> Result { + let builtin = self.map_store_builtin(&container, &value); + let args = if builtin == BuiltinFunction::MapEdit { + vec![container, key, value, Expr::Bool(false)] + } else { + vec![container, key, value] + }; + self.build_builtin_call_expr(builtin, args) + } + + fn map_store_call_with_provenance( + &mut self, + container: Expr, + key: Expr, + value: Expr, + callee_span: Span, + expr_span: Span, + ) -> Result { + let builtin = self.map_store_builtin(&container, &value); + let args = if builtin == BuiltinFunction::MapEdit { + vec![container, key, value, Expr::Bool(false)] + } else { + vec![container, key, value] + }; + self.build_postfix_builtin_call(builtin, args, callee_span, expr_span, "set".to_string()) + } + + /// RSS map stores keep Unit and other possibly-null data. Proven non-null + /// values stay on the existing three-argument set path. + fn map_store_builtin(&self, container: &Expr, value: &Expr) -> BuiltinFunction { + if !self.dialect.reject_legacy_null() || !self.container_is_map(container) { + return BuiltinFunction::Set; + } + if self.value_proven_physical_non_null(value) { + BuiltinFunction::Set + } else { + BuiltinFunction::MapEdit + } + } + + fn container_is_map(&self, expr: &Expr) -> bool { + match expr { + Expr::Var(slot) => self.local_schemas.get(slot).is_some_and(|schema| { + matches!( + schema, + TypeSchema::Map(_) + | TypeSchema::Object(_) + | TypeSchema::Named(_, _) + | TypeSchema::Option(_) + | TypeSchema::Result { .. } + ) + }), + Expr::Call(index, _, args, _, _) => match BuiltinFunction::from_call_index(*index) { + Some(BuiltinFunction::MapNew) => true, + Some(BuiltinFunction::Set | BuiltinFunction::MapEdit) => args + .first() + .is_some_and(|container| self.container_is_map(container)), + _ => false, + }, + _ => false, + } + } + + fn value_proven_physical_non_null(&self, expr: &Expr) -> bool { + match expr { + Expr::Int(_) | Expr::Bool(_) | Expr::String(_) | Expr::Float(_) | Expr::Bytes(_) => { + true + } + Expr::Unit | Expr::Null => false, + Expr::Var(slot) => self.local_schemas.get(slot).is_some_and(|schema| { + !matches!( + schema, + TypeSchema::Null + | TypeSchema::Unit + | TypeSchema::GenericParam(_) + | TypeSchema::Unknown + | TypeSchema::Optional(_) + ) + }), + Expr::OptionConstruct { .. } | Expr::ResultConstruct { .. } => true, + Expr::Call(index, _, _, _, _) => matches!( + BuiltinFunction::from_call_index(*index), + Some( + BuiltinFunction::ArrayNew + | BuiltinFunction::MapNew + | BuiltinFunction::ArrayPush + | BuiltinFunction::Set + | BuiltinFunction::MapEdit + | BuiltinFunction::Len + | BuiltinFunction::Has + | BuiltinFunction::Keys + | BuiltinFunction::Count + | BuiltinFunction::TypeOf + ) + ), + _ => false, + } + } + + fn reject_record_map_removal(&self, receiver: &Expr) -> Result<(), ParseError> { + let Expr::Var(slot) = receiver else { + return Ok(()); + }; + let Some(schema) = self.local_schemas.get(slot) else { + return Ok(()); + }; + if matches!( + schema, + TypeSchema::Named(_, _) + | TypeSchema::Object(_) + | TypeSchema::Option(_) + | TypeSchema::Result { .. } + ) { + return Err(ParseError { + span: Some(self.current_span()), + code: Some("E_MAP_REMOVE_RECORD".to_string()), + line: self.current_line(), + message: "E_MAP_REMOVE_RECORD: cannot remove a field from a record, object, Option, or Result; removal requires an explicit map" + .to_string(), + }); + } + Ok(()) + } + /// Build a postfix-source builtin call (index get, `.length`, `.has`, /// `.keys`, member get) with a recorded provenance site. The callee span /// is the operator/member token range and the expr span is the full @@ -1700,6 +2149,9 @@ impl Parser { ); Ok(Expr::OptionalGet { field_projection: false, + logical_projection: self.dialect.allow_result_syntax(), + option_receiver: false, + logical_unit_field: false, container: Box::new(container), key: Box::new(key), container_slot: self.allocate_hidden_local()?, @@ -1789,6 +2241,41 @@ impl Parser { }) } + /// Build the structured, type-preserving carrier for an Option-only + /// conversion (`ok_or`, `ok_or_else`). Shared combinator names keep the + /// Result carrier and are resolved by the receiver's owner. + pub(super) fn build_option_method_expr( + &mut self, + method: OptionMethod, + receiver: Expr, + argument: Expr, + callee_span: Span, + expr_span: Span, + ) -> Result { + let semantic_id = self.alloc_call_id( + callee_span, + expr_span, + ParsedCallTarget::Unresolved, + method.name().to_string(), + false, + ); + let receiver_slot = self.allocate_hidden_local()?; + let argument_slot = self.allocate_hidden_local()?; + let some_binding = self.allocate_hidden_local()?; + Ok(Expr::OptionMethod { + method, + receiver: Box::new(receiver), + argument: Box::new(argument), + slots: OptionMethodSlots { + receiver: receiver_slot, + argument: argument_slot, + some_binding, + }, + span: expr_span, + semantic_id, + }) + } + pub(super) fn bind_hidden_local_expr( &mut self, value_slot: LocalSlot, @@ -1882,15 +2369,11 @@ impl Parser { for entry in entries { match entry { BraceLiteralEntry::Array(value) => { - out = self.build_builtin_call_expr( - BuiltinFunction::Set, - vec![out, Expr::Int(next_array_index), value], - )?; + out = self.map_store_call(out, Expr::Int(next_array_index), value)?; next_array_index = next_array_index.saturating_add(1); } BraceLiteralEntry::Map { key, value } => { - out = self - .build_builtin_call_expr(BuiltinFunction::Set, vec![out, key, value])?; + out = self.map_store_call(out, key, value)?; } } } @@ -1932,6 +2415,12 @@ impl Parser { if self.match_kind(&TokenKind::False) { return Ok(Expr::Bool(false)); } + if self.check(&TokenKind::Null) && self.dialect.reject_legacy_null() { + return Err(super::legacy_null_error( + self, + "RSS rejects null map keys; use Option::None or ()", + )); + } if self.match_kind(&TokenKind::Null) { return Ok(Expr::Null); } @@ -2091,6 +2580,50 @@ impl Parser { ) } + /// Select the direct host target for a bare `print`/`println` call. + /// + /// Explicit direct imports (`use demo::{print};` or an `as` alias) choose + /// their target even when the language builtin shares the spelling. A + /// single wildcard import selects a matching wildcard member; an + /// unmatched or non-matching-arity wildcard member keeps intrinsic + /// print. A source-defined implementation always wins. + fn selected_catalog_print_target(&self, name: &str, arity: usize) -> Option { + if !matches!(name, "print" | "println") { + return None; + } + if self + .functions + .get(name) + .is_some_and(|decl| self.function_impls.contains_key(&decl.index)) + { + return None; + } + if let Some(target) = self.direct_host_call_aliases.get(name) { + return Some(target.clone()); + } + let catalog = self.host_catalog.as_ref()?; + if catalog + .functions_named(name) + .iter() + .any(|schema| schema.params.len() == arity) + { + return Some(name.to_string()); + } + if self.direct_host_wildcard_imports.len() == 1 + && let Some(namespace) = self.direct_host_wildcard_imports.iter().next() + { + let target = format!("{namespace}::{name}"); + if catalog + .functions_named(&target) + .iter() + .any(|schema| schema.params.len() == arity) + { + return Some(target); + } + } + None + } + pub(super) fn try_build_language_builtin_call( &mut self, name: &str, @@ -2719,14 +3252,11 @@ impl Parser { member: &str, subpath: &[String], ) -> Option { - let mut host_name = if let Some(host_root) = self.host_namespace_aliases.get(namespace) { - if member.is_empty() { - host_root.clone() - } else { - format!("{host_root}::{member}") - } + let host_root = self.host_namespace_aliases.get(namespace)?; + let mut host_name = if member.is_empty() { + host_root.clone() } else { - return None; + format!("{host_root}::{member}") }; for segment in subpath { @@ -2789,6 +3319,11 @@ impl Parser { "expected '=' after indexed assignment target", )?; let value = self.parse_expr()?; + let expr_hi = self + .tokens + .get(self.pos.saturating_sub(1)) + .map(|token| token.span.hi) + .unwrap_or(ident_span.hi); if expect_terminator { self.consume_stmt_terminator("expected ';' after indexed assignment")?; } @@ -2797,8 +3332,14 @@ impl Parser { self.require_local_mutable_for_operation(index, Some(name.as_str()), line, "mutate")?; // Record the indexed-assignment root as a local reference site. self.record_local_ref(ident_span, index, name); - let expr = - self.build_builtin_call_expr(BuiltinFunction::Set, vec![Expr::Var(index), key, value])?; + let expr_span = Span::new(ident_span.source_id, ident_span.lo, expr_hi); + let expr = self.map_store_call_with_provenance( + Expr::Var(index), + key, + value, + ident_span, + expr_span, + )?; Ok(Stmt::Assign { kind: AssignmentKind::Set, index, @@ -3092,6 +3633,7 @@ impl Parser { self.closure_capture_contexts.push(ClosureCaptureContext { by_name: HashMap::new(), capture_copies: Vec::new(), + scope_base: self.closure_scopes.len() - 1, }); let body_open = self.current_span(); let body_result = self.with_scope(body_open, |parser| { @@ -3190,6 +3732,10 @@ fn builtin_generic_type_arg_arity(builtin: BuiltinFunction) -> GenericCallableTy display_name: "json::decode", arity: Some(1), }, + BuiltinFunction::JsonEncode => GenericCallableTypeArgSpec { + display_name: "json::encode", + arity: Some(1), + }, _ => GenericCallableTypeArgSpec { display_name: builtin.name(), arity: None, @@ -3199,7 +3745,7 @@ fn builtin_generic_type_arg_arity(builtin: BuiltinFunction) -> GenericCallableTy pub(crate) fn host_generic_type_arg_arity(host_name: &str) -> Option { match host_name { - "json::decode" => Some(1), + "json::decode" | "json::encode" => Some(1), _ => None, } } diff --git a/src/compiler/parser/format.rs b/src/compiler/parser/format.rs index 82c5e605..895bffef 100644 --- a/src/compiler/parser/format.rs +++ b/src/compiler/parser/format.rs @@ -994,6 +994,7 @@ impl<'a> SourceFormatter<'a> { fn find_generic_angle_group_close(&self, less_index: usize) -> Option { let mut angle_depth = 0usize; let mut bracket_depth = 0usize; + let mut paren_depth = 0usize; let mut saw_content = false; for index in less_index + 1..self.tokens.len() { @@ -1002,6 +1003,19 @@ impl<'a> SourceFormatter<'a> { saw_content = true; } TokenKind::Comma | TokenKind::Ellipsis => {} + TokenKind::LParen => { + // Callable signatures and the `()` unit type both live in + // type-argument position; arbitrary expression + // parentheses never carry a generic close. + paren_depth += 1; + saw_content = true; + } + TokenKind::RParen => { + if paren_depth == 0 { + return None; + } + paren_depth -= 1; + } TokenKind::LBracket => { bracket_depth += 1; } @@ -1018,6 +1032,9 @@ impl<'a> SourceFormatter<'a> { if bracket_depth > 0 { return None; } + if paren_depth > 0 { + return None; + } if angle_depth == 0 { return saw_content.then_some(index); } diff --git a/src/compiler/parser/mod.rs b/src/compiler/parser/mod.rs index c1460f71..f0b0475d 100644 --- a/src/compiler/parser/mod.rs +++ b/src/compiler/parser/mod.rs @@ -28,9 +28,9 @@ use super::{ AssignmentKind, CatalogVisibility, ClosureExpr, Expr, FunctionDecl, FunctionDeclSite, FunctionImpl, FunctionParam, FunctionRefSite, FunctionRefTarget, HostApiIrMetadata, LexerToken, LocalDeclSite, LocalRefSite, LocalSlot, MatchPattern, MatchTypePattern, - ModuleNamespaceAlias, ParsedCallSite, ParsedCallTarget, ParsedLexicalScope, - ParsedSemanticIndex, ResolvedHostCall, ResultMethod, ResultMethodSlots, ScopeId, - SemanticNodeId, Stmt, StmtSpanSite, StructDecl, StructDeclSite, TypeSchema, + ModuleNamespaceAlias, OptionMethod, OptionMethodSlots, ParsedCallSite, ParsedCallTarget, + ParsedLexicalScope, ParsedSemanticIndex, ResolvedHostCall, ResultMethod, ResultMethodSlots, + ScopeId, SemanticNodeId, Stmt, StmtSpanSite, StructDecl, StructDeclSite, TypeSchema, }, }; @@ -107,12 +107,24 @@ pub trait ParserDialect { false } + /// Logical `Option`, `Option::Some`/`Option::None`, `()` and the Unit + /// block facts. Default off so other frontends keep their contracts. + fn allow_option_syntax(&self) -> bool { + false + } + /// Plain postfix `?` as lexical-function Result propagation. /// /// Default off. `?.` optional access and type-position `T?` are unaffected. fn allow_result_try(&self) -> bool { false } + + /// RSS closes null literals, patterns, types, map keys and `T?`. + /// Other dialects keep those forms. Expression `?` is not this switch. + fn reject_legacy_null(&self) -> bool { + false + } } pub(super) fn lint_trailing_function_return_semicolons( @@ -205,6 +217,19 @@ pub(super) struct Parser { struct ClosureCaptureContext { by_name: HashMap, capture_copies: Vec<(LocalSlot, LocalSlot)>, + /// Index of this frame's scope in `closure_scopes`. Scopes at or after + /// this index are the same function or closure frame. A match-arm binding + /// scope is one of those scopes, not a capture boundary. + scope_base: usize, +} + +pub(super) fn legacy_null_error(parser: &Parser, message: &str) -> ParseError { + ParseError { + span: Some(parser.current_span()), + code: Some("E_RSS_NULL".to_string()), + line: parser.current_line(), + message: message.to_string(), + } } impl Parser { diff --git a/src/compiler/parser/statements.rs b/src/compiler/parser/statements.rs index ad3b9523..34c633d4 100644 --- a/src/compiler/parser/statements.rs +++ b/src/compiler/parser/statements.rs @@ -536,6 +536,15 @@ impl Parser { .to_string(), }); } + if self.dialect.allow_option_syntax() && name == "Option" { + return Err(ParseError { + span: Some(name_span), + code: None, + line: self.current_line(), + message: "Option is a built-in generic type and cannot be declared as a struct" + .to_string(), + }); + } let type_params = self.parse_type_params("struct", &name)?; self.push_active_type_params(&type_params); self.expect(&TokenKind::LBrace, "expected '{' after struct name")?; @@ -591,7 +600,11 @@ impl Parser { || self.check(&TokenKind::RBrace) || self.check(&TokenKind::Eof) { - Expr::Null + if self.dialect.allow_option_syntax() { + Expr::Unit + } else { + Expr::Null + } } else { let expr = self.parse_expr()?; self.consume_stmt_terminator("expected ';' after return value")?; @@ -856,6 +869,12 @@ impl Parser { } let mut schema = self.parse_declared_type_schema()?; + if self.check(&TokenKind::Question) && self.dialect.reject_legacy_null() { + return Err(super::legacy_null_error( + self, + "RSS rejects T?; rewrite it as Option", + )); + } if self.match_kind(&TokenKind::Question) { schema = TypeSchema::Optional(Box::new(schema)); } @@ -885,7 +904,15 @@ impl Parser { } pub(super) fn parse_declared_type_schema(&mut self) -> Result { - let mut schema = if self.match_kind(&TokenKind::LBracket) { + let mut schema = if self.dialect.allow_option_syntax() + && self.match_kind(&TokenKind::LParen) + { + self.expect( + &TokenKind::RParen, + "expected ')' after '()' unit type schema", + )?; + TypeSchema::Unit + } else if self.match_kind(&TokenKind::LBracket) { let mut elements = Vec::new(); let mut rest = None; if self.check(&TokenKind::RBracket) { @@ -967,6 +994,11 @@ impl Parser { params, result: Box::new(result), } + } else if self.check(&TokenKind::Null) && self.dialect.reject_legacy_null() { + return Err(super::legacy_null_error( + self, + "RSS rejects the null type; rewrite it as Option or ()", + )); } else if self.match_kind(&TokenKind::Null) { TypeSchema::Null } else { @@ -1004,6 +1036,19 @@ impl Parser { TypeSchema::Map(Box::new(TypeSchema::Unknown)) } } + "Option" if self.dialect.allow_option_syntax() => { + if !self.match_kind(&TokenKind::Less) { + return Err(ParseError { + span: Some(span), + code: None, + line: self.current_line(), + message: "Option requires one type argument".to_string(), + }); + } + let inner = self.parse_declared_type_schema()?; + self.expect(&TokenKind::Greater, "expected '>' after Option")?; + TypeSchema::Option(Box::new(inner)) + } "Result" if self.dialect.allow_result_syntax() => { self.expect(&TokenKind::Less, "expected '<' after Result")?; let ok = self.parse_declared_type_schema()?; @@ -1080,6 +1125,12 @@ impl Parser { schema = TypeSchema::Array(Box::new(schema)); } + if self.check(&TokenKind::Question) && self.dialect.reject_legacy_null() { + return Err(super::legacy_null_error( + self, + "RSS rejects T?; rewrite it as Option", + )); + } if self.match_kind(&TokenKind::Question) { schema = TypeSchema::Optional(Box::new(schema)); } @@ -1129,23 +1180,29 @@ impl Parser { trailing_expr_line.expect("trailing expr should record a line"), ) } else { - let Some(last_stmt) = body_stmts.pop() else { - return Err(ParseError { - span: None, - code: None, - line: parser.current_line(), - message: "function body must end with an expression statement".to_string(), - }); - }; - if let Stmt::Expr { expr, line } = last_stmt { - (expr, line) - } else { - return Err(ParseError { - span: None, - code: None, - line: parser.current_line(), - message: "function body must end with an expression statement".to_string(), - }); + match body_stmts.last() { + Some(Stmt::Expr { .. }) => { + let last_stmt = body_stmts.pop().expect("checked last statement"); + let Stmt::Expr { expr, line } = last_stmt else { + unreachable!("checked expression statement"); + }; + (expr, line) + } + Some(_) if parser.dialect.allow_option_syntax() => { + (Expr::Unit, parser.current_line_u32()) + } + None if parser.dialect.allow_option_syntax() => { + (Expr::Unit, parser.current_line_u32()) + } + _ => { + return Err(ParseError { + span: None, + code: None, + line: parser.current_line(), + message: "function body must end with an expression statement" + .to_string(), + }); + } } }; @@ -1187,6 +1244,7 @@ impl Parser { self.closure_capture_contexts.push(ClosureCaptureContext { by_name: HashMap::new(), capture_copies: Vec::new(), + scope_base: self.closure_scopes.len() - 1, }); self.function_body_depth += 1; let body_result = self.with_scope(body_open, |parser| { diff --git a/src/compiler/parser/symbols.rs b/src/compiler/parser/symbols.rs index ea12fe0c..dfe68ab3 100644 --- a/src/compiler/parser/symbols.rs +++ b/src/compiler/parser/symbols.rs @@ -136,13 +136,31 @@ impl Parser { return Ok(index); } + // Match-arm binding scopes sit inside the enclosing function or + // closure frame. Names bound in that same frame, including its + // parameters and lets, are direct locals. Only a name bound outside + // this capture context is copied into the current frame. + let same_frame_base = self + .closure_capture_contexts + .last() + .map(|context| context.scope_base) + .unwrap_or(0); if self.closure_scopes.len() > 1 { - for scope in self.closure_scopes[..self.closure_scopes.len() - 1] + let outer_end = self.closure_scopes.len() - 1; + let same_frame_start = same_frame_base.min(outer_end); + for scope in self.closure_scopes[same_frame_start..outer_end] .iter() .rev() { - if let Some(&source_index) = scope.get(name) { - return self.capture_or_direct_local(name, source_index); + if let Some(&index) = scope.get(name) { + return Ok(index); + } + } + if same_frame_base > 0 { + for scope in self.closure_scopes[..same_frame_base].iter().rev() { + if let Some(&source_index) = scope.get(name) { + return self.capture_or_direct_local(name, source_index); + } } } } diff --git a/src/compiler/pipeline.rs b/src/compiler/pipeline.rs index 7feaf695..64fa04f3 100644 --- a/src/compiler/pipeline.rs +++ b/src/compiler/pipeline.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::path::{Path, PathBuf}; use std::sync::Arc; @@ -175,6 +175,13 @@ fn prepare_typed_units_with_entry( error: preserve_strict_diagnostic_path(error, diagnostic_path.as_deref()), sources: sources.clone(), })?; + let logical_scope = rss_source_scope(&owners); + super::logical_boundary::reject_dynamic_host_output(&ir, &logical_scope).map_err(|error| { + SourcePathError::SourceWithMap { + error: SourceError::Compile(error), + sources: sources.clone(), + } + })?; let (owned_ir, entry_local_types) = if let Some(runtime) = runtime { // Admission has already checked every source and invoked each hook once. // Link/codegen authority is independently restricted to Value paths. @@ -195,6 +202,22 @@ fn prepare_typed_units_with_entry( sources: sources.clone(), } })?; + super::option::resolve_option_types(&mut runtime, &runtime_types).map_err(|error| { + SourcePathError::SourceWithMap { + error: SourceError::Compile(error), + sources: sources.clone(), + } + })?; + super::logical_boundary::lower_logical_print( + &mut runtime, + TypingMode::DynamicHints, + &runtime_types, + &logical_scope, + ) + .map_err(|error| SourcePathError::SourceWithMap { + error: SourceError::Compile(error), + sources: sources.clone(), + })?; } if !behavior.recover_host_errors && let Some(error) = error @@ -205,6 +228,17 @@ fn prepare_typed_units_with_entry( }); } let facts = typing::infer_types(&runtime, TypingMode::DynamicHints, &runtime_types); + typing::classify_map_stores( + &mut runtime, + TypingMode::DynamicHints, + &runtime_types, + rss_map_store_scope(&owners), + ) + .map_err(typing::map_admission_source_error) + .map_err(|error| SourcePathError::SourceWithMap { + error, + sources: sources.clone(), + })?; let owned = enforce_owned_sources( runtime, &facts, @@ -360,6 +394,10 @@ fn prepare_owned_typed_ir( } // Legalization and inference do not impose the root's language policy on // dependencies. Admission below uses each producer's own descriptor. + // Omitted Option fields must be materialized before host admission. + // Legalizing first turns a lawful `{}` into a host error, and the + // host_error gate below would then skip this normalization entirely. + super::option::normalize_omitted_option_fields(&mut parsed); let (mut legalized, host_error, rejections) = typing::legalize_for_admission(parsed, TypingMode::DynamicHints, entry.types); // Host-call resolutions and merged module formals exist only after @@ -368,6 +406,9 @@ fn prepare_owned_typed_ir( if host_error.is_none() { super::result::resolve_result_types(&mut legalized, entry.types) .map_err(SourceError::Compile)?; + super::option::normalize_omitted_option_fields(&mut legalized); + super::option::resolve_option_types(&mut legalized, entry.types) + .map_err(SourceError::Compile)?; } super::try_anf::lower_partial_resource_args(&mut legalized).map_err(SourceError::Parse)?; parsed = legalized; @@ -422,6 +463,23 @@ fn prepare_owned_typed_ir( } let logical_schemas = type_info.logical_schemas(); parsed.semantic_index = Some(SemanticIndex::build(logical_schemas.clone(), &parsed)); + let logical_scope = rss_source_scope(owners); + super::logical_boundary::lower_logical_print( + &mut parsed, + TypingMode::DynamicHints, + entry.types, + &logical_scope, + ) + .map_err(SourceError::Compile)?; + super::logical_boundary::lower_typed_json(&mut parsed, &logical_schemas, &logical_scope) + .map_err(SourceError::Compile)?; + typing::classify_map_stores( + &mut parsed, + TypingMode::DynamicHints, + entry.types, + rss_map_store_scope(owners), + ) + .map_err(typing::map_admission_source_error)?; let owned_ir = enforce_owned_sources( parsed.clone(), &type_info, @@ -614,6 +672,7 @@ fn record_expr_local_debug_ranges( ) { match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -634,7 +693,7 @@ fn record_expr_local_debug_ranges( key, container_slot, key_slot, - semantic_id: _, + .. } => { note_local_use(ranges, *container_slot, line); note_local_use(ranges, *key_slot, line); @@ -741,6 +800,51 @@ fn record_expr_local_debug_ranges( record_expr_local_debug_ranges(receiver, line, ranges); record_expr_local_debug_ranges(argument, line, ranges); } + Expr::OptionMethod { + receiver, + argument, + slots, + .. + } => { + note_local_use(ranges, slots.receiver, line); + note_local_use(ranges, slots.argument, line); + note_local_use(ranges, slots.some_binding, line); + record_expr_local_debug_ranges(receiver, line, ranges); + record_expr_local_debug_ranges(argument, line, ranges); + } + Expr::ResultConvert { + value, + value_slot, + ok_binding, + err_binding, + .. + } => { + note_local_use(ranges, *value_slot, line); + note_local_use(ranges, *ok_binding, line); + note_local_use(ranges, *err_binding, line); + record_expr_local_debug_ranges(value, line, ranges); + } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + record_expr_local_debug_ranges(payload, line, ranges); + } + } + Expr::OptionMatch { + value, + value_slot, + some_binding, + some_expr, + none_expr, + .. + } => { + note_local_use(ranges, *value_slot, line); + if let Some(slot) = some_binding { + note_local_use(ranges, *slot, line); + } + record_expr_local_debug_ranges(value, line, ranges); + record_expr_local_debug_ranges(some_expr, line, ranges); + record_expr_local_debug_ranges(none_expr, line, ranges); + } other @ (Expr::ResultConstruct { .. } | Expr::ResultQuery { .. } | Expr::ResultMatch { .. } @@ -811,6 +915,36 @@ fn is_compiler_primitive_import(name: &str) -> bool { name.starts_with("__prim_") } +fn rss_map_store_scope(owners: &[TypedSourceOwner<'_>]) -> typing::MapStoreScope { + typing::MapStoreScope::Sources( + owners + .iter() + .filter(|owner| owner.plugin.is_none()) + .map(|owner| owner.source_id) + .collect::>(), + ) +} + +/// Logical/JSON policy follows actual source provenance, never the root +/// frontend flavor. A single-producer IR without a source name keeps the +/// whole-IR scope it was compiled with. +fn rss_source_scope(owners: &[TypedSourceOwner<'_>]) -> super::logical_boundary::SourceScope { + if owners.iter().any(|owner| owner.source_name.is_empty()) { + return if owners.iter().all(|owner| owner.plugin.is_none()) { + super::logical_boundary::SourceScope::all() + } else { + super::logical_boundary::SourceScope::none() + }; + } + super::logical_boundary::SourceScope::sources( + owners + .iter() + .filter(|owner| owner.plugin.is_none()) + .map(|owner| (owner.source_name.clone(), owner.source_id)) + .collect(), + ) +} + fn frontend_policy_for_flavor( flavor: SourceFlavor, options: &CompileSourceFileOptions, @@ -859,6 +993,7 @@ fn compile_parsed_output_with_entry_locals( enable_local_move_semantics, None, None, + typing::MapStoreScope::RustScript, ) } @@ -873,6 +1008,7 @@ fn compile_parsed_output_with_capture_output( enable_local_move_semantics: bool, certification: Option<&mut super::capture_certification::CaptureCertification>, capture_source_policies: Option>, + map_store_scope: typing::MapStoreScope, ) -> Result { // Normal compilation passes no entry locals. The REPL uses this hook to treat // carried-over locals from prior entries as already available at snippet start. @@ -881,8 +1017,19 @@ fn compile_parsed_output_with_capture_output( } let local_debug_ranges = collect_named_local_debug_ranges(&parsed); let mut parsed = parsed; + let logical_scope = super::logical_boundary::SourceScope::all(); + super::logical_boundary::lower_logical_print( + &mut parsed, + typing_mode, + entry_local_types, + &logical_scope, + ) + .map_err(SourceError::Compile)?; super::result::resolve_result_types(&mut parsed, entry_local_types) .map_err(SourceError::Compile)?; + super::option::normalize_omitted_option_fields(&mut parsed); + super::option::resolve_option_types(&mut parsed, entry_local_types) + .map_err(SourceError::Compile)?; let mut parsed = typing::legalize_builtins_and_bind_types(parsed, typing_mode, entry_local_types) .map_err(SourceError::Compile)?; @@ -894,6 +1041,21 @@ fn compile_parsed_output_with_capture_output( enforce_strict_type_resolution(&parsed, &pre_lifetime_type_info) .map_err(SourceError::Compile)?; } + super::logical_boundary::lower_logical_print( + &mut parsed, + typing_mode, + entry_local_types, + &logical_scope, + ) + .map_err(SourceError::Compile)?; + super::logical_boundary::lower_typed_json( + &mut parsed, + &pre_lifetime_type_info.local_schemas, + &logical_scope, + ) + .map_err(SourceError::Compile)?; + typing::classify_map_stores(&mut parsed, typing_mode, entry_local_types, map_store_scope) + .map_err(typing::map_admission_source_error)?; let owned_local_slots = pre_lifetime_type_info .local_schemas .iter() @@ -1375,6 +1537,7 @@ fn schema_is_fully_known(schema: &TypeSchema) -> bool { match schema { TypeSchema::Unknown => false, TypeSchema::Null + | TypeSchema::Unit | TypeSchema::Int | TypeSchema::Float | TypeSchema::Number @@ -1390,6 +1553,9 @@ fn schema_is_fully_known(schema: &TypeSchema) -> bool { // treat unknown payloads as fully known. matches!(inner.as_ref(), TypeSchema::Unknown) || schema_is_fully_known(inner) } + // A logical Option must have a resolved payload schema; `Unknown` + // inside is exactly the missing type argument this pass rejects. + TypeSchema::Option(inner) => schema_is_fully_known(inner), TypeSchema::Named(_, type_args) => type_args.iter().all(schema_is_fully_known), TypeSchema::Array(item) | TypeSchema::Map(item) => { matches!(item.as_ref(), TypeSchema::Unknown) || schema_is_fully_known(item) @@ -2382,6 +2548,11 @@ fn compile_source_for_repl_with_locals_impl( repl: Some((predefined_locals, moved_names)), }, )?; + let result_schema = typing::root_expression_result_schema( + &prepared.ir, + policy.typing_mode, + &prepared.entry_local_types, + ); let debug_ranges = collect_named_local_debug_ranges(&prepared.ir); let (compiled, root_projection) = compile_owned_ir_with_root_projection( source.to_string(), @@ -2417,7 +2588,11 @@ fn compile_source_for_repl_with_locals_impl( binding.schema = fact.schema.clone(); binding.optional = fact.optional; } - Ok(CompiledReplProgram { compiled, bindings }) + Ok(CompiledReplProgram { + compiled, + bindings, + result_schema, + }) }; compile().map_err(|error| match error { SourcePathError::Source(error) | SourcePathError::SourceWithMap { error, .. } => error, @@ -2540,6 +2715,11 @@ fn compile_source_with_flavor_capture_output( policies: &capture_source_policies, }, ), + if matches!(flavor, SourceFlavor::RustScript) { + typing::MapStoreScope::RustScript + } else { + typing::MapStoreScope::Legacy + }, ) { Err(SourceError::Parse(err)) => Err(SourceError::Parse( err.with_line_span_from_source(&source_map, source_id), @@ -3348,12 +3528,7 @@ mod tests { #[test] fn native_ir_available_optional_sibling_after_normal_projection() { let source = "struct Entry { name: string, note: string? }\nlet entry: Entry = { name: \"first\" };\nlet name = entry.name;\nlet note = entry?.note;\nnote;\n"; - let mut parsed = crate::compiler::frontends::parse_source( - source, - SourceFlavor::RustScript, - &CompileSourceFileOptions::new(), - ) - .unwrap(); + let mut parsed = crate::compiler::frontends::legacy_rss::parse(source).unwrap(); // Native parser IR, with explicit language-neutral field intent. // Native `?.` syntax itself keeps its established value semantics. for stmt in &mut parsed.stmts { @@ -3377,7 +3552,17 @@ mod tests { .unwrap_or_else(|error| panic!("native strict sibling projection: {error}")); let mut machine = crate::Vm::new(compiled.program); assert_eq!(machine.run().unwrap(), crate::VmStatus::Halted); - assert_eq!(machine.stack(), &[crate::Value::Null]); + let stack = machine.stack(); + assert_eq!(stack.len(), 1); + match &stack[0] { + crate::Value::Map(map) => { + assert_eq!( + map.get(&crate::Value::string("tag")), + Some(&crate::Value::Bool(false)) + ); + } + other => panic!("logical projection must yield None, found {other:?}"), + } } #[test] @@ -3386,12 +3571,7 @@ mod tests { let source = format!( "struct Entry {{ name: string, note: string? }}\nlet entry: Entry = {{ name: \"first\"{field} }};\nlet note = entry?.note;\nnote;" ); - let mut parsed = crate::compiler::frontends::parse_source( - &source, - SourceFlavor::RustScript, - &CompileSourceFileOptions::new(), - ) - .unwrap(); + let mut parsed = crate::compiler::frontends::legacy_rss::parse(&source).unwrap(); let root = parsed .local_bindings .iter() @@ -3433,12 +3613,7 @@ mod tests { #[test] fn native_ir_nested_optional_projection_detaches_noncopy_child() { let source = "struct Entry { note: string? }\nlet entry: Entry = { note: \"ready\" };\nlet note = (entry?.note).unwrap_or(\"fallback\");\nnote;"; - let mut parsed = crate::compiler::frontends::parse_source( - source, - SourceFlavor::RustScript, - &CompileSourceFileOptions::new(), - ) - .unwrap(); + let mut parsed = crate::compiler::frontends::legacy_rss::parse(source).unwrap(); let root = parsed .local_bindings .iter() @@ -3487,12 +3662,7 @@ mod tests { #[test] fn native_ir_nested_optional_value_mode_retains_child() { let source = "struct Entry { note: string? }\nlet entry: Entry = { note: \"ready\" };\nlet note = (entry?.note).unwrap_or(\"fallback\");\nnote;"; - let parsed = crate::compiler::frontends::parse_source( - source, - SourceFlavor::RustScript, - &CompileSourceFileOptions::new(), - ) - .unwrap(); + let parsed = crate::compiler::frontends::legacy_rss::parse(source).unwrap(); let root = parsed .local_bindings .iter() diff --git a/src/compiler/result.rs b/src/compiler/result.rs index 324dc584..1b7769e1 100644 --- a/src/compiler/result.rs +++ b/src/compiler/result.rs @@ -11,7 +11,8 @@ use std::sync::Arc; use super::CompileError; use super::ir::{ AssignmentKind, ClosureExpr, Expr, FrontendIr, FunctionDecl, FunctionImpl, LocalSlot, - ResultMethod, ResultMethodSlots, SemanticNodeId, Stmt, TypeSchema, + OptionMethod, OptionMethodSlots, ResultMethod, ResultMethodSlots, SemanticNodeId, Stmt, + TypeSchema, }; use super::source_map::Span; @@ -20,6 +21,8 @@ pub(crate) const RESULT_PAYLOAD_KEY: &str = "payload"; struct Env { slots: HashMap, + /// Declaration-position schemas. Assignments update `slots` only. + declaration_schemas: HashMap, functions: HashMap, function_list: Vec, function_impls: HashMap, @@ -76,6 +79,7 @@ pub(crate) fn resolve_result_types( } } let mut env = Env { + declaration_schemas: slots.clone(), slots, functions, function_list, @@ -87,9 +91,7 @@ pub(crate) fn resolve_result_types( }; resolve_stmts(&mut ir.stmts, None, &mut env)?; let mut impls = std::mem::take(&mut ir.function_impls); - for (index, function_impl) in impls.iter_mut() { - resolve_function(*index, function_impl, &mut env)?; - } + resolve_function_impls(&mut impls, &mut env)?; ir.function_impls = impls; Ok(()) } @@ -112,8 +114,12 @@ fn resolve_function( { if let Some(schema) = schema { env.slots.insert(*slot, schema.clone()); + env.declaration_schemas + .entry(*slot) + .or_insert(schema.clone()); } } + seed_captured_schemas(function_impl, env); resolve_stmts( &mut function_impl.body_stmts, function.return_schema.clone(), @@ -125,6 +131,7 @@ fn resolve_function( env, )?; } else { + seed_captured_schemas(function_impl, env); resolve_stmts(&mut function_impl.body_stmts, None, env)?; resolve_expr(&mut function_impl.body_expr, None, env)?; } @@ -135,6 +142,49 @@ fn resolve_function( Ok(()) } +fn seed_captured_schemas(function_impl: &FunctionImpl, env: &mut Env) { + for (source, captured) in &function_impl.capture_copies { + let Some(schema) = env + .declaration_schemas + .get(source) + .cloned() + .or_else(|| env.slots.get(source).cloned()) + else { + continue; + }; + env.slots.insert(*captured, schema.clone()); + env.declaration_schemas.entry(*captured).or_insert(schema); + } +} + +fn resolve_function_impls( + impls: &mut HashMap, + env: &mut Env, +) -> Result<(), CompileError> { + let owners = super::option::local_slot_owners(impls); + let mut pending: Vec = impls.keys().copied().collect(); + while !pending.is_empty() { + let ready = pending.iter().position(|index| { + impls[index].capture_copies.iter().all(|(source, _)| { + if env.declaration_schemas.contains_key(source) { + return true; + } + !matches!( + owners.get(source), + Some(owner) if *owner != *index && pending.contains(owner) + ) + }) + }); + let index = match ready { + Some(position) => pending.remove(position), + None => pending.remove(0), + }; + let function_impl = impls.get_mut(&index).expect("pending function impl"); + resolve_function(index, function_impl, env)?; + } + Ok(()) +} + fn resolve_stmts( stmts: &mut [Stmt], expected_tail: Option, @@ -183,6 +233,9 @@ fn resolve_stmt( }) .filter(|schema| !matches!(schema, TypeSchema::Unknown)) { + env.declaration_schemas + .entry(*index) + .or_insert_with(|| schema.clone()); env.slots.insert(*index, schema); } let is_callable = env @@ -345,6 +398,46 @@ fn resolve_expr( "is_ok/is_err", ) } + Expr::ResultConvert { + ok, + value, + value_slot, + ok_binding, + err_binding, + span, + .. + } => { + let value = std::mem::replace(value.as_mut(), Expr::Null); + let lowered = resolve_convert( + *ok, + value, + ResultConvertSlots { + value: *value_slot, + ok: *ok_binding, + err: *err_binding, + }, + *span, + expected, + env, + )?; + *expr = lowered; + Ok(()) + } + Expr::OptionMethod { + method, + receiver, + argument, + slots, + span, + .. + } => { + let receiver = std::mem::replace(receiver.as_mut(), Expr::Null); + let argument = std::mem::replace(argument.as_mut(), Expr::Null); + let lowered = + resolve_option_method(*method, receiver, argument, *slots, *span, expected, env)?; + *expr = lowered; + Ok(()) + } Expr::ResultMethod { method, receiver, @@ -414,24 +507,34 @@ fn resolve_expr( resolve_expr(value, None, env)?; resolve_expr(fallback, expected, env) } - Expr::Call(index, type_args, args, _, _) => { - let expected_args = super::typing::argument_schemas_for_call( - &env.function_list, - &env.function_impls, - &env.struct_schemas, - *index, - type_args, - ) - .or_else(|| { - super::typing::expected_container_argument_schemas( - &env.function_list, - &env.function_impls, - &env.struct_schemas, - *index, - args, - expected, - ) + Expr::Call(index, type_args, args, resolution, _) => { + let expected_args = resolution.as_ref().map(|resolved| { + resolved + .params + .iter() + .map(|param| Some(param.schema.clone())) + .collect::>() }); + let expected_args = expected_args + .or_else(|| { + super::typing::argument_schemas_for_call( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + *index, + type_args, + ) + }) + .or_else(|| { + super::typing::expected_container_argument_schemas( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + *index, + args, + expected, + ) + }); for (offset, arg) in args.iter_mut().enumerate() { let expected_arg = expected_args .as_ref() @@ -506,7 +609,56 @@ fn resolve_expr( mark_env_volatile_sources(env, sources); Ok(()) } + Expr::OptionConstruct { + some, + payload, + option_schema, + .. + } => { + let declared = match expected { + Some(schema @ TypeSchema::Option(inner)) => Some((schema.clone(), inner.as_ref())), + _ => None, + }; + if let Some(payload) = payload { + let inner = declared.as_ref().and_then(|(_, inner)| { + (!matches!(inner, TypeSchema::Unknown)).then_some(*inner) + }); + resolve_expr(payload, if *some { inner } else { None }, env)?; + } + if let Some((schema, inner)) = declared + && !matches!(inner, TypeSchema::Unknown) + && matches!(option_schema, TypeSchema::Unknown) + { + // Seed the logical Option schema from the surrounding Result + // payload expectation so payload validation can compare it + // before the standalone Option resolver runs. + *option_schema = schema; + } + Ok(()) + } + Expr::OptionMatch { + value, + some_binding, + some_expr, + none_expr, + .. + } => { + resolve_expr(value, None, env)?; + let schema = authoritative_schema(value, env); + let inner = match &schema { + Some(TypeSchema::Option(inner)) => Some(inner.as_ref().clone()), + _ => None, + }; + let saved = env.slots.clone(); + if let (Some(slot), Some(inner)) = (*some_binding, inner) { + env.slots.insert(slot, inner); + } + resolve_expr(some_expr, expected, env)?; + env.slots = saved; + resolve_expr(none_expr, expected, env) + } Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -544,6 +696,33 @@ fn resolve_method( None | Some(TypeSchema::Unknown) => None, Some(schema) => Some(schema.clone()), }; + // Shared combinator names are owned by the receiver's logical type. Peek + // the receiver schema before applying a Result-only contextual hint so a + // bare `Option::None.unwrap_or(...)` is never typed as a Result constructor. + if let Some(option_method) = method.as_option_method() { + let static_schema = authoritative_schema(&receiver, env); + let syntactic_option = matches!( + receiver, + Expr::OptionConstruct { .. } | Expr::OptionMatch { .. } | Expr::OptionMethod { .. } + ); + if matches!(static_schema, Some(TypeSchema::Option(_))) + || (static_schema.is_none() && syntactic_option) + { + return resolve_option_method( + option_method, + receiver, + argument, + OptionMethodSlots { + receiver: slots.receiver, + argument: slots.argument, + some_binding: slots.ok_binding, + }, + span, + expected, + env, + ); + } + } // Propagate the one Result branch the method keeps into the receiver so // unannotated chains such as `value.map(..).map_err(..).and_then(..)` can // still type callback constructors from the head annotation. A branch the @@ -553,6 +732,33 @@ fn resolve_method( let receiver_schema = authoritative_schema(&receiver, env); let result_schema = match receiver_schema { Some(schema @ TypeSchema::Result { .. }) => schema, + Some(TypeSchema::Option(_)) => { + // A receiver whose schema only became visible after resolution is + // still owned by the logical Option lowering. `map_err` stays + // Result-only. + if let Some(option_method) = method.as_option_method() { + return resolve_option_method( + option_method, + receiver, + argument, + OptionMethodSlots { + receiver: slots.receiver, + argument: slots.argument, + some_binding: slots.ok_binding, + }, + span, + expected, + env, + ); + } + return Err(result_error( + span, + format!( + ".{} requires a Result receiver, found a logical Option", + method.name() + ), + )); + } other => { // `.unwrap_or(...)` keeps the pre-existing nullable lowering for // every non-Result receiver; the Result methods are new syntax and @@ -671,11 +877,23 @@ fn resolve_method( ), )); } + let mut contextual = ok_schema.clone(); if let Some(annotated) = &annotated && !schema_contains_unknown(annotated) { + require_method_schema(env, annotated, &fallback_schema, span, method, "fallback")?; require_method_schema(env, annotated, &ok_schema, span, method, "result")?; + contextual = annotated.clone(); } + require_materialized_context_result( + &argument, + &contextual, + span, + method.name(), + "fallback", + env, + )?; + let fallback_schema = retain_contextual_eager_schema(fallback_schema, &contextual); reject_resource_capturing_closure(&argument, &fallback_schema, span, method, env)?; env.slots.insert(slots.receiver, result_schema.clone()); env.slots.insert(slots.argument, fallback_schema.clone()); @@ -704,14 +922,24 @@ fn resolve_method( } ResultMethod::UnwrapOrElse => { let hint = annotated.clone().unwrap_or(TypeSchema::Unknown); + let callback_context = annotated + .as_ref() + .filter(|schema| !schema_contains_unknown(schema)) + .unwrap_or(&ok_schema); let callback_hint = TypeSchema::Callable { params: vec![err_schema.clone()], result: Box::new(hint), }; resolve_expr(&mut argument, Some(&callback_hint), env)?; - let callback_schema = - method_callback_schema(&argument, &err_schema, span, method, env)?; - let TypeSchema::Callable { params, result } = &callback_schema else { + let callback_schema = method_callback_schema( + &argument, + &err_schema, + Some(callback_context), + span, + method, + env, + )?; + let TypeSchema::Callable { params, .. } = &callback_schema else { unreachable!("method_callback_schema returns a Callable"); }; if params.len() != 1 { @@ -731,14 +959,25 @@ fn resolve_method( method, "callback parameter", )?; - let returned = result.as_ref(); - require_concrete_schema(returned, span, method, "callback return")?; - require_method_schema(env, &ok_schema, returned, span, method, "callback result")?; + let returned = callback_result_schema(&callback_schema); + require_concrete_schema(&returned, span, method, "callback return")?; + require_method_schema(env, &ok_schema, &returned, span, method, "callback result")?; + let mut contextual = ok_schema.clone(); if let Some(annotated) = &annotated && !schema_contains_unknown(annotated) { require_method_schema(env, annotated, &ok_schema, span, method, "result")?; + require_method_schema(env, annotated, &returned, span, method, "callback result")?; + contextual = annotated.clone(); } + let callback_schema = retain_contextual_callback_result_for_expr( + &argument, + callback_schema, + &contextual, + span, + method.name(), + env, + )?; reject_resource_capturing_closure(&argument, &callback_schema, span, method, env)?; env.slots.insert(slots.receiver, result_schema.clone()); env.slots.insert(slots.argument, callback_schema.clone()); @@ -777,35 +1016,35 @@ fn resolve_method( } } - let (param, callback_result_hint) = match method { - ResultMethod::Map => ( - ok_schema.clone(), - annotated_result - .as_ref() - .and_then(branch_schemas) - .map(|(ok, _)| ok), - ), - ResultMethod::MapErr => ( - err_schema.clone(), - annotated_result - .as_ref() - .and_then(branch_schemas) - .map(|(_, err)| err), - ), + let annotated_branches = annotated_result.as_ref().and_then(branch_schemas); + let (param, callback_result_hint, callback_context) = match method { + ResultMethod::Map => { + let annotated_ok = annotated_branches.as_ref().map(|(ok, _)| ok.clone()); + (ok_schema.clone(), annotated_ok.clone(), annotated_ok) + } + ResultMethod::MapErr => { + let annotated_err = annotated_branches.as_ref().map(|(_, err)| err.clone()); + (err_schema.clone(), annotated_err.clone(), annotated_err) + } ResultMethod::AndThen => ( ok_schema.clone(), // and_then keeps the receiver's error type, so the callback's // Result error branch is always known even without an annotation. Some(TypeSchema::Result { ok: Box::new( - annotated_result + annotated_branches .as_ref() - .and_then(branch_schemas) - .map(|(ok, _)| ok) + .map(|(ok, _)| ok.clone()) .unwrap_or(TypeSchema::Unknown), ), err: Box::new(err_schema.clone()), }), + annotated_branches + .as_ref() + .map(|(ok, _)| TypeSchema::Result { + ok: Box::new(ok.clone()), + err: Box::new(err_schema.clone()), + }), ), ResultMethod::OrElse => ( err_schema.clone(), @@ -814,13 +1053,18 @@ fn resolve_method( Some(TypeSchema::Result { ok: Box::new(ok_schema.clone()), err: Box::new( - annotated_result + annotated_branches .as_ref() - .and_then(branch_schemas) - .map(|(_, err)| err) + .map(|(_, err)| err.clone()) .unwrap_or(TypeSchema::Unknown), ), }), + annotated_branches + .as_ref() + .map(|(_, err)| TypeSchema::Result { + ok: Box::new(ok_schema.clone()), + err: Box::new(err.clone()), + }), ), ResultMethod::UnwrapOr | ResultMethod::UnwrapOrElse => unreachable!("handled above"), }; @@ -829,8 +1073,15 @@ fn resolve_method( result: Box::new(callback_result_hint.unwrap_or(TypeSchema::Unknown)), }; resolve_expr(&mut argument, Some(&callback_hint), env)?; - let callback_schema = method_callback_schema(&argument, ¶m, span, method, env)?; - let TypeSchema::Callable { params, result } = &callback_schema else { + let mut callback_schema = method_callback_schema( + &argument, + ¶m, + callback_context.as_ref(), + span, + method, + env, + )?; + let TypeSchema::Callable { params, .. } = &callback_schema else { unreachable!("method_callback_schema returns a Callable"); }; if params.len() != 1 { @@ -844,16 +1095,38 @@ fn resolve_method( )); } require_method_schema(env, ¶m, ¶ms[0], span, method, "callback parameter")?; - require_concrete_schema(result, span, method, "callback return")?; + require_concrete_schema( + &callback_result_schema(&callback_schema), + span, + method, + "callback return", + )?; + let inferred_result = callback_result_schema(&callback_schema); reject_resource_capturing_closure(&argument, &callback_schema, span, method, env)?; let (ok_expr, err_expr) = match method { ResultMethod::Map => { - let output_ok = result.as_ref().clone(); + let mut output_ok = inferred_result.clone(); if let Some(annotated_result) = &annotated_result { let (annotated_ok, _) = branch_schemas(annotated_result).expect("matched Result annotation"); - require_method_schema(env, &annotated_ok, &output_ok, span, method, "result")?; + require_method_schema( + env, + &annotated_ok, + &inferred_result, + span, + method, + "result", + )?; + callback_schema = retain_contextual_callback_result_for_expr( + &argument, + callback_schema, + &annotated_ok, + span, + method.name(), + env, + )?; + output_ok = callback_result_schema(&callback_schema); } let output = TypeSchema::Result { ok: Box::new(output_ok), @@ -879,11 +1152,27 @@ fn resolve_method( (ok_expr, err_expr) } ResultMethod::MapErr => { - let output_err = result.as_ref().clone(); + let mut output_err = inferred_result.clone(); if let Some(annotated_result) = &annotated_result { let (_, annotated_err) = branch_schemas(annotated_result).expect("matched Result annotation"); - require_method_schema(env, &annotated_err, &output_err, span, method, "result")?; + require_method_schema( + env, + &annotated_err, + &inferred_result, + span, + method, + "result", + )?; + callback_schema = retain_contextual_callback_result_for_expr( + &argument, + callback_schema, + &annotated_err, + span, + method.name(), + env, + )?; + output_err = callback_result_schema(&callback_schema); } let output = TypeSchema::Result { ok: Box::new(ok_schema.clone()), @@ -906,20 +1195,20 @@ fn resolve_method( } ResultMethod::AndThen => { let TypeSchema::Result { - ok: output_ok, + ok: inferred_ok, err: callback_err, - } = result.as_ref() + } = &inferred_result else { return Err(result_error( span, format!( ".and_then callback must return Result, found {}", - render_schema(result) + render_schema(&inferred_result) ), )); }; - let output_ok = output_ok.as_ref().clone(); - require_concrete_schema(&output_ok, span, method, "callback success type")?; + let inferred_ok = inferred_ok.as_ref().clone(); + require_concrete_schema(&inferred_ok, span, method, "callback success type")?; let callback_err = callback_err.as_ref().clone(); require_method_schema( env, @@ -929,10 +1218,11 @@ fn resolve_method( method, "callback error type", )?; + let mut contextual_ok = inferred_ok; if let Some(annotated_result) = &annotated_result { let (annotated_ok, annotated_err) = branch_schemas(annotated_result).expect("matched Result annotation"); - require_method_schema(env, &annotated_ok, &output_ok, span, method, "result")?; + require_method_schema(env, &annotated_ok, &contextual_ok, span, method, "result")?; require_method_schema( env, &annotated_err, @@ -941,7 +1231,21 @@ fn resolve_method( method, "result error type", )?; + contextual_ok = annotated_ok; } + let callback_context = TypeSchema::Result { + ok: Box::new(contextual_ok.clone()), + err: Box::new(err_schema.clone()), + }; + callback_schema = retain_contextual_callback_result_for_expr( + &argument, + callback_schema, + &callback_context, + span, + method.name(), + env, + )?; + let output_ok = contextual_ok; let output = TypeSchema::Result { ok: Box::new(output_ok), err: Box::new(err_schema.clone()), @@ -964,13 +1268,13 @@ fn resolve_method( let TypeSchema::Result { ok: callback_ok, err: output_err, - } = result.as_ref() + } = &inferred_result else { return Err(result_error( span, format!( ".or_else callback must return Result, found {}", - render_schema(result) + render_schema(&inferred_result) ), )); }; @@ -998,6 +1302,18 @@ fn resolve_method( "result error type", )?; } + let callback_context = TypeSchema::Result { + ok: Box::new(ok_schema.clone()), + err: Box::new(output_err.clone()), + }; + callback_schema = retain_contextual_callback_result_for_expr( + &argument, + callback_schema, + &callback_context, + span, + method.name(), + env, + )?; let output = TypeSchema::Result { ok: Box::new(ok_schema.clone()), err: Box::new(output_err), @@ -1044,6 +1360,959 @@ fn resolve_method( Ok(lowered) } +#[derive(Clone, Copy)] +struct ResultConvertSlots { + value: LocalSlot, + ok: LocalSlot, + err: LocalSlot, +} + +/// Lower `Result.ok()` / `.err()` into an ordinary typed Result match +/// whose arms construct the logical Option result. The unselected branch +/// discards its payload, so a resource-bearing or unresolved-generic branch +/// has no legitimate destination and is rejected statically. +fn resolve_convert( + ok: bool, + mut value: Expr, + slots: ResultConvertSlots, + span: Span, + expected: Option<&TypeSchema>, + env: &mut Env, +) -> Result { + resolve_expr(&mut value, None, env)?; + let schema = authoritative_schema(&value, env).ok_or_else(|| { + result_error( + span, + format!( + ".{} requires a Result receiver, found an untyped value", + if ok { "ok" } else { "err" } + ), + ) + })?; + let TypeSchema::Result { + ok: ok_schema, + err: err_schema, + } = &schema + else { + return Err(result_error( + span, + format!( + ".{} requires a Result receiver, found {}", + if ok { "ok" } else { "err" }, + render_schema(&schema) + ), + )); + }; + let ok_schema = ok_schema.as_ref().clone(); + let err_schema = err_schema.as_ref().clone(); + let selected = if ok { + ok_schema.clone() + } else { + err_schema.clone() + }; + let discarded_name = if ok { "Err" } else { "Ok" }; + let discarded = if ok { + err_schema.clone() + } else { + ok_schema.clone() + }; + if discarded.contains_resource_with_named_types(&env.struct_schemas) { + return Err(result_error( + span, + format!( + ".{} on Result<{}, {}> cannot discard the selected {} payload owner: the unselected branch has no destination for resource-typed value {}", + if ok { "ok" } else { "err" }, + render_schema(&ok_schema), + render_schema(&err_schema), + discarded_name, + render_schema(&discarded) + ), + )); + } + // A generic parameter is not proven resource-free at this definition: + // a later instantiation can bind it to a resource type and the unselected + // branch would drop that owner. + if schema_contains_generic_param(&discarded) { + return Err(result_error( + span, + format!( + ".{} on Result<{}, {}> cannot discard the selected {} payload owner: it contains an unresolved generic parameter that may be instantiated with a resource", + if ok { "ok" } else { "err" }, + render_schema(&ok_schema), + render_schema(&err_schema), + discarded_name + ), + )); + } + let output = TypeSchema::Option(Box::new(selected.clone())); + if let Some(annotated) = expected + && !schema_contains_unknown(annotated) + { + let TypeSchema::Option(annotated_inner) = annotated else { + return Err(result_error( + span, + format!( + ".{} produces a logical Option value, not {}", + if ok { "ok" } else { "err" }, + render_schema(annotated) + ), + )); + }; + if !matches!(annotated_inner.as_ref(), TypeSchema::Unknown) { + require_method_schema_named( + env, + annotated_inner, + &selected, + span, + if ok { "ok" } else { "err" }, + "result payload", + )?; + } + } + env.slots.insert(slots.value, schema.clone()); + env.slots.insert(slots.ok, ok_schema.clone()); + env.slots.insert(slots.err, err_schema); + let selected_construct = |payload: Expr| Expr::OptionConstruct { + some: true, + payload: Some(Box::new(payload)), + option_schema: output.clone(), + span, + semantic_id: None, + }; + let none_construct = || Expr::OptionConstruct { + some: false, + payload: None, + option_schema: output.clone(), + span, + semantic_id: None, + }; + let (ok_expr, err_expr) = if ok { + (selected_construct(Expr::Var(slots.ok)), none_construct()) + } else { + (none_construct(), selected_construct(Expr::Var(slots.err))) + }; + let mut branch = Expr::ResultMatch { + value: Box::new(value), + value_slot: slots.value, + ok_binding: Some(slots.ok), + ok_expr: Box::new(ok_expr), + err_binding: Some(slots.err), + err_expr: Box::new(err_expr), + scrutinee_schema: schema, + span, + semantic_id: None, + }; + resolve_lowered_result_match(&mut branch, expected, env)?; + Ok(branch) +} + +/// The partial logical `Option` expectation propagated into a method receiver. +/// Only methods whose output reveals the receiver payload constrain it; a +/// method that replaces the payload (`map`, `and_then`) does not invent one. +fn option_method_receiver_hint( + method: OptionMethod, + annotated: Option<&TypeSchema>, +) -> Option { + let known = |schema: &TypeSchema| !matches!(schema, TypeSchema::Unknown); + match method { + OptionMethod::Map | OptionMethod::AndThen => None, + OptionMethod::OrElse => annotated.and_then(|schema| match schema { + TypeSchema::Option(inner) if known(inner) => Some(schema.clone()), + _ => None, + }), + OptionMethod::UnwrapOr | OptionMethod::UnwrapOrElse => annotated + .filter(|schema| known(schema)) + .map(|schema| TypeSchema::Option(Box::new(schema.clone()))), + OptionMethod::OkOr | OptionMethod::OkOrElse => annotated.and_then(|schema| { + let TypeSchema::Result { ok, .. } = schema else { + return None; + }; + known(ok).then(|| TypeSchema::Option(ok.clone())) + }), + } +} + +fn option_authoritative_schema(expr: &Expr, env: &Env) -> Option { + authoritative_schema(expr, env) +} + +/// Resolve one Option combinator/conversion into an ordinary typed Option +/// match. The receiver and the callback/fallback argument are evaluated once +/// each, in source order, through parser-assigned hidden locals. Eager +/// arguments (`unwrap_or`, `ok_or`) are evaluated on both branches; callable +/// bodies run only on the selected branch. +fn resolve_option_method( + method: OptionMethod, + mut receiver: Expr, + mut argument: Expr, + slots: OptionMethodSlots, + span: Span, + expected: Option<&TypeSchema>, + env: &mut Env, +) -> Result { + let method_name = method.name(); + let annotated = match expected { + None | Some(TypeSchema::Unknown) => None, + Some(schema) => Some(schema.clone()), + }; + let receiver_hint = option_method_receiver_hint(method, annotated.as_ref()); + resolve_expr(&mut receiver, receiver_hint.as_ref(), env)?; + let receiver_schema = option_authoritative_schema(&receiver, env).ok_or_else(|| { + result_error( + span, + format!( + ".{} requires a logical Option receiver, found an untyped value", + method_name + ), + ) + })?; + let TypeSchema::Option(inner) = &receiver_schema else { + return Err(result_error( + span, + format!( + ".{} requires a logical Option receiver, found {}", + method_name, + super::result::render_schema(&receiver_schema) + ), + )); + }; + let inner = inner.as_ref().clone(); + if matches!(inner, TypeSchema::Unknown) { + return Err(result_error( + span, + format!( + ".{} receiver payload type is unresolved; annotate Option", + method_name + ), + )); + } + + let (argument_schema, mut some_expr, mut none_expr) = match method { + OptionMethod::Map => { + let annotated_payload = annotated.as_ref().and_then(|schema| match schema { + TypeSchema::Option(inner) if !matches!(inner.as_ref(), TypeSchema::Unknown) => { + Some(inner.as_ref().clone()) + } + _ => None, + }); + let callback_result_hint = annotated_payload.clone().unwrap_or(TypeSchema::Unknown); + let mut callback_schema = option_callback_schema( + &mut argument, + vec![inner.clone()], + callback_result_hint, + annotated_payload.as_ref(), + span, + method_name, + env, + )?; + let inferred_payload = callback_result_schema(&callback_schema); + if let Some(annotated) = &annotated { + let TypeSchema::Option(annotated_inner) = annotated else { + return Err(result_error( + span, + format!( + ".map produces a logical Option value, not {}", + super::result::render_schema(annotated) + ), + )); + }; + if !matches!(annotated_inner.as_ref(), TypeSchema::Unknown) { + super::result::require_method_schema_named( + env, + annotated_inner, + &inferred_payload, + span, + method_name, + "result", + )?; + callback_schema = retain_contextual_callback_result_for_expr( + &argument, + callback_schema, + annotated_inner, + span, + method_name, + env, + )?; + } + } + let output_payload = callback_result_schema(&callback_schema); + let output = TypeSchema::Option(Box::new(output_payload.clone())); + super::result::reject_resource_capturing_closure_named( + &argument, + &callback_schema, + span, + method_name, + env, + )?; + ( + callback_schema, + Expr::OptionConstruct { + some: true, + payload: Some(Box::new(Expr::LocalCall( + slots.argument, + Vec::new(), + vec![Expr::Var(slots.some_binding)], + None, + ))), + option_schema: output.clone(), + span, + semantic_id: None, + }, + Expr::OptionConstruct { + some: false, + payload: None, + option_schema: output, + span, + semantic_id: None, + }, + ) + } + OptionMethod::AndThen => { + let annotated_payload = annotated.as_ref().and_then(|schema| match schema { + TypeSchema::Option(inner) if !matches!(inner.as_ref(), TypeSchema::Unknown) => { + Some(inner.as_ref().clone()) + } + _ => None, + }); + let callback_result_hint = annotated_payload + .as_ref() + .map(|inner| TypeSchema::Option(Box::new(inner.clone()))) + .unwrap_or(TypeSchema::Unknown); + let callback_context = annotated_payload + .as_ref() + .map(|inner| TypeSchema::Option(Box::new(inner.clone()))); + let mut callback_schema = option_callback_schema( + &mut argument, + vec![inner.clone()], + callback_result_hint, + callback_context.as_ref(), + span, + method_name, + env, + )?; + let TypeSchema::Callable { result, .. } = &callback_schema else { + unreachable!("option_callback_schema returns a Callable"); + }; + let TypeSchema::Option(inferred_payload) = result.as_ref() else { + return Err(result_error( + span, + format!( + ".and_then callback must return Option, found {}", + super::result::render_schema(result) + ), + )); + }; + let inferred_payload = inferred_payload.as_ref().clone(); + if matches!(inferred_payload, TypeSchema::Unknown) { + return Err(result_error( + span, + ".and_then callback Option payload is unresolved; annotate Option" + .to_string(), + )); + } + if let Some(annotated) = &annotated { + let TypeSchema::Option(annotated_inner) = annotated else { + return Err(result_error( + span, + format!( + ".and_then produces a logical Option value, not {}", + super::result::render_schema(annotated) + ), + )); + }; + if !matches!(annotated_inner.as_ref(), TypeSchema::Unknown) { + super::result::require_method_schema_named( + env, + annotated_inner, + &inferred_payload, + span, + method_name, + "result", + )?; + let callback_context = + TypeSchema::Option(Box::new((**annotated_inner).clone())); + callback_schema = retain_contextual_callback_result_for_expr( + &argument, + callback_schema, + &callback_context, + span, + method_name, + env, + )?; + } + } + let TypeSchema::Option(output_payload) = callback_result_schema(&callback_schema) + else { + unreachable!("retained and_then callback result is Option"); + }; + let output = TypeSchema::Option(output_payload); + super::result::reject_resource_capturing_closure_named( + &argument, + &callback_schema, + span, + method_name, + env, + )?; + ( + callback_schema, + Expr::LocalCall( + slots.argument, + Vec::new(), + vec![Expr::Var(slots.some_binding)], + None, + ), + Expr::OptionConstruct { + some: false, + payload: None, + option_schema: output, + span, + semantic_id: None, + }, + ) + } + OptionMethod::OrElse => { + let mut callback_schema = option_callback_schema( + &mut argument, + Vec::new(), + receiver_schema.clone(), + Some(&receiver_schema), + span, + method_name, + env, + )?; + let TypeSchema::Callable { result, .. } = &callback_schema else { + unreachable!("option_callback_schema returns a Callable"); + }; + let result = result.as_ref().clone(); + let TypeSchema::Option(payload) = &result else { + return Err(result_error( + span, + format!( + ".or_else callback must return Option, found {}", + super::result::render_schema(&result) + ), + )); + }; + super::result::require_method_schema_named( + env, + &receiver_schema, + &result, + span, + method_name, + "callback result", + )?; + let mut contextual = receiver_schema.clone(); + if let Some(annotated) = &annotated { + let TypeSchema::Option(annotated_inner) = annotated else { + return Err(result_error( + span, + format!( + ".or_else produces a logical Option value, not {}", + super::result::render_schema(annotated) + ), + )); + }; + if !matches!(annotated_inner.as_ref(), TypeSchema::Unknown) { + super::result::require_method_schema_named( + env, + annotated_inner, + payload, + span, + method_name, + "result", + )?; + contextual = annotated.clone(); + } + } + callback_schema = retain_contextual_callback_result_for_expr( + &argument, + callback_schema, + &contextual, + span, + method_name, + env, + )?; + super::result::reject_resource_capturing_closure_named( + &argument, + &callback_schema, + span, + method_name, + env, + )?; + ( + callback_schema, + Expr::OptionConstruct { + some: true, + payload: Some(Box::new(Expr::Var(slots.some_binding))), + option_schema: receiver_schema.clone(), + span, + semantic_id: None, + }, + Expr::LocalCall(slots.argument, Vec::new(), Vec::new(), None), + ) + } + OptionMethod::UnwrapOr => { + let fallback_schema = option_eager_argument( + &mut argument, + Some(&inner), + span, + method_name, + "fallback", + env, + )?; + super::result::require_method_schema_named( + env, + &inner, + &fallback_schema, + span, + method_name, + "fallback", + )?; + if inner.contains_resource_with_named_types(&env.struct_schemas) { + return Err(result_error( + span, + format!( + ".unwrap_or on Option<{}> cannot discard the eager fallback owner: the unselected Some branch has no destination for resource-typed fallback value {}", + super::result::render_schema(&inner), + super::result::render_schema(&fallback_schema) + ), + )); + } + if super::result::schema_contains_generic_param(&inner) { + return Err(result_error( + span, + format!( + ".unwrap_or on Option<{}> cannot discard the eager fallback owner: the payload contains an unresolved generic parameter that may be instantiated with a resource", + super::result::render_schema(&inner) + ), + )); + } + let mut contextual = inner.clone(); + if let Some(annotated) = &annotated + && !schema_contains_unknown(annotated) + { + super::result::require_method_schema_named( + env, + annotated, + &fallback_schema, + span, + method_name, + "fallback", + )?; + super::result::require_method_schema_named( + env, + annotated, + &inner, + span, + method_name, + "result", + )?; + contextual = annotated.clone(); + } + require_materialized_context_result( + &argument, + &contextual, + span, + method_name, + "fallback", + env, + )?; + let fallback_schema = retain_contextual_eager_schema(fallback_schema, &contextual); + ( + fallback_schema, + Expr::Var(slots.some_binding), + Expr::Var(slots.argument), + ) + } + OptionMethod::UnwrapOrElse => { + let callback_result_hint = annotated.clone().unwrap_or_else(|| inner.clone()); + let callback_context = callback_result_hint.clone(); + let mut callback_schema = option_callback_schema( + &mut argument, + Vec::new(), + callback_result_hint, + Some(&callback_context), + span, + method_name, + env, + )?; + let result = callback_result_schema(&callback_schema); + super::result::require_method_schema_named( + env, + &inner, + &result, + span, + method_name, + "callback result", + )?; + let mut contextual = inner.clone(); + if let Some(annotated) = &annotated + && !schema_contains_unknown(annotated) + { + super::result::require_method_schema_named( + env, + annotated, + &inner, + span, + method_name, + "result", + )?; + super::result::require_method_schema_named( + env, + annotated, + &result, + span, + method_name, + "callback result", + )?; + contextual = annotated.clone(); + } + callback_schema = retain_contextual_callback_result_for_expr( + &argument, + callback_schema, + &contextual, + span, + method_name, + env, + )?; + super::result::reject_resource_capturing_closure_named( + &argument, + &callback_schema, + span, + method_name, + env, + )?; + ( + callback_schema, + Expr::Var(slots.some_binding), + Expr::LocalCall(slots.argument, Vec::new(), Vec::new(), None), + ) + } + OptionMethod::OkOr => { + let err_hint = annotated.as_ref().and_then(|schema| match schema { + TypeSchema::Result { err, .. } if !matches!(err.as_ref(), TypeSchema::Unknown) => { + Some(err.as_ref().clone()) + } + _ => None, + }); + let error_schema = option_eager_argument( + &mut argument, + err_hint.as_ref(), + span, + method_name, + "error argument", + env, + )?; + if error_schema.contains_resource_with_named_types(&env.struct_schemas) { + return Err(result_error( + span, + format!( + ".ok_or on Option<{}> cannot discard the eager error owner: the unselected Some branch has no destination for resource-typed error value {}", + super::result::render_schema(&inner), + super::result::render_schema(&error_schema) + ), + )); + } + if super::result::schema_contains_generic_param(&error_schema) { + return Err(result_error( + span, + format!( + ".ok_or on Option<{}> cannot discard the eager error owner: the error type contains an unresolved generic parameter that may be instantiated with a resource", + super::result::render_schema(&inner) + ), + )); + } + let mut context_err = error_schema.clone(); + if let Some(annotated) = &annotated + && !schema_contains_unknown(annotated) + { + let TypeSchema::Result { + ok: annotated_ok, + err: annotated_err, + } = annotated + else { + return Err(result_error( + span, + format!( + ".ok_or produces Result, not {}", + super::result::render_schema(annotated) + ), + )); + }; + super::result::require_method_schema_named( + env, + annotated_ok, + &inner, + span, + method_name, + "result Ok payload", + )?; + super::result::require_method_schema_named( + env, + annotated_err, + &error_schema, + span, + method_name, + "result Err payload", + )?; + context_err = (**annotated_err).clone(); + } + require_materialized_context_result( + &argument, + &context_err, + span, + method_name, + "error argument", + env, + )?; + let output_err = retain_contextual_eager_schema(error_schema.clone(), &context_err); + let output = TypeSchema::Result { + ok: Box::new(inner.clone()), + err: Box::new(output_err.clone()), + }; + ( + output_err, + Expr::ResultConstruct { + ok: true, + payload: Box::new(Expr::Var(slots.some_binding)), + result_schema: output.clone(), + span, + semantic_id: None, + }, + Expr::ResultConstruct { + ok: false, + payload: Box::new(Expr::Var(slots.argument)), + result_schema: output, + span, + semantic_id: None, + }, + ) + } + OptionMethod::OkOrElse => { + let err_hint = annotated.as_ref().and_then(|schema| match schema { + TypeSchema::Result { err, .. } if !matches!(err.as_ref(), TypeSchema::Unknown) => { + Some(err.as_ref().clone()) + } + _ => None, + }); + let callback_result_hint = err_hint.clone().unwrap_or(TypeSchema::Unknown); + let mut callback_schema = option_callback_schema( + &mut argument, + Vec::new(), + callback_result_hint, + err_hint.as_ref(), + span, + method_name, + env, + )?; + let error_schema = callback_result_schema(&callback_schema); + if matches!(error_schema, TypeSchema::Unknown) { + return Err(result_error( + span, + ".ok_or_else callback error type is unresolved; annotate Result" + .to_string(), + )); + } + let mut context_err = error_schema.clone(); + if let Some(annotated) = &annotated + && !schema_contains_unknown(annotated) + { + let TypeSchema::Result { + ok: annotated_ok, + err: annotated_err, + } = annotated + else { + return Err(result_error( + span, + format!( + ".ok_or_else produces Result, not {}", + super::result::render_schema(annotated) + ), + )); + }; + super::result::require_method_schema_named( + env, + annotated_ok, + &inner, + span, + method_name, + "result Ok payload", + )?; + super::result::require_method_schema_named( + env, + annotated_err, + &error_schema, + span, + method_name, + "result Err payload", + )?; + context_err = (**annotated_err).clone(); + } + callback_schema = retain_contextual_callback_result_for_expr( + &argument, + callback_schema, + &context_err, + span, + method_name, + env, + )?; + let output_err = callback_result_schema(&callback_schema); + let output = TypeSchema::Result { + ok: Box::new(inner.clone()), + err: Box::new(output_err), + }; + super::result::reject_resource_capturing_closure_named( + &argument, + &callback_schema, + span, + method_name, + env, + )?; + ( + callback_schema, + Expr::ResultConstruct { + ok: true, + payload: Box::new(Expr::Var(slots.some_binding)), + result_schema: output.clone(), + span, + semantic_id: None, + }, + Expr::ResultConstruct { + ok: false, + payload: Box::new(Expr::LocalCall( + slots.argument, + Vec::new(), + Vec::new(), + None, + )), + result_schema: output, + span, + semantic_id: None, + }, + ) + } + }; + + env.slots.insert(slots.receiver, receiver_schema.clone()); + env.slots.insert(slots.argument, argument_schema.clone()); + env.slots.insert(slots.some_binding, inner); + resolve_expr(&mut some_expr, expected, env)?; + resolve_expr(&mut none_expr, expected, env)?; + let branch = Expr::OptionMatch { + value: Box::new(Expr::Var(slots.receiver)), + value_slot: slots.receiver, + some_binding: Some(slots.some_binding), + some_expr: Box::new(some_expr), + none_expr: Box::new(none_expr), + scrutinee_schema: receiver_schema.clone(), + span, + semantic_id: None, + }; + Ok(Expr::Block { + stmts: vec![ + Stmt::Let { + index: slots.receiver, + declared_schema: Some(receiver_schema), + expr: receiver, + line: 1, + }, + Stmt::Let { + index: slots.argument, + declared_schema: Some(argument_schema), + expr: argument, + line: 1, + }, + ], + expr: Box::new(branch), + }) +} + +fn option_eager_argument( + argument: &mut Expr, + expected: Option<&TypeSchema>, + span: Span, + method_name: &str, + position: &str, + env: &mut Env, +) -> Result { + resolve_expr(argument, expected, env)?; + let schema = option_authoritative_schema(argument, env).ok_or_else(|| { + result_error( + span, + format!( + ".{} {} must have a concrete compile-time type", + method_name, position + ), + ) + })?; + super::result::require_concrete_schema_named(&schema, span, method_name, position)?; + Ok(schema) +} + +fn option_callback_schema( + argument: &mut Expr, + params: Vec, + result_hint: TypeSchema, + contextual_result: Option<&TypeSchema>, + span: Span, + method_name: &str, + env: &mut Env, +) -> Result { + let hint = TypeSchema::Callable { + params: params.clone(), + result: Box::new(result_hint), + }; + resolve_expr(argument, Some(&hint), env)?; + let callback_schema = super::result::callback_schema_for_params( + argument, + ¶ms, + contextual_result, + span, + method_name, + env, + )?; + let TypeSchema::Callable { + params: actual_params, + result, + } = &callback_schema + else { + unreachable!("callback_schema_for_params returns a Callable"); + }; + let expected_arity = params.len(); + if actual_params.len() != expected_arity { + return Err(result_error( + span, + if expected_arity == 0 { + format!( + ".{} callback must take exactly zero arguments, found {}", + method_name, + actual_params.len() + ) + } else { + format!( + ".{} callback must take exactly {} argument{}, found {}", + method_name, + expected_arity, + if expected_arity == 1 { "" } else { "s" }, + actual_params.len() + ) + }, + )); + } + for (expected, actual) in params.iter().zip(actual_params.iter()) { + super::result::require_method_schema_named( + env, + expected, + actual, + span, + method_name, + "callback parameter", + )?; + } + super::result::require_concrete_schema_named(result, span, method_name, "callback return")?; + Ok(callback_schema) +} + /// Resolve the lowered branch exactly once: it validates the typed arms and /// payload schemas through the shared ResultMatch owner without re-walking the /// already-resolved receiver/callback binder expressions. @@ -1185,13 +2454,22 @@ fn require_concrete_schema( span: Span, method: ResultMethod, position: &str, +) -> Result<(), CompileError> { + require_concrete_schema_named(schema, span, method.name(), position) +} + +fn require_concrete_schema_named( + schema: &TypeSchema, + span: Span, + method_name: &str, + position: &str, ) -> Result<(), CompileError> { if schema_contains_unknown(schema) { return Err(result_error( span, format!( ".{} cannot type {}: callback/fallback schema {} is unresolved", - method.name(), + method_name, position, render_schema(schema) ), @@ -1222,10 +2500,173 @@ fn schema_contains_unknown(schema: &TypeSchema) -> bool { } } +/// Whether a contextual schema is fully resolved, including nested logical +/// `Option`/payload schemas. `schema_contains_unknown` intentionally keeps the +/// older `Optional` handling for Result payload checks; method lowering must +/// not store an unresolved `Option` context on a hidden argument binding. +fn method_context_schema_is_known(schema: &TypeSchema) -> bool { + match schema { + TypeSchema::Unknown => false, + TypeSchema::Optional(inner) + | TypeSchema::Option(inner) + | TypeSchema::Array(inner) + | TypeSchema::Map(inner) => method_context_schema_is_known(inner), + TypeSchema::Result { ok, err } => { + method_context_schema_is_known(ok) && method_context_schema_is_known(err) + } + TypeSchema::ArrayTuple(items) => items.iter().all(method_context_schema_is_known), + TypeSchema::ArrayTupleRest { prefix, rest } => { + prefix.iter().all(method_context_schema_is_known) + && method_context_schema_is_known(rest) + } + TypeSchema::Object(fields) => fields.values().all(method_context_schema_is_known), + TypeSchema::Named(_, args) => args.iter().all(method_context_schema_is_known), + TypeSchema::Callable { params, result } => { + params.iter().all(method_context_schema_is_known) + && method_context_schema_is_known(result) + } + _ => true, + } +} + +/// Whether the contextual value carries a named/object record shape whose +/// omitted logical `Option` fields need physical materialization. Scalar-only +/// contexts keep the actual inferred schema instead of relabelling, for example +/// a legacy `Optional` annotation over an `int` payload. +fn contextual_schema_carries_record(schema: &TypeSchema) -> bool { + match schema { + TypeSchema::Named(_, _) | TypeSchema::Object(_) => true, + TypeSchema::Optional(inner) + | TypeSchema::Option(inner) + | TypeSchema::Array(inner) + | TypeSchema::Map(inner) => contextual_schema_carries_record(inner), + TypeSchema::Result { ok, err } => { + contextual_schema_carries_record(ok) || contextual_schema_carries_record(err) + } + TypeSchema::Callable { result, .. } => contextual_schema_carries_record(result), + TypeSchema::ArrayTuple(items) => items.iter().any(contextual_schema_carries_record), + TypeSchema::ArrayTupleRest { prefix, rest } => { + prefix.iter().any(contextual_schema_carries_record) + || contextual_schema_carries_record(rest) + } + _ => false, + } +} + +fn callback_result_schema(schema: &TypeSchema) -> TypeSchema { + match schema { + TypeSchema::Callable { result, .. } => (**result).clone(), + _ => unreachable!("method callback schema is Callable"), + } +} + +/// Keep the already-validated contextual return schema on a method-lowered +/// callback. The inferred callback still supplies the actual parameter list and +/// arity; the closure expression retains its own captures and targets. Only the +/// checked result branch is replaced, so the hidden argument let and the +/// compiler-owned normalization walker see the instantiated named/generic +/// record the surrounding context requires instead of a present-key object. +fn retain_contextual_callback_result( + callback_schema: TypeSchema, + contextual: &TypeSchema, +) -> TypeSchema { + if !method_context_schema_is_known(contextual) || !contextual_schema_carries_record(contextual) + { + return callback_schema; + } + match callback_schema { + TypeSchema::Callable { params, .. } => TypeSchema::Callable { + params, + result: Box::new(contextual.clone()), + }, + other => other, + } +} + +/// Keep the already-validated contextual payload/error schema on a +/// method-lowered eager argument binding. An unresolved or absent context +/// keeps the actual inferred schema. +fn retain_contextual_eager_schema(inferred: TypeSchema, contextual: &TypeSchema) -> TypeSchema { + if method_context_schema_is_known(contextual) && contextual_schema_carries_record(contextual) { + contextual.clone() + } else { + inferred + } +} + +/// Reject a contextual named-record label when the expression at the eager or +/// callback result position is not a construction the normalizer can +/// materialize. A typed/materialized record value has no omitted field and +/// passes; an ordinary partial map variable does not acquire the label. +fn require_materialized_context_result( + expression: &Expr, + expected: &TypeSchema, + span: Span, + method_name: &str, + position: &str, + env: &Env, +) -> Result<(), CompileError> { + if !method_context_schema_is_known(expected) || !contextual_schema_carries_record(expected) { + return Ok(()); + } + if let Some(detail) = super::typing::omitted_option_materialization_detail_for_slots( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + expression, + expected, + &env.slots, + ) { + return Err(result_error( + span, + format!( + ".{} {} requires a materialized {} ({detail})", + method_name, + position, + render_schema(expected) + ), + )); + } + Ok(()) +} + +/// Retain the checked contextual callback result only after proving the +/// callback result position can realize the omitted logical `Option` field. +fn retain_contextual_callback_result_for_expr( + expression: &Expr, + callback_schema: TypeSchema, + contextual: &TypeSchema, + span: Span, + method_name: &str, + env: &Env, +) -> Result { + if method_context_schema_is_known(contextual) && contextual_schema_carries_record(contextual) { + let TypeSchema::Callable { params, .. } = &callback_schema else { + return Ok(callback_schema); + }; + let expected = TypeSchema::Callable { + params: params.clone(), + result: Box::new(contextual.clone()), + }; + require_materialized_context_result( + expression, + &expected, + span, + method_name, + "callback result", + env, + )?; + } + Ok(retain_contextual_callback_result( + callback_schema, + contextual, + )) +} + /// Whether a schema still contains an unsubstituted generic parameter. A /// branch carrying one cannot be proven resource-free at the generic /// definition, because a later instantiation may bind it to a resource type. -fn schema_contains_generic_param(schema: &TypeSchema) -> bool { +pub(crate) fn schema_contains_generic_param(schema: &TypeSchema) -> bool { match schema { TypeSchema::GenericParam(_) => true, TypeSchema::Optional(inner) | TypeSchema::Array(inner) | TypeSchema::Map(inner) => { @@ -1255,6 +2696,18 @@ fn require_method_schema( span: Span, method: ResultMethod, position: &str, +) -> Result<(), CompileError> { + require_method_schema_named(env, expected, actual, span, method.name(), position) +} + +/// Shared assignment check used by both Result and logical Option methods. +fn require_method_schema_named( + env: &Env, + expected: &TypeSchema, + actual: &TypeSchema, + span: Span, + method_name: &str, + position: &str, ) -> Result<(), CompileError> { let mismatch = super::typing::schema_assignment_mismatch( &env.function_list, @@ -1268,7 +2721,7 @@ fn require_method_schema( span, format!( ".{} {} expects {} but found {} ({detail})", - method.name(), + method_name, position, render_schema(expected), render_schema(actual) @@ -1284,9 +2737,35 @@ fn require_method_schema( fn method_callback_schema( argument: &Expr, param: &TypeSchema, + contextual_result: Option<&TypeSchema>, span: Span, method: ResultMethod, env: &Env, +) -> Result { + callback_schema_for_params( + argument, + std::slice::from_ref(param), + contextual_result, + span, + method.name(), + env, + ) +} + +/// Shared contextual callback inference for Result and logical Option +/// methods. `params` is the exact expected callable parameter list, including +/// the empty list for zero-argument Option fallbacks. `contextual_result` +/// carries the already-known callback return schema, when the method owner has +/// one, so each branch can be checked and substituted before the strict +/// callable merge. Without a known result context the branches keep their +/// inferred schemas. +fn callback_schema_for_params( + argument: &Expr, + params: &[TypeSchema], + contextual_result: Option<&TypeSchema>, + span: Span, + method_name: &str, + env: &Env, ) -> Result { // Callable-producing expressions must contribute a callable schema whose // closure parameters are the contextually expected ones. Inferring an @@ -1301,37 +2780,192 @@ fn method_callback_schema( &env.struct_schemas, &env.slots, &closure.param_slots, - Some(std::slice::from_ref(param)), + Some(params), &closure.capture_copies, )); - infer_callable_callback(argument, &nested, span, method, env) + let callback_schema = + infer_callable_callback(argument, &nested, span, method_name, env)?; + contextualize_callback_result( + argument, + callback_schema, + contextual_result, + span, + method_name, + env, + ) } Expr::IfElse { then_expr, else_expr, .. } => { - let then_schema = method_callback_schema(then_expr, param, span, method, env)?; - let else_schema = method_callback_schema(else_expr, param, span, method, env)?; - merge_callable_branches(then_schema, else_schema, span, method) + let then_schema = callback_schema_for_params( + then_expr, + params, + contextual_result, + span, + method_name, + env, + )?; + let else_schema = callback_schema_for_params( + else_expr, + params, + contextual_result, + span, + method_name, + env, + )?; + merge_callable_branches_named(then_schema, else_schema, span, method_name) } Expr::Match { arms, default, .. } => { let mut merged: Option = None; for (_, arm) in arms { - let arm_schema = method_callback_schema(arm, param, span, method, env)?; + let arm_schema = callback_schema_for_params( + arm, + params, + contextual_result, + span, + method_name, + env, + )?; merged = Some(match merged { - Some(existing) => merge_callable_branches(existing, arm_schema, span, method)?, + Some(existing) => { + merge_callable_branches_named(existing, arm_schema, span, method_name)? + } None => arm_schema, }); } - let default_schema = method_callback_schema(default, param, span, method, env)?; + let default_schema = callback_schema_for_params( + default, + params, + contextual_result, + span, + method_name, + env, + )?; match merged { - Some(existing) => merge_callable_branches(existing, default_schema, span, method), + Some(existing) => { + merge_callable_branches_named(existing, default_schema, span, method_name) + } None => Ok(default_schema), } } - Expr::Block { expr, .. } => method_callback_schema(expr, param, span, method, env), - _ => infer_callable_callback(argument, &env.slots, span, method, env), + Expr::Block { expr, .. } => { + callback_schema_for_params(expr, params, contextual_result, span, method_name, env) + } + _ => { + let callback_schema = + infer_callable_callback(argument, &env.slots, span, method_name, env)?; + contextualize_callback_result( + argument, + callback_schema, + contextual_result, + span, + method_name, + env, + ) + } + } +} + +/// Apply the known contextual callback result to one branch before the shared +/// exact merge. The branch result expression must still pass the existing +/// named structural check and the construction-admission walk; incompatible +/// shapes keep their precise rejection instead of being relabelled. +fn contextualize_callback_result( + expression: &Expr, + callback_schema: TypeSchema, + contextual_result: Option<&TypeSchema>, + span: Span, + method_name: &str, + env: &Env, +) -> Result { + let Some(contextual) = contextual_result else { + return Ok(callback_schema); + }; + if !method_context_schema_is_known(contextual) || !contextual_schema_carries_record(contextual) + { + return Ok(callback_schema); + } + let TypeSchema::Callable { params, .. } = &callback_schema else { + return Ok(callback_schema); + }; + let inferred_result = callback_result_schema(&callback_schema); + if let Some(detail) = contextual_callback_result_mismatch(env, contextual, &inferred_result) { + return Err(result_error( + span, + format!( + ".{} callback result expects {} but found {} ({detail})", + method_name, + render_schema(contextual), + render_schema(&inferred_result) + ), + )); + } + let expected = TypeSchema::Callable { + params: params.clone(), + result: Box::new(contextual.clone()), + }; + require_materialized_context_result( + expression, + &expected, + span, + method_name, + "callback result", + env, + )?; + Ok(TypeSchema::Callable { + params: params.clone(), + result: Box::new(contextual.clone()), + }) +} + +/// Compare a contextual callback result against its inferred counterpart +/// through the same structural field rules used elsewhere, while descending +/// `Option`/`Result` payload branches instead of applying the special exact +/// Result-branch equality used when a whole Result value is compared. +fn contextual_callback_result_mismatch( + env: &Env, + expected: &TypeSchema, + actual: &TypeSchema, +) -> Option { + match expected { + TypeSchema::Option(expected_inner) => match actual { + TypeSchema::Option(actual_inner) => { + contextual_callback_result_mismatch(env, expected_inner, actual_inner) + } + _ => super::typing::schema_assignment_mismatch( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + expected, + actual, + ), + }, + TypeSchema::Result { + ok: expected_ok, + err: expected_err, + } => match actual { + TypeSchema::Result { + ok: actual_ok, + err: actual_err, + } => contextual_callback_result_mismatch(env, expected_ok, actual_ok) + .or_else(|| contextual_callback_result_mismatch(env, expected_err, actual_err)), + _ => super::typing::schema_assignment_mismatch( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + expected, + actual, + ), + }, + _ => super::typing::schema_assignment_mismatch( + &env.function_list, + &env.function_impls, + &env.struct_schemas, + expected, + actual, + ), } } @@ -1339,7 +2973,7 @@ fn infer_callable_callback( argument: &Expr, slots: &HashMap, span: Span, - method: ResultMethod, + method_name: &str, env: &Env, ) -> Result { let inferred = super::typing::infer_value_schema( @@ -1354,7 +2988,7 @@ fn infer_callable_callback( span, format!( ".{} requires a typed callable callback, found an untyped value", - method.name() + method_name ), ) })?; @@ -1363,7 +2997,7 @@ fn infer_callable_callback( span, format!( ".{} requires a callable callback, found {}", - method.name(), + method_name, render_schema(&inferred) ), )); @@ -1371,18 +3005,18 @@ fn infer_callable_callback( Ok(inferred) } -fn merge_callable_branches( +fn merge_callable_branches_named( then_schema: TypeSchema, else_schema: TypeSchema, span: Span, - method: ResultMethod, + method_name: &str, ) -> Result { if then_schema != else_schema { return Err(result_error( span, format!( ".{} callback branches produce different callable schemas: {} vs {}", - method.name(), + method_name, render_schema(&then_schema), render_schema(&else_schema) ), @@ -1404,6 +3038,16 @@ fn reject_resource_capturing_closure( span: Span, method: ResultMethod, env: &Env, +) -> Result<(), CompileError> { + reject_resource_capturing_closure_named(argument, argument_schema, span, method.name(), env) +} + +fn reject_resource_capturing_closure_named( + argument: &Expr, + argument_schema: &TypeSchema, + span: Span, + method_name: &str, + env: &Env, ) -> Result<(), CompileError> { // The inline capture and temporary-factory checks both describe callable // callback ownership. A non-callable fallback (`unwrap_or` on a scalar @@ -1416,7 +3060,7 @@ fn reject_resource_capturing_closure( span, format!( ".{} callback/fallback cannot move a resource value into a closure capture ({}): the unselected branch has no destination for that owner; pass a named function or bind the callable before the method", - method.name(), + method_name, render_schema(&schema) ), )); @@ -1427,7 +3071,7 @@ fn reject_resource_capturing_closure( span, format!( ".{} callback/fallback cannot use a temporary callable whose factory moves a resource value into its captures ({}): the unselected branch has no destination for that owner; bind the callable before the method", - method.name(), + method_name, render_schema(&schema) ), )), @@ -1435,7 +3079,7 @@ fn reject_resource_capturing_closure( span, format!( ".{} cannot prove the capture ownership of the temporary callable produced by this call: bind the callable before the method or pass a named function", - method.name() + method_name ), )), } @@ -1588,6 +3232,7 @@ fn temporary_callable_capture( // Scalar leaves cannot produce a callable value; every other // unmodeled callable-producing expression is unproven, not Free. Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -2588,6 +4233,9 @@ fn collect_capture_expr_producers( Expr::ResultTry { value, .. } => { collect_capture_expr_producers(value, env, written, visiting_functions); } + Expr::ResultConvert { value, .. } => { + collect_capture_expr_producers(value, env, written, visiting_functions); + } Expr::ResultMatch { value, ok_expr, @@ -2604,7 +4252,29 @@ fn collect_capture_expr_producers( collect_capture_expr_producers(receiver, env, written, visiting_functions); collect_capture_expr_producers(argument, env, written, visiting_functions); } + Expr::OptionMethod { + receiver, argument, .. + } => { + collect_capture_expr_producers(receiver, env, written, visiting_functions); + collect_capture_expr_producers(argument, env, written, visiting_functions); + } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + collect_capture_expr_producers(payload, env, written, visiting_functions); + } + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + collect_capture_expr_producers(value, env, written, visiting_functions); + collect_capture_expr_producers(some_expr, env, written, visiting_functions); + collect_capture_expr_producers(none_expr, env, written, visiting_functions); + } Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -2657,6 +4327,7 @@ fn copy_outer_callable_state(walk: &mut CaptureWalk<'_>, nested: &CaptureWalk<'_ fn apply_expr_effects(expr: &Expr, walk: &mut CaptureWalk<'_>) { match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -2767,6 +4438,7 @@ fn apply_expr_effects(expr: &Expr, walk: &mut CaptureWalk<'_>) { Expr::ResultConstruct { payload, .. } => apply_expr_effects(payload, walk), Expr::ResultQuery { value, .. } => apply_expr_effects(value, walk), Expr::ResultTry { value, .. } => apply_expr_effects(value, walk), + Expr::ResultConvert { value, .. } => apply_expr_effects(value, walk), Expr::ResultMatch { value, ok_expr, @@ -2786,6 +4458,30 @@ fn apply_expr_effects(expr: &Expr, walk: &mut CaptureWalk<'_>) { apply_expr_effects(receiver, walk); apply_expr_effects(argument, walk); } + Expr::OptionMethod { + receiver, argument, .. + } => { + apply_expr_effects(receiver, walk); + apply_expr_effects(argument, walk); + } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + apply_expr_effects(payload, walk); + } + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + apply_expr_effects(value, walk); + let mut some_walk = walk.clone(); + apply_expr_effects(some_expr, &mut some_walk); + let mut none_walk = walk.clone(); + apply_expr_effects(none_expr, &mut none_walk); + join_branch_walks(walk, &some_walk, &none_walk); + } } } @@ -2848,6 +4544,7 @@ fn collect_stmt_writes(stmt: &Stmt, written: &mut HashSet) { fn collect_expr_writes(expr: &Expr, written: &mut HashSet) { match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -2929,6 +4626,7 @@ fn collect_expr_writes(expr: &Expr, written: &mut HashSet) { Expr::ResultConstruct { payload, .. } => collect_expr_writes(payload, written), Expr::ResultQuery { value, .. } => collect_expr_writes(value, written), Expr::ResultTry { value, .. } => collect_expr_writes(value, written), + Expr::ResultConvert { value, .. } => collect_expr_writes(value, written), Expr::ResultMatch { value, ok_expr, @@ -2945,6 +4643,27 @@ fn collect_expr_writes(expr: &Expr, written: &mut HashSet) { collect_expr_writes(receiver, written); collect_expr_writes(argument, written); } + Expr::OptionMethod { + receiver, argument, .. + } => { + collect_expr_writes(receiver, written); + collect_expr_writes(argument, written); + } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + collect_expr_writes(payload, written); + } + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + collect_expr_writes(value, written); + collect_expr_writes(some_expr, written); + collect_expr_writes(none_expr, written); + } } } @@ -3008,6 +4727,7 @@ fn find_resource_capturing_closure(expr: &Expr, env: &Env) -> Option Expr::ResultConstruct { payload, .. } => find_resource_capturing_closure(payload, env), Expr::ResultQuery { value, .. } => find_resource_capturing_closure(value, env), Expr::ResultTry { value, .. } => find_resource_capturing_closure(value, env), + Expr::ResultConvert { value, .. } => find_resource_capturing_closure(value, env), Expr::ResultMatch { value, ok_expr, @@ -3020,6 +4740,21 @@ fn find_resource_capturing_closure(expr: &Expr, env: &Env) -> Option receiver, argument, .. } => find_resource_capturing_closure(receiver, env) .or_else(|| find_resource_capturing_closure(argument, env)), + Expr::OptionMethod { + receiver, argument, .. + } => find_resource_capturing_closure(receiver, env) + .or_else(|| find_resource_capturing_closure(argument, env)), + Expr::OptionConstruct { payload, .. } => payload + .as_deref() + .and_then(|payload| find_resource_capturing_closure(payload, env)), + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => find_resource_capturing_closure(value, env) + .or_else(|| find_resource_capturing_closure(some_expr, env)) + .or_else(|| find_resource_capturing_closure(none_expr, env)), Expr::OptionalGet { container, key, .. } => find_resource_capturing_closure(container, env) .or_else(|| find_resource_capturing_closure(key, env)), Expr::OptionUnwrapOr { @@ -3027,6 +4762,7 @@ fn find_resource_capturing_closure(expr: &Expr, env: &Env) -> Option } => find_resource_capturing_closure(value, env) .or_else(|| find_resource_capturing_closure(fallback, env)), Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -3292,19 +5028,41 @@ fn resolve_try( env: &mut Env, ) -> Result<(), CompileError> { resolve_expr(value, None, env)?; - let operand = operand_schema_of(value, env).ok_or_else(|| { - result_error( - span, - "? operand must be Result, found an untyped value".to_string(), - ) - })?; + let operand = match operand_schema_of(value, env) { + Some(operand) => operand, + None if is_unresolved_option_producer(value) => { + // The Option resolver types Option constructors/matches and + // shared combinator methods after this pass. + return Ok(()); + } + None => { + return Err(result_error( + span, + "? operand must be Result or a logical Option, found an untyped value" + .to_string(), + )); + } + }; let (operand_ok, operand_err) = match &operand { TypeSchema::Result { ok, err } => ((**ok).clone(), (**err).clone()), + TypeSchema::Option(inner) => { + // Option is not a Result operand and not a scalar Option + // propagation. Reject at the '?' so the caller unwraps first. + if matches!(inner.as_ref(), TypeSchema::Result { .. }) { + return Err(result_error( + span, + "? operand must be Result, not Option>; unwrap the Option before ?" + .to_string(), + )); + } + // Scalar Option propagation is owned by the Option resolver. + return Ok(()); + } other => { return Err(result_error( span, format!( - "? operand must be Result, found {}", + "? operand must be Result or a logical Option, found {}", render_schema(other) ), )); @@ -3346,6 +5104,25 @@ fn resolve_try( Ok(()) } +fn is_unresolved_option_producer(expr: &Expr) -> bool { + match expr { + Expr::OptionConstruct { .. } | Expr::OptionMatch { .. } | Expr::OptionMethod { .. } => true, + Expr::ResultMethod { .. } => true, + Expr::Block { expr, .. } => is_unresolved_option_producer(expr), + Expr::IfElse { + then_expr, + else_expr, + .. + } => is_unresolved_option_producer(then_expr) || is_unresolved_option_producer(else_expr), + Expr::Match { arms, default, .. } => { + arms.iter() + .any(|(_, arm)| is_unresolved_option_producer(arm)) + || is_unresolved_option_producer(default) + } + _ => false, + } +} + fn operand_schema_of(expr: &Expr, env: &Env) -> Option { if let Some(schema) = authoritative_schema(expr, env) { return Some(schema); @@ -3437,7 +5214,7 @@ fn substitute_schema_bindings(schema: TypeSchema, bindings: &[(&str, &TypeSchema } } -fn render_schema(schema: &TypeSchema) -> String { +pub(crate) fn render_schema(schema: &TypeSchema) -> String { super::typing::render_schema_label(schema) } @@ -3719,6 +5496,7 @@ mod tests { }, ); Env { + declaration_schemas: slots.clone(), slots, functions: HashMap::new(), function_list: Vec::new(), diff --git a/src/compiler/semantic_model.rs b/src/compiler/semantic_model.rs index 2a29dd6b..48990be2 100644 --- a/src/compiler/semantic_model.rs +++ b/src/compiler/semantic_model.rs @@ -619,6 +619,7 @@ impl SemanticModel { match schema { TypeSchema::Unknown => crate::host_api::HostTypeSchema::Unknown, TypeSchema::Null => HostTypeSchema::Null, + TypeSchema::Unit => HostTypeSchema::Unit, TypeSchema::Int => HostTypeSchema::Int, TypeSchema::Float => HostTypeSchema::Float, TypeSchema::Number => HostTypeSchema::Number, @@ -634,6 +635,9 @@ impl SemanticModel { TypeSchema::Optional(inner) => { HostTypeSchema::Optional(Box::new(self.compiler_schema_to_host_schema(inner))) } + TypeSchema::Option(inner) => { + HostTypeSchema::Option(Box::new(self.compiler_schema_to_host_schema(inner))) + } TypeSchema::Callable { params, result } => HostTypeSchema::Callable { params: params .iter() @@ -1021,10 +1025,7 @@ impl SemanticModel { let mut segments: Vec = Vec::new(); let mut walk = cursor - 1; let mut expect_ident = true; - loop { - let Some(token) = tokens.get(walk) else { - break; - }; + while let Some(token) = tokens.get(walk) { if token.span.source_id != position.source_id { break; } @@ -1066,10 +1067,7 @@ impl SemanticModel { let mut segments: Vec = Vec::new(); let mut cursor = idx; let mut expect_ident = true; - loop { - let Some(token) = tokens.get(cursor) else { - break; - }; + while let Some(token) = tokens.get(cursor) { if token.span.source_id != position.source_id { break; } @@ -1639,6 +1637,7 @@ impl std::fmt::Display for TypeSchema { match self { TypeSchema::Unknown => write!(f, "unknown"), TypeSchema::Null => write!(f, "null"), + TypeSchema::Unit => write!(f, "()"), TypeSchema::Int => write!(f, "int"), TypeSchema::Float => write!(f, "float"), TypeSchema::Number => write!(f, "number"), @@ -1646,6 +1645,7 @@ impl std::fmt::Display for TypeSchema { TypeSchema::String => write!(f, "string"), TypeSchema::Bytes => write!(f, "bytes"), TypeSchema::Optional(inner) => write!(f, "optional<{inner}>"), + TypeSchema::Option(inner) => write!(f, "Option<{inner}>"), TypeSchema::GenericParam(name) => write!(f, "{name}"), TypeSchema::Named(name, args) => { if args.is_empty() { diff --git a/src/compiler/source_loader.rs b/src/compiler/source_loader.rs index 340a8724..69dc6918 100644 --- a/src/compiler/source_loader.rs +++ b/src/compiler/source_loader.rs @@ -52,6 +52,9 @@ use super::{ mod graph; mod imports; + +#[cfg(test)] +pub(crate) use imports::test_module_imports_from_uses; mod model; pub use self::model::{DeclarationNamespace, ImportClause, ModuleImport, NamedImport}; diff --git a/src/compiler/source_loader/graph.rs b/src/compiler/source_loader/graph.rs index 1144f56b..84d84e1a 100644 --- a/src/compiler/source_loader/graph.rs +++ b/src/compiler/source_loader/graph.rs @@ -448,7 +448,10 @@ fn collect_named_schema_names(schema: &TypeSchema, names: &mut HashSet) collect_named_schema_names(argument, names); } } - TypeSchema::Optional(inner) | TypeSchema::Array(inner) | TypeSchema::Map(inner) => { + TypeSchema::Optional(inner) + | TypeSchema::Option(inner) + | TypeSchema::Array(inner) + | TypeSchema::Map(inner) => { collect_named_schema_names(inner, names); } TypeSchema::ArrayTuple(items) => { @@ -479,6 +482,7 @@ fn collect_named_schema_names(schema: &TypeSchema, names: &mut HashSet) } TypeSchema::Unknown | TypeSchema::Null + | TypeSchema::Unit | TypeSchema::Int | TypeSchema::Float | TypeSchema::Number @@ -1795,6 +1799,7 @@ fn resolve_expr_imported_calls( } } Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -1810,9 +1815,7 @@ fn resolve_expr_imported_calls( field_projection: _, container, key, - container_slot: _, - key_slot: _, - semantic_id: _, + .. } => { resolve_expr_imported_calls( ctx, @@ -1881,6 +1884,15 @@ fn resolve_expr_imported_calls( resolve_expr_imported_calls(ctx, receiver, line, parsed_semantic_index.as_deref_mut())?; resolve_expr_imported_calls(ctx, argument, line, parsed_semantic_index.as_deref_mut())?; } + Expr::OptionMethod { + receiver, argument, .. + } => { + resolve_expr_imported_calls(ctx, receiver, line, parsed_semantic_index.as_deref_mut())?; + resolve_expr_imported_calls(ctx, argument, line, parsed_semantic_index.as_deref_mut())?; + } + Expr::ResultConvert { value, .. } => { + resolve_expr_imported_calls(ctx, value, line, parsed_semantic_index.as_deref_mut())?; + } Expr::ResultConstruct { payload, .. } => { resolve_expr_imported_calls(ctx, payload, line, parsed_semantic_index.as_deref_mut())?; } @@ -1900,6 +1912,36 @@ fn resolve_expr_imported_calls( resolve_expr_imported_calls(ctx, ok_expr, line, parsed_semantic_index.as_deref_mut())?; resolve_expr_imported_calls(ctx, err_expr, line, parsed_semantic_index.as_deref_mut())?; } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + resolve_expr_imported_calls( + ctx, + payload, + line, + parsed_semantic_index.as_deref_mut(), + )?; + } + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + resolve_expr_imported_calls(ctx, value, line, parsed_semantic_index.as_deref_mut())?; + resolve_expr_imported_calls( + ctx, + some_expr, + line, + parsed_semantic_index.as_deref_mut(), + )?; + resolve_expr_imported_calls( + ctx, + none_expr, + line, + parsed_semantic_index.as_deref_mut(), + )?; + } Expr::IfElse { condition, then_expr, diff --git a/src/compiler/source_loader/imports.rs b/src/compiler/source_loader/imports.rs index 36c21301..8e1e44e4 100644 --- a/src/compiler/source_loader/imports.rs +++ b/src/compiler/source_loader/imports.rs @@ -173,6 +173,14 @@ fn use_declarations_to_module_imports( .collect() } +#[cfg(test)] +pub(crate) fn test_module_imports_from_uses( + path: &Path, + decls: &[UseDecl], +) -> Result, SourcePathError> { + use_declarations_to_module_imports(path, decls) +} + pub(super) fn is_valid_ident(input: &str) -> bool { let mut chars = input.chars(); let Some(first) = chars.next() else { diff --git a/src/compiler/try_anf.rs b/src/compiler/try_anf.rs index 9932cca9..e7c2b3a8 100644 --- a/src/compiler/try_anf.rs +++ b/src/compiler/try_anf.rs @@ -112,6 +112,15 @@ fn attach_expr_schema( attach_expr_schema(value, schemas, structs); attach_expr_schema(fallback, schemas, structs); } + Expr::OptionMethod { + receiver, argument, .. + } => { + attach_expr_schema(receiver, schemas, structs); + attach_expr_schema(argument, schemas, structs); + } + Expr::ResultConvert { value, .. } => { + attach_expr_schema(value, schemas, structs); + } Expr::Call(_, _, args, _, _) | Expr::ModuleCall(_, _, args, _) | Expr::LocalCall(_, _, args, _) @@ -188,7 +197,23 @@ fn attach_expr_schema( attach_expr_schema(receiver, schemas, structs); attach_expr_schema(argument, schemas, structs); } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + attach_expr_schema(payload, schemas, structs); + } + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + attach_expr_schema(value, schemas, structs); + attach_expr_schema(some_expr, schemas, structs); + attach_expr_schema(none_expr, schemas, structs); + } Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -293,6 +318,13 @@ fn rewrite_children(expr: &mut Expr, locals: &mut usize) -> Result<(), ParseErro rewrite_expr(value, locals)?; rewrite_expr(fallback, locals) } + Expr::OptionMethod { + receiver, argument, .. + } => { + rewrite_expr(receiver, locals)?; + rewrite_expr(argument, locals) + } + Expr::ResultConvert { value, .. } => rewrite_expr(value, locals), Expr::Call(_, _, args, _, _) | Expr::ModuleCall(_, _, args, _) | Expr::LocalCall(_, _, args, _) @@ -368,7 +400,24 @@ fn rewrite_children(expr: &mut Expr, locals: &mut usize) -> Result<(), ParseErro rewrite_expr(receiver, locals)?; rewrite_expr(argument, locals) } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + rewrite_expr(payload, locals)?; + } + Ok(()) + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + rewrite_expr(value, locals)?; + rewrite_expr(some_expr, locals)?; + rewrite_expr(none_expr, locals) + } Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -535,6 +584,10 @@ fn contains_try(expr: &Expr) -> bool { Expr::OptionUnwrapOr { value, fallback, .. } => contains_try(value) || contains_try(fallback), + Expr::OptionMethod { + receiver, argument, .. + } => contains_try(receiver) || contains_try(argument), + Expr::ResultConvert { value, .. } => contains_try(value), Expr::Call(_, _, args, _, _) | Expr::ModuleCall(_, _, args, _) | Expr::LocalCall(_, _, args, _) @@ -582,7 +635,15 @@ fn contains_try(expr: &Expr) -> bool { Expr::ResultMethod { receiver, argument, .. } => contains_try(receiver) || contains_try(argument), + Expr::OptionConstruct { payload, .. } => payload.as_deref().is_some_and(contains_try), + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => contains_try(value) || contains_try(some_expr) || contains_try(none_expr), Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) diff --git a/src/compiler/type_schema_codec.rs b/src/compiler/type_schema_codec.rs new file mode 100644 index 00000000..73e419fb --- /dev/null +++ b/src/compiler/type_schema_codec.rs @@ -0,0 +1,552 @@ +//! Pure TypeSchema wire codec. Program and host-schema encoders stay in +//! `vmbc`; this module is the only owner of TypeSchema tag order, depth and +//! blob checks, and it compiles without the VM runtime. + +use std::collections::HashMap; +use std::hash::Hash; + +use crate::host_api::ResourceTypeKey; + +use super::ir::TypeSchema; + +pub(crate) const MAX_WIRE_BLOB_BYTES: usize = 16 * 1024 * 1024; +pub(crate) const MAX_WIRE_COUNT: usize = 1_000_000; +pub(crate) const MAX_WIRE_AGGREGATE_ITEMS: usize = 1_000_000; +pub(crate) const MAX_SCHEMA_DEPTH: usize = 64; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum SchemaCodecError { + UnexpectedEof, + InvalidValueType(u8), + InvalidResourceKey(String), + InvalidUtf8, + LengthTooLarge(&'static str, usize), + TrailingBytes, +} + +impl std::fmt::Display for SchemaCodecError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + SchemaCodecError::UnexpectedEof => write!(f, "unexpected end of input"), + SchemaCodecError::InvalidValueType(value) => write!(f, "invalid value type: {value}"), + SchemaCodecError::InvalidResourceKey(reason) => { + write!(f, "invalid resource key: {reason}") + } + SchemaCodecError::InvalidUtf8 => write!(f, "invalid utf-8 string"), + SchemaCodecError::LengthTooLarge(field, len) => { + write!(f, "{field} length too large: {len}") + } + SchemaCodecError::TrailingBytes => write!(f, "trailing bytes after program payload"), + } + } +} + +impl std::error::Error for SchemaCodecError {} + +pub(crate) trait SchemaReader { + fn read_u8(&mut self) -> Result; + fn read_string(&mut self) -> Result; + fn read_count( + &mut self, + field: &'static str, + min_item_bytes: usize, + ) -> Result; + fn read_count_with_overhead( + &mut self, + field: &'static str, + min_item_bytes: usize, + fixed_bytes: usize, + ) -> Result; + fn debit_count(&mut self, field: &'static str, count: usize) -> Result<(), SchemaCodecError>; + fn remaining(&self) -> usize; +} + +pub(crate) fn encode_type_schema_bytes(schema: &TypeSchema) -> Result, SchemaCodecError> { + let mut out = Vec::new(); + write_schema(schema, &mut out)?; + Ok(out) +} + +pub(crate) fn decode_type_schema_bytes(bytes: &[u8]) -> Result { + let mut cursor = SchemaCursor::new(bytes); + let schema = read_schema(&mut cursor, 0)?; + if cursor.remaining() != 0 { + return Err(SchemaCodecError::TrailingBytes); + } + Ok(schema) +} + +pub(crate) fn write_schema(schema: &TypeSchema, out: &mut Vec) -> Result<(), SchemaCodecError> { + match schema { + TypeSchema::Unknown => out.push(0), + TypeSchema::Null => out.push(1), + TypeSchema::Unit => out.push(20), + TypeSchema::Int => out.push(2), + TypeSchema::Float => out.push(3), + TypeSchema::Number => out.push(4), + TypeSchema::Bool => out.push(5), + TypeSchema::String => out.push(6), + TypeSchema::Bytes => out.push(7), + TypeSchema::Optional(inner) => { + out.push(16); + write_schema(inner, out)?; + } + TypeSchema::Option(inner) => { + out.push(19); + write_schema(inner, out)?; + } + TypeSchema::GenericParam(name) => { + out.push(8); + write_string("schema generic", name, out)?; + } + TypeSchema::Named(name, type_args) => { + out.push(9); + write_string("schema name", name, out)?; + write_u32_count("schema type args", type_args.len(), out)?; + for type_arg in type_args { + write_schema(type_arg, out)?; + } + } + TypeSchema::Array(item) => { + out.push(10); + write_schema(item, out)?; + } + TypeSchema::ArrayTuple(items) => { + out.push(11); + write_u32_count("schema tuple items", items.len(), out)?; + for item in items { + write_schema(item, out)?; + } + } + TypeSchema::ArrayTupleRest { prefix, rest } => { + out.push(12); + write_u32_count("schema tuple prefix", prefix.len(), out)?; + for item in prefix { + write_schema(item, out)?; + } + write_schema(rest, out)?; + } + TypeSchema::Map(item) => { + out.push(13); + write_schema(item, out)?; + } + TypeSchema::Object(fields) => { + out.push(14); + let mut entries = fields.iter().collect::>(); + entries.sort_unstable_by_key(|(name, _)| *name); + write_u32_count("schema object fields", entries.len(), out)?; + for (name, value) in entries { + write_string("schema object field", name, out)?; + write_schema(value, out)?; + } + } + TypeSchema::Callable { params, result } => { + out.push(15); + write_u32_count("schema callable params", params.len(), out)?; + for param in params { + write_schema(param, out)?; + } + write_schema(result, out)?; + } + TypeSchema::Resource(key) => { + out.push(17); + write_string("schema resource key", key.as_str(), out)?; + } + TypeSchema::Result { ok, err } => { + out.push(18); + write_schema(ok, out)?; + write_schema(err, out)?; + } + } + Ok(()) +} + +pub(crate) fn read_schema( + cursor: &mut impl SchemaReader, + depth: usize, +) -> Result { + if depth >= MAX_SCHEMA_DEPTH { + return Err(SchemaCodecError::LengthTooLarge( + "schema nesting depth", + depth, + )); + } + cursor.debit_count("schema nodes", 1)?; + let nested_depth = depth + .checked_add(1) + .ok_or(SchemaCodecError::LengthTooLarge( + "schema nesting depth", + depth, + ))?; + match cursor.read_u8()? { + 0 => Ok(TypeSchema::Unknown), + 1 => Ok(TypeSchema::Null), + 2 => Ok(TypeSchema::Int), + 3 => Ok(TypeSchema::Float), + 4 => Ok(TypeSchema::Number), + 5 => Ok(TypeSchema::Bool), + 6 => Ok(TypeSchema::String), + 7 => Ok(TypeSchema::Bytes), + 16 => Ok(TypeSchema::Optional(Box::new(read_schema( + cursor, + nested_depth, + )?))), + 8 => Ok(TypeSchema::GenericParam(cursor.read_string()?)), + 9 => { + let name = cursor.read_string()?; + let count = cursor.read_count("schema type args", 1)?; + let mut type_args = Vec::new(); + reserve_vec(&mut type_args, "schema type args", count)?; + for _ in 0..count { + type_args.push(read_schema(cursor, nested_depth)?); + } + Ok(TypeSchema::Named(name, type_args)) + } + 10 => Ok(TypeSchema::Array(Box::new(read_schema( + cursor, + nested_depth, + )?))), + 11 => { + let count = cursor.read_count("schema tuple items", 1)?; + let mut items = Vec::new(); + reserve_vec(&mut items, "schema tuple items", count)?; + for _ in 0..count { + items.push(read_schema(cursor, nested_depth)?); + } + Ok(TypeSchema::ArrayTuple(items)) + } + 12 => { + let count = cursor.read_count_with_overhead("schema tuple prefix", 1, 1)?; + let mut prefix = Vec::new(); + reserve_vec(&mut prefix, "schema tuple prefix", count)?; + for _ in 0..count { + prefix.push(read_schema(cursor, nested_depth)?); + } + let rest = Box::new(read_schema(cursor, nested_depth)?); + Ok(TypeSchema::ArrayTupleRest { prefix, rest }) + } + 13 => Ok(TypeSchema::Map(Box::new(read_schema( + cursor, + nested_depth, + )?))), + 14 => { + let count = cursor.read_count("schema object fields", 5)?; + let mut fields = HashMap::new(); + reserve_map(&mut fields, "schema object fields", count)?; + for _ in 0..count { + let name = cursor.read_string()?; + let value = read_schema(cursor, nested_depth)?; + fields.insert(name, value); + } + Ok(TypeSchema::Object(fields)) + } + 15 => { + let count = cursor.read_count_with_overhead("schema callable params", 1, 1)?; + let mut params = Vec::new(); + reserve_vec(&mut params, "schema callable params", count)?; + for _ in 0..count { + params.push(read_schema(cursor, nested_depth)?); + } + let result = Box::new(read_schema(cursor, nested_depth)?); + Ok(TypeSchema::Callable { params, result }) + } + 17 => { + let key_text = cursor.read_string()?; + let key = ResourceTypeKey::new(key_text) + .map_err(|err| SchemaCodecError::InvalidResourceKey(err.to_string()))?; + Ok(TypeSchema::Resource(key)) + } + 18 => Ok(TypeSchema::Result { + ok: Box::new(read_schema(cursor, nested_depth)?), + err: Box::new(read_schema(cursor, nested_depth)?), + }), + 19 => Ok(TypeSchema::Option(Box::new(read_schema( + cursor, + nested_depth, + )?))), + 20 => Ok(TypeSchema::Unit), + other => Err(SchemaCodecError::InvalidValueType(other)), + } +} + +fn write_string( + field: &'static str, + value: &str, + out: &mut Vec, +) -> Result<(), SchemaCodecError> { + write_u32_len(field, value.len(), out)?; + out.extend_from_slice(value.as_bytes()); + Ok(()) +} + +fn write_u32_len( + field: &'static str, + len: usize, + out: &mut Vec, +) -> Result<(), SchemaCodecError> { + let len_u32 = u32::try_from(len).map_err(|_| SchemaCodecError::LengthTooLarge(field, len))?; + out.extend_from_slice(&len_u32.to_le_bytes()); + Ok(()) +} + +fn write_u32_count( + field: &'static str, + count: usize, + out: &mut Vec, +) -> Result<(), SchemaCodecError> { + write_u32_len(field, count, out) +} + +fn reserve_vec( + items: &mut Vec, + field: &'static str, + count: usize, +) -> Result<(), SchemaCodecError> { + items + .try_reserve_exact(count) + .map_err(|_| SchemaCodecError::LengthTooLarge(field, count)) +} + +fn reserve_map( + items: &mut HashMap, + field: &'static str, + count: usize, +) -> Result<(), SchemaCodecError> { + items + .try_reserve(count) + .map_err(|_| SchemaCodecError::LengthTooLarge(field, count)) +} + +struct SchemaCursor<'a> { + bytes: &'a [u8], + offset: usize, + remaining_budget: usize, +} + +impl<'a> SchemaCursor<'a> { + fn new(bytes: &'a [u8]) -> Self { + Self { + bytes, + offset: 0, + remaining_budget: MAX_WIRE_AGGREGATE_ITEMS, + } + } + + fn read_u32(&mut self) -> Result { + let bytes = self.read_exact(4)?; + Ok(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])) + } + + fn read_exact(&mut self, len: usize) -> Result<&'a [u8], SchemaCodecError> { + let end = self + .offset + .checked_add(len) + .ok_or(SchemaCodecError::UnexpectedEof)?; + if end > self.bytes.len() { + return Err(SchemaCodecError::UnexpectedEof); + } + let slice = &self.bytes[self.offset..end]; + self.offset = end; + Ok(slice) + } + + fn read_blob(&mut self, field: &'static str) -> Result<&'a [u8], SchemaCodecError> { + let len = self.read_u32()? as usize; + if len > MAX_WIRE_BLOB_BYTES { + return Err(SchemaCodecError::LengthTooLarge(field, len)); + } + self.read_exact(len) + } + + fn validate_count_with_overhead( + &self, + field: &'static str, + count: usize, + min_item_bytes: usize, + fixed_bytes: usize, + ) -> Result<(), SchemaCodecError> { + if count > MAX_WIRE_COUNT { + return Err(SchemaCodecError::LengthTooLarge(field, count)); + } + let item_bytes = count + .checked_mul(min_item_bytes) + .ok_or(SchemaCodecError::LengthTooLarge(field, count))?; + let required_bytes = item_bytes + .checked_add(fixed_bytes) + .ok_or(SchemaCodecError::LengthTooLarge(field, count))?; + if required_bytes > self.remaining() { + return Err(SchemaCodecError::LengthTooLarge(field, count)); + } + Ok(()) + } +} + +impl SchemaReader for SchemaCursor<'_> { + fn read_u8(&mut self) -> Result { + let value = self + .bytes + .get(self.offset) + .ok_or(SchemaCodecError::UnexpectedEof)?; + self.offset += 1; + Ok(*value) + } + + fn read_string(&mut self) -> Result { + let bytes = self.read_blob("string")?; + let text = std::str::from_utf8(bytes).map_err(|_| SchemaCodecError::InvalidUtf8)?; + let mut owned = String::new(); + owned + .try_reserve_exact(text.len()) + .map_err(|_| SchemaCodecError::LengthTooLarge("string", text.len()))?; + owned.push_str(text); + Ok(owned) + } + + fn read_count( + &mut self, + field: &'static str, + min_item_bytes: usize, + ) -> Result { + self.read_count_with_overhead(field, min_item_bytes, 0) + } + + fn read_count_with_overhead( + &mut self, + field: &'static str, + min_item_bytes: usize, + fixed_bytes: usize, + ) -> Result { + let count = self.read_u32()? as usize; + self.validate_count_with_overhead(field, count, min_item_bytes, fixed_bytes)?; + self.debit_count(field, count)?; + Ok(count) + } + + fn debit_count(&mut self, field: &'static str, count: usize) -> Result<(), SchemaCodecError> { + if count > MAX_WIRE_COUNT { + return Err(SchemaCodecError::LengthTooLarge(field, count)); + } + self.remaining_budget = self + .remaining_budget + .checked_sub(count) + .ok_or(SchemaCodecError::LengthTooLarge(field, count))?; + Ok(()) + } + + fn remaining(&self) -> usize { + self.bytes.len().saturating_sub(self.offset) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::Value; + + fn roundtrip(schema: &TypeSchema) -> TypeSchema { + let bytes = encode_type_schema_bytes(schema).expect("schema should encode"); + decode_type_schema_bytes(&bytes).expect("schema should decode") + } + + #[test] + fn type_schema_tags_and_sorted_fields_keep_wire_bytes() { + assert_eq!( + encode_type_schema_bytes(&TypeSchema::Unit).unwrap(), + vec![20] + ); + assert_eq!( + encode_type_schema_bytes(&TypeSchema::Option(Box::new(TypeSchema::Unit))).unwrap(), + vec![19, 20] + ); + assert_eq!( + encode_type_schema_bytes(&TypeSchema::Result { + ok: Box::new(TypeSchema::Int), + err: Box::new(TypeSchema::String), + }) + .unwrap(), + vec![18, 2, 6] + ); + assert_eq!( + encode_type_schema_bytes(&TypeSchema::Map(Box::new(TypeSchema::Int))).unwrap(), + vec![13, 2] + ); + let mut fields = HashMap::new(); + fields.insert("b".to_string(), TypeSchema::Bool); + fields.insert("a".to_string(), TypeSchema::Int); + assert_eq!( + encode_type_schema_bytes(&TypeSchema::Object(fields)).unwrap(), + vec![14, 2, 0, 0, 0, 1, 0, 0, 0, b'a', 2, 1, 0, 0, 0, b'b', 5] + ); + assert_eq!( + encode_type_schema_bytes(&TypeSchema::Named("Box".to_string(), vec![TypeSchema::Int])) + .unwrap(), + vec![9, 3, 0, 0, 0, b'B', b'o', b'x', 1, 0, 0, 0, 2] + ); + let key = ResourceTypeKey::new("io.file").unwrap(); + assert_eq!( + encode_type_schema_bytes(&TypeSchema::Resource(key.clone())).unwrap(), + vec![17, 7, 0, 0, 0, b'i', b'o', b'.', b'f', b'i', b'l', b'e'] + ); + assert_eq!( + roundtrip(&TypeSchema::Resource(key)), + TypeSchema::Resource(ResourceTypeKey::new("io.file").unwrap()) + ); + } + + #[test] + fn type_schema_reader_keeps_depth_trailing_and_utf8_limits() { + let mut nested = TypeSchema::Unit; + for _ in 0..63 { + nested = TypeSchema::Option(Box::new(nested)); + } + assert_eq!(roundtrip(&nested), nested); + + let mut too_deep = TypeSchema::Unit; + for _ in 0..64 { + too_deep = TypeSchema::Option(Box::new(too_deep)); + } + let encoded = encode_type_schema_bytes(&too_deep).unwrap(); + let error = decode_type_schema_bytes(&encoded).expect_err("depth 64 must fail"); + assert!( + matches!( + error, + SchemaCodecError::LengthTooLarge("schema nesting depth", 64) + ), + "{error}" + ); + + let trailing = decode_type_schema_bytes(&[20, 0]).expect_err("trailing byte must fail"); + assert_eq!(trailing, SchemaCodecError::TrailingBytes); + let tag = decode_type_schema_bytes(&[99]).expect_err("unknown tag must fail"); + assert_eq!(tag, SchemaCodecError::InvalidValueType(99)); + let utf8 = + decode_type_schema_bytes(&[8, 1, 0, 0, 0, 0xff]).expect_err("invalid utf-8 must fail"); + assert_eq!(utf8, SchemaCodecError::InvalidUtf8); + } + + fn constants_contain(program: &crate::Program, expected: &[u8]) -> bool { + program.constants.iter().any(|value| match value { + Value::Bytes(bytes) => bytes.as_slice() == expected, + _ => false, + }) + } + + #[test] + fn compiler_only_json_and_print_embed_the_same_descriptor() { + let unit = encode_type_schema_bytes(&TypeSchema::Unit).unwrap(); + let option_unit = + encode_type_schema_bytes(&TypeSchema::Option(Box::new(TypeSchema::Unit))).unwrap(); + let encoded = crate::compile_source("use json; json::encode::<()>(());") + .expect("compiler-only json encode should compile"); + assert!( + constants_contain(&encoded.program, &unit), + "json encode descriptor bytes diverged from the pure codec" + ); + let printed = crate::compile_source("print(Option::Some(()));") + .expect("compiler-only presentation should compile"); + assert!( + constants_contain(&printed.program, &option_unit), + "presentation descriptor bytes diverged from the pure codec" + ); + } +} diff --git a/src/compiler/typing.rs b/src/compiler/typing.rs index f4bb3fe2..224092be 100644 --- a/src/compiler/typing.rs +++ b/src/compiler/typing.rs @@ -1,6 +1,7 @@ mod collect; mod context; mod helpers; +mod map_store; mod state; mod validate; @@ -20,6 +21,7 @@ use self::helpers::{ build_function_names, build_host_import_return_types, legalize_function_impl, legalize_stmts, validate_function_impl, validate_stmts, }; +pub(crate) use self::map_store::{MapStoreScope, classify_map_stores, map_admission_source_error}; pub(crate) use self::state::{ BoundType, HostCallableSignature, LocalTypeState, TypeInferenceResult, }; @@ -120,6 +122,7 @@ impl FrontendTypedIr<'_> { fn scalar_schema_from_bound_type(bound_type: BoundType) -> Option { match bound_type { BoundType::Null => Some(TypeSchema::Null), + BoundType::Unit => Some(TypeSchema::Unit), BoundType::Int => Some(TypeSchema::Int), BoundType::Float => Some(TypeSchema::Float), BoundType::Number => Some(TypeSchema::Number), @@ -433,6 +436,124 @@ pub(super) fn infer_types_with_root_projection( (facts, roots) } +/// Checked schema of the last root expression statement, taken before callee +/// slot aggregation. `None` means the snippet has no expression result. An +/// unknown expression stays unknown and is never reported as Unit. +pub(super) fn root_expression_result_schema( + ir: &FrontendIr, + typing_mode: TypingMode, + entry_local_types: &[EntryLocalType], +) -> Option { + let function_names = build_function_names(&ir.functions); + let function_decls = build_function_decl_map(&ir.functions); + let host_import_return_types = + build_host_import_return_types(&ir.functions, &ir.function_impls); + let host_import_signatures = build_host_import_signatures(&ir.functions, &ir.function_impls); + let mut root_state = LocalTypeState::default(); + seed_entry_local_state(&mut root_state, entry_local_types); + let mut root_context = TypeContext::new( + &ir.function_impls, + &function_decls, + &ir.struct_schemas, + &function_names, + &host_import_return_types, + &host_import_signatures, + typing_mode, + ir.parsed_semantic_index.as_ref(), + ); + let entry_slots = entry_local_types + .iter() + .map(|entry| entry.slot) + .collect::>(); + let value = collect::collect_root_expression_result( + &ir.stmts, + &mut root_state, + &entry_slots, + &mut root_context, + )?; + root_context + .infer_expr_schema(&Expr::Var(0), &value) + .or_else(|| scalar_schema_from_bound_type(value.get(0))) + .filter(|schema| !matches!(schema, TypeSchema::Unknown)) +} + +pub(super) fn presentation_site_schemas( + ir: &FrontendIr, + typing_mode: TypingMode, + entry_local_types: &[EntryLocalType], +) -> Vec> { + let function_names = build_function_names(&ir.functions); + let function_decls = build_function_decl_map(&ir.functions); + let host_import_return_types = + build_host_import_return_types(&ir.functions, &ir.function_impls); + let host_import_signatures = build_host_import_signatures(&ir.functions, &ir.function_impls); + let mut state = LocalTypeState::default(); + seed_entry_local_state(&mut state, entry_local_types); + let mut context = TypeContext::new( + &ir.function_impls, + &function_decls, + &ir.struct_schemas, + &function_names, + &host_import_return_types, + &host_import_signatures, + typing_mode, + ir.parsed_semantic_index.as_ref(), + ); + let mut visible = entry_local_types + .iter() + .map(|entry| entry.slot) + .collect::>(); + let mut capture_seeds = HashMap::new(); + collect::walk_presentation_root( + &ir.stmts, + &mut state, + &mut visible, + &mut context, + &mut capture_seeds, + ); + let mut indexes = ir.function_impls.keys().copied().collect::>(); + indexes.sort_unstable(); + let bodies = indexes + .into_iter() + .filter_map(|index| { + let function = ir.function_impls.get(&index)?; + let arg_schemas = function_decls + .get(&index) + .map(|decl| decl.arg_schemas.clone()) + .unwrap_or_default(); + Some(( + function.param_slots.clone(), + arg_schemas, + function.body_stmts.clone(), + function.body_expr.clone(), + capture_seeds.remove(&index), + )) + }) + .collect::>(); + for (param_slots, arg_schemas, body_stmts, body_expr, seed) in bodies { + let mut function_state = LocalTypeState::default(); + collect::seed_function_param_state( + &mut function_state, + ¶m_slots, + Some(arg_schemas.as_slice()), + None, + None, + None, + None, + ); + if let Some(seed) = seed { + function_state.copy_all_bindings_from(&seed); + } + collect::walk_presentation_function( + &body_stmts, + &body_expr, + &mut function_state, + &mut context, + ); + } + context.presentation_sites +} + pub(super) fn infer_types_with_capture_origins( ir: &FrontendIr, typing_mode: TypingMode, @@ -560,13 +681,9 @@ pub(super) fn infer_types_with_capture_origins( callable_slots: &mut callable_slots, optional_slots: &mut optional_slots, }; - origins.extend(collect_function_types( - decl.index, - function_impl, - decl, - &mut outputs, - &env, - )); + let (function_origins, _) = + collect_function_types(decl.index, function_impl, decl, &mut outputs, &env); + origins.extend(function_origins); } let mut facts = TypeInferenceResult { @@ -597,6 +714,82 @@ pub(super) fn infer_types_with_capture_origins( (facts, root_projection, origins) } +fn collect_checked_map_store_decisions( + ir: &FrontendIr, + typing_mode: TypingMode, + entry_local_types: &[EntryLocalType], +) -> HashMap { + let function_names = build_function_names(&ir.functions); + let function_decls = build_function_decl_map(&ir.functions); + let host_import_return_types = + build_host_import_return_types(&ir.functions, &ir.function_impls); + let host_import_signatures = build_host_import_signatures(&ir.functions, &ir.function_impls); + let mut local_types = vec![ValueType::Unknown; ir.locals]; + let mut local_schemas = vec![None; ir.locals]; + let mut local_schema_labels = vec![None; ir.locals]; + let mut callable_slots = vec![false; ir.locals]; + let mut optional_slots = vec![false; ir.locals]; + let mut top_state = LocalTypeState::default(); + seed_entry_local_state(&mut top_state, entry_local_types); + let mut context = TypeContext::new( + &ir.function_impls, + &function_decls, + &ir.struct_schemas, + &function_names, + &host_import_return_types, + &host_import_signatures, + typing_mode, + ir.parsed_semantic_index.as_ref(), + ); + for stmt in &ir.stmts { + collect_stmt_types( + std::slice::from_ref(stmt), + &mut top_state, + &mut local_types, + &mut local_schemas, + &mut local_schema_labels, + &mut callable_slots, + &mut optional_slots, + &mut context, + ); + } + let mut decisions = context.map_store_decisions; + let observed_function_param_types = context.observed_function_param_types; + let observed_function_param_schemas = context.observed_function_param_schemas; + let observed_function_param_callables = context.observed_function_param_callables; + let observed_function_param_capture_states = context.observed_function_param_capture_states; + let observed_function_capture_states = context.observed_function_capture_states; + let env = CollectFunctionTypesEnv { + function_impls: &ir.function_impls, + function_decls: &function_decls, + function_names: &function_names, + struct_schemas: &ir.struct_schemas, + host_import_return_types: &host_import_return_types, + host_import_signatures: &host_import_signatures, + observed_function_param_types: &observed_function_param_types, + observed_function_param_schemas: &observed_function_param_schemas, + observed_function_param_callables: &observed_function_param_callables, + observed_function_param_capture_states: &observed_function_param_capture_states, + observed_function_capture_states: &observed_function_capture_states, + }; + for decl in &ir.functions { + let Some(function_impl) = ir.function_impls.get(&decl.index) else { + continue; + }; + let mut outputs = CollectFunctionTypeOutputs { + local_types: &mut local_types, + local_schemas: &mut local_schemas, + local_schema_labels: &mut local_schema_labels, + callable_slots: &mut callable_slots, + optional_slots: &mut optional_slots, + }; + let (_, function_decisions) = + collect_function_types(decl.index, function_impl, decl, &mut outputs, &env); + decisions.extend(function_decisions); + } + decisions +} + pub(super) fn validate_if_else_type_consistency( ir: &FrontendIr, typing_mode: TypingMode, @@ -1052,6 +1245,34 @@ fn inferred_value_schema( .filter(|schema| !matches!(schema, TypeSchema::Unknown)) } +/// Construction-admission check for producer-side method lowering. The same +/// expression-aware walker used by declaration/argument validation decides +/// whether an omitted logical `Option` field can be materialized at this +/// result position. It is not a schema rewrite and does not infer a new type. +pub(crate) fn omitted_option_materialization_detail_for_slots( + functions: &[FunctionDecl], + function_impls: &HashMap, + struct_schemas: &HashMap, + expr: &Expr, + expected: &TypeSchema, + slots: &HashMap, +) -> Option { + let state = result_local_state(slots); + let typing = ResultTyping::new(functions, function_impls, struct_schemas); + let mut context = typing.context(); + let actual = context + .infer_expr_schema(expr, &state) + .or_else(|| scalar_schema_from_bound_type(infer_expr_type(expr, &state))) + .filter(|schema| !matches!(schema, TypeSchema::Unknown)); + helpers::omitted_option_materialization_detail( + expr, + expected, + actual.as_ref(), + &mut context, + &state, + ) +} + /// Infer and check a Result payload with the typing owner's schema rules. /// Numeric compatibility applies only at this selected-payload position. /// `Unknown` and unbound generic parameters are not success. @@ -1157,6 +1378,40 @@ pub(crate) fn infer_value_schema( inferred_value_schema(&mut typing, expr, slots) } +pub(crate) fn object_fields_for_schema( + functions: &[FunctionDecl], + function_impls: &HashMap, + struct_schemas: &HashMap, + schema: &TypeSchema, +) -> Option> { + match schema { + TypeSchema::Object(fields) => Some(fields.clone()), + TypeSchema::Named(name, type_args) => { + let typing = ResultTyping::new(functions, function_impls, struct_schemas); + let mut context = typing.context(); + context::resolved_named_struct_fields(name, type_args, &mut context) + } + _ => None, + } +} + +/// Resolve one field/index read against a concrete container schema using the +/// same access rules as the typing owner. Used by the Option resolver to +/// classify a logical projection leaf (for example a required Unit field). +pub(crate) fn infer_access_value_schema( + functions: &[FunctionDecl], + function_impls: &HashMap, + struct_schemas: &HashMap, + container_schema: &TypeSchema, + key: &Expr, + slots: &HashMap, +) -> Option { + let typing = ResultTyping::new(functions, function_impls, struct_schemas); + let state = result_local_state(slots); + let mut context = typing.context(); + context::infer_access_schema(container_schema, key, &mut context, &state).ok() +} + pub(crate) fn argument_schemas_for_call( functions: &[FunctionDecl], function_impls: &HashMap, @@ -1185,8 +1440,9 @@ pub(crate) fn expected_container_argument_schemas( match builtin { crate::builtins::BuiltinFunction::ArrayPush if args.len() == 2 => { expected.array_prefix_and_rest()?; + let index = array_push_literal_index(&args[0]).unwrap_or(0); let element = expected - .array_item_schema_at(0) + .array_item_schema_at(index) .or_else(|| expected.collapsed_array_item_schema()) .filter(|schema| !matches!(schema, TypeSchema::Unknown)); Some(vec![Some(expected.clone()), element]) @@ -1201,6 +1457,33 @@ pub(crate) fn expected_container_argument_schemas( ); Some(vec![Some(expected.clone()), None, value]) } + crate::builtins::BuiltinFunction::MapEdit if args.len() == 4 => { + if matches!(args.get(3), Some(Expr::Bool(true))) { + Some(vec![Some(expected.clone()), None, None, None]) + } else { + let value = expected_named_or_container_field_schema( + functions, + function_impls, + struct_schemas, + expected, + &args[1], + ); + Some(vec![Some(expected.clone()), None, value, None]) + } + } + _ => None, + } +} + +fn array_push_literal_index(container: &Expr) -> Option { + let Expr::Call(index, _, args, _, _) = container else { + return None; + }; + match crate::builtins::BuiltinFunction::from_call_index(*index)? { + crate::builtins::BuiltinFunction::ArrayNew if args.is_empty() => Some(0), + crate::builtins::BuiltinFunction::ArrayPush if args.len() == 2 => { + Some(array_push_literal_index(&args[0])? + 1) + } _ => None, } } @@ -1308,7 +1591,8 @@ mod catalog_call_resolution_tests { false, ), ] { - let ir = parse(source, catalog(vec![], vec![])); + let ir = super::super::frontends::legacy_rss::parse(source) + .expect("legacy nullable fixture must parse"); let slot = ir .local_bindings .iter() @@ -1353,10 +1637,10 @@ mod catalog_call_resolution_tests { #[test] fn root_result_join_unknown_snapshot_is_not_null_or_a_reused_slot() { for reverse in [false, true] { - let mut ir = parse( + let mut ir = super::super::frontends::legacy_rss::parse( "let maybe: float? = 2.5; let y = if true => { maybe } else => { null };", - catalog(vec![], vec![]), - ); + ) + .expect("legacy nullable fixture must parse"); let Stmt::Let { index, expr, .. } = &mut ir.stmts[1] else { panic!("result binding") }; @@ -1788,13 +2072,14 @@ mod catalog_call_resolution_tests { vec![HostParamSchema::value("path", HostTypeSchema::String)], resource.clone(), ), - HostFunctionSchema::new( + HostFunctionSchema::with_return( "acme::consume", vec![HostParamSchema::with_passing( "file", resource, HostParamPassing::TakeOwned, )], + HostTypeSchema::Unit, ), ], ); diff --git a/src/compiler/typing/collect.rs b/src/compiler/typing/collect.rs index fe73f6fa..e307159e 100644 --- a/src/compiler/typing/collect.rs +++ b/src/compiler/typing/collect.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use crate::bytecode::ValueType; @@ -23,7 +23,17 @@ pub(super) fn collect_root_stmt_state( context: &mut TypeContext<'_>, ) { let mut visible = entry_slots.iter().copied().collect(); - collect_root_stmts(stmts, state, &mut visible, context); + let _ = collect_root_stmts(stmts, state, &mut visible, context); +} + +pub(super) fn collect_root_expression_result( + stmts: &[Stmt], + state: &mut LocalTypeState, + entry_slots: &[LocalSlot], + context: &mut TypeContext<'_>, +) -> Option { + let mut visible = entry_slots.iter().copied().collect(); + collect_root_stmts(stmts, state, &mut visible, context) } fn commit_root_scope( @@ -41,7 +51,8 @@ fn collect_root_stmts( state: &mut LocalTypeState, visible: &mut std::collections::HashSet, context: &mut TypeContext<'_>, -) { +) -> Option { + let mut last_expression = None; for stmt in stmts { match stmt { Stmt::Let { @@ -59,7 +70,7 @@ fn collect_root_stmts( bind_root_value(state, *index, None, &value, context); } Stmt::Expr { expr, .. } => { - collect_root_expr(expr, state, visible, context); + last_expression = Some(collect_root_expr(expr, state, visible, context)); } Stmt::Drop { index, .. } => state.set(*index, BoundType::Null), Stmt::IfElse { @@ -132,6 +143,7 @@ fn collect_root_stmts( | Stmt::Continue { .. } => {} } } + last_expression } // Expression values occupy slot zero in a standalone facts state. Operand @@ -348,6 +360,13 @@ fn collect_root_expr( operand(receiver, false); operand(argument, false); } + Expr::OptionMethod { + receiver, argument, .. + } => { + operand(receiver, false); + operand(argument, false); + } + Expr::ResultConvert { value, .. } => operand(value, false), Expr::And(lhs, rhs) | Expr::Or(lhs, rhs) => { operand(lhs, false); operand(rhs, true); @@ -377,6 +396,7 @@ fn collect_root_expr( } } Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -403,6 +423,21 @@ fn collect_root_expr( operand(ok_expr, true); operand(err_expr, true); } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + operand(payload, false); + } + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + operand(value, false); + operand(some_expr, true); + operand(none_expr, true); + } Expr::IfElse { .. } | Expr::Match { .. } | Expr::Block { .. } => unreachable!(), } root_expr_value(&inferred_expr, &operand_state, context) @@ -548,7 +583,10 @@ pub(super) fn collect_function_types( function_decl: &FunctionDecl, outputs: &mut CollectFunctionTypeOutputs<'_>, env: &CollectFunctionTypesEnv<'_>, -) -> super::super::capture_certification::DeclarationCaptures { +) -> ( + super::super::capture_certification::DeclarationCaptures, + HashMap, +) { let mut state = LocalTypeState::default(); let mut context = TypeContext::new( env.function_impls, @@ -639,7 +677,7 @@ pub(super) fn collect_function_types( &mut context, ); let _ = context.infer_expr_type(&function_impl.body_expr, &state); - context.capture_origins + (context.capture_origins, context.map_store_decisions) } #[allow(clippy::too_many_arguments)] @@ -983,6 +1021,7 @@ fn collect_expr_types( ) { match expr { Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -1096,6 +1135,7 @@ fn collect_expr_types( context, ); } + super::map_store::note_checked_map_store(expr, state, context); let _ = context.infer_expr_type(expr, state); } Expr::Closure(closure) => { @@ -1230,6 +1270,44 @@ fn collect_expr_types( ); let _ = context.infer_expr_type(expr, state); } + Expr::OptionMethod { + receiver, argument, .. + } => { + collect_expr_types( + receiver, + state, + local_types, + local_schemas, + local_schema_labels, + callable_slots, + optional_slots, + context, + ); + collect_expr_types( + argument, + state, + local_types, + local_schemas, + local_schema_labels, + callable_slots, + optional_slots, + context, + ); + let _ = context.infer_expr_type(expr, state); + } + Expr::ResultConvert { value, .. } => { + collect_expr_types( + value, + state, + local_types, + local_schemas, + local_schema_labels, + callable_slots, + optional_slots, + context, + ); + let _ = context.infer_expr_type(expr, state); + } Expr::ResultConstruct { payload, .. } => { collect_expr_types( payload, @@ -1243,6 +1321,21 @@ fn collect_expr_types( ); let _ = context.infer_expr_type(expr, state); } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + collect_expr_types( + payload, + state, + local_types, + local_schemas, + local_schema_labels, + callable_slots, + optional_slots, + context, + ); + } + let _ = context.infer_expr_type(expr, state); + } Expr::ResultQuery { value, .. } => { collect_expr_types( value, @@ -1361,6 +1454,81 @@ fn collect_expr_types( ); let _ = context.infer_expr_type(expr, state); } + Expr::OptionMatch { + value, + value_slot, + some_binding, + some_expr, + none_expr, + scrutinee_schema, + .. + } => { + collect_expr_types( + value, + state, + local_types, + local_schemas, + local_schema_labels, + callable_slots, + optional_slots, + context, + ); + let value_ty = context.infer_expr_type(value, state); + let mut nested = state.clone(); + bind_expr_result_to_slot( + &mut nested, + *value_slot, + None, + value, + state, + value_ty, + context, + ); + record_local_type(local_types, *value_slot, nested.get(*value_slot)); + record_local_schema(local_schemas, *value_slot, nested.schema(*value_slot)); + record_local_schema_label(local_schema_labels, *value_slot, nested.schema(*value_slot)); + let inner = match scrutinee_schema { + TypeSchema::Option(inner) => inner.as_ref().clone(), + _ => TypeSchema::Unknown, + }; + let mut some_state = nested.clone(); + if let Some(slot) = *some_binding { + some_state.set_with_optional_schema_origin( + slot, + context.bound_type_for_schema(&inner), + Some(inner.clone()), + true, + matches!(inner, TypeSchema::Optional(_)), + ); + record_local_type(local_types, slot, some_state.get(slot)); + record_local_schema(local_schemas, slot, some_state.schema(slot)); + record_local_schema_label(local_schema_labels, slot, some_state.schema(slot)); + if some_state.is_optional(slot) { + record_optional_slot(optional_slots, slot); + } + } + collect_expr_types( + some_expr, + &some_state, + local_types, + local_schemas, + local_schema_labels, + callable_slots, + optional_slots, + context, + ); + collect_expr_types( + none_expr, + &nested, + local_types, + local_schemas, + local_schema_labels, + callable_slots, + optional_slots, + context, + ); + let _ = context.infer_expr_type(expr, state); + } Expr::IfElse { condition, then_expr, @@ -1631,3 +1799,312 @@ pub(super) fn record_local_schema_label( _ => None, }; } + +pub(super) fn walk_presentation_root( + stmts: &[Stmt], + state: &mut LocalTypeState, + visible: &mut HashSet, + context: &mut TypeContext<'_>, + capture_seeds: &mut HashMap, +) { + presentation_walk_stmts(stmts, state, visible, context, capture_seeds); +} + +pub(super) fn walk_presentation_function( + body_stmts: &[Stmt], + body_expr: &Expr, + state: &mut LocalTypeState, + context: &mut TypeContext<'_>, +) { + let mut visible = HashSet::new(); + let mut seeds = HashMap::new(); + presentation_walk_stmts(body_stmts, state, &mut visible, context, &mut seeds); + presentation_walk_expr(body_expr, state, &mut visible, context, &mut seeds); +} + +fn presentation_walk_stmts( + stmts: &[Stmt], + state: &mut LocalTypeState, + visible: &mut HashSet, + context: &mut TypeContext<'_>, + capture_seeds: &mut HashMap, +) { + for stmt in stmts { + presentation_walk_stmt(stmt, state, visible, context, capture_seeds); + } +} + +fn presentation_walk_stmt( + stmt: &Stmt, + state: &mut LocalTypeState, + visible: &mut HashSet, + context: &mut TypeContext<'_>, + capture_seeds: &mut HashMap, +) { + match stmt { + Stmt::Let { + index, + declared_schema, + expr, + .. + } => { + presentation_walk_expr(expr, state, visible, context, capture_seeds); + let value = root_expr_value(expr, state, context); + bind_root_value(state, *index, declared_schema.as_ref(), &value, context); + visible.insert(*index); + } + Stmt::Assign { index, expr, .. } => { + presentation_walk_expr(expr, state, visible, context, capture_seeds); + let value = root_expr_value(expr, state, context); + bind_root_value(state, *index, None, &value, context); + } + Stmt::Expr { expr, .. } => { + presentation_walk_expr(expr, state, visible, context, capture_seeds); + let _ = collect_root_expr(expr, state, visible, context); + } + Stmt::IfElse { + condition, + then_branch, + else_branch, + .. + } => { + presentation_walk_expr(condition, state, visible, context, capture_seeds); + let mut then_state = refine_state_for_condition(state, condition, true); + let mut else_state = refine_state_for_condition(state, condition, false); + presentation_walk_stmts( + then_branch, + &mut then_state, + &mut visible.clone(), + context, + capture_seeds, + ); + presentation_walk_stmts( + else_branch, + &mut else_state, + &mut visible.clone(), + context, + capture_seeds, + ); + let mut joined = LocalTypeState::default(); + joined.merge_from_branches(&then_state, &else_state); + commit_root_scope(state, &joined, visible); + } + Stmt::While { + condition, body, .. + } => { + presentation_walk_expr(condition, state, visible, context, capture_seeds); + let mut nested = state.clone(); + presentation_walk_stmts( + body, + &mut nested, + &mut visible.clone(), + context, + capture_seeds, + ); + commit_root_scope(state, &nested, visible); + } + Stmt::For { + init, + condition, + post, + body, + .. + } => { + presentation_walk_stmt(init, state, visible, context, capture_seeds); + presentation_walk_expr(condition, state, visible, context, capture_seeds); + presentation_walk_stmt(post, state, visible, context, capture_seeds); + presentation_walk_stmts(body, state, visible, context, capture_seeds); + } + Stmt::ClosureLet { closure, .. } => { + presentation_walk_closure(closure, state, context, capture_seeds); + } + Stmt::FuncDecl { index, .. } => { + if let Some(function) = context.function_impls.get(index) { + let mut seed = LocalTypeState::default(); + for (source, capture) in &function.capture_copies { + seed.copy_exact_binding_from(state, *source, *capture); + } + capture_seeds.insert(*index, seed); + } + } + Stmt::Drop { index, .. } => state.set(*index, BoundType::Null), + Stmt::Noop { .. } | Stmt::Break { .. } | Stmt::Continue { .. } => {} + } +} + +fn presentation_walk_expr( + expr: &Expr, + state: &mut LocalTypeState, + visible: &mut HashSet, + context: &mut TypeContext<'_>, + capture_seeds: &mut HashMap, +) { + if let Expr::Closure(closure) = expr { + presentation_walk_closure(closure, state, context, capture_seeds); + return; + } + if let Expr::Block { stmts, expr } = expr { + let mut nested = state.clone(); + let mut scope = visible.clone(); + presentation_walk_stmts(stmts, &mut nested, &mut scope, context, capture_seeds); + presentation_walk_expr(expr, &mut nested, &mut scope, context, capture_seeds); + commit_root_scope(state, &nested, visible); + return; + } + if let Expr::LocalCall(slot, _, args, _) = expr + && args.len() == 1 + && language_print_alias(state, *slot, context) + { + let schema = presentation_schema(&args[0], state, context); + context.presentation_sites.push(schema); + presentation_walk_expr(&args[0], state, visible, context, capture_seeds); + return; + } + if let Some(values) = super::super::logical_boundary::presentation_inputs( + context.function_impls, + context.function_names, + expr, + ) { + let mut noted = Vec::with_capacity(values.len()); + for value in &values { + noted.push(presentation_schema(value, state, context)); + } + context.presentation_sites.extend(noted); + for value in values { + presentation_walk_expr(value, state, visible, context, capture_seeds); + } + return; + } + match expr { + Expr::IfElse { + condition, + then_expr, + else_expr, + } => { + presentation_walk_expr(condition, state, visible, context, capture_seeds); + presentation_walk_expr(then_expr, state, visible, context, capture_seeds); + presentation_walk_expr(else_expr, state, visible, context, capture_seeds); + } + Expr::Call(_, _, args, _, _) + | Expr::LocalCall(_, _, args, _) + | Expr::ModuleCall(_, _, args, _) + | Expr::ClosureCall(_, args) => { + for arg in args { + presentation_walk_expr(arg, state, visible, context, capture_seeds); + } + } + Expr::Add(lhs, rhs) + | Expr::Sub(lhs, rhs) + | Expr::Mul(lhs, rhs) + | Expr::Div(lhs, rhs) + | Expr::Mod(lhs, rhs) + | Expr::Eq(lhs, rhs) + | Expr::Lt(lhs, rhs) + | Expr::Gt(lhs, rhs) + | Expr::And(lhs, rhs) + | Expr::Or(lhs, rhs) => { + presentation_walk_expr(lhs, state, visible, context, capture_seeds); + presentation_walk_expr(rhs, state, visible, context, capture_seeds); + } + Expr::Neg(inner) + | Expr::Not(inner) + | Expr::ToOwned(inner) + | Expr::Borrow(inner) + | Expr::BorrowMut(inner) => { + presentation_walk_expr(inner, state, visible, context, capture_seeds); + } + Expr::OptionConstruct { + payload: Some(payload), + .. + } => { + presentation_walk_expr(payload, state, visible, context, capture_seeds); + } + Expr::ResultConstruct { payload, .. } + | Expr::ResultQuery { value: payload, .. } + | Expr::ResultTry { value: payload, .. } => { + presentation_walk_expr(payload, state, visible, context, capture_seeds); + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + presentation_walk_expr(value, state, visible, context, capture_seeds); + presentation_walk_expr(some_expr, state, visible, context, capture_seeds); + presentation_walk_expr(none_expr, state, visible, context, capture_seeds); + } + _ => {} + } +} + +fn presentation_walk_closure( + closure: &ClosureExpr, + outer: &LocalTypeState, + context: &mut TypeContext<'_>, + capture_seeds: &mut HashMap, +) { + let mut inner = LocalTypeState::default(); + for (source, capture) in &closure.capture_copies { + inner.copy_exact_binding_from(outer, *source, *capture); + } + let mut visible = HashSet::new(); + presentation_walk_expr( + &closure.body, + &mut inner, + &mut visible, + context, + capture_seeds, + ); +} + +fn language_print_alias( + state: &LocalTypeState, + slot: LocalSlot, + context: &TypeContext<'_>, +) -> bool { + match state.callable(slot) { + Some(InferredCallable::Function(index, _)) => { + super::super::logical_boundary::is_language_print( + context.function_impls, + context.function_names, + *index, + ) + } + _ => false, + } +} + +fn presentation_schema( + expr: &Expr, + state: &LocalTypeState, + context: &mut TypeContext<'_>, +) -> Option { + let schema = context + .infer_expr_schema(expr, state) + .or_else(|| constructor_presentation_schema(expr)); + schema.filter(super::super::logical_boundary::contains_logical_display) +} + +fn constructor_presentation_schema(expr: &Expr) -> Option { + match expr { + Expr::Unit => Some(TypeSchema::Unit), + Expr::OptionConstruct { + option_schema, + some, + .. + } => { + if matches!(option_schema, TypeSchema::Option(_)) { + return Some(option_schema.clone()); + } + if !*some { + return Some(TypeSchema::Option(Box::new(TypeSchema::Unknown))); + } + None + } + Expr::ResultConstruct { result_schema, .. } => { + matches!(result_schema, TypeSchema::Result { .. }).then(|| result_schema.clone()) + } + _ => None, + } +} diff --git a/src/compiler/typing/context.rs b/src/compiler/typing/context.rs index 27cffd3f..80b9ea27 100644 --- a/src/compiler/typing/context.rs +++ b/src/compiler/typing/context.rs @@ -5,7 +5,8 @@ use crate::builtins::BuiltinFunction; use super::super::CompileError; use super::super::TypingMode; use super::super::ir::{ - ClosureExpr, Expr, FunctionDecl, FunctionImpl, LocalSlot, Stmt, StructDecl, TypeSchema, + ClosureExpr, Expr, FunctionDecl, FunctionImpl, LocalSlot, SemanticNodeId, Stmt, StructDecl, + TypeSchema, }; use super::super::source_map::Span; use super::helpers::{ @@ -51,21 +52,16 @@ use super::validate::{ /// instead of restarting the growth - the failed resolution closes the /// expansion rather than reopening it. /// -/// 32 re-entries of one declaration is far beyond any practical JSON -/// payload depth (each level is one more container wrap of the type -/// argument), and the walk cost grows quadratically with the budget -/// (every level re-resolves its own increasingly wrapped arguments), so -/// the bound also keeps the compile-time walk fast and shallow enough -/// for the constrained-stack regression probes. -const MAX_NAMED_SCHEMA_REENTRY: usize = 32; +const MAX_NAMED_SCHEMA_REENTRY: usize = 2; /// True when `schema` mentions the generic parameter `name` anywhere. -/// Used to break self-referential bindings (`T` bound to `[T]`): such a -/// binding can only arise from an unbound parameter root, and expanding -/// it would loop through containers forever without ever re-entering a -/// named declaration (so the named re-entry budget would never trip), so -/// the parameter is left unresolved instead - the honest marker for a -/// circular binding. +/// Used to break self-referential bindings (`T` bound to `[T]` or +/// `Option`): such a binding can only arise from an unbound parameter +/// root, and expanding it would loop through containers forever without +/// ever re-entering a named declaration (so the named re-entry budget +/// would never trip), so the parameter is left unresolved instead - the +/// honest marker for a circular binding. `Option` is a container here: +/// omitting it made `T` bound through `Option` expand forever. fn schema_mentions_generic_param(schema: &TypeSchema, name: &str) -> bool { match schema { TypeSchema::GenericParam(other) => other == name, @@ -83,7 +79,9 @@ fn schema_mentions_generic_param(schema: &TypeSchema, name: &str) -> bool { || schema_mentions_generic_param(rest, name) } TypeSchema::Map(value) => schema_mentions_generic_param(value, name), - TypeSchema::Optional(inner) => schema_mentions_generic_param(inner, name), + TypeSchema::Optional(inner) | TypeSchema::Option(inner) => { + schema_mentions_generic_param(inner, name) + } TypeSchema::Result { ok, err } => { schema_mentions_generic_param(ok, name) || schema_mentions_generic_param(err, name) } @@ -100,6 +98,17 @@ fn schema_mentions_generic_param(schema: &TypeSchema, name: &str) -> bool { } } +/// Store action recorded at a checked call, keyed later by semantic id. +/// This is the decision only. It is not a source-position schema. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum MapStoreAction { + RewriteInsert, + NormalizeSet, + RejectCarrier, + RejectUnresolved, + RejectRemove, +} + pub(super) struct TypeContext<'a> { pub(super) function_impls: &'a HashMap, pub(super) function_decls: &'a HashMap, @@ -133,6 +142,11 @@ pub(super) struct TypeContext<'a> { /// A catalog rejection already owns this exact parser-proven call site. /// Skip only its flat legacy signature check, never child/body validation. rejected_host_calls: Vec<(Span, Span)>, + /// Schemas of presentation sites in source visit order. Empty unless a + /// presentation walk is recording. Not a second solver. + pub(super) presentation_sites: Vec>, + /// Set-versus-MapEdit decisions taken from the checked state at each call. + pub(super) map_store_decisions: HashMap, } struct CallableBody<'a> { @@ -181,6 +195,8 @@ impl<'a> TypeContext<'a> { parsed, result_payload_strict: false, rejected_host_calls: Vec::new(), + presentation_sites: Vec::new(), + map_store_decisions: HashMap::new(), } } @@ -344,6 +360,9 @@ impl<'a> TypeContext<'a> { TypeSchema::Optional(inner) => { TypeSchema::Optional(Box::new(self.substitute_schema_generics(inner))) } + TypeSchema::Option(inner) => { + TypeSchema::Option(Box::new(self.substitute_schema_generics(inner))) + } TypeSchema::Object(fields) => TypeSchema::Object( fields .iter() @@ -424,6 +443,23 @@ impl<'a> TypeContext<'a> { } } TypeSchema::Named(name, type_args) => { + let reentry_count = reentries.get(name.as_str()).copied().unwrap_or(0); + if reentry_count >= MAX_NAMED_SCHEMA_REENTRY { + // Stop before walking arguments. Container-wrapped + // recursion (`Node` -> `Node<[T]>`) makes each + // argument one container deeper, and walking that nest + // before the budget check keeps every ancestor frame + // live. The 512 KiB json-encode probes abort on that + // nest in debug builds. The marker keeps the unresolved + // arguments; a raw parameter cannot restart growth + // because `schema_mentions_generic_param` refuses a + // binding that mentions itself, including through Option. + let key = schema_instance_key(name, type_args, &vec![None; type_args.len()]); + return ( + TypeSchema::Named(name.clone(), type_args.clone()), + Some(key), + ); + } let mut resolved_args = Vec::with_capacity(type_args.len()); let mut arg_trips = Vec::with_capacity(type_args.len()); for arg in type_args { @@ -534,6 +570,11 @@ impl<'a> TypeContext<'a> { self.resolve_schema_with_seen_tripped(inner, seen, reentries); (TypeSchema::Optional(Box::new(resolved)), trip) } + TypeSchema::Option(inner) => { + let (resolved, trip) = + self.resolve_schema_with_seen_tripped(inner, seen, reentries); + (TypeSchema::Option(Box::new(resolved)), trip) + } TypeSchema::Result { ok, err } => { let (resolved_ok, ok_trip) = self.resolve_schema_with_seen_tripped(ok, seen, reentries); @@ -556,7 +597,7 @@ impl<'a> TypeContext<'a> { // contributes it must be deterministic: visit fields in // sorted name order. let mut sorted_fields: Vec<(&String, &TypeSchema)> = fields.iter().collect(); - sorted_fields.sort_by(|(a, _), (b, _)| a.cmp(b)); + sorted_fields.sort_by_key(|(name, _)| *name); for (name, value) in sorted_fields { let (resolved_value, trip) = self.resolve_schema_with_seen_tripped(value, seen, reentries); @@ -640,6 +681,7 @@ impl<'a> TypeContext<'a> { } match schema { TypeSchema::Result { ok, .. } => Some(*ok), + TypeSchema::Option(inner) => Some(*inner), _ => None, } } @@ -680,6 +722,7 @@ impl<'a> TypeContext<'a> { Expr::Call(index, _, args, _, _) => match BuiltinFunction::from_call_index(*index) { Some(BuiltinFunction::Get) | Some(BuiltinFunction::Set) + | Some(BuiltinFunction::MapEdit) | Some(BuiltinFunction::Slice) | Some(BuiltinFunction::Keys) | Some(BuiltinFunction::Len) => args @@ -753,6 +796,7 @@ impl<'a> TypeContext<'a> { Expr::Call(index, _, args, _, _) => match BuiltinFunction::from_call_index(*index) { Some(BuiltinFunction::Get) | Some(BuiltinFunction::Set) + | Some(BuiltinFunction::MapEdit) | Some(BuiltinFunction::Slice) | Some(BuiltinFunction::Keys) | Some(BuiltinFunction::Len) => args @@ -807,19 +851,19 @@ impl<'a> TypeContext<'a> { match expr { Expr::Var(slot) | Expr::MoveVar(slot) => state.is_optional(*slot), Expr::MoveField { root, .. } | Expr::MoveIndex { root, .. } => state.is_optional(*root), - Expr::OptionalGet { .. } => true, + Expr::OptionalGet { + logical_projection, .. + } => !*logical_projection, Expr::OptionUnwrapOr { .. } => false, Expr::Call(index, _, _, resolution, _) => { resolution .as_ref() .is_some_and(|resolved| resolved.return_type.is_optional()) - || BuiltinFunction::from_call_index(*index) == Some(BuiltinFunction::ReFind) || self.function_returns_optional(*index) } Expr::LocalCall(slot, _, _, _) => match state.callable(*slot) { Some(InferredCallable::Function(index, _)) => { - BuiltinFunction::from_call_index(*index) == Some(BuiltinFunction::ReFind) - || self.function_returns_optional(*index) + self.function_returns_optional(*index) } _ => false, }, @@ -949,11 +993,30 @@ impl<'a> TypeContext<'a> { ) -> Option { match expr { Expr::Var(slot) | Expr::MoveVar(slot) => { - state.schema(*slot).map(TypeSchema::clone_inner_if_optional) + state.schema(*slot).map(|schema| match schema { + TypeSchema::Option(inner) => inner.as_ref().clone(), + other => other.clone_inner_if_optional(), + }) + } + Expr::OptionalGet { + logical_projection, + container, + key, + .. + } => { + let container_schema = + self.infer_expr_schema(container, state) + .map(|schema| match schema { + TypeSchema::Option(inner) | TypeSchema::Optional(inner) + if *logical_projection => + { + *inner + } + other => other, + }); + container_schema + .and_then(|schema| infer_access_schema(&schema, key, self, state).ok()) } - Expr::OptionalGet { container, key, .. } => self - .infer_expr_schema(container, state) - .and_then(|schema| infer_access_schema(&schema, key, self, state).ok()), Expr::OptionUnwrapOr { value, fallback, .. } => { @@ -991,6 +1054,10 @@ impl<'a> TypeContext<'a> { } else { self.infer_expr_schema(container, state) }; + let container_schema = container_schema.map(|schema| match schema { + TypeSchema::Option(inner) | TypeSchema::Optional(inner) => *inner, + other => other, + }); if let Some(schema) = container_schema.and_then(|schema| infer_access_schema(&schema, key, self, state).ok()) { @@ -1238,14 +1305,35 @@ impl<'a> TypeContext<'a> { ) -> Option { match expr { Expr::Null => Some(TypeSchema::Null), + Expr::Unit => Some(TypeSchema::Unit), Expr::Int(_) => Some(TypeSchema::Int), Expr::Float(_) => Some(TypeSchema::Float), Expr::Bool(_) => Some(TypeSchema::Bool), Expr::Bytes(_) => Some(TypeSchema::Bytes), Expr::String(_) => Some(TypeSchema::String), - Expr::OptionalGet { container, key, .. } => self - .infer_expr_schema(container, state) - .and_then(|schema| infer_access_schema(&schema, key, self, state).ok()), + Expr::OptionalGet { + logical_projection, + container, + key, + .. + } => { + let container_schema = self.infer_expr_schema(container, state); + let access_schema = container_schema + .map(|schema| match schema { + TypeSchema::Option(inner) | TypeSchema::Optional(inner) + if *logical_projection => + { + *inner + } + other => other, + }) + .and_then(|schema| infer_access_schema(&schema, key, self, state).ok()); + if *logical_projection { + access_schema.map(|schema| TypeSchema::Option(Box::new(schema))) + } else { + access_schema + } + } Expr::OptionUnwrapOr { value, fallback, .. } => { @@ -1469,6 +1557,59 @@ impl<'a> TypeContext<'a> { _ => None, } } + Expr::OptionConstruct { + option_schema, + some, + payload, + .. + } => match option_schema { + TypeSchema::Unknown => { + let payload = payload.as_deref()?; + let actual = self.infer_expr_schema(payload, state).or_else(|| { + super::scalar_schema_from_bound_type(self.infer_expr_type(payload, state)) + })?; + if matches!(actual, TypeSchema::Unknown | TypeSchema::GenericParam(_)) + || schema_contains_unknown_schema(&actual) + { + return None; + } + // `Some` can infer its payload; `None` never invents a + // missing type argument without surrounding context. + (*some).then(|| TypeSchema::Option(Box::new(actual))) + } + other => Some(self.substitute_schema_generics(other)), + }, + Expr::OptionMatch { + value, + some_binding, + some_expr, + none_expr, + scrutinee_schema, + .. + } => { + let inner = match scrutinee_schema { + TypeSchema::Option(inner) => inner.as_ref().clone(), + _ => match self.infer_expr_schema(value, state) { + Some(TypeSchema::Option(inner)) => inner.as_ref().clone(), + _ => TypeSchema::Unknown, + }, + }; + let mut some_state = state.clone(); + if let Some(slot) = *some_binding { + some_state.set_with_schema_origin( + slot, + self.bound_type_for_schema(&inner), + Some(inner), + true, + ); + } + let some_schema = self.infer_expr_schema(some_expr, &some_state); + let none_schema = self.infer_expr_schema(none_expr, state); + match (some_schema, none_schema) { + (Some(lhs), Some(rhs)) if lhs == rhs => Some(lhs), + _ => None, + } + } _ => None, } } @@ -1476,6 +1617,7 @@ impl<'a> TypeContext<'a> { pub(super) fn infer_expr_type(&mut self, expr: &Expr, state: &LocalTypeState) -> BoundType { match expr { Expr::Null => BoundType::Null, + Expr::Unit => BoundType::Unit, Expr::Int(_) => BoundType::Int, Expr::Float(_) => BoundType::Float, Expr::Bool(_) => BoundType::Bool, @@ -1605,12 +1747,42 @@ impl<'a> TypeContext<'a> { BoundType::Bool } Expr::ResultMethod { + method, + receiver, + argument, + .. + } => { + let _ = self.infer_expr_type(receiver, state); + let _ = self.infer_expr_type(argument, state); + if method.retains_option_lowering_on_non_result() { + // `.unwrap_or` on a nullable chain keeps the pre-existing + // Option lowering. Inference must publish the unwrapped + // payload type before the Result resolution pass runs, or + // dynamic editor hints lose the concrete local type. + let value_ty = self.infer_optional_expr_inner_type(receiver, state); + let fallback_ty = self.infer_expr_type(argument, state); + if value_ty == BoundType::Unknown { + fallback_ty + } else if fallback_ty == BoundType::Unknown || fallback_ty == value_ty { + value_ty + } else { + merge_bound_types(value_ty, fallback_ty) + } + } else { + BoundType::Unknown + } + } + Expr::OptionMethod { receiver, argument, .. } => { let _ = self.infer_expr_type(receiver, state); let _ = self.infer_expr_type(argument, state); BoundType::Unknown } + Expr::ResultConvert { value, .. } => { + let _ = self.infer_expr_type(value, state); + BoundType::Map + } Expr::ResultTry { value, operand_schema, @@ -1663,6 +1835,43 @@ impl<'a> TypeContext<'a> { BoundType::Unknown } } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + let _ = self.infer_expr_type(payload, state); + } + BoundType::Map + } + Expr::OptionMatch { + value, + some_binding, + some_expr, + none_expr, + scrutinee_schema, + .. + } => { + let _ = self.infer_expr_type(value, state); + let inner = match scrutinee_schema { + TypeSchema::Option(inner) => inner.as_ref().clone(), + _ => TypeSchema::Unknown, + }; + let mut some_state = state.clone(); + if let Some(slot) = *some_binding { + some_state.set_with_optional_schema_origin( + slot, + self.bound_type_for_schema(&inner), + Some(inner), + true, + false, + ); + } + let some_ty = self.infer_expr_type(some_expr, &some_state); + let none_ty = self.infer_expr_type(none_expr, state); + if some_ty == none_ty { + some_ty + } else { + BoundType::Unknown + } + } Expr::Block { stmts, expr } => { let mut nested = state.clone(); self.apply_stmts(stmts, &mut nested); @@ -1772,10 +1981,19 @@ impl<'a> TypeContext<'a> { BuiltinFunction::Set if args.len() == 3 => { self.infer_set_return_type(&args[0], &args[2], state) } + BuiltinFunction::MapEdit if args.len() == 4 => { + if matches!(args.get(3), Some(Expr::Bool(true))) { + self.infer_expr_type(&args[0], state) + } else { + self.infer_set_return_type(&args[0], &args[2], state) + } + } BuiltinFunction::Get if args.len() == 2 => { self.infer_get_return_type(&args[0], &args[1], state) } - BuiltinFunction::ReFind => BoundType::String, + BuiltinFunction::ReFind => { + self.bound_type_for_schema(&TypeSchema::Option(Box::new(TypeSchema::String))) + } BuiltinFunction::Keys if args.len() == 1 => { self.infer_keys_return_type(&args[0], state) } @@ -1830,10 +2048,25 @@ impl<'a> TypeContext<'a> { .or_else(|| schema_from_bound_type(self.infer_expr_type(&args[2], state))); infer_set_schema(container_schema, &args[1], value_schema) } + BuiltinFunction::MapEdit if args.len() == 4 => { + if matches!(args.get(3), Some(Expr::Bool(true))) { + self.infer_expr_schema(&args[0], state) + } else { + let container_schema = self.infer_expr_schema(&args[0], state); + let value_schema = self + .infer_expr_schema(&args[2], state) + .or_else(|| schema_from_bound_type(self.infer_expr_type(&args[2], state))); + infer_set_schema(container_schema, &args[1], value_schema) + } + } BuiltinFunction::Get if args.len() == 2 => self .infer_expr_schema(&args[0], state) .and_then(|schema| infer_access_schema(&schema, &args[1], self, state).ok()), - BuiltinFunction::ReFind => Some(TypeSchema::String), + BuiltinFunction::ReFind => Some(TypeSchema::Option(Box::new(TypeSchema::String))), + BuiltinFunction::ReSplit => Some(TypeSchema::Array(Box::new(TypeSchema::String))), + BuiltinFunction::ReCaptures => Some(TypeSchema::Array(Box::new(TypeSchema::Option( + Box::new(TypeSchema::String), + )))), BuiltinFunction::Slice if args.len() == 3 => { match self.infer_expr_schema(&args[0], state) { Some(schema) if schema.array_prefix_and_rest().is_some() => schema @@ -2525,11 +2758,14 @@ impl<'a> TypeContext<'a> { if let Some(builtin) = BuiltinFunction::from_call_index(*index) { self.validate_builtin_argument_types( builtin, + type_args, args, state, - line_context, - source_name, - expr_span, + DiagnosticSite { + line: line_context, + source_name, + span: expr_span, + }, ) } else if let Some(signature) = self.host_import_signatures.get(index).cloned() { self.validate_host_argument_types( @@ -2572,11 +2808,14 @@ impl<'a> TypeContext<'a> { if let Some(builtin) = BuiltinFunction::from_call_index(index) { self.validate_builtin_argument_types( builtin, + type_args, args, state, - line_context, - source_name, - expr_span, + DiagnosticSite { + line: line_context, + source_name, + span: expr_span, + }, ) } else if let Some(signature) = self.host_import_signatures.get(&index).cloned() { @@ -2645,24 +2884,14 @@ impl<'a> TypeContext<'a> { pub(super) fn validate_builtin_argument_types( &mut self, builtin: BuiltinFunction, + type_args: &[crate::compiler::ir::TypeSchema], args: &[Expr], state: &LocalTypeState, - line_context: Option, - source_name: Option<&str>, - span: Option, + site: DiagnosticSite<'_>, ) -> Result<(), CompileError> { - if builtin == BuiltinFunction::JsonEncode { + if builtin == BuiltinFunction::JsonEncode && type_args.is_empty() { let arg = args.first().expect("json::encode arity is fixed"); - return validate_json_encode_argument( - arg, - state, - self, - DiagnosticSite { - line: line_context, - source_name, - span, - }, - ); + return validate_json_encode_argument(arg, state, self, site); } validate_signature_overloads( &display_name_for_builtin(builtin), @@ -2671,11 +2900,7 @@ impl<'a> TypeContext<'a> { args, state, self, - super::validate::DiagnosticSite { - line: line_context, - source_name, - span, - }, + site, ) } @@ -2970,6 +3195,7 @@ fn callable_param_schema(param: crate::builtins::CallableParamType) -> crate::co match param { CallableParamType::Any => TypeSchema::Unknown, CallableParamType::Null => TypeSchema::Null, + CallableParamType::Unit => TypeSchema::Unit, CallableParamType::Int => TypeSchema::Int, CallableParamType::Float => TypeSchema::Float, CallableParamType::Number => TypeSchema::Number, @@ -3040,6 +3266,7 @@ fn schema_from_bound_type(ty: BoundType) -> Option { match ty { BoundType::Unknown => None, BoundType::Null => Some(TypeSchema::Null), + BoundType::Unit => Some(TypeSchema::Unit), BoundType::Int => Some(TypeSchema::Int), BoundType::Float => Some(TypeSchema::Float), BoundType::Number => Some(TypeSchema::Number), @@ -3063,7 +3290,10 @@ fn logical_optional_schema(schema: TypeSchema, optional: bool) -> TypeSchema { if optional && !matches!( schema, - TypeSchema::Optional(_) | TypeSchema::Unknown | TypeSchema::Null + TypeSchema::Optional(_) + | TypeSchema::Option(_) + | TypeSchema::Unknown + | TypeSchema::Null ) { TypeSchema::Optional(Box::new(schema)) @@ -3072,6 +3302,41 @@ fn logical_optional_schema(schema: TypeSchema, optional: bool) -> TypeSchema { } } +fn schema_contains_unknown_schema(schema: &TypeSchema) -> bool { + match schema { + TypeSchema::Unknown => true, + TypeSchema::Optional(inner) + | TypeSchema::Option(inner) + | TypeSchema::Array(inner) + | TypeSchema::Map(inner) => schema_contains_unknown_schema(inner), + TypeSchema::Named(_, args) | TypeSchema::ArrayTuple(args) => { + args.iter().any(schema_contains_unknown_schema) + } + TypeSchema::ArrayTupleRest { prefix, rest } => { + prefix.iter().any(schema_contains_unknown_schema) + || schema_contains_unknown_schema(rest) + } + TypeSchema::Object(fields) => fields.values().any(schema_contains_unknown_schema), + TypeSchema::Callable { params, result } => { + params.iter().any(schema_contains_unknown_schema) + || schema_contains_unknown_schema(result) + } + TypeSchema::Result { ok, err } => { + schema_contains_unknown_schema(ok) || schema_contains_unknown_schema(err) + } + TypeSchema::Null + | TypeSchema::Unit + | TypeSchema::Int + | TypeSchema::Float + | TypeSchema::Number + | TypeSchema::Bool + | TypeSchema::String + | TypeSchema::Bytes + | TypeSchema::Resource(_) + | TypeSchema::GenericParam(_) => false, + } +} + fn observed_return_key(index: u16, type_args: &[TypeSchema]) -> (u16, Vec) { ( index, @@ -3086,6 +3351,7 @@ pub(crate) fn bound_type_from_schema(schema: &TypeSchema) -> BoundType { match schema { TypeSchema::Unknown => BoundType::Unknown, TypeSchema::Null => BoundType::Null, + TypeSchema::Unit => BoundType::Unit, TypeSchema::Int => BoundType::Int, TypeSchema::Float => BoundType::Float, TypeSchema::Number => BoundType::Number, @@ -3099,7 +3365,10 @@ pub(crate) fn bound_type_from_schema(schema: &TypeSchema) -> BoundType { TypeSchema::Array(_) | TypeSchema::ArrayTuple(_) | TypeSchema::ArrayTupleRest { .. } => { BoundType::Array } - TypeSchema::Map(_) | TypeSchema::Object(_) | TypeSchema::Result { .. } => BoundType::Map, + TypeSchema::Map(_) + | TypeSchema::Option(_) + | TypeSchema::Object(_) + | TypeSchema::Result { .. } => BoundType::Map, // Resources are opaque (nominal) values: they are never reduced to the // `Map` bound or to an integral token in semantic inference. TypeSchema::Resource(_) => BoundType::Unknown, @@ -3299,6 +3568,32 @@ pub(super) fn infer_access_schema( } } +pub(super) fn resolved_named_struct_fields( + struct_name: &str, + type_args: &[TypeSchema], + context: &mut TypeContext<'_>, +) -> Option> { + let declaration = context.struct_schemas.get(struct_name)?.clone(); + if declaration.type_params.len() != type_args.len() { + return None; + } + let TypeSchema::Object(fields) = declaration.body_schema else { + return None; + }; + let bound = !declaration.type_params.is_empty(); + if bound { + context.push_generic_bindings(&declaration.type_params, type_args); + } + let resolved = fields + .iter() + .map(|(name, schema)| (name.clone(), context.substitute_schema_generics(schema))) + .collect(); + if bound { + context.pop_generic_bindings(); + } + Some(resolved) +} + pub(super) fn source_named_struct_field_schema( struct_name: &str, type_args: &[TypeSchema], @@ -3340,6 +3635,7 @@ pub(super) fn schema_label(schema: &TypeSchema) -> &'static str { match schema { TypeSchema::Unknown => "unknown", TypeSchema::Null => "null", + TypeSchema::Unit => "()", TypeSchema::Int => "int", TypeSchema::Float => "float", TypeSchema::Number => "number", @@ -3353,7 +3649,10 @@ pub(super) fn schema_label(schema: &TypeSchema) -> &'static str { TypeSchema::Array(_) | TypeSchema::ArrayTuple(_) | TypeSchema::ArrayTupleRest { .. } => { "array" } - TypeSchema::Map(_) | TypeSchema::Object(_) | TypeSchema::Result { .. } => "map", + TypeSchema::Map(_) + | TypeSchema::Option(_) + | TypeSchema::Object(_) + | TypeSchema::Result { .. } => "map", TypeSchema::Resource(_) => "resource", } } @@ -3371,6 +3670,7 @@ pub(crate) fn render_schema_label(schema: &TypeSchema) -> String { match schema { TypeSchema::Unknown => "unknown".to_string(), TypeSchema::Null => "null".to_string(), + TypeSchema::Unit => "()".to_string(), TypeSchema::Int => "int".to_string(), TypeSchema::Float => "float".to_string(), TypeSchema::Number => "number".to_string(), @@ -3378,6 +3678,7 @@ pub(crate) fn render_schema_label(schema: &TypeSchema) -> String { TypeSchema::String => "string".to_string(), TypeSchema::Bytes => "bytes".to_string(), TypeSchema::Optional(inner) => format!("{}?", render_schema_label(inner)), + TypeSchema::Option(inner) => format!("Option<{}>", render_schema_label(inner)), TypeSchema::GenericParam(name) => name.clone(), TypeSchema::Callable { params, result } => format!( "fn({}) -> {}", @@ -3460,7 +3761,9 @@ fn builtin_generic_return_schema( type_args: &[TypeSchema], ) -> Option { match builtin { - BuiltinFunction::JsonDecode if type_args.len() == 1 => Some(type_args[0].clone()), + BuiltinFunction::JsonDecode | BuiltinFunction::JsonDecodeTyped if type_args.len() == 1 => { + Some(type_args[0].clone()) + } _ => None, } } diff --git a/src/compiler/typing/helpers.rs b/src/compiler/typing/helpers.rs index e73c5998..dd9e2f39 100644 --- a/src/compiler/typing/helpers.rs +++ b/src/compiler/typing/helpers.rs @@ -17,7 +17,7 @@ use super::collect::{ observed_function_param_schema_slice, observed_function_param_slice, seed_function_capture_state, seed_function_param_state, }; -use super::context::{TypeContext, render_schema_label}; +use super::context::{TypeContext, bound_type_from_schema, render_schema_label}; use super::infer_expr_type; use super::state::{ BoundType, HostCallableSignature, LocalTypeState, merge_bound_types, @@ -308,9 +308,10 @@ fn actual_passing(arg: &Expr, schema: &TypeSchema) -> Option { fn schema_contains_unresolved(schema: &TypeSchema) -> bool { match schema { TypeSchema::Unknown | TypeSchema::GenericParam(_) => true, - TypeSchema::Optional(inner) | TypeSchema::Array(inner) | TypeSchema::Map(inner) => { - schema_contains_unresolved(inner) - } + TypeSchema::Optional(inner) + | TypeSchema::Option(inner) + | TypeSchema::Array(inner) + | TypeSchema::Map(inner) => schema_contains_unresolved(inner), TypeSchema::Named(_, args) | TypeSchema::ArrayTuple(args) => { args.iter().any(schema_contains_unresolved) } @@ -325,6 +326,7 @@ fn schema_contains_unresolved(schema: &TypeSchema) -> bool { schema_contains_unresolved(ok) || schema_contains_unresolved(err) } TypeSchema::Null + | TypeSchema::Unit | TypeSchema::Int | TypeSchema::Float | TypeSchema::Number @@ -335,7 +337,7 @@ fn schema_contains_unresolved(schema: &TypeSchema) -> bool { } } -fn stmt_line(stmt: &Stmt) -> u32 { +pub(super) fn stmt_line(stmt: &Stmt) -> u32 { match stmt { Stmt::Noop { line } | Stmt::Let { line, .. } @@ -529,6 +531,8 @@ pub(super) fn validate_function_impl( observed_body_ty, actual_optional, actual_schema.as_ref(), + &function_impl.body_expr, + &state, context, Some(function_impl.body_expr_line), source_name, @@ -792,9 +796,13 @@ pub(super) fn validate_stmts( let actual_schema = inferred_expr_assignment_schema(expr, &expr_state, context); validate_declared_local_schema( declared_schema.as_ref(), - ty, - context.expr_is_optional(expr, &expr_state), - actual_schema.as_ref(), + DeclaredLocalAssignment { + ty, + optional: context.expr_is_optional(expr, &expr_state), + schema: actual_schema.as_ref(), + expr, + state: &expr_state, + }, context, Some(*line), source_name, @@ -860,9 +868,13 @@ pub(super) fn validate_stmts( let actual_schema = inferred_expr_assignment_schema(expr, &expr_state, context); validate_declared_local_schema( declared_schema.as_ref(), - ty, - context.expr_is_optional(expr, &expr_state), - actual_schema.as_ref(), + DeclaredLocalAssignment { + ty, + optional: context.expr_is_optional(expr, &expr_state), + schema: actual_schema.as_ref(), + expr, + state: &expr_state, + }, context, Some(*line), source_name, @@ -1014,11 +1026,17 @@ pub(super) fn validate_stmts( Ok(()) } +struct DeclaredLocalAssignment<'a> { + ty: BoundType, + optional: bool, + schema: Option<&'a TypeSchema>, + expr: &'a Expr, + state: &'a LocalTypeState, +} + fn validate_declared_local_schema( schema: Option<&TypeSchema>, - actual: BoundType, - actual_optional: bool, - actual_schema: Option<&TypeSchema>, + assignment: DeclaredLocalAssignment<'_>, context: &mut TypeContext<'_>, line: Option, source_name: Option<&str>, @@ -1029,7 +1047,7 @@ fn validate_declared_local_schema( let resolved = context.resolve_schema(schema); let (expected_schema, expected_optional) = resolved.split_optional(); let expected = context.bound_type_for_schema(&expected_schema); - if actual_optional && !expected_optional { + if assignment.optional && !expected_optional { return Err(CompileError::InvalidFieldAccess { line, source_name: owned_source_name(source_name), @@ -1040,7 +1058,7 @@ fn validate_declared_local_schema( span: context.stmt_span(line.unwrap_or_default()), }); } - if actual == BoundType::Null && !expected_optional && expected != BoundType::Null { + if assignment.ty == BoundType::Null && !expected_optional && expected != BoundType::Null { return Err(CompileError::InvalidFieldAccess { line, source_name: owned_source_name(source_name), @@ -1051,14 +1069,14 @@ fn validate_declared_local_schema( span: context.stmt_span(line.unwrap_or_default()), }); } - if actual == BoundType::Unknown - || (expected_optional && actual == BoundType::Null) - || actual == expected - || (expected == BoundType::Number && is_numeric_bound_type(actual)) - || (expected == BoundType::Array && matches!(actual, BoundType::ArrayOf(_))) - || (expected == BoundType::Map && matches!(actual, BoundType::MapOf(_))) + if assignment.ty == BoundType::Unknown + || (expected_optional && assignment.ty == BoundType::Null) + || assignment.ty == expected + || (expected == BoundType::Number && is_numeric_bound_type(assignment.ty)) + || (expected == BoundType::Array && matches!(assignment.ty, BoundType::ArrayOf(_))) + || (expected == BoundType::Map && matches!(assignment.ty, BoundType::MapOf(_))) { - if let Some(actual_schema) = actual_schema + if let Some(actual_schema) = assignment.schema && let Some(detail) = find_declared_schema_mismatch( &expected_schema, actual_schema, @@ -1073,6 +1091,22 @@ fn validate_declared_local_schema( span: context.stmt_span(line.unwrap_or_default()), }); } + if let Some(actual_schema) = assignment.schema + && let Some(detail) = omitted_option_materialization_detail( + assignment.expr, + &expected_schema, + Some(actual_schema), + context, + assignment.state, + ) + { + return Err(CompileError::InvalidFieldAccess { + line, + source_name: owned_source_name(source_name), + detail, + span: context.stmt_span(line.unwrap_or_default()), + }); + } return Ok(()); } Err(CompileError::InvalidFieldAccess { @@ -1081,7 +1115,7 @@ fn validate_declared_local_schema( detail: format!( "local is declared as schema type '{}' but was assigned {}", schema_type_label(schema), - bound_type_label(actual) + bound_type_label(assignment.ty) ), span: context.stmt_span(line.unwrap_or_default()), }) @@ -1094,6 +1128,8 @@ fn validate_declared_return_schema( actual: BoundType, actual_optional: bool, actual_schema: Option<&TypeSchema>, + expr: &Expr, + expr_state: &LocalTypeState, context: &mut TypeContext<'_>, line: Option, source_name: Option<&str>, @@ -1145,6 +1181,22 @@ fn validate_declared_return_schema( span: context.stmt_span(line.unwrap_or_default()), }); } + if let Some(actual_schema) = actual_schema + && let Some(detail) = omitted_option_materialization_detail( + expr, + &expected_schema, + Some(actual_schema), + context, + expr_state, + ) + { + return Err(CompileError::StrictTypingRequired { + line, + source_name: owned_source_name(source_name), + detail: format!("function '{function_name}' return type mismatch: {detail}"), + span: context.stmt_span(line.unwrap_or_default()), + }); + } return Ok(()); } Err(CompileError::StrictTypingRequired { @@ -1236,6 +1288,378 @@ pub(super) fn find_declared_schema_mismatch( ) } +/// Reject a logical `Option` field omission when the expression at the +/// requiring position is not a construction the compiler-owned normalizer +/// can materialize. The schema mismatch owner deliberately admits a missing +/// `Option` field so that map literals can be completed before this check. +/// A plain variable, call, parameter or other non-construction value carries +/// no such construction, so admitting it would install a named record label +/// without the tagged `None` entry. +pub(super) fn omitted_option_materialization_detail( + expr: &Expr, + expected: &TypeSchema, + actual: Option<&TypeSchema>, + context: &mut TypeContext<'_>, + state: &LocalTypeState, +) -> Option { + omission_admission_detail(expr, expected, actual, context, state, "") +} + +fn omission_admission_detail( + expr: &Expr, + expected: &TypeSchema, + actual: Option<&TypeSchema>, + context: &mut TypeContext<'_>, + state: &LocalTypeState, + path: &str, +) -> Option { + let expected = context.substitute_schema_generics(expected); + if matches!(expected, TypeSchema::Named(_, _)) { + let resolved = context.resolve_schema(&expected); + return omission_admission_detail(expr, &resolved, actual, context, state, path); + } + match &expected { + // Legacy nullable `Optional` fields keep their pre-existing admission + // rule until the later schema transition. + TypeSchema::Optional(_) => None, + TypeSchema::Object(fields) => { + record_omission_admission_detail(expr, fields, &expected, actual, context, state, path) + } + TypeSchema::Option(inner) => { + if let Expr::OptionConstruct { + payload: Some(payload), + .. + } = expr + { + let actual_payload = inferred_expr_assignment_schema(payload, state, context); + return omission_admission_detail( + payload, + inner, + actual_payload.as_ref(), + context, + state, + path, + ); + } + let actual_inner = actual.and_then(|schema| match context.resolve_schema(schema) { + TypeSchema::Option(inner) => Some(*inner), + _ => None, + }); + actual_inner.and_then(|actual_inner| { + omission_admission_detail(expr, inner, Some(&actual_inner), context, state, path) + }) + } + TypeSchema::Result { ok, err } => { + if let Expr::ResultConstruct { + ok: constructed_ok, + payload, + .. + } = expr + { + let branch = if *constructed_ok { ok } else { err }; + let actual_payload = inferred_expr_assignment_schema(payload, state, context); + return omission_admission_detail( + payload, + branch, + actual_payload.as_ref(), + context, + state, + path, + ); + } + let branches = actual.and_then(|schema| match context.resolve_schema(schema) { + TypeSchema::Result { ok, err } => Some((*ok, *err)), + _ => None, + }); + branches.and_then(|(actual_ok, actual_err)| { + omission_admission_detail(expr, ok, Some(&actual_ok), context, state, path).or_else( + || { + omission_admission_detail( + expr, + err, + Some(&actual_err), + context, + state, + path, + ) + }, + ) + }) + } + TypeSchema::Array(inner) => { + let mut values = Vec::new(); + if array_literal_values(expr, &mut values) { + for value in values { + let actual_value = inferred_expr_assignment_schema(value, state, context); + if let Some(detail) = omission_admission_detail( + value, + inner, + actual_value.as_ref(), + context, + state, + path, + ) { + return Some(detail); + } + } + return None; + } + let actual_inner = actual.and_then(|schema| match context.resolve_schema(schema) { + TypeSchema::Array(inner) => Some(*inner), + _ => None, + }); + actual_inner.and_then(|actual_inner| { + omission_admission_detail(expr, inner, Some(&actual_inner), context, state, path) + }) + } + TypeSchema::Callable { result, .. } => { + let expected_callable = expected.clone(); + match expr { + Expr::Closure(closure) => { + let actual_result = + actual.and_then(|schema| callable_result_schema(schema, context)); + omission_admission_detail( + &closure.body, + result, + actual_result.as_ref(), + context, + state, + path, + ) + } + Expr::IfElse { + then_expr, + else_expr, + .. + } => { + for branch in [then_expr.as_ref(), else_expr.as_ref()] { + let branch_actual = inferred_expr_assignment_schema(branch, state, context); + if let Some(detail) = omission_admission_detail( + branch, + &expected_callable, + branch_actual.as_ref(), + context, + state, + path, + ) { + return Some(detail); + } + } + None + } + Expr::Match { arms, default, .. } => { + for (_, arm) in arms { + let branch_actual = inferred_expr_assignment_schema(arm, state, context); + if let Some(detail) = omission_admission_detail( + arm, + &expected_callable, + branch_actual.as_ref(), + context, + state, + path, + ) { + return Some(detail); + } + } + let branch_actual = inferred_expr_assignment_schema(default, state, context); + omission_admission_detail( + default, + &expected_callable, + branch_actual.as_ref(), + context, + state, + path, + ) + } + Expr::Block { expr: tail, .. } => omission_admission_detail( + tail, + &expected_callable, + actual, + context, + state, + path, + ), + _ => { + let actual_result = + actual.and_then(|schema| callable_result_schema(schema, context)); + omission_admission_detail( + expr, + result, + actual_result.as_ref(), + context, + state, + path, + ) + } + } + } + _ => None, + } +} + +fn record_omission_admission_detail( + expr: &Expr, + fields: &HashMap, + expected: &TypeSchema, + actual: Option<&TypeSchema>, + context: &mut TypeContext<'_>, + state: &LocalTypeState, + path: &str, +) -> Option { + let mut entries = Vec::new(); + if map_literal_entries(expr, &mut entries) { + let mut seen = HashSet::new(); + for (name, value) in entries.iter().rev() { + if !seen.insert(*name) { + continue; + } + let Some(expected_field) = fields.get(*name) else { + continue; + }; + let field_path = extend_schema_path(path, name); + let actual_value = inferred_expr_assignment_schema(value, state, context); + if let Some(detail) = omission_admission_detail( + value, + expected_field, + actual_value.as_ref(), + context, + state, + &field_path, + ) { + return Some(detail); + } + } + return None; + } + if let Expr::IfElse { + then_expr, + else_expr, + .. + } = expr + { + for branch in [then_expr.as_ref(), else_expr.as_ref()] { + let branch_actual = inferred_expr_assignment_schema(branch, state, context); + if let Some(detail) = omission_admission_detail( + branch, + expected, + branch_actual.as_ref(), + context, + state, + path, + ) { + return Some(detail); + } + } + return None; + } + if let Expr::Match { arms, default, .. } = expr { + for (_, arm) in arms { + let branch_actual = inferred_expr_assignment_schema(arm, state, context); + if let Some(detail) = omission_admission_detail( + arm, + expected, + branch_actual.as_ref(), + context, + state, + path, + ) { + return Some(detail); + } + } + let branch_actual = inferred_expr_assignment_schema(default, state, context); + return omission_admission_detail( + default, + expected, + branch_actual.as_ref(), + context, + state, + path, + ); + } + if let Expr::Block { stmts, expr: tail } = expr { + let mut nested = state.clone(); + context.apply_stmts(stmts, &mut nested); + let tail_actual = inferred_expr_assignment_schema(tail, &nested, context); + return omission_admission_detail( + tail, + expected, + tail_actual.as_ref(), + context, + &nested, + path, + ); + } + let actual = context.resolve_schema(actual?); + let TypeSchema::Object(actual_fields) = &actual else { + return None; + }; + for (name, expected_field) in fields { + let expected_field = context.substitute_schema_generics(expected_field); + if matches!(expected_field, TypeSchema::Option(_)) && !actual_fields.contains_key(name) { + let field_path = extend_schema_path(path, name); + return Some(format!( + "field '{}' is missing from a non-construction value: an omitted logical Option field must be materialized at a map literal construction with the declared schema", + field_path + )); + } + } + None +} + +fn map_literal_entries<'a>(expr: &'a Expr, entries: &mut Vec<(&'a str, &'a Expr)>) -> bool { + match expr { + Expr::Call(index, _, args, _, _) + if BuiltinFunction::from_call_index(*index) == Some(BuiltinFunction::MapNew) => + { + args.is_empty() + } + Expr::Call(index, _, args, _, _) + if matches!( + BuiltinFunction::from_call_index(*index), + Some(BuiltinFunction::Set) + ) && args.len() == 3 + || matches!( + BuiltinFunction::from_call_index(*index), + Some(BuiltinFunction::MapEdit) + ) && args.len() == 4 => + { + if let Expr::String(key) = &args[1] { + entries.push((key.as_str(), &args[2])); + } + map_literal_entries(&args[0], entries) + } + _ => false, + } +} + +fn array_literal_values<'a>(expr: &'a Expr, values: &mut Vec<&'a Expr>) -> bool { + match expr { + Expr::Call(index, _, args, _, _) + if BuiltinFunction::from_call_index(*index) == Some(BuiltinFunction::ArrayNew) => + { + args.is_empty() + } + Expr::Call(index, _, args, _, _) + if BuiltinFunction::from_call_index(*index) == Some(BuiltinFunction::ArrayPush) + && args.len() == 2 => + { + values.push(&args[1]); + array_literal_values(&args[0], values) + } + _ => false, + } +} + +fn callable_result_schema( + schema: &TypeSchema, + context: &mut TypeContext<'_>, +) -> Option { + match context.resolve_schema(schema) { + TypeSchema::Callable { result, .. } => Some(*result), + _ => None, + } +} + fn find_declared_schema_mismatch_with_recursion( expected: &TypeSchema, actual: &TypeSchema, @@ -1278,6 +1702,21 @@ fn find_declared_schema_mismatch_with_recursion( } let instance = render_schema_path_segment(name, type_args); let is_recursive = !recursive_named.insert(instance.clone()); + // Instance keys include the type arguments, so container-wrapped + // recursion (`Node` then `Node<[int]>`) never repeats a key + // and would resolve a deeper argument nest on every descent. + // The declaration name is the stable identity for that family: + // the outer instantiation already walks the struct body, and a + // second sight of the same name is the recursion edge. Expanding + // it again overflows the 512 KiB json-encode probes. + let decl_key = format!("\u{0}{name}"); + let decl_seen = !recursive_named.insert(decl_key.clone()); + if decl_seen { + if !is_recursive { + recursive_named.remove(&instance); + } + return None; + } let resolved = context.resolve_schema(expected); let mismatch = find_declared_schema_mismatch_with_recursion( &resolved, @@ -1290,6 +1729,7 @@ fn find_declared_schema_mismatch_with_recursion( if !is_recursive { recursive_named.remove(&instance); } + recursive_named.remove(&decl_key); return mismatch; } _ => context.substitute_schema_generics(expected), @@ -1314,6 +1754,7 @@ fn find_declared_schema_mismatch_with_recursion( } (_, TypeSchema::Unknown | TypeSchema::Null) | (TypeSchema::Unknown, _) => None, (TypeSchema::Int, TypeSchema::Int) + | (TypeSchema::Unit, TypeSchema::Unit) | (TypeSchema::Number, TypeSchema::Int) | (TypeSchema::Float, TypeSchema::Float) | (TypeSchema::Number, TypeSchema::Float) @@ -1435,7 +1876,10 @@ fn find_declared_schema_mismatch_with_recursion( for (name, expected_field) in expected_fields { let field_path = extend_schema_path(&path, name); let Some(actual_field) = actual_fields.get(name) else { - if matches!(expected_field, TypeSchema::Optional(_)) { + if matches!( + expected_field, + TypeSchema::Optional(_) | TypeSchema::Option(_) + ) { continue; } return Some(format!( @@ -1500,6 +1944,18 @@ fn find_declared_schema_mismatch_with_recursion( )) } } + (TypeSchema::Option(expected), TypeSchema::Option(actual)) => { + let expected = context.substitute_schema_generics(expected); + let actual = context.substitute_schema_generics(actual); + find_declared_schema_mismatch_with_recursion( + &expected, + &actual, + context, + path, + recursive_named, + allow_partial_object, + ) + } _ => Some(format!( "{} is declared as schema type '{}' but was assigned {}", schema_path_label(&path), @@ -1707,10 +2163,18 @@ pub(super) fn refine_state_for_match_pattern( ) -> LocalTypeState { let mut refined = state.clone(); if let Some(binding_slot) = pattern.binding_slot() { + // Option::Some binds the payload, not the Option carrier. The carrier's + // bound type is Map; projecting only that type makes `v + 1` untyped. + let (ty, schema) = match (pattern, state.schema(value_slot)) { + (MatchPattern::SomeBinding(_), Some(TypeSchema::Option(inner))) => { + (bound_type_from_schema(inner), Some(inner.as_ref().clone())) + } + _ => (state.get(value_slot), state.schema(value_slot).cloned()), + }; refined.set_with_optional_schema_origin( binding_slot, - state.get(value_slot), - state.schema(value_slot).cloned(), + ty, + schema, state.has_declared_schema(value_slot), false, ); @@ -1894,6 +2358,7 @@ pub(super) fn expr_contains_param_add(expr: &Expr, param_slots: &[LocalSlot]) -> || expr_contains_param_add(rhs, param_slots) } Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -1973,6 +2438,7 @@ pub(super) fn expr_contains_param_add(expr: &Expr, param_slots: &[LocalSlot]) -> } other @ (Expr::ResultConstruct { .. } | Expr::ResultQuery { .. } + | Expr::ResultConvert { .. } | Expr::ResultMatch { .. } | Expr::ResultTry { .. }) => { let mut found = false; @@ -1981,6 +2447,15 @@ pub(super) fn expr_contains_param_add(expr: &Expr, param_slots: &[LocalSlot]) -> }); found } + other @ (Expr::OptionConstruct { .. } + | Expr::OptionMatch { .. } + | Expr::OptionMethod { .. }) => { + let mut found = false; + other.visit_option_children(|child| { + found |= expr_contains_param_add(child, param_slots) + }); + found + } Expr::Block { stmts, expr } => function_body_contains_param_add(param_slots, stmts, expr), } } @@ -1989,6 +2464,7 @@ pub(super) fn expr_uses_param(expr: &Expr, param_slots: &[LocalSlot]) -> bool { match expr { Expr::Var(slot) | Expr::MoveVar(slot) => param_slots.contains(slot), Expr::Null + | Expr::Unit | Expr::Int(_) | Expr::Float(_) | Expr::Bool(_) @@ -2008,6 +2484,10 @@ pub(super) fn expr_uses_param(expr: &Expr, param_slots: &[LocalSlot]) -> bool { Expr::ResultMethod { receiver, argument, .. } => expr_uses_param(receiver, param_slots) || expr_uses_param(argument, param_slots), + Expr::OptionMethod { + receiver, argument, .. + } => expr_uses_param(receiver, param_slots) || expr_uses_param(argument, param_slots), + Expr::ResultConvert { value, .. } => expr_uses_param(value, param_slots), Expr::Call(_, _, args, _, _) | Expr::LocalCall(_, _, args, _) | Expr::ModuleCall(_, _, args, _) => { @@ -2064,6 +2544,11 @@ pub(super) fn expr_uses_param(expr: &Expr, param_slots: &[LocalSlot]) -> bool { other.visit_result_children(|child| found |= expr_uses_param(child, param_slots)); found } + other @ (Expr::OptionConstruct { .. } | Expr::OptionMatch { .. }) => { + let mut found = false; + other.visit_option_children(|child| found |= expr_uses_param(child, param_slots)); + found + } Expr::Block { stmts, expr } => { stmts.iter().any(|stmt| stmt_uses_param(stmt, param_slots)) || expr_uses_param(expr, param_slots) @@ -2153,6 +2638,7 @@ pub(super) fn legalize_expr( ) -> BoundType { match expr { Expr::Null => BoundType::Null, + Expr::Unit => BoundType::Unit, Expr::Int(_) => BoundType::Int, Expr::Float(_) => BoundType::Float, Expr::Bool(_) => BoundType::Bool, @@ -2170,6 +2656,17 @@ pub(super) fn legalize_expr( let _ = legalize_expr(fallback, state, context, site, host_resolution); context.infer_expr_type(expr, state) } + Expr::OptionMethod { + receiver, argument, .. + } => { + let _ = legalize_expr(receiver, state, context, site, host_resolution); + let _ = legalize_expr(argument, state, context, site, host_resolution); + BoundType::Unknown + } + Expr::ResultConvert { value, .. } => { + let _ = legalize_expr(value, state, context, site, host_resolution); + BoundType::Map + } Expr::FunctionRef(..) | Expr::ModuleFunctionRef(..) | Expr::UnresolvedFunctionRef { .. } @@ -2328,6 +2825,43 @@ pub(super) fn legalize_expr( BoundType::Unknown } } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + let _ = legalize_expr(payload, state, context, site, host_resolution); + } + BoundType::Map + } + Expr::OptionMatch { + value, + some_binding, + some_expr, + none_expr, + scrutinee_schema, + .. + } => { + let _ = legalize_expr(value, state, context, site, host_resolution); + let inner = match scrutinee_schema { + TypeSchema::Option(inner) => inner.as_ref().clone(), + _ => TypeSchema::Unknown, + }; + let mut some_state = state.clone(); + if let Some(slot) = *some_binding { + some_state.set_with_optional_schema_origin( + slot, + context.bound_type_for_schema(&inner), + Some(inner.clone()), + true, + matches!(inner, TypeSchema::Optional(_)), + ); + } + let some_ty = legalize_expr(some_expr, &some_state, context, site, host_resolution); + let none_ty = legalize_expr(none_expr, state, context, site, host_resolution); + if some_ty == none_ty { + some_ty + } else { + BoundType::Unknown + } + } Expr::Block { stmts, expr } => { let mut nested = state.clone(); legalize_stmts( @@ -2391,7 +2925,11 @@ pub(super) fn fold_builtin_call(expr: &mut Expr, builtin: BuiltinFunction, state if args.len() == 1 { let inferred = infer_expr_type(&args[0], state); if let Some(name) = inferred.type_name() { - *expr = Expr::String(name.to_string()); + // Map and null are shared with Option/Result carriers. + // Leave those calls for the checked presentation lower. + if name != "map" && name != "null" { + *expr = Expr::String(name.to_string()); + } } } } @@ -2426,6 +2964,7 @@ pub(super) fn infer_static_len(expr: &Expr) -> Option { infer_static_len(&args[0]).map(|base| base.saturating_add(1)) } BuiltinFunction::Set if args.len() == 3 => infer_static_len(&args[0]), + BuiltinFunction::MapEdit if args.len() == 4 => infer_static_len(&args[0]), _ => None, } } @@ -2513,6 +3052,7 @@ pub(super) fn bound_type_label(ty: BoundType) -> &'static str { match ty { BoundType::Unknown => "unknown", BoundType::Null => "null", + BoundType::Unit => "()", BoundType::Int => "int", BoundType::Float => "float", BoundType::Number => "number", diff --git a/src/compiler/typing/map_store.rs b/src/compiler/typing/map_store.rs new file mode 100644 index 00000000..617a9364 --- /dev/null +++ b/src/compiler/typing/map_store.rs @@ -0,0 +1,461 @@ +//! Classify map stores from the checked walk, then rewrite by semantic id. +//! +//! The decision uses the state already bound by collection: closure parameters, +//! match bindings, branch merges and loop stabilization. This pass does not +//! solve schemas again and does not read final slot aggregates as source +//! position types. + +use std::collections::{HashMap, HashSet}; + +use crate::BuiltinFunction; + +use super::super::ir::{ + ClosureExpr, Expr, FrontendIr, FunctionImpl, SemanticNodeId, Stmt, TypeSchema, +}; +use super::super::source_map::{SourceId, Span}; +use super::super::{CompileError, ParseError}; +use super::EntryLocalType; +use super::context::{MapStoreAction, TypeContext}; +use super::state::LocalTypeState; + +pub(crate) enum MapAdmissionError { + Compile(CompileError), + Parse(ParseError), +} + +/// Which producers receive RustScript map-store policy. +#[derive(Clone, Debug)] +pub(crate) enum MapStoreScope { + /// Every spanned call in this IR belongs to one RustScript producer. + RustScript, + /// Only these source ids are RustScript. Other producers keep Set. + Sources(HashSet), + /// Leave legacy Set deletion unchanged. + Legacy, +} + +pub(crate) fn classify_map_stores( + ir: &mut FrontendIr, + typing_mode: super::super::TypingMode, + entry_local_types: &[EntryLocalType], + scope: MapStoreScope, +) -> Result<(), MapAdmissionError> { + let decisions = super::collect_checked_map_store_decisions(ir, typing_mode, entry_local_types); + let sites = ir + .parsed_semantic_index + .as_ref() + .map(|index| index.call_sites.clone()) + .unwrap_or_default(); + for stmt in &mut ir.stmts { + apply_stmt(stmt, &decisions, &sites, &scope)?; + } + for function in ir.function_impls.values_mut() { + apply_function(function, &decisions, &sites, &scope)?; + } + Ok(()) +} + +pub(super) fn note_checked_map_store( + expr: &Expr, + state: &LocalTypeState, + context: &mut TypeContext<'_>, +) { + let Expr::Call(index, _, args, _, Some(id)) = expr else { + return; + }; + let action = match BuiltinFunction::from_call_index(*index) { + Some(BuiltinFunction::MapEdit) if args.len() == 4 => decide_edit(args, state, context), + Some(BuiltinFunction::Set) if args.len() == 3 => decide_set(args, state, context), + _ => None, + }; + if let Some(action) = action { + context.map_store_decisions.insert(*id, action); + } +} + +fn decide_set( + args: &[Expr], + state: &LocalTypeState, + context: &mut TypeContext<'_>, +) -> Option { + let receiver = context.infer_expr_schema(&args[0], state); + if is_logical_carrier(receiver.as_ref()) { + return Some(MapStoreAction::RejectCarrier); + } + let value = context.infer_expr_schema(&args[2], state); + if !value_may_be_unit(value.as_ref()) { + return None; + } + if is_map_object_or_named(receiver.as_ref()) { + return Some(MapStoreAction::RewriteInsert); + } + if receiver_kind_unresolved(receiver.as_ref()) { + return Some(MapStoreAction::RejectUnresolved); + } + None +} + +fn decide_edit( + args: &[Expr], + state: &LocalTypeState, + context: &mut TypeContext<'_>, +) -> Option { + let receiver = context.infer_expr_schema(&args[0], state); + if matches!(args.get(3), Some(Expr::Bool(true))) { + return if matches!(receiver, Some(TypeSchema::Map(_))) { + None + } else { + Some(MapStoreAction::RejectRemove) + }; + } + if is_logical_carrier(receiver.as_ref()) { + return Some(MapStoreAction::RejectCarrier); + } + let value = context.infer_expr_schema(&args[2], state); + if is_map_object_or_named(receiver.as_ref()) && !value_may_be_unit(value.as_ref()) { + return Some(MapStoreAction::NormalizeSet); + } + None +} + +fn apply_function( + function: &mut FunctionImpl, + decisions: &HashMap, + sites: &[super::super::ir::ParsedCallSite], + scope: &MapStoreScope, +) -> Result<(), MapAdmissionError> { + for stmt in &mut function.body_stmts { + apply_stmt(stmt, decisions, sites, scope)?; + } + apply_expr( + &mut function.body_expr, + decisions, + sites, + scope, + function.body_expr_line, + ) +} + +fn apply_stmt( + stmt: &mut Stmt, + decisions: &HashMap, + sites: &[super::super::ir::ParsedCallSite], + scope: &MapStoreScope, +) -> Result<(), MapAdmissionError> { + let line = super::helpers::stmt_line(stmt); + match stmt { + Stmt::Noop { .. } + | Stmt::FuncDecl { .. } + | Stmt::Break { .. } + | Stmt::Continue { .. } + | Stmt::Drop { .. } => {} + Stmt::Let { expr, .. } | Stmt::Assign { expr, .. } | Stmt::Expr { expr, .. } => { + apply_expr(expr, decisions, sites, scope, line)?; + } + Stmt::ClosureLet { closure, .. } => apply_closure(closure, decisions, sites, scope, line)?, + Stmt::IfElse { + condition, + then_branch, + else_branch, + .. + } => { + apply_expr(condition, decisions, sites, scope, line)?; + for stmt in then_branch.iter_mut().chain(else_branch.iter_mut()) { + apply_stmt(stmt, decisions, sites, scope)?; + } + } + Stmt::For { + init, + condition, + post, + body, + .. + } => { + apply_stmt(init, decisions, sites, scope)?; + apply_expr(condition, decisions, sites, scope, line)?; + apply_stmt(post, decisions, sites, scope)?; + for stmt in body { + apply_stmt(stmt, decisions, sites, scope)?; + } + } + Stmt::While { + condition, body, .. + } => { + apply_expr(condition, decisions, sites, scope, line)?; + for stmt in body { + apply_stmt(stmt, decisions, sites, scope)?; + } + } + } + Ok(()) +} + +fn apply_closure( + closure: &mut ClosureExpr, + decisions: &HashMap, + sites: &[super::super::ir::ParsedCallSite], + scope: &MapStoreScope, + line: u32, +) -> Result<(), MapAdmissionError> { + apply_expr(&mut closure.body, decisions, sites, scope, line) +} + +fn apply_expr( + expr: &mut Expr, + decisions: &HashMap, + sites: &[super::super::ir::ParsedCallSite], + scope: &MapStoreScope, + line: u32, +) -> Result<(), MapAdmissionError> { + match expr { + Expr::Call(_, _, args, _, _) => { + for arg in args.iter_mut() { + apply_expr(arg, decisions, sites, scope, line)?; + } + apply_call(expr, decisions, sites, scope, line)?; + } + Expr::ModuleCall(_, _, args, _) + | Expr::LocalCall(_, _, args, _) + | Expr::ClosureCall(_, args) => { + for arg in args { + apply_expr(arg, decisions, sites, scope, line)?; + } + } + Expr::Closure(closure) => apply_closure(closure, decisions, sites, scope, line)?, + Expr::Add(lhs, rhs) + | Expr::Sub(lhs, rhs) + | Expr::Mul(lhs, rhs) + | Expr::Div(lhs, rhs) + | Expr::Mod(lhs, rhs) + | Expr::And(lhs, rhs) + | Expr::Or(lhs, rhs) + | Expr::Eq(lhs, rhs) + | Expr::Lt(lhs, rhs) + | Expr::Gt(lhs, rhs) => { + apply_expr(lhs, decisions, sites, scope, line)?; + apply_expr(rhs, decisions, sites, scope, line)?; + } + Expr::Neg(inner) + | Expr::Not(inner) + | Expr::ToOwned(inner) + | Expr::Borrow(inner) + | Expr::BorrowMut(inner) => { + apply_expr(inner, decisions, sites, scope, line)?; + } + Expr::IfElse { + condition, + then_expr, + else_expr, + } => { + apply_expr(condition, decisions, sites, scope, line)?; + apply_expr(then_expr, decisions, sites, scope, line)?; + apply_expr(else_expr, decisions, sites, scope, line)?; + } + Expr::Match { + value, + arms, + default, + .. + } => { + apply_expr(value, decisions, sites, scope, line)?; + for (_, arm) in arms { + apply_expr(arm, decisions, sites, scope, line)?; + } + apply_expr(default, decisions, sites, scope, line)?; + } + Expr::Block { stmts, expr } => { + for stmt in stmts { + apply_stmt(stmt, decisions, sites, scope)?; + } + apply_expr(expr, decisions, sites, scope, line)?; + } + Expr::OptionalGet { container, key, .. } => { + apply_expr(container, decisions, sites, scope, line)?; + apply_expr(key, decisions, sites, scope, line)?; + } + Expr::OptionUnwrapOr { + value, fallback, .. + } => { + apply_expr(value, decisions, sites, scope, line)?; + apply_expr(fallback, decisions, sites, scope, line)?; + } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + apply_expr(payload, decisions, sites, scope, line)?; + } + } + Expr::OptionMatch { + value, + some_expr, + none_expr, + .. + } => { + apply_expr(value, decisions, sites, scope, line)?; + apply_expr(some_expr, decisions, sites, scope, line)?; + apply_expr(none_expr, decisions, sites, scope, line)?; + } + Expr::OptionMethod { + receiver, argument, .. + } + | Expr::ResultMethod { + receiver, argument, .. + } => { + apply_expr(receiver, decisions, sites, scope, line)?; + apply_expr(argument, decisions, sites, scope, line)?; + } + Expr::ResultConstruct { payload, .. } => { + apply_expr(payload, decisions, sites, scope, line)?; + } + Expr::ResultMatch { + value, + ok_expr, + err_expr, + .. + } => { + apply_expr(value, decisions, sites, scope, line)?; + apply_expr(ok_expr, decisions, sites, scope, line)?; + apply_expr(err_expr, decisions, sites, scope, line)?; + } + Expr::ResultQuery { value, .. } + | Expr::ResultTry { value, .. } + | Expr::ResultConvert { value, .. } => { + apply_expr(value, decisions, sites, scope, line)?; + } + Expr::Null + | Expr::Unit + | Expr::Int(_) + | Expr::Float(_) + | Expr::Bool(_) + | Expr::String(_) + | Expr::Bytes(_) + | Expr::FunctionRef(..) + | Expr::ModuleFunctionRef(..) + | Expr::UnresolvedFunctionRef { .. } + | Expr::Var(_) + | Expr::MoveVar(_) + | Expr::MoveField { .. } + | Expr::MoveIndex { .. } => {} + } + Ok(()) +} + +fn apply_call( + expr: &mut Expr, + decisions: &HashMap, + sites: &[super::super::ir::ParsedCallSite], + scope: &MapStoreScope, + line: u32, +) -> Result<(), MapAdmissionError> { + let Expr::Call(index, _, args, _, semantic_id) = expr else { + return Ok(()); + }; + let Some(id) = *semantic_id else { + return Ok(()); + }; + let Some(action) = decisions.get(&id).copied() else { + return Ok(()); + }; + if !scope_allows(scope, sites, id) { + return Ok(()); + } + let span = span_for(sites, id); + match action { + MapStoreAction::RewriteInsert => { + *index = BuiltinFunction::MapEdit.call_index(); + args.push(Expr::Bool(false)); + Ok(()) + } + MapStoreAction::NormalizeSet => { + *index = BuiltinFunction::Set.call_index(); + if args.len() == 4 { + args.pop(); + } + Ok(()) + } + MapStoreAction::RejectCarrier => Err(MapAdmissionError::Compile( + CompileError::InvalidFieldAccess { + line: Some(line), + source_name: None, + detail: "cannot store into an Option or Result by index or field; use Option/Result methods, or store into a map, object, or named record".to_string(), + span, + }, + )), + MapStoreAction::RejectUnresolved => Err(MapAdmissionError::Compile( + CompileError::InvalidFieldAccess { + line: Some(line), + source_name: None, + detail: "map store receiver schema is unresolved; a Unit or other possibly-null value cannot use set, which deletes null keys. Declare the receiver as map, an object, or a named record".to_string(), + span, + }, + )), + MapStoreAction::RejectRemove => Err(MapAdmissionError::Parse(ParseError { + line: usize::try_from(line).unwrap_or(1), + message: "E_MAP_REMOVE_RECORD: cannot remove a field from a record, object, Option, or Result; removal requires an explicit map".to_string(), + span, + code: Some("E_MAP_REMOVE_RECORD".to_string()), + })), + } +} + +fn scope_allows( + scope: &MapStoreScope, + sites: &[super::super::ir::ParsedCallSite], + id: SemanticNodeId, +) -> bool { + match scope { + MapStoreScope::Legacy => false, + MapStoreScope::RustScript => true, + MapStoreScope::Sources(ids) => { + span_for(sites, id).is_some_and(|span| ids.contains(&span.source_id)) + } + } +} + +fn span_for(sites: &[super::super::ir::ParsedCallSite], id: SemanticNodeId) -> Option { + sites + .iter() + .find(|site| site.id == id) + .map(|site| site.expr_span) +} + +pub(crate) fn map_admission_source_error(error: MapAdmissionError) -> super::super::SourceError { + match error { + MapAdmissionError::Compile(error) => super::super::SourceError::Compile(error), + MapAdmissionError::Parse(error) => super::super::SourceError::Parse(error), + } +} + +fn is_logical_carrier(schema: Option<&TypeSchema>) -> bool { + matches!( + schema, + Some(TypeSchema::Option(_) | TypeSchema::Result { .. }) + ) +} + +fn is_map_object_or_named(schema: Option<&TypeSchema>) -> bool { + matches!( + schema, + Some(TypeSchema::Map(_) | TypeSchema::Object(_) | TypeSchema::Named(_, _)) + ) +} + +fn receiver_kind_unresolved(schema: Option<&TypeSchema>) -> bool { + matches!( + schema, + None | Some(TypeSchema::Unknown | TypeSchema::GenericParam(_)) + ) +} + +fn value_may_be_unit(schema: Option<&TypeSchema>) -> bool { + match schema { + None + | Some( + TypeSchema::Unit + | TypeSchema::Null + | TypeSchema::GenericParam(_) + | TypeSchema::Unknown + | TypeSchema::Optional(_), + ) => true, + Some(_) => false, + } +} diff --git a/src/compiler/typing/state.rs b/src/compiler/typing/state.rs index ef07c487..27fbc59b 100644 --- a/src/compiler/typing/state.rs +++ b/src/compiler/typing/state.rs @@ -19,6 +19,7 @@ pub(crate) enum SimpleType { pub(crate) enum BoundType { Unknown, Null, + Unit, Int, Float, Number, @@ -37,6 +38,7 @@ impl BoundType { match self { BoundType::Unknown => None, BoundType::Null => Some("null"), + BoundType::Unit => Some("()"), BoundType::Int => Some("int"), BoundType::Float => Some("float"), BoundType::Number => Some("number"), @@ -78,6 +80,7 @@ impl From for ValueType { match value { BoundType::Unknown => ValueType::Unknown, BoundType::Null => ValueType::Null, + BoundType::Unit => ValueType::Null, BoundType::Int => ValueType::Int, BoundType::Float => ValueType::Float, BoundType::Number => ValueType::Unknown, diff --git a/src/compiler/typing/validate.rs b/src/compiler/typing/validate.rs index 1bfa81ad..5119089c 100644 --- a/src/compiler/typing/validate.rs +++ b/src/compiler/typing/validate.rs @@ -8,7 +8,8 @@ use super::super::source_map::Span; use super::context::{TypeContext, infer_access_schema, render_schema_label}; use super::helpers::{ bind_expr_result_to_slot, bound_type_label, find_declared_schema_mismatch, infer_binary_type, - infer_unary_type, is_numeric_bound_type, refine_state_for_match_pattern, validate_stmts, + infer_unary_type, is_numeric_bound_type, omitted_option_materialization_detail, + refine_state_for_match_pattern, validate_stmts, }; use super::state::{ BoundType, InferredCallable, LocalTypeState, are_compatible_bound_types, merge_bound_types, @@ -231,6 +232,15 @@ fn validate_expr_matches_schema( { return callable_argument_mismatch(site, format!("{label} type mismatch: {detail}")); } + if let Some(detail) = omitted_option_materialization_detail( + expr, + &expected_schema, + Some(actual_schema), + context, + state, + ) { + return callable_argument_mismatch(site, format!("{label} type mismatch: {detail}")); + } return Ok(()); } if bound_type_matches_schema(&expected_schema, actual_ty, context) { @@ -351,7 +361,7 @@ fn validate_json_schema( /// remains the final gate for actual values (string keys, bytes, /// callables, NaN/infinity), and every runtime value of a structurally /// recursive type is finite. -const MAX_JSON_SCHEMA_VALIDATION_REENTRY: usize = 32; +const MAX_JSON_SCHEMA_VALIDATION_REENTRY: usize = 2; /// Walks `schema` for `json::encode` legality. `seen` tracks the named /// schemas currently being expanded on the active path, so a self- or @@ -406,6 +416,15 @@ fn validate_json_schema_with_seen( } TypeSchema::Named(name, _) => { let reentry_count = reentries.get(name.as_str()).copied().unwrap_or(0); + if reentry_count > 0 { + // The body was already walked on this path. Another wrap of + // the same declaration is the recursion edge. Re-resolving + // it rebuilds the argument nest and overflows the 512 KiB + // json-encode probes. Fixed unsupported fields were + // rejected on the first walk; distinct declaration names + // still have a zero count and are walked in full. + return Ok(()); + } if reentry_count >= MAX_JSON_SCHEMA_VALIDATION_REENTRY { // Budget exhausted: this re-entry is a pure structural // recursion edge (container-wrapped recursion has no @@ -450,6 +469,7 @@ fn validate_json_schema_with_seen( | TypeSchema::Number | TypeSchema::Bool | TypeSchema::String => Ok(()), + TypeSchema::Unit => Ok(()), TypeSchema::Bytes => Err(format!( "{path} uses bytes, which json::encode does not support" )), @@ -463,6 +483,9 @@ fn validate_json_schema_with_seen( TypeSchema::Optional(inner) => { validate_json_schema_with_seen(inner, context, path, seen, reentries) } + TypeSchema::Option(inner) => { + validate_json_schema_with_seen(inner, context, path, seen, reentries) + } TypeSchema::Callable { .. } => Err(format!( "{path} is callable, which json::encode does not support" )), @@ -473,7 +496,7 @@ fn validate_json_schema_with_seen( // the diagnostic (and probe assertions on it) deterministic // across processes and runs. let mut sorted_fields: Vec<(&String, &TypeSchema)> = fields.iter().collect(); - sorted_fields.sort_by(|(a, _), (b, _)| a.cmp(b)); + sorted_fields.sort_by_key(|(name, _)| *name); for (field, value_schema) in sorted_fields { let child_path = if path.is_empty() { format!("field '{field}'") @@ -602,6 +625,7 @@ fn param_accepts_bound_type(expected: CallableParamType, actual: BoundType, stri match expected { CallableParamType::Any => true, CallableParamType::Null => actual == BoundType::Null, + CallableParamType::Unit => actual == BoundType::Unit, CallableParamType::Int => actual == BoundType::Int, CallableParamType::Float => actual == BoundType::Float, CallableParamType::Bool => actual == BoundType::Bool, @@ -658,6 +682,7 @@ pub(super) fn validate_expr( ) -> Result { Ok(match expr { Expr::Null => BoundType::Null, + Expr::Unit => BoundType::Unit, Expr::Int(_) => BoundType::Int, Expr::Float(_) => BoundType::Float, Expr::Bool(_) => BoundType::Bool, @@ -817,7 +842,11 @@ pub(super) fn validate_expr( context, strict_function_add_types, )?; - if context.expr_is_optional(lhs, state) || context.expr_is_optional(rhs, state) { + if context.expr_is_optional(lhs, state) + || context.expr_is_optional(rhs, state) + || logical_option_operand(context, lhs, state) + || logical_option_operand(context, rhs, state) + { if matches!(expr, Expr::Eq(_, _)) && comparison_accepts_optional(lhs, rhs) { return Ok(BoundType::Bool); } else { @@ -941,6 +970,15 @@ pub(super) fn validate_expr( context.infer_expr_schema(else_expr, &else_state), context.stmt_span(line_context.unwrap_or_default()), )?; + ensure_compatible_logical_schemas( + line_context, + source_name, + "if/else expression result", + context.infer_expr_schema(then_expr, &then_state), + context.infer_expr_schema(else_expr, &else_state), + context.is_strict(), + context.stmt_span(line_context.unwrap_or_default()), + )?; if then_ty == else_ty || matches!(static_condition, Some(true)) { then_ty } else if matches!(static_condition, Some(false)) { @@ -1003,6 +1041,15 @@ pub(super) fn validate_expr( schema.clone(), context.stmt_span(line_context.unwrap_or_default()), )?; + ensure_compatible_logical_schemas( + line_context, + source_name, + "match arm result", + arm_schema.clone(), + schema.clone(), + context.is_strict(), + context.stmt_span(line_context.unwrap_or_default()), + )?; arm_schema = arm_schema.or(schema); arm_type = Some(match arm_type { None => ty, @@ -1043,11 +1090,20 @@ pub(super) fn validate_expr( context.stmt_span(line_context.unwrap_or_default()), )?; ensure_compatible_callable_schemas( + line_context, + source_name, + "match result", + arm_schema.clone(), + default_schema.clone(), + context.stmt_span(line_context.unwrap_or_default()), + )?; + ensure_compatible_logical_schemas( line_context, source_name, "match result", arm_schema, default_schema, + context.is_strict(), context.stmt_span(line_context.unwrap_or_default()), )?; merge_bound_types(arm_type, default_ty) @@ -1074,6 +1130,38 @@ pub(super) fn validate_expr( )?; BoundType::Unknown } + Expr::OptionMethod { + receiver, argument, .. + } => { + let _ = validate_expr( + receiver, + state, + line_context, + source_name, + context, + strict_function_add_types, + )?; + let _ = validate_expr( + argument, + state, + line_context, + source_name, + context, + strict_function_add_types, + )?; + BoundType::Unknown + } + Expr::ResultConvert { value, .. } => { + let _ = validate_expr( + value, + state, + line_context, + source_name, + context, + strict_function_add_types, + )?; + BoundType::Map + } Expr::ResultConstruct { payload, .. } => { let _ = validate_expr( payload, @@ -1173,6 +1261,67 @@ pub(super) fn validate_expr( )?; merge_bound_types(ok_ty, err_ty) } + Expr::OptionConstruct { payload, .. } => { + if let Some(payload) = payload { + let _ = validate_expr( + payload, + state, + line_context, + source_name, + context, + strict_function_add_types, + )?; + } + BoundType::Map + } + Expr::OptionMatch { + value, + some_binding, + some_expr, + none_expr, + scrutinee_schema, + .. + } => { + let _ = validate_expr( + value, + state, + line_context, + source_name, + context, + strict_function_add_types, + )?; + let inner = match scrutinee_schema { + TypeSchema::Option(inner) => inner.as_ref().clone(), + _ => TypeSchema::Unknown, + }; + let mut some_state = state.clone(); + if let Some(slot) = some_binding { + some_state.set_with_optional_schema_origin( + *slot, + context.bound_type_for_schema(&inner), + Some(inner.clone()), + true, + matches!(inner, TypeSchema::Optional(_)), + ); + } + let some_ty = validate_expr( + some_expr, + &some_state, + line_context, + source_name, + context, + strict_function_add_types, + )?; + let none_ty = validate_expr( + none_expr, + state, + line_context, + source_name, + context, + strict_function_add_types, + )?; + merge_bound_types(some_ty, none_ty) + } Expr::Block { stmts, expr } => { let mut nested = state.clone(); validate_stmts( @@ -1472,6 +1621,18 @@ fn validate_optional_get_access( }) } +/// A logical `Option` value is still an unwrapped optional at a binary +/// operation site: it must be handled with an explicit match/unwrap method. +fn logical_option_operand( + context: &mut TypeContext<'_>, + expr: &Expr, + state: &LocalTypeState, +) -> bool { + context + .infer_expr_schema(expr, state) + .is_some_and(|schema| matches!(schema, TypeSchema::Option(_))) +} + fn optional_usage_error( line: Option, source_name: Option<&str>, @@ -1640,6 +1801,43 @@ fn eval_static_bool(expr: &Expr) -> Option { } } +fn ensure_compatible_logical_schemas( + line: Option, + source_name: Option<&str>, + context: &str, + lhs: Option, + rhs: Option, + strict: bool, + span: Option, +) -> Result<(), CompileError> { + if !strict { + return Ok(()); + } + let (Some(lhs), Some(rhs)) = (lhs, rhs) else { + return Ok(()); + }; + if are_compatible_schemas(&lhs, &rhs) { + return Ok(()); + } + // Named record literals materialize through the expected-type path. + // Only Option and Result joins are rejected here. + let logical = + |schema: &TypeSchema| matches!(schema, TypeSchema::Option(_) | TypeSchema::Result { .. }); + if !logical(&lhs) && !logical(&rhs) { + return Ok(()); + } + Err(CompileError::StrictTypingRequired { + line, + source_name: owned_source_name(source_name), + detail: format!( + "{context} joined incompatible schemas: {} vs {}", + render_schema_label(&lhs), + render_schema_label(&rhs) + ), + span, + }) +} + fn ensure_compatible_if_else_types( line: Option, source_name: Option<&str>, diff --git a/src/host_api.rs b/src/host_api.rs index 755b7d86..5b84bcb2 100644 --- a/src/host_api.rs +++ b/src/host_api.rs @@ -4,7 +4,7 @@ //! the functions and resource types a host exposes to scripts. It is deliberately //! independent of the compiler's inference types, the VM's runtime //! [`crate::vm`] resource table, the wire format ([`crate::vmbc`]) and the -//! generated builtin catalog ([`crate::builtins`]), so all of those can be +//! generated builtin catalog (`crate::builtins`), so all of those can be //! consumed without introducing a reverse dependency. //! //! ## Design invariants @@ -915,6 +915,9 @@ impl HostStructSchema { pub enum HostTypeSchema { Unknown, Null, + /// Guest `()`. Distinct from [`Self::Null`]; the physical carrier is the + /// internal Null value. + Unit, Int, Float, Number, @@ -924,6 +927,9 @@ pub enum HostTypeSchema { Array(Box), Map(Box), Optional(Box), + /// Guest `Option`. Runtime values are maps with a Boolean `tag` + /// (`true` = Some) and the active `payload`. Distinct from [`Self::Optional`]. + Option(Box), Callable { params: Vec, result: Box, @@ -1038,6 +1044,10 @@ impl Serialize for HostTypeSchema { state.serialize_field("err", err)?; state.end() } + Self::Option(inner) => { + serializer.serialize_newtype_variant("HostTypeSchema", 15, "Option", inner) + } + Self::Unit => serializer.serialize_unit_variant("HostTypeSchema", 16, "Unit"), } } } @@ -1059,6 +1069,8 @@ enum HostTypeSchemaVariant { Resource, Named, Result, + Option, + Unit, } struct HostTypeSchemaSeed<'a> { @@ -1091,7 +1103,7 @@ impl<'de> DeserializeSeed<'de> for HostTypeSchemaSeed<'_> { "HostTypeSchema", &[ "Unknown", "Null", "Int", "Float", "Number", "Bool", "String", "Bytes", "Array", - "Map", "Optional", "Callable", "Resource", "Named", "Result", + "Map", "Optional", "Callable", "Resource", "Named", "Result", "Option", "Unit", ], HostTypeSchemaVisitor { budget: self.budget, @@ -1180,6 +1192,17 @@ impl<'de> Visitor<'de> for HostTypeSchemaVisitor<'_> { depth: self.depth, }) .map(|(ok, err)| HostTypeSchema::Result { ok, err }), + HostTypeSchemaVariant::Option => { + let depth = next_schema_depth::(self.depth)?; + access + .newtype_variant_seed(HostTypeSchemaSeed { + budget: self.budget, + depth, + property: false, + }) + .map(|inner| HostTypeSchema::Option(Box::new(inner))) + } + HostTypeSchemaVariant::Unit => access.unit_variant().map(|()| HostTypeSchema::Unit), } } } @@ -1685,6 +1708,7 @@ impl fmt::Display for HostTypeSchema { match self { Self::Unknown => write!(f, "unknown"), Self::Null => write!(f, "null"), + Self::Unit => write!(f, "()"), Self::Int => write!(f, "int"), Self::Float => write!(f, "float"), Self::Number => write!(f, "number"), @@ -1694,6 +1718,7 @@ impl fmt::Display for HostTypeSchema { Self::Array(inner) => write!(f, "array<{inner}>"), Self::Map(inner) => write!(f, "map<{inner}>"), Self::Optional(inner) => write!(f, "optional<{inner}>"), + Self::Option(inner) => write!(f, "Option<{inner}>"), Self::Callable { params, result } => { write!(f, "fn(")?; for (index, param) in params.iter().enumerate() { @@ -2998,7 +3023,8 @@ where } HostTypeSchema::Array(inner) | HostTypeSchema::Map(inner) - | HostTypeSchema::Optional(inner) => { + | HostTypeSchema::Optional(inner) + | HostTypeSchema::Option(inner) => { let child_depth = depth .checked_add(1) @@ -3069,6 +3095,7 @@ where } HostTypeSchema::Unknown | HostTypeSchema::Null + | HostTypeSchema::Unit | HostTypeSchema::Int | HostTypeSchema::Float | HostTypeSchema::Number @@ -3259,7 +3286,7 @@ pub enum HostApiCatalogError { GuestResourceEffectMismatch { function: String, }, - /// A [`HostResourceType`] declaration used an invalid catalog key. + /// A `HostResourceType` declaration used an invalid catalog key. InvalidResourceTypeKey { type_name: &'static str, reason: String, @@ -4196,6 +4223,7 @@ fn try_push_type( match current { HostTypeSchema::Unknown => push_tag(bytes, b'U'), HostTypeSchema::Null => push_tag(bytes, b'N'), + HostTypeSchema::Unit => push_tag(bytes, b'.'), HostTypeSchema::Int => push_tag(bytes, b'I'), HostTypeSchema::Float => push_tag(bytes, b'F'), HostTypeSchema::Number => push_tag(bytes, b'#'), @@ -4266,6 +4294,15 @@ fn try_push_type( pending.push(err); pending.push(ok); } + HostTypeSchema::Option(inner) => { + push_tag(bytes, b'O'); + pending.try_reserve(1).map_err(|_| { + HostSchemaValidationError::AllocationFailed { + field: "schema encoding", + } + })?; + pending.push(inner); + } } } Ok(()) @@ -4400,7 +4437,8 @@ fn validate_named_struct_refs( } HostTypeSchema::Array(inner) | HostTypeSchema::Map(inner) - | HostTypeSchema::Optional(inner) => validate_named_struct_refs(function, inner, structs), + | HostTypeSchema::Optional(inner) + | HostTypeSchema::Option(inner) => validate_named_struct_refs(function, inner, structs), HostTypeSchema::Callable { params, result } => { for param in params { validate_named_struct_refs(function, param, structs)?; diff --git a/src/lib.rs b/src/lib.rs index 67f6fb93..75c32f4a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,9 @@ pub mod debug_info; #[cfg(feature = "runtime")] pub mod debugger; pub mod host_api; +pub mod option; pub mod result; +pub use option::GuestOption; pub use result::GuestResult; #[cfg(feature = "runtime")] pub use result::{ScriptValueContext, VmScriptArgs, VmScriptValue}; @@ -67,8 +69,8 @@ pub use builtins::runtime::http::{ }; #[cfg(feature = "runtime")] pub use builtins::runtime::{ - BorrowVmValue, FromVmValue, HostCallResult, IntoHostCallOutcome, IntoVmValue, TakeVmValue, arg, - borrow_arg, return_one, take_arg, + BorrowVmValue, FromVmValue, HostCallResult, IntoHostCallOutcome, IntoVmValue, TakeVmValue, + VmCallable, arg, borrow_arg, return_one, take_arg, }; #[cfg(feature = "runtime")] pub use builtins::runtime::{ @@ -138,7 +140,7 @@ pub use compiler::{ lint_trailing_function_return_semicolons, lint_unknown_inferred_local_types, lint_unknown_inferred_local_types_at_path_with_options, lint_unknown_inferred_local_types_with_options, lint_unknown_type_annotations, - parse_source_with_dialect, + parse_source_with_dialect, parse_source_with_dialect_and_catalog, }; pub use compiler::{ CertifiedCompilation, compile_source_file_with_capture_certification, diff --git a/src/option.rs b/src/option.rs new file mode 100644 index 00000000..7b095b94 --- /dev/null +++ b/src/option.rs @@ -0,0 +1,84 @@ +//! Typed guest `Option`, separate from the low-level nullable Rust +//! `Option` codec and from the VM's internal Null value. +//! +//! A guest Option is a physical map with a Boolean `tag` (`true` = Some, +//! `false` = None) and the active `payload`. None carries no payload; Some +//! carries exactly one payload. The payload may be physically Null for the +//! logical Unit value, in which case a canonicalizing writer is allowed to +//! omit the entry; the Boolean tag still separates `Some(())` from `None`. + +/// A guest-language `Option`. +/// +/// `VmResult>` keeps fatal VM errors in the outer result. +/// Only the selected variant is encoded; payload truthiness never selects it. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum GuestOption { + Some(T), + None, +} + +impl From> for GuestOption { + fn from(value: Option) -> Self { + match value { + Some(value) => Self::Some(value), + None => Self::None, + } + } +} + +impl From> for Option { + fn from(value: GuestOption) -> Self { + match value { + GuestOption::Some(value) => Some(value), + GuestOption::None => None, + } + } +} + +#[cfg(feature = "runtime")] +#[doc(hidden)] +pub fn payload(value: &crate::Value) -> crate::VmResult<(bool, Option<&crate::Value>)> { + use crate::{Value, VmError}; + let Value::Map(entries) = value else { + return Err(VmError::TypeMismatch("Option")); + }; + if entries.len() > 2 { + return Err(VmError::TypeMismatch("canonical Option fields")); + } + for (key, _) in entries.iter() { + let Value::String(name) = key else { + return Err(VmError::TypeMismatch("canonical Option fields")); + }; + if name.as_str() != "tag" && name.as_str() != "payload" { + return Err(VmError::TypeMismatch("canonical Option fields")); + } + } + let Some(Value::Bool(some)) = entries.get(&Value::string("tag")) else { + return Err(VmError::TypeMismatch("Option Boolean tag")); + }; + let payload = entries.get(&Value::string("payload")); + if !*some && payload.is_some() { + return Err(VmError::TypeMismatch("Option None payload")); + } + Ok((*some, payload)) +} + +/// Carrier for `None`: a tag only, with no active payload entry. +#[cfg(feature = "runtime")] +#[doc(hidden)] +pub fn none_carrier() -> crate::Value { + crate::Value::map(vec![( + crate::Value::string("tag"), + crate::Value::Bool(false), + )]) +} + +/// Carrier for `Some(payload)`. +#[cfg(feature = "runtime")] +#[doc(hidden)] +pub fn some_carrier(payload: crate::Value) -> crate::Value { + crate::Value::map(vec![ + (crate::Value::string("tag"), crate::Value::Bool(true)), + (crate::Value::string("payload"), payload), + ]) +} diff --git a/src/vm/aot/cfg.rs b/src/vm/aot/cfg.rs index 2834ea47..c47736b9 100644 --- a/src/vm/aot/cfg.rs +++ b/src/vm/aot/cfg.rs @@ -201,10 +201,8 @@ fn collect_block_starts( starts.insert(next_ip); } } - OpCode::CallValue | OpCode::CallScript => { - if next_ip < code.len() { - starts.insert(next_ip); - } + OpCode::CallValue | OpCode::CallScript if next_ip < code.len() => { + starts.insert(next_ip); } _ => {} } diff --git a/src/vm/aot/ssa.rs b/src/vm/aot/ssa.rs index a1e789b5..e367fa73 100644 --- a/src/vm/aot/ssa.rs +++ b/src/vm/aot/ssa.rs @@ -2960,6 +2960,15 @@ mod tests { use super::*; use crate::{BytecodeBuilder, HostImport, Value}; + fn compile_legacy(source: &str) -> crate::CompiledProgram { + crate::compile_source_with_flavor_and_options( + source, + crate::compiler::legacy_rss::flavor(), + crate::compiler::legacy_rss::options_with(crate::CompileSourceFileOptions::new()), + ) + .unwrap_or_else(|error| panic!("legacy fixture must compile: {error}")) + } + #[cfg(feature = "cranelift-jit")] #[test] fn r4_dynamic_result_aot_ssa_payload_checkpoints_and_host_root_execution() { @@ -2981,7 +2990,7 @@ mod tests { let source = format!( "pub fn increment(n: int) -> int {{ n + 1 }} pub fn relay(flag: bool, payload: {ty}) -> Result<{ty}, {ty}> {{ let carrier: Result<{ty}, {ty}> = if flag => {{ Result::Ok(payload) }} else => {{ Result::Err(payload) }}; let selected = match carrier {{ Result::Ok(v) => v, Result::Err(e) => e, }}; if flag => {{ Result::Ok(selected) }} else => {{ Result::Err(selected) }} }} 0;" ); - let compiled = crate::compile_source(&source).unwrap(); + let compiled = compile_legacy(&source); let program = compiled.program.with_local_count(compiled.locals); let ssa = build_aot_ssa(&program).unwrap(); ssa.verify().unwrap(); @@ -3093,9 +3102,11 @@ mod tests { )); let compiled = crate::compile_source_with_flavor_and_options( "fn absent() -> int? { null } fn relay() -> Result { let x = absent().unwrap_or(test::make_err_int()?); Result::Ok(0) } let carried = relay(); carried;", - crate::SourceFlavor::RustScript, - crate::CompileSourceFileOptions::new() - .with_host_api_catalog(Arc::new(catalog.build().unwrap())), + crate::compiler::legacy_rss::flavor(), + crate::compiler::legacy_rss::options_with( + crate::CompileSourceFileOptions::new() + .with_host_api_catalog(Arc::new(catalog.build().unwrap())), + ), ) .unwrap(); let program = compiled.program.with_local_count(compiled.locals); @@ -3137,7 +3148,7 @@ mod tests { let source = format!( "fn absent() -> int? {{ {expression} }} let carried = absent().unwrap_or(41); carried;" ); - let compiled = crate::compile_source(&source).unwrap(); + let compiled = compile_legacy(&source); let program = compiled.program.with_local_count(compiled.locals); println!( "AOTNULL source={source}\n{}\nTYPEMAP={:#?}", @@ -3195,7 +3206,7 @@ mod tests { ), format!("let x:int? = {expression}; x.unwrap_or(41);"), ] { - let compiled = crate::compile_source(&source).unwrap(); + let compiled = compile_legacy(&source); let program = compiled.program.with_local_count(compiled.locals); println!( "AOTNULL source={source}\n{}\n{}", @@ -3231,7 +3242,11 @@ mod tests { format!("use self::nullable as imported; imported::relay({flag}).unwrap_or(41);"), ) .unwrap(); - let compiled = crate::compile_source_file(&main).unwrap(); + let compiled = crate::compile_source_file_with_options( + &main, + crate::compiler::legacy_rss::options_with(crate::CompileSourceFileOptions::new()), + ) + .unwrap_or_else(|error| panic!("legacy import must compile: {error}")); let program = compiled.program.with_local_count(compiled.locals); let ssa = build_aot_ssa(&program).unwrap(); assert!(!ssa.text().contains("InterpreterBoundary")); diff --git a/src/vm/async_host/mod.rs b/src/vm/async_host/mod.rs index 613c851b..21d34bb0 100644 --- a/src/vm/async_host/mod.rs +++ b/src/vm/async_host/mod.rs @@ -4,7 +4,7 @@ //! functions that do not borrow the VM across a poll. The two pieces are: //! //! * [`HostFutureOutput`] — the terminal result of an async host call, -//! either an already-produced [`CallReturn`] or a [`VmCompletion`] closure +//! either an already-produced [`CallReturn`] or a `VmCompletion` closure //! that must run against the VM (e.g. to insert a resource into the //! execution scope) before the call can return to the guest. //! * [`CaptureAsyncHostContext`] — the trait async host functions use to @@ -13,7 +13,7 @@ //! Submitted futures are handed to the configured [`HostAsyncBridge`], which //! owns their polling on its own executor. The returned host-operation id is //! tracked in the host runtime's `submitted_host_ops` set so the VM routes -//! the waiting dispatch to the bridge's [`poll_submitted_op`] instead of a +//! the waiting dispatch to the bridge's `poll_submitted_op` instead of a //! runtime-owned operation driver. Async host operations therefore go //! through the bridge (the concrete driver of the submitted future); the VM //! never builds a second cancellation framework or static poller table. diff --git a/src/vm/capture_origin.rs b/src/vm/capture_origin.rs index 15a882bf..3e1b87fb 100644 --- a/src/vm/capture_origin.rs +++ b/src/vm/capture_origin.rs @@ -493,7 +493,7 @@ mod tests { HostTypeSchema::Resource(key()), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, )); let catalog = Arc::new(b.build().unwrap()); let options = CompileSourceFileOptions::new().with_host_api_catalog(catalog.clone()); diff --git a/src/vm/epoch.rs b/src/vm/epoch.rs index 4a5c9b67..ea4796f2 100644 --- a/src/vm/epoch.rs +++ b/src/vm/epoch.rs @@ -42,7 +42,7 @@ impl EpochHandle { } let previous = self .current - .fetch_update(Ordering::AcqRel, Ordering::Acquire, |current| { + .try_update(Ordering::AcqRel, Ordering::Acquire, |current| { Some(current.saturating_add(delta)) }) .unwrap_or_else(|current| current); diff --git a/src/vm/execution_scope.rs b/src/vm/execution_scope.rs index cacee2b9..36f07523 100644 --- a/src/vm/execution_scope.rs +++ b/src/vm/execution_scope.rs @@ -12,7 +12,7 @@ //! class or a host operation domain. Concrete drivers own poll/cancel (see //! [`HostOperation`](crate::vm::operation::HostOperation)) and concrete //! resources own their close (see -//! [`HostResource`](crate::vm::resource::HostResource)). +//! [`HostResource`]). use std::task::{Context, Poll}; @@ -63,7 +63,7 @@ pub enum ExecutionScopeError { ScopeNotClosing, /// Construction of a fresh scope failed because the process-unique /// resource-arena identity space is exhausted. Carries the typed resource - /// error ([`ResourceErrorCode::ResourceTableArenaExhausted`]); the scope + /// error (`ResourceErrorCode::ResourceTableArenaExhausted`); the scope /// was not created and no partial state exists. ArenaExhausted(ResourceError), /// The underlying resource insert/close failed. diff --git a/src/vm/host.rs b/src/vm/host.rs index df0b9791..0c695460 100644 --- a/src/vm/host.rs +++ b/src/vm/host.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, RwLock}; -use std::task::{Context, Poll, Wake, Waker}; +use std::task::{Context, Poll, Waker}; use crate::BuiltinFunction; use crate::host_api::{HostImportSchema, HostParamPassing, HostTypeSchema, ResourceTypeKey}; @@ -390,7 +390,8 @@ fn host_schema_contains_resource(schema: &HostTypeSchema) -> Result return Ok(true), HostTypeSchema::Array(inner) | HostTypeSchema::Map(inner) - | HostTypeSchema::Optional(inner) => pending.push((inner, depth + 1)), + | HostTypeSchema::Optional(inner) + | HostTypeSchema::Option(inner) => pending.push((inner, depth + 1)), HostTypeSchema::Callable { params, result } => { pending.push((result, depth + 1)); for param in params { @@ -408,6 +409,7 @@ fn host_schema_contains_resource(schema: &HostTypeSchema) -> Result Option<(ResourceTyp } } -/// Precisely supported union resource paths: Result/Optional nodes leading to -/// a direct resource or a resource-free leaf. Other aggregates remain refused. +/// Precisely supported union resource paths: Result, Option and Optional +/// nodes leading to a direct resource or a resource-free leaf. Other +/// aggregates remain refused. fn addressable_result_resources(schema: &HostTypeSchema) -> bool { fn branch(schema: &HostTypeSchema, depth: u8) -> bool { if depth > MAX_REGISTRATION_SCHEMA_DEPTH { @@ -446,13 +449,14 @@ fn addressable_result_resources(schema: &HostTypeSchema) -> bool { } match schema { HostTypeSchema::Result { ok, err } => branch(ok, depth + 1) && branch(err, depth + 1), + HostTypeSchema::Option(inner) => branch(inner, depth + 1), HostTypeSchema::Optional(inner) => branch(inner, depth + 1), HostTypeSchema::Resource(_) => true, _ => host_schema_contains_resource(schema) == Ok(false), } } match schema { - HostTypeSchema::Result { .. } => branch(schema, 0), + HostTypeSchema::Result { .. } | HostTypeSchema::Option(_) => branch(schema, 0), HostTypeSchema::Optional(inner) => addressable_result_resources(inner), _ => false, } @@ -467,6 +471,18 @@ fn active_result_resource<'a>( let (tag, payload) = crate::result::payload(value)?; active_result_resource(if tag { ok } else { err }, payload) } + HostTypeSchema::Option(inner) => { + let (some, payload) = crate::option::payload(value)?; + if !some { + return Ok(None); + } + let Some(payload) = payload else { + return Err(VmError::HostError( + "Option Some resource payload is missing".into(), + )); + }; + active_result_resource(inner, payload) + } HostTypeSchema::Optional(_) if matches!(value, Value::Null) => Ok(None), HostTypeSchema::Optional(inner) => active_result_resource(inner, value), HostTypeSchema::Resource(key) => Ok(Some((key.clone(), value))), @@ -2107,7 +2123,12 @@ pub(crate) fn validate_host_call_return( if matches!(schema.return_type, HostTypeSchema::Unknown) { return Ok(()); } - if value_slice.is_empty() && matches!(schema.return_type, HostTypeSchema::Null) { + if value_slice.is_empty() + && matches!( + schema.return_type, + HostTypeSchema::Null | HostTypeSchema::Unit + ) + { return Ok(()); } if value_slice.len() != 1 { @@ -2144,6 +2165,7 @@ fn callable_schema_matches( match (expected, actual) { (HostTypeSchema::Unknown, _) => true, (HostTypeSchema::Null, TypeSchema::Null) + | (HostTypeSchema::Unit, TypeSchema::Unit) | (HostTypeSchema::Int, TypeSchema::Int) | (HostTypeSchema::Float, TypeSchema::Float) | (HostTypeSchema::Bool, TypeSchema::Bool) @@ -2171,6 +2193,16 @@ fn callable_schema_matches( (HostTypeSchema::Optional(expected), TypeSchema::Optional(actual)) => { callable_schema_matches(expected, actual) } + (HostTypeSchema::Option(expected), TypeSchema::Option(actual)) => { + callable_schema_matches(expected, actual) + } + ( + HostTypeSchema::Result { ok, err }, + TypeSchema::Result { + ok: actual_ok, + err: actual_err, + }, + ) => callable_schema_matches(ok, actual_ok) && callable_schema_matches(err, actual_err), ( HostTypeSchema::Callable { params: expected_params, @@ -2227,6 +2259,7 @@ fn host_callable_schema_matches(expected: &HostTypeSchema, actual: &HostTypeSche match (expected, actual) { (HostTypeSchema::Unknown, _) => true, (HostTypeSchema::Null, HostTypeSchema::Null) + | (HostTypeSchema::Unit, HostTypeSchema::Unit) | (HostTypeSchema::Int, HostTypeSchema::Int) | (HostTypeSchema::Float, HostTypeSchema::Float) | (HostTypeSchema::Bool, HostTypeSchema::Bool) @@ -2235,9 +2268,23 @@ fn host_callable_schema_matches(expected: &HostTypeSchema, actual: &HostTypeSche | (HostTypeSchema::Number, HostTypeSchema::Number) => true, (HostTypeSchema::Array(expected), HostTypeSchema::Array(actual)) | (HostTypeSchema::Map(expected), HostTypeSchema::Map(actual)) - | (HostTypeSchema::Optional(expected), HostTypeSchema::Optional(actual)) => { + | (HostTypeSchema::Optional(expected), HostTypeSchema::Optional(actual)) + | (HostTypeSchema::Option(expected), HostTypeSchema::Option(actual)) => { host_callable_schema_matches(expected, actual) } + ( + HostTypeSchema::Result { + ok: expected_ok, + err: expected_err, + }, + HostTypeSchema::Result { + ok: actual_ok, + err: actual_err, + }, + ) => { + host_callable_schema_matches(expected_ok, actual_ok) + && host_callable_schema_matches(expected_err, actual_err) + } ( HostTypeSchema::Callable { params: expected_params, @@ -2361,6 +2408,13 @@ fn validate_host_value( Err(VmError::TypeMismatch("null")) } } + HostTypeSchema::Unit => { + if matches!(value, Value::Null) { + Ok(()) + } else { + Err(VmError::TypeMismatch("()")) + } + } HostTypeSchema::Int => { if matches!(value, Value::Int(_)) { Ok(()) @@ -2428,6 +2482,21 @@ fn validate_host_value( validate_host_value(value, inner, program, resources) } } + HostTypeSchema::Option(inner) => { + let (some, payload) = crate::option::payload(value)?; + if !some { + return Ok(()); + } + match payload { + Some(payload) => validate_host_value(payload, inner, program, resources), + // Only a proven logical Unit payload may be restored from a + // missing physical entry; every other Some must carry one. + None if matches!(inner.as_ref(), HostTypeSchema::Unit) => { + validate_host_value(&Value::Null, inner, program, resources) + } + None => Err(VmError::TypeMismatch("Option Some payload")), + } + } HostTypeSchema::Callable { params, result } => { validate_callable_value(value, params, result, program) } @@ -2491,16 +2560,6 @@ pub(super) enum WaitingHostOpSource { CallableStreamTermination, } -struct NoopWake; - -impl Wake for NoopWake { - fn wake(self: Arc) {} -} - -fn noop_waker() -> Waker { - Waker::from(Arc::new(NoopWake)) -} - #[inline] fn builtin_for_binding_name(name: &str) -> Option { if !name.contains("::") { @@ -3360,8 +3419,8 @@ impl Vm { } pub fn wait_for_host_op_blocking(&mut self) -> VmResult<()> { - let waker = noop_waker(); - let mut cx = Context::from_waker(&waker); + let waker = Waker::noop(); + let mut cx = Context::from_waker(waker); loop { match self.poll_waiting_host_op(&mut cx) { Poll::Ready(result) => return result, @@ -3402,6 +3461,11 @@ impl Vm { got: argc_u8, }); } + if let Some(outcome) = + self.try_redirect_typed_json_encode_to_public_override(builtin, argc_u8, call_ip)? + { + return Ok(outcome); + } if self.host.builtin_overrides.contains_key(&index) { return self.execute_builtin_override_call(index, argc_u8, call_ip); } @@ -3493,6 +3557,54 @@ impl Vm { } } + /// Typed `json::encode` lowering calls the private helper. A host override is + /// installed only on the public call index and must still receive the value. + fn try_redirect_typed_json_encode_to_public_override( + &mut self, + builtin: BuiltinFunction, + argc_u8: u8, + call_ip: usize, + ) -> VmResult> { + if builtin != BuiltinFunction::JsonEncodeTyped { + return Ok(None); + } + let public_index = BuiltinFunction::JsonEncode.call_index(); + if !self.host.builtin_overrides.contains_key(&public_index) { + return Ok(None); + } + let value_argc = BuiltinFunction::JsonEncode.arity(); + if argc_u8 < value_argc { + return Err(VmError::InvalidCallArity { + import: BuiltinFunction::JsonEncode.name().to_string(), + expected: value_argc, + got: argc_u8, + }); + } + let extra = usize::from(argc_u8 - value_argc); + let mut hidden = Vec::with_capacity(extra); + for _ in 0..extra { + hidden.push(self.pop_value()?); + } + // The public override sees only the value. A retrying Yield re-executes + // this same Call, so the hidden descriptor must be back in its original + // slot. Return, Halt, Pending and errors consume the invocation and + // release it without a guest drop-contract event. + let arg_boundary = self.instance.stack.len(); + match self.execute_builtin_override_call(public_index, value_argc, call_ip) { + Ok(HostCallExecOutcome::Yielded) => { + let at = arg_boundary.min(self.instance.stack.len()); + for (offset, operand) in hidden.into_iter().rev().enumerate() { + self.instance.stack.insert(at + offset, operand); + } + Ok(Some(HostCallExecOutcome::Yielded)) + } + other => { + drop(hidden); + other.map(Some) + } + } + } + pub(super) fn execute_builtin_override_call( &mut self, builtin_call_index: u16, diff --git a/src/vm/host_context.rs b/src/vm/host_context.rs index b138efb1..b3245deb 100644 --- a/src/vm/host_context.rs +++ b/src/vm/host_context.rs @@ -4,15 +4,15 @@ //! [`HostContext`] is the public, builtin-agnostic surface that a host //! embedding or an external host extension (a module living outside //! `src/builtins/**`) uses to register typed, per-VM module state, push typed -//! [`HostResource`]s, start [`HostOperation`]s, and read back resources / +//! [`HostResource`]s, start `HostOperation`s, and read back resources / //! operation status. Every scope SDK method delegates to the //! [`ExecutionScope`] owned by the underlying -//! [`HostRuntime`](super::host_runtime::HostRuntime), so all inserts land in +//! `HostRuntime`, so all inserts land in //! the same live scope and a Closing/Quiescent scope rejects them with a //! structured [`ExecutionScopeError::ScopeClosing`] (propagated through //! [`HostContextErrorKind::Scope`]). //! -//! It never hands out the underlying [`HostRuntime`](super::host_runtime::HostRuntime) +//! It never hands out the underlying `HostRuntime` //! and never names a builtin domain module; concrete SQLite / IO / HTTP / SSE //! remain same-crate builtins, but `src/vm` must not depend on any of their //! implementation modules or on `rusqlite`. @@ -20,7 +20,7 @@ //! **Boundary contract (enforced by `tests/host_context_arch_tests.rs`):** //! this module references neither concrete builtin paths nor `rusqlite`. //! -//! Host module state is owned directly by [`HostRuntime`]: typed, per-VM, and +//! Host module state is owned directly by `HostRuntime`: typed, per-VM, and //! deliberately **not** cleared on //! [`Vm::reset_for_reuse`](super::Vm::reset_for_reuse) or on execution-scope //! close. Registered state therefore survives invocation resets — and scope @@ -50,7 +50,7 @@ use super::resource::{ /// /// A host extension implements this for exactly one concrete `State` type and /// registers it through [`HostContext::set_module_state`]. State is typed at -/// compile time (keyed by [`TypeId`]) and is per-`Vm`; it is intentionally not +/// compile time (keyed by `TypeId`) and is per-`Vm`; it is intentionally not /// cleared by [`Vm::reset_for_reuse`](super::Vm::reset_for_reuse) or by /// execution-scope close, so policy / extension configuration survives across /// invocation resets. @@ -173,10 +173,10 @@ impl std::error::Error for HostContextError {} /// Result type used by the generic host boundary. pub type HostContextResult = Result; -/// The public, generic host boundary for one [`Vm`](super::Vm). +/// The public, generic host boundary for one [`Vm`]. /// /// Obtained from [`Vm::host_context`](super::Vm::host_context). It never leaks -/// the underlying [`HostRuntime`] and never references a builtin domain module, +/// the underlying `HostRuntime` and never references a builtin domain module, /// so external host extensions can register typed per-VM state and drive the /// generic execution scope through a stable public surface. pub struct HostContext<'a> { @@ -449,10 +449,10 @@ impl<'a> HostContext<'a> { } /// Starts a host operation in the current execution scope from a full - /// generic [`OperationSpec`] (concrete [`HostOperation`] driver, optional + /// generic [`OperationSpec`] (concrete `HostOperation` driver, optional /// deadline, optional cleanup). /// - /// External operations must produce concrete [`HostOperation`] drivers: + /// External operations must produce concrete `HostOperation` drivers: /// the scope and its registry own poll/cancel, so a driver is the only /// thing the extension supplies. There is deliberately no second registry /// and no adapter-specific generic helper on this surface. diff --git a/src/vm/host_extension.rs b/src/vm/host_extension.rs index 2da875f7..6ad985a3 100644 --- a/src/vm/host_extension.rs +++ b/src/vm/host_extension.rs @@ -2,7 +2,7 @@ //! //! This module is the controlled extension boundary through which an external //! host crate installs persistent policy state and registers host functions -//! without accessing any [`HostRuntime`](super::host_runtime::HostRuntime) +//! without accessing any `HostRuntime` //! private field or naming a builtin domain module: //! //! - [`HostExtension::install`] installs typed per-VM module state (policy / @@ -11,7 +11,7 @@ //! [`Vm::reset_for_reuse`](super::Vm::reset_for_reuse) and execution-scope //! close, and it never participates in resource close. //! - [`HostExtension::register`] registers host functions into a -//! [`HostFunctionRegistry`]. Registration is validated against the +//! `HostFunctionRegistry`. Registration is validated against the //! extension's [`HostApiCatalog`] via [`catalog_import_schemas`] so the //! registered function declarations — parameter labels, type schemas and //! passing modes — match the catalog exactly. Named-struct bodies from @@ -700,6 +700,7 @@ fn expected_guest_resource_effects(schema: &HostFunctionSchema) -> Vec visit(inner, effect, out), + HostTypeSchema::Option(inner) => visit(inner, effect, out), _ => {} } } @@ -774,7 +775,8 @@ fn collect_named_structs_from_type( } HostTypeSchema::Array(inner) | HostTypeSchema::Map(inner) - | HostTypeSchema::Optional(inner) => { + | HostTypeSchema::Optional(inner) + | HostTypeSchema::Option(inner) => { collect_named_structs_from_type(inner, seen, builder)?; } HostTypeSchema::Callable { params, result } => { @@ -804,9 +806,9 @@ fn collect_named_structs_from_type( /// /// Used directly by embedders; the `register` / `install` lifecycle is split /// so an extension can also be registered into a caller-supplied (e.g. -/// restricted / capability-granted) [`HostFunctionRegistry`] by calling +/// restricted / capability-granted) `HostFunctionRegistry` by calling /// [`register_host_extension`] and binding it with -/// [`HostFunctionRegistry::bind_vm_cached`]. +/// `HostFunctionRegistry::bind_vm_cached`. pub trait HostExtension: Send + Sync + 'static { /// Catalog whose named-struct bodies are installed atomically before /// [`Self::register`] by [`super::Vm::install_extension`] and @@ -886,7 +888,7 @@ pub fn catalog_import_schemas(catalog: &HostApiCatalog, name: &str) -> Vec>(); + let drained = std::mem::take(&mut self.stack); for value in drained { self.drop_value_with_contract(value); } diff --git a/src/vm/invocation.rs b/src/vm/invocation.rs index ea098f83..21bb2369 100644 --- a/src/vm/invocation.rs +++ b/src/vm/invocation.rs @@ -8,7 +8,7 @@ //! one event item is buffered between polls (natural backpressure). //! //! The invocation reuses the existing callable execution state -//! ([`Vm::start_callable`], [`Vm::run`], [`Vm::take_invocation_result`]) and the +//! ([`Vm::start_callable`], [`Vm::run`], `Vm::take_invocation_result`) and the //! existing async host bridge; it does not duplicate interpreter or host loops, //! and it does not add an executor, generator syntax, an event queue, or event //! persistence policy. Cancellation is a per-invocation typed reason carried diff --git a/src/vm/jit/recorder.rs b/src/vm/jit/recorder.rs index fde45d90..7a6ba5f7 100644 --- a/src/vm/jit/recorder.rs +++ b/src/vm/jit/recorder.rs @@ -472,6 +472,8 @@ fn inline_schema_guard_type(schema: &TypeSchema) -> Option> { Some(Some(ValueType::Array)) } TypeSchema::Null => Some(Some(ValueType::Null)), + TypeSchema::Unit => Some(Some(ValueType::Null)), + TypeSchema::Option(_) => Some(Some(ValueType::Map)), TypeSchema::Number | TypeSchema::Optional(_) | TypeSchema::Callable { .. } => None, TypeSchema::Resource(_) => None, } @@ -1315,10 +1317,7 @@ pub(crate) fn record_trace_with_local_count( let mut entry_callable_guards = Vec::new(); let mut inline_frame: Option = None; - loop { - let Some(decoded) = cursor.next()? else { - break; - }; + while let Some(decoded) = cursor.next()? { let is_useful_native_computation = decoded.is_useful_native_computation(); let instruction_failure_exit = if decoded.may_need_inline_failure_exit() { inline_frame diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 4ea788a7..0481002d 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -456,7 +456,7 @@ fn compute_program_cache_key(program: &Program) -> u64 { } hash_type_map(program.type_map.as_ref(), &mut hasher); let mut named_struct_decls = program.named_struct_decls.iter().collect::>(); - named_struct_decls.sort_unstable_by(|(lhs, _), (rhs, _)| lhs.cmp(rhs)); + named_struct_decls.sort_unstable_by_key(|(name, _)| *name); named_struct_decls.len().hash(&mut hasher); for (name, decl) in named_struct_decls { name.hash(&mut hasher); @@ -552,6 +552,17 @@ impl<'a> NamedStructLookup<'a> { } } +pub(crate) fn resolve_named_struct_body( + host: &HashMap, + guest: &HashMap, + name: &str, + args: &[crate::compiler::TypeSchema], +) -> Option { + NamedStructLookup { host, guest } + .body(name, args) + .map(|(body, _origin)| body) +} + fn substitute_named_struct_schema( schema: &crate::compiler::TypeSchema, bindings: &HashMap, @@ -566,6 +577,9 @@ fn substitute_named_struct_schema( TypeSchema::Optional(inner) => { TypeSchema::Optional(Box::new(substitute_named_struct_schema(inner, bindings))) } + TypeSchema::Option(inner) => { + TypeSchema::Option(Box::new(substitute_named_struct_schema(inner, bindings))) + } TypeSchema::Named(name, args) => TypeSchema::Named( name.clone(), args.iter() @@ -642,6 +656,7 @@ fn result_schema_accepts_missing_payload(schema: &crate::compiler::TypeSchema) - matches!( schema, crate::compiler::TypeSchema::Null + | crate::compiler::TypeSchema::Unit | crate::compiler::TypeSchema::Optional(_) | crate::compiler::TypeSchema::Unknown ) @@ -704,6 +719,13 @@ fn validate_value_against_type_schema_walk( Err(VmError::TypeMismatch("null")) } } + TypeSchema::Unit => { + if !validate_scalars || matches!(value, Value::Null) { + Ok(()) + } else { + Err(VmError::TypeMismatch("()")) + } + } TypeSchema::Int => { if !validate_scalars || matches!(value, Value::Int(_)) { Ok(()) @@ -761,6 +783,37 @@ fn validate_value_against_type_schema_walk( ) } } + TypeSchema::Option(inner) => { + let (some, payload) = crate::option::payload(value)?; + if !some { + return Ok(()); + } + match payload { + Some(payload) => validate_value_against_type_schema_walk( + payload, + inner, + resources, + validate_scalars, + named_struct_schemas, + depth + 1, + nodes, + ), + // Only a proven logical Unit payload may be restored from a + // missing physical entry. + None if matches!(inner.as_ref(), TypeSchema::Unit) => { + validate_value_against_type_schema_walk( + &Value::Null, + inner, + resources, + validate_scalars, + named_struct_schemas, + depth + 1, + nodes, + ) + } + None => Err(VmError::TypeMismatch("Option Some payload")), + } + } TypeSchema::Named(name, args) => { if !matches!(value, Value::Map(_)) { return Err(VmError::TypeMismatch("map")); @@ -1030,7 +1083,10 @@ fn schema_contains_resource_walk( nodes, ) } - TypeSchema::Optional(inner) | TypeSchema::Array(inner) | TypeSchema::Map(inner) => { + TypeSchema::Optional(inner) + | TypeSchema::Option(inner) + | TypeSchema::Array(inner) + | TypeSchema::Map(inner) => { schema_contains_resource_walk(inner, named_struct_schemas, depth + 1, active, nodes) } TypeSchema::Object(fields) => fields.values().any(|field| { @@ -1076,6 +1132,7 @@ fn schema_contains_resource_walk( | TypeSchema::Callable { .. } | TypeSchema::Unknown | TypeSchema::Null + | TypeSchema::Unit | TypeSchema::Int | TypeSchema::Float | TypeSchema::Number @@ -1098,6 +1155,7 @@ fn hash_type_schema(schema: &crate::compiler::TypeSchema, state: &mut impl Hashe match schema { TypeSchema::Unknown => 0u8.hash(state), TypeSchema::Null => 1u8.hash(state), + TypeSchema::Unit => 20u8.hash(state), TypeSchema::Int => 2u8.hash(state), TypeSchema::Float => 3u8.hash(state), TypeSchema::Number => 4u8.hash(state), @@ -1108,6 +1166,10 @@ fn hash_type_schema(schema: &crate::compiler::TypeSchema, state: &mut impl Hashe 16u8.hash(state); hash_type_schema(inner, state); } + TypeSchema::Option(inner) => { + 19u8.hash(state); + hash_type_schema(inner, state); + } TypeSchema::GenericParam(name) => { 8u8.hash(state); name.hash(state); @@ -2491,7 +2553,7 @@ impl Vm { } pub(super) fn clear_stack_with_drop_contract(&mut self) { - let drained = self.instance.stack.drain(..).collect::>(); + let drained = std::mem::take(&mut self.instance.stack); for value in drained { self.drop_value_with_contract(value); } @@ -3831,7 +3893,7 @@ impl Vm { /// Returns the generic host boundary for this VM. /// - /// [`HostContext`](crate::vm::host_context::HostContext) exposes typed + /// [`HostContext`] exposes typed /// per-VM module state and the generic host-agnostic execution-scope SDK /// to external host extensions without leaking the underlying host runtime /// or naming a builtin domain module. @@ -3865,7 +3927,7 @@ impl Vm { self.host.install_host_state_requirements(requirements) } - /// Installs a [`HostExtension`](crate::vm::host_extension::HostExtension) + /// Installs a [`HostExtension`] /// onto this VM. /// /// Registration (into the VM's bound host-function registry) is @@ -4354,9 +4416,10 @@ impl RetCleanupWalk<'_> { let next = depth + 1; match schema { TypeSchema::Resource(_) => Ok(true), - TypeSchema::Optional(inner) | TypeSchema::Array(inner) | TypeSchema::Map(inner) => { - self.schema_owns(inner, next) - } + TypeSchema::Optional(inner) + | TypeSchema::Option(inner) + | TypeSchema::Array(inner) + | TypeSchema::Map(inner) => self.schema_owns(inner, next), TypeSchema::ArrayTuple(items) => { let mut owned = false; for item in items { @@ -4409,6 +4472,7 @@ impl RetCleanupWalk<'_> { } TypeSchema::Unknown | TypeSchema::Null + | TypeSchema::Unit | TypeSchema::Int | TypeSchema::Float | TypeSchema::Number @@ -4461,6 +4525,21 @@ impl RetCleanupWalk<'_> { } self.walk(value, inner, next) } + TypeSchema::Option(inner) => { + let (some, payload) = crate::option::payload(value)?; + if !some { + return Ok(()); + } + let Some(payload) = payload else { + if matches!(inner.as_ref(), TypeSchema::Unit) { + return Ok(()); + } + return Err(VmError::HostError( + "Err cleanup expected an Option Some payload".to_string(), + )); + }; + self.walk(payload, inner, next) + } TypeSchema::Array(inner) => { let Value::Array(items) = value else { return Err(VmError::HostError( @@ -4572,6 +4651,7 @@ impl RetCleanupWalk<'_> { TypeSchema::Callable { .. } | TypeSchema::Unknown | TypeSchema::Null + | TypeSchema::Unit | TypeSchema::Int | TypeSchema::Float | TypeSchema::Number diff --git a/src/vm/operation/id.rs b/src/vm/operation/id.rs index a708b386..32e9e454 100644 --- a/src/vm/operation/id.rs +++ b/src/vm/operation/id.rs @@ -3,8 +3,8 @@ //! An [`OperationId`] is an opaque 63-bit token that *packs* the three //! identifiers that uniquely address an in-flight operation in this VM: //! -//! * a **registry tag** identifying which [`registry::OperationRegistry`] -//! owns the id (allocated by [`allocate_registry_tag`]); +//! * a **registry tag** identifying which `registry::OperationRegistry` +//! owns the id (allocated by `allocate_registry_tag`); //! * a one-based **slot identity** selecting an entry inside that registry; //! * a **generation** that distinguishes successive occupants of the same //! slot. @@ -188,7 +188,7 @@ pub(super) fn allocate_registry_tag() -> OperationResult { let source = test_seam::source().unwrap_or(&NEXT_REGISTRY_TAG); #[cfg(not(test))] let source = &NEXT_REGISTRY_TAG; - match source.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |current| { + match source.try_update(Ordering::Relaxed, Ordering::Relaxed, |current| { // Hand out `current` (1..=MAX), advancing to `current + 1`; once // `current` exceeds `MAX_REGISTRY_TAG` the space is exhausted. if current <= MAX_REGISTRY_TAG { diff --git a/src/vm/resource/table.rs b/src/vm/resource/table.rs index e854c81d..3bc6a31d 100644 --- a/src/vm/resource/table.rs +++ b/src/vm/resource/table.rs @@ -171,7 +171,7 @@ pub struct ResourceTable { /// is exhausted. /// /// Allocation is atomic and monotonic: the counter is advanced exactly once -/// per successful handout (via `fetch_update`), never on failure, and ids are +/// per successful handout (via `try_update`), never on failure, and ids are /// never recycled or wrapped. Under `#[cfg(test)]`, the current thread's /// [`test_seam`] override (if installed) replaces the process-global /// `NEXT_ARENA_ID` so exhaustion tests are deterministic and never consume the @@ -182,7 +182,7 @@ fn allocate_arena_id() -> Result { #[cfg(not(test))] let source = &NEXT_ARENA_ID; source - .fetch_update(Ordering::AcqRel, Ordering::Acquire, |arena_id| { + .try_update(Ordering::AcqRel, Ordering::Acquire, |arena_id| { (arena_id <= MAX_HANDLE_ARENA_ID).then_some(arena_id + 1) }) .map_err(|_| { diff --git a/src/vm/store.rs b/src/vm/store.rs index f6d99646..bbff5992 100644 --- a/src/vm/store.rs +++ b/src/vm/store.rs @@ -231,7 +231,22 @@ macro_rules! impl_vm_script_scalar { impl_vm_script_scalar!(i64, Int); impl_vm_script_scalar!(bool, Bool); impl_vm_script_scalar!(String, String); -impl_vm_script_scalar!((), Null); + +impl VmScriptValue for () { + fn schema() -> VmResult { + Ok(HostTypeSchema::Unit) + } + fn into_value(self, _: &mut ScriptValueContext<'_>) -> VmResult { + Ok(Value::Null) + } + fn from_value(_: &mut Vm, value: Value) -> VmResult { + if matches!(value, Value::Null) { + Ok(()) + } else { + Err(VmError::TypeMismatch("() callback result")) + } + } +} impl VmScriptValue for crate::GuestResult { fn schema() -> VmResult { @@ -273,6 +288,28 @@ impl VmScriptValue for Option { } } +impl VmScriptValue for crate::GuestOption { + fn schema() -> VmResult { + Ok(HostTypeSchema::Option(Box::new(T::schema()?))) + } + fn into_value(self, context: &mut ScriptValueContext<'_>) -> VmResult { + match self { + Self::Some(value) => Ok(crate::option::some_carrier(value.into_value(context)?)), + Self::None => Ok(crate::option::none_carrier()), + } + } + fn from_value(vm: &mut Vm, value: Value) -> VmResult { + vm.validate_guest_result_value(&value, &Self::schema()?)?; + let (some, payload) = crate::option::payload(&value)?; + if some { + let payload = payload.cloned().unwrap_or(Value::Null); + T::from_value(vm, payload).map(Self::Some) + } else { + Ok(Self::None) + } + } +} + impl VmScriptArgs for () { fn schemas() -> VmResult> { Ok(Vec::new()) @@ -557,12 +594,12 @@ impl ScriptResult for () { fn from_value(value: Value) -> VmResult { match value { Value::Null => Ok(()), - _ => Err(VmError::TypeMismatch("null callback result")), + _ => Err(VmError::TypeMismatch("() callback result")), } } fn schema() -> Option { - Some(TypeSchema::Null) + Some(TypeSchema::Unit) } } @@ -647,6 +684,24 @@ impl IntoScriptValue for Option { } } +impl IntoScriptValue for crate::GuestOption { + fn into_script_value(self) -> Value { + match self { + Self::Some(value) => crate::option::some_carrier(value.into_script_value()), + Self::None => crate::option::none_carrier(), + } + } + fn schema() -> Option { + Some(TypeSchema::Option(Box::new(T::schema()?))) + } +} + +impl IntoScriptValue for crate::VmCallable { + fn into_script_value(self) -> Value { + self.into_value() + } +} + impl ScriptResult for Option { fn from_value(value: Value) -> VmResult { if matches!(value, Value::Null) { @@ -660,6 +715,27 @@ impl ScriptResult for Option { } } +impl ScriptResult for crate::GuestOption { + fn from_value(value: Value) -> VmResult { + let (some, payload) = crate::option::payload(&value)?; + if some { + let payload = payload.cloned().unwrap_or(Value::Null); + T::from_value(payload).map(Self::Some) + } else { + Ok(Self::None) + } + } + fn schema() -> Option { + Some(TypeSchema::Option(Box::new(T::schema()?))) + } +} + +impl ScriptResult for crate::VmCallable { + fn from_value(value: Value) -> VmResult { + ::from_vm_value(&value, "callable") + } +} + impl IntoScriptValue for Vec { fn into_script_value(self) -> Value { Value::array(self.into_iter().map(T::into_script_value).collect()) diff --git a/src/vm/store/publication.rs b/src/vm/store/publication.rs index d063367b..cbc4e937 100644 --- a/src/vm/store/publication.rs +++ b/src/vm/store/publication.rs @@ -83,6 +83,7 @@ impl Graph<'_> { for _ in 0..MAX_DEPTH { owned_schema = match owned_schema { Some(TypeSchema::Optional(inner)) => Some(*inner), + Some(TypeSchema::Option(inner)) => Some(*inner), Some(TypeSchema::Named(ref name, ref args)) => super::super::NamedStructLookup { host: &self.vm.host.named_struct_schemas, guest: &self.vm.program.named_struct_decls, @@ -160,6 +161,14 @@ impl Graph<'_> { _ => None, } } + (Some(TypeSchema::Option(inner)), Value::String(name)) + if name.as_str() == "payload" => + { + match values.get(&Value::string("tag")) { + Some(Value::Bool(true)) => Some(inner.as_ref()), + _ => None, + } + } (Some(TypeSchema::Object(fields)), Value::String(name)) => { fields.get(name.as_str()) } diff --git a/src/vm/tests.rs b/src/vm/tests.rs index a8f11310..7c6d46ec 100644 --- a/src/vm/tests.rs +++ b/src/vm/tests.rs @@ -22,6 +22,79 @@ fn native_cache_test_lock() -> &'static Mutex<()> { LOCK.get_or_init(|| Mutex::new(())) } +struct PublicJsonEncodeOverride { + seen: Arc>>>, +} + +impl HostFunction for PublicJsonEncodeOverride { + fn call(&mut self, _vm: &mut Vm, args: &[Value]) -> VmResult { + self.seen.lock().expect("override args").push(args.to_vec()); + Ok(CallOutcome::Return(CallReturn::one(Value::string( + "override", + )))) + } +} + +fn json_encode_override_source() -> &'static str { + "use json;\njson::encode(\"request_body\");\n" +} + +fn builtin_call_sites(code: &[u8]) -> Vec<(u16, u8, Option<&'static str>)> { + let mut calls = Vec::new(); + let mut ip = 0; + while ip < code.len() { + let opcode = crate::OpCode::try_from(code[ip]).expect("opcode"); + if opcode == crate::OpCode::Call { + let index = u16::from_le_bytes([code[ip + 1], code[ip + 2]]); + let argc = code[ip + 3]; + let name = BuiltinFunction::from_call_index(index).map(|builtin| builtin.name()); + calls.push((index, argc, name)); + } + ip += 1 + opcode.operand_len(); + } + calls +} + +#[test] +fn typed_json_encode_honors_public_override_slot_with_value_only() { + let compiled = crate::compile_source(json_encode_override_source()) + .expect("json encode source should compile"); + let calls = builtin_call_sites(&compiled.program.code); + assert_eq!( + calls, + vec![( + BuiltinFunction::JsonEncodeTyped.call_index(), + 2, + Some(BuiltinFunction::JsonEncodeTyped.name()) + )], + "typed lowering must keep the private encode call" + ); + + let seen = Arc::new(Mutex::new(Vec::new())); + let mut vm = Vm::new(compiled.program); + vm.bind_function( + "json::encode", + Box::new(PublicJsonEncodeOverride { + seen: Arc::clone(&seen), + }), + ); + let mut override_keys: Vec = vm.host.builtin_overrides.keys().copied().collect(); + override_keys.sort_unstable(); + assert_eq!( + override_keys, + vec![BuiltinFunction::JsonEncode.call_index()], + "public bind installs only the json::encode slot" + ); + + let status = vm.run().expect("overridden encode should run"); + assert_eq!(status, VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("override")]); + assert_eq!( + seen.lock().expect("override args").as_slice(), + &[vec![Value::string("request_body")]] + ); +} + #[test] fn program_cache_key_includes_full_host_import_schema() { let function = @@ -2572,11 +2645,6 @@ fn async_host_future_completion_error_cleans_up_bridge_operation_once() { } } - struct NoopWake; - impl std::task::Wake for NoopWake { - fn wake(self: Arc) {} - } - let cleanup_calls = Arc::new(Mutex::new(Vec::new())); let mut vm = Vm::new(Program::new(Vec::new(), vec![OpCode::Ret as u8])); vm.set_async_bridge(Box::new(FailingCompletionBridge { @@ -2594,7 +2662,7 @@ fn async_host_future_completion_error_cleans_up_bridge_operation_once() { vm.set_waiting_host_op_with_return(op_id, WaitingHostOpSource::HostBridge, None, None) .expect("vm should wait on submitted operation"); - let waker = Waker::from(Arc::new(NoopWake)); + let waker = Waker::noop().clone(); let mut cx = Context::from_waker(&waker); let result = vm.poll_waiting_host_op(&mut cx); assert!( @@ -4910,7 +4978,7 @@ fn sol_lifecycle_vm(source: &str, stats: &Arc) -> Vm { crate::HostTypeSchema::Resource(key.clone()), crate::HostParamPassing::TakeOwned, )], - crate::HostTypeSchema::Null, + crate::HostTypeSchema::Unit, )); catalog.function(crate::HostFunctionSchema::with_return( "test::fail", diff --git a/src/vmbc.rs b/src/vmbc.rs index 95ea08a1..e651f978 100644 --- a/src/vmbc.rs +++ b/src/vmbc.rs @@ -19,13 +19,13 @@ use crate::host_api::{ use crate::vm::{HostImport, OpCode, Program, Value}; const MAGIC: [u8; 4] = *b"VMBC"; -const VERSION_V13: u16 = 13; +const VERSION_V14: u16 = 14; const FLAGS: u16 = 0; const MAX_WIRE_PAYLOAD_BYTES: usize = 64 * 1024 * 1024; -const MAX_WIRE_BLOB_BYTES: usize = 16 * 1024 * 1024; -const MAX_WIRE_COUNT: usize = 1_000_000; -const MAX_WIRE_AGGREGATE_ITEMS: usize = 1_000_000; -const MAX_SCHEMA_DEPTH: usize = 64; +use crate::compiler::type_schema_codec::{ + self, MAX_SCHEMA_DEPTH, MAX_WIRE_AGGREGATE_ITEMS, MAX_WIRE_BLOB_BYTES, MAX_WIRE_COUNT, + SchemaCodecError, SchemaReader, +}; #[derive(Debug, Clone, PartialEq, Eq)] pub enum WireError { @@ -511,7 +511,7 @@ fn cleanup_schema_owns_resource( let next = depth + 1; match schema { TypeSchema::Resource(_) => Ok(true), - TypeSchema::Optional(inner) | TypeSchema::Array(inner) => { + TypeSchema::Optional(inner) | TypeSchema::Option(inner) | TypeSchema::Array(inner) => { cleanup_schema_owns_resource(inner, decls, next, nodes, active) } TypeSchema::ArrayTuple(items) => { @@ -537,7 +537,7 @@ fn cleanup_schema_owns_resource( TypeSchema::Object(fields) => { let mut owned = false; let mut fields = fields.iter().collect::>(); - fields.sort_unstable_by(|(lhs, _), (rhs, _)| lhs.cmp(rhs)); + fields.sort_unstable_by_key(|(name, _)| *name); for (_, field) in fields { owned |= cleanup_schema_owns_resource(field, decls, next, nodes, active)?; } @@ -572,6 +572,7 @@ fn cleanup_schema_owns_resource( } TypeSchema::Unknown | TypeSchema::Null + | TypeSchema::Unit | TypeSchema::Int | TypeSchema::Float | TypeSchema::Number @@ -596,6 +597,9 @@ fn substitute_cleanup_schema( TypeSchema::Optional(inner) => { TypeSchema::Optional(Box::new(substitute_cleanup_schema(inner, params, args))) } + TypeSchema::Option(inner) => { + TypeSchema::Option(Box::new(substitute_cleanup_schema(inner, params, args))) + } TypeSchema::Array(inner) => { TypeSchema::Array(Box::new(substitute_cleanup_schema(inner, params, args))) } @@ -644,6 +648,7 @@ fn substitute_cleanup_schema( }, TypeSchema::Unknown | TypeSchema::Null + | TypeSchema::Unit | TypeSchema::Int | TypeSchema::Float | TypeSchema::Number @@ -657,7 +662,7 @@ fn substitute_cleanup_schema( pub fn encode_program(program: &Program) -> Result, WireError> { let mut out = Vec::new(); out.extend_from_slice(&MAGIC); - out.extend_from_slice(&VERSION_V13.to_le_bytes()); + out.extend_from_slice(&VERSION_V14.to_le_bytes()); out.extend_from_slice(&FLAGS.to_le_bytes()); write_u32_count("constants", program.constants.len(), &mut out)?; @@ -712,7 +717,7 @@ pub fn decode_program(bytes: &[u8]) -> Result { } let version = cursor.read_u16()?; - if version != VERSION_V13 { + if version != VERSION_V14 { return Err(WireError::UnsupportedVersion(version)); } @@ -1994,6 +1999,7 @@ fn write_host_type_schema( match schema { HostTypeSchema::Unknown => out.push(0), HostTypeSchema::Null => out.push(1), + HostTypeSchema::Unit => out.push(16), HostTypeSchema::Int => out.push(2), HostTypeSchema::Float => out.push(3), HostTypeSchema::Number => out.push(4), @@ -2038,6 +2044,10 @@ fn write_host_type_schema( write_host_type_schema(ok, out, next_host_schema_depth(depth)?)?; write_host_type_schema(err, out, next_host_schema_depth(depth)?)?; } + HostTypeSchema::Option(inner) => { + out.push(15); + write_host_type_schema(inner, out, next_host_schema_depth(depth)?)?; + } } Ok(()) } @@ -2131,6 +2141,11 @@ fn read_host_type_schema( next_host_schema_depth(depth)?, )?), }), + 15 => Ok(HostTypeSchema::Option(Box::new(read_host_type_schema( + cursor, + next_host_schema_depth(depth)?, + )?))), + 16 => Ok(HostTypeSchema::Unit), other => Err(WireError::InvalidHostSchemaTag(other)), } } @@ -2142,177 +2157,100 @@ fn next_host_schema_depth(depth: usize) -> Result { )) } +pub(crate) fn decode_type_schema_bytes(bytes: &[u8]) -> Result { + type_schema_codec::decode_type_schema_bytes(bytes).map_err(schema_codec_error) +} + fn write_schema(schema: &TypeSchema, out: &mut Vec) -> Result<(), WireError> { - match schema { - TypeSchema::Unknown => out.push(0), - TypeSchema::Null => out.push(1), - TypeSchema::Int => out.push(2), - TypeSchema::Float => out.push(3), - TypeSchema::Number => out.push(4), - TypeSchema::Bool => out.push(5), - TypeSchema::String => out.push(6), - TypeSchema::Bytes => out.push(7), - TypeSchema::Optional(inner) => { - out.push(16); - write_schema(inner, out)?; - } - TypeSchema::GenericParam(name) => { - out.push(8); - write_string("schema generic", name, out)?; - } - TypeSchema::Named(name, type_args) => { - out.push(9); - write_string("schema name", name, out)?; - write_u32_count("schema type args", type_args.len(), out)?; - for type_arg in type_args { - write_schema(type_arg, out)?; - } - } - TypeSchema::Array(item) => { - out.push(10); - write_schema(item, out)?; - } - TypeSchema::ArrayTuple(items) => { - out.push(11); - write_u32_count("schema tuple items", items.len(), out)?; - for item in items { - write_schema(item, out)?; - } - } - TypeSchema::ArrayTupleRest { prefix, rest } => { - out.push(12); - write_u32_count("schema tuple prefix", prefix.len(), out)?; - for item in prefix { - write_schema(item, out)?; - } - write_schema(rest, out)?; - } - TypeSchema::Map(item) => { - out.push(13); - write_schema(item, out)?; - } - TypeSchema::Object(fields) => { - out.push(14); - let mut entries = fields.iter().collect::>(); - entries.sort_unstable_by(|(lhs, _), (rhs, _)| lhs.cmp(rhs)); - write_u32_count("schema object fields", entries.len(), out)?; - for (name, value) in entries { - write_string("schema object field", name, out)?; - write_schema(value, out)?; - } - } - TypeSchema::Callable { params, result } => { - out.push(15); - write_u32_count("schema callable params", params.len(), out)?; - for param in params { - write_schema(param, out)?; - } - write_schema(result, out)?; - } - TypeSchema::Resource(key) => { - out.push(17); - write_string("schema resource key", key.as_str(), out)?; - } - TypeSchema::Result { ok, err } => { - out.push(18); - write_schema(ok, out)?; - write_schema(err, out)?; - } - } - Ok(()) + type_schema_codec::write_schema(schema, out).map_err(schema_codec_error) } fn read_schema(cursor: &mut Cursor<'_>, depth: usize) -> Result { - if depth >= MAX_SCHEMA_DEPTH { - return Err(WireError::LengthTooLarge("schema nesting depth", depth)); + type_schema_codec::read_schema(&mut InlineSchemaCursor { cursor }, depth) + .map_err(schema_codec_error) +} + +struct InlineSchemaCursor<'a, 'b> { + cursor: &'a mut Cursor<'b>, +} + +impl SchemaReader for InlineSchemaCursor<'_, '_> { + fn read_u8(&mut self) -> Result { + self.cursor.read_u8().map_err(schema_codec_from_wire) } - cursor.debit_count("schema nodes", 1)?; - let nested_depth = depth - .checked_add(1) - .ok_or(WireError::LengthTooLarge("schema nesting depth", depth))?; - match cursor.read_u8()? { - 0 => Ok(TypeSchema::Unknown), - 1 => Ok(TypeSchema::Null), - 2 => Ok(TypeSchema::Int), - 3 => Ok(TypeSchema::Float), - 4 => Ok(TypeSchema::Number), - 5 => Ok(TypeSchema::Bool), - 6 => Ok(TypeSchema::String), - 7 => Ok(TypeSchema::Bytes), - 16 => Ok(TypeSchema::Optional(Box::new(read_schema( - cursor, - nested_depth, - )?))), - 8 => Ok(TypeSchema::GenericParam(cursor.read_string()?)), - 9 => { - let name = cursor.read_string()?; - let count = cursor.read_count("schema type args", 1)?; - let mut type_args = Vec::new(); - reserve_vec(&mut type_args, "schema type args", count)?; - for _ in 0..count { - type_args.push(read_schema(cursor, nested_depth)?); - } - Ok(TypeSchema::Named(name, type_args)) - } - 10 => Ok(TypeSchema::Array(Box::new(read_schema( - cursor, - nested_depth, - )?))), - 11 => { - let count = cursor.read_count("schema tuple items", 1)?; - let mut items = Vec::new(); - reserve_vec(&mut items, "schema tuple items", count)?; - for _ in 0..count { - items.push(read_schema(cursor, nested_depth)?); - } - Ok(TypeSchema::ArrayTuple(items)) - } - 12 => { - let count = cursor.read_count_with_overhead("schema tuple prefix", 1, 1)?; - let mut prefix = Vec::new(); - reserve_vec(&mut prefix, "schema tuple prefix", count)?; - for _ in 0..count { - prefix.push(read_schema(cursor, nested_depth)?); - } - let rest = Box::new(read_schema(cursor, nested_depth)?); - Ok(TypeSchema::ArrayTupleRest { prefix, rest }) - } - 13 => Ok(TypeSchema::Map(Box::new(read_schema( - cursor, - nested_depth, - )?))), - 14 => { - let count = cursor.read_count("schema object fields", 5)?; - let mut fields = HashMap::new(); - reserve_map(&mut fields, "schema object fields", count)?; - for _ in 0..count { - let name = cursor.read_string()?; - let value = read_schema(cursor, nested_depth)?; - fields.insert(name, value); - } - Ok(TypeSchema::Object(fields)) - } - 15 => { - let count = cursor.read_count_with_overhead("schema callable params", 1, 1)?; - let mut params = Vec::new(); - reserve_vec(&mut params, "schema callable params", count)?; - for _ in 0..count { - params.push(read_schema(cursor, nested_depth)?); - } - let result = Box::new(read_schema(cursor, nested_depth)?); - Ok(TypeSchema::Callable { params, result }) - } - 17 => { - let key_text = cursor.read_string()?; - let key = ResourceTypeKey::new(key_text) - .map_err(|err| WireError::InvalidResourceKey(err.to_string()))?; - Ok(TypeSchema::Resource(key)) - } - 18 => Ok(TypeSchema::Result { - ok: Box::new(read_schema(cursor, nested_depth)?), - err: Box::new(read_schema(cursor, nested_depth)?), - }), - other => Err(WireError::InvalidValueType(other)), + + fn read_string(&mut self) -> Result { + self.cursor.read_string().map_err(schema_codec_from_wire) + } + + fn read_count( + &mut self, + field: &'static str, + min_item_bytes: usize, + ) -> Result { + self.cursor + .read_count(field, min_item_bytes) + .map_err(schema_codec_from_wire) + } + + fn read_count_with_overhead( + &mut self, + field: &'static str, + min_item_bytes: usize, + fixed_bytes: usize, + ) -> Result { + self.cursor + .read_count_with_overhead(field, min_item_bytes, fixed_bytes) + .map_err(schema_codec_from_wire) + } + + fn debit_count(&mut self, field: &'static str, count: usize) -> Result<(), SchemaCodecError> { + self.cursor + .debit_count(field, count) + .map_err(schema_codec_from_wire) + } + + fn remaining(&self) -> usize { + self.cursor.remaining() + } +} + +fn schema_codec_error(err: SchemaCodecError) -> WireError { + match err { + SchemaCodecError::UnexpectedEof => WireError::UnexpectedEof, + SchemaCodecError::InvalidValueType(tag) => WireError::InvalidValueType(tag), + SchemaCodecError::InvalidResourceKey(reason) => WireError::InvalidResourceKey(reason), + SchemaCodecError::InvalidUtf8 => WireError::InvalidUtf8, + SchemaCodecError::LengthTooLarge(field, len) => WireError::LengthTooLarge(field, len), + SchemaCodecError::TrailingBytes => WireError::TrailingBytes, + } +} + +fn schema_codec_from_wire(err: WireError) -> SchemaCodecError { + match err { + WireError::UnexpectedEof => SchemaCodecError::UnexpectedEof, + WireError::InvalidUtf8 => SchemaCodecError::InvalidUtf8, + WireError::LengthTooLarge(field, len) => SchemaCodecError::LengthTooLarge(field, len), + WireError::InvalidValueType(tag) => SchemaCodecError::InvalidValueType(tag), + WireError::InvalidResourceKey(reason) => SchemaCodecError::InvalidResourceKey(reason), + WireError::TrailingBytes => SchemaCodecError::TrailingBytes, + WireError::InvalidMagic(_) + | WireError::UnsupportedVersion(_) + | WireError::UnsupportedFlags(_) + | WireError::InvalidConstantTag(_) + | WireError::InvalidBool(_) + | WireError::InvalidTypeMapFlag(_) + | WireError::InvalidDebugFlag(_) + | WireError::InvalidCaptureBindingMode(_) + | WireError::InvalidHostSchemaTag(_) + | WireError::InvalidHostParamPassing(_) + | WireError::InvalidHostResourceKey + | WireError::HostSchemaImportMismatch + | WireError::InvalidHostSchemaComplexity(_) + | WireError::StringTooLong(_) + | WireError::CodeTooLong(_) + | WireError::UnsupportedConstantType(_) + | WireError::InvalidRetCleanup(_) => SchemaCodecError::UnexpectedEof, } } @@ -2989,4 +2927,16 @@ mod ret_cleanup_wire_tests { Err(WireError::InvalidRetCleanup("named cleanup schema cycle")) ); } + + #[test] + fn schema_wire_decode_matches_the_pure_codec() { + let schema = TypeSchema::Option(Box::new(TypeSchema::Unit)); + let encoded = crate::compiler::type_schema_codec::encode_type_schema_bytes(&schema) + .expect("pure codec should encode"); + assert_eq!(encoded, vec![19, 20]); + assert_eq!( + decode_type_schema_bytes(&encoded).expect("vmbc wire decode should decode"), + schema + ); + } } diff --git a/stdlib/rss/iter.rss b/stdlib/rss/iter.rss index e04458d3..47f11229 100644 --- a/stdlib/rss/iter.rss +++ b/stdlib/rss/iter.rss @@ -154,9 +154,9 @@ pub fn reduce(values: [T], init: U, func: fn(U, T) -> U) -> U { } // Calls a function for each value in an array and returns null. -pub fn each(values: [T], func: fn(T) -> U) -> null { +pub fn each(values: [T], func: fn(T) -> U) -> () { for index in 0..values.length { let each_result = func(values[index]); } - null + () } diff --git a/stdlib/rss/lrucache.rss b/stdlib/rss/lrucache.rss index 1926781c..711f9883 100644 --- a/stdlib/rss/lrucache.rss +++ b/stdlib/rss/lrucache.rss @@ -4,8 +4,8 @@ // - `lookup`: user key -> node id // - `nodes`: node id -> LruNode // -// Node ids start at 1. The cache uses `0` as the internal "no link" sentinel, -// because storing `null` into a map entry now deletes that entry. +// Node ids start at 1. The cache uses `0` as the internal "no link" sentinel. +// Missing map entries are removed explicitly; absent result payloads are Option::None. struct LruNode { key: K, value: V, @@ -31,19 +31,19 @@ struct LruCacheState { struct LruPeekResult { found: bool, - value: V + value: Option } struct LruGetResult { cache: LruCacheState, found: bool, - value: V + value: Option } struct LruEntryReport { found: bool, - key: K, - value: V + key: Option, + value: Option } struct LruEntryRow { @@ -188,8 +188,8 @@ fn evict_oldest( let evicted_key = head_node.key.copy(); let mut next_lookup = lookup; let mut next_nodes = nodes; - next_lookup[evicted_key] = null; - next_nodes[head] = null; + next_lookup = next_lookup.remove(evicted_key); + next_nodes = next_nodes.remove(head); let next_tail = if tail == head => { 0 @@ -219,9 +219,9 @@ fn evict_oldest( fn entry_report(cache: LruCacheState, id: int) -> LruEntryReport { let out: LruEntryReport = if id != 0 => { let node: LruNode =&((&cache).nodes[id]); - {found: true, key: node.key.copy(), value: node.value.copy()} + {found: true, key: Option::Some(node.key.copy()), value: Option::Some(node.value.copy())} } else => { - {found: false, key: null, value: null} + {found: false, key: Option::None, value: Option::None} }; out } @@ -263,9 +263,9 @@ pub fn peek(cache: LruCacheState, key: K) -> LruPeekResult { let out: LruPeekResult = if lookup.has(key) => { let id =(&lookup)[key]; let node: LruNode =&((&state.nodes)[id]); - {found: true, value: node.value.copy()} + {found: true, value: Option::Some(node.value.copy())} } else => { - {found: false, value: null} + {found: false, value: Option::None} }; out } @@ -308,12 +308,12 @@ pub fn get(cache: LruCacheState, key: K) -> LruGetResult { relinked.nodes ) }; - {cache: next_cache, found: true, value: value} + {cache: next_cache, found: true, value: Option::Some(value)} } else => { { cache: make_cache::(limit, size, next_id, head, tail, lookup, nodes), found: false, - value: null + value: Option::None } }; out @@ -452,10 +452,10 @@ pub fn remove(cache: LruCacheState, key: K) -> LruCacheState { let id =(&lookup)[key]; let node: LruNode =(&nodes)[id]; let mut next_lookup = lookup; - next_lookup[key] = null; + next_lookup = next_lookup.remove(key); let unlinked: LinkState = unlink_node::(nodes, head, tail, id, node); let mut next_nodes = unlinked.nodes; - next_nodes[id] = null; + next_nodes = next_nodes.remove(id); make_cache::( limit, size - 1, diff --git a/stdlib/rss/parse.rss b/stdlib/rss/parse.rss index efd5dfda..cb4ff9c2 100644 --- a/stdlib/rss/parse.rss +++ b/stdlib/rss/parse.rss @@ -1,26 +1,26 @@ -// Parsing helpers implemented in pure RustScript. +// Parsing helpers implemented in pure RustScript. fn is_whitespace(ch: string) -> bool { - match ch { - " " => true, - "\n" => true, - "\r" => true, - "\t" => true, - _ => false, - } -} - + match ch { + " " => true, + "\n" => true, + "\r" => true, + "\t" => true, + _ => false, + } +} + fn trim(value: string) -> string { - let value_len =(&value).length; - let mut start = 0; - while start < value_len { - if is_whitespace((&value)[start:(start + 1)]) { - start = start + 1; - } else { - break; - } - } - - let mut end = value_len; + let value_len =(&value).length; + let mut start = 0; + while start < value_len { + if is_whitespace((&value)[start:(start + 1)]) { + start = start + 1; + } else { + break; + } + } + + let mut end = value_len; while end > start { if is_whitespace((&value)[(end - 1):end]) { end = end - 1; @@ -30,184 +30,178 @@ fn trim(value: string) -> string { } (&value)[start:end] } - + fn digit_value(ch: string) -> int { - match ch { - "0" => 0, - "1" => 1, - "2" => 2, - "3" => 3, - "4" => 4, - "5" => 5, - "6" => 6, - "7" => 7, - "8" => 8, - "9" => 9, - "a" => 10, - "A" => 10, - "b" => 11, - "B" => 11, - "c" => 12, - "C" => 12, - "d" => 13, - "D" => 13, - "e" => 14, - "E" => 14, - "f" => 15, - "F" => 15, - "g" => 16, - "G" => 16, - "h" => 17, - "H" => 17, - "i" => 18, - "I" => 18, - "j" => 19, - "J" => 19, - "k" => 20, - "K" => 20, - "l" => 21, - "L" => 21, - "m" => 22, - "M" => 22, - "n" => 23, - "N" => 23, - "o" => 24, - "O" => 24, - "p" => 25, - "P" => 25, - "q" => 26, - "Q" => 26, - "r" => 27, - "R" => 27, - "s" => 28, - "S" => 28, - "t" => 29, - "T" => 29, - "u" => 30, - "U" => 30, - "v" => 31, - "V" => 31, - "w" => 32, - "W" => 32, - "x" => 33, - "X" => 33, - "y" => 34, - "Y" => 34, - "z" => 35, - "Z" => 35, - _ =>-1, - } -} - + match ch { + "0" => 0, + "1" => 1, + "2" => 2, + "3" => 3, + "4" => 4, + "5" => 5, + "6" => 6, + "7" => 7, + "8" => 8, + "9" => 9, + "a" => 10, + "A" => 10, + "b" => 11, + "B" => 11, + "c" => 12, + "C" => 12, + "d" => 13, + "D" => 13, + "e" => 14, + "E" => 14, + "f" => 15, + "F" => 15, + "g" => 16, + "G" => 16, + "h" => 17, + "H" => 17, + "i" => 18, + "I" => 18, + "j" => 19, + "J" => 19, + "k" => 20, + "K" => 20, + "l" => 21, + "L" => 21, + "m" => 22, + "M" => 22, + "n" => 23, + "N" => 23, + "o" => 24, + "O" => 24, + "p" => 25, + "P" => 25, + "q" => 26, + "Q" => 26, + "r" => 27, + "R" => 27, + "s" => 28, + "S" => 28, + "t" => 29, + "T" => 29, + "u" => 30, + "U" => 30, + "v" => 31, + "V" => 31, + "w" => 32, + "W" => 32, + "x" => 33, + "X" => 33, + "y" => 34, + "Y" => 34, + "z" => 35, + "Z" => 35, + _ =>-1, + } +} + fn is_valid_base(base: int) -> bool { - base >= 2 && base <= 36 -} - -// Returns whether a string is a valid integer literal for the given base. + base >= 2 && base <= 36 +} + +// Returns whether a string is a valid integer literal for the given base. pub fn is_int_literal_base(value: string, base: int) -> bool { - let text = trim(value); - let text_len =(&text).length; - let mut out = true; - let mut index = 0; - if is_valid_base(base) == false || text_len == 0 { - out = false; - } else { - let first =(&text)[0:1]; - if first == "+" || first == "-" { - index = 1; - } - if index == text_len { - out = false; - } else { - while index < text_len { - let digit = digit_value((&text)[index:(index + 1)]); - if digit < 0 || digit >= base { - out = false; - break; - } - index = index + 1; - } + let text = trim(value); + let text_len =(&text).length; + let mut out = true; + let mut index = 0; + if is_valid_base(base) == false || text_len == 0 { + out = false; + } else { + let first =(&text)[0:1]; + if first == "+" || first == "-" { + index = 1; + } + if index == text_len { + out = false; + } else { + while index < text_len { + let digit = digit_value((&text)[index:(index + 1)]); + if digit < 0 || digit >= base { + out = false; + break; + } + index = index + 1; + } } } out } - -// Returns whether a string is a base-10 integer literal. + +// Returns whether a string is a base-10 integer literal. pub fn is_int_literal(value: string) -> bool { - is_int_literal_base(value, 10) -} - -// Parses an integer literal for the given base and returns null on failure. -pub fn try_parse_int_base(value: string, base: int) -> int? { + is_int_literal_base(value, 10) +} + +// Parses an integer literal for the given base and returns null on failure. +pub fn try_parse_int_base(value: string, base: int) -> Option { let text = trim(value); - let mut out: int? = null; - if is_int_literal_base(text, base) { - let mut sign = 1; - let mut index = 0; - let mut parsed = 0; - let first =(&text)[0:1]; - if first == "-" { - sign =-1; - index = 1; - } else if first == "+" { - index = 1; - } + let mut out: Option = Option::None; + if is_int_literal_base(text, base) { + let mut sign = 1; + let mut index = 0; + let mut parsed = 0; + let first =(&text)[0:1]; + if first == "-" { + sign =-1; + index = 1; + } else if first == "+" { + index = 1; + } while index <(&text).length { parsed = parsed * base + digit_value((&text)[index:(index + 1)]); index = index + 1; } - out = parsed * sign; + out = Option::Some(parsed * sign); } out } - -// Parses a base-10 integer literal and returns null on failure. -pub fn try_parse_int(value: string) -> int? { - try_parse_int_base(value, 10) -} - -// Parses an integer literal for the given base or returns a fallback. + +// Parses a base-10 integer literal and returns null on failure. +pub fn try_parse_int(value: string) -> Option { + try_parse_int_base(value, 10) +} + +// Parses an integer literal for the given base or returns a fallback. pub fn parse_int_base_or(value: string, base: int, fallback: int) -> int { - let out = try_parse_int_base(value, base); - let mut parsed = fallback; - if type(out) == "int" { - parsed = out; + match try_parse_int_base(value, base) { + Option::Some(value) => value, + Option::None => fallback, } - parsed } - -// Parses a base-10 integer literal or returns a fallback. + +// Parses a base-10 integer literal or returns a fallback. pub fn parse_int_or(value: string, fallback: int) -> int { - let out = try_parse_int(value); - let mut parsed = fallback; - if type(out) == "int" { - parsed = out; + match try_parse_int(value) { + Option::Some(value) => value, + Option::None => fallback, } - parsed } - -// Returns whether a string is a boolean literal. + +// Returns whether a string is a boolean literal. pub fn is_bool_literal(value: string) -> bool { - let text = trim(value); - text == "true" || text == "false" -} - -// Parses a boolean literal and returns null on failure. -pub fn try_parse_bool(value: string) -> bool? { - let text = trim(value); - match text { - "true" => true, - "false" => false, - _ => null, - } -} - -// Parses a boolean literal or returns a fallback. + let text = trim(value); + text == "true" || text == "false" +} + +// Parses a boolean literal and returns null on failure. +pub fn try_parse_bool(value: string) -> Option { + let text = trim(value); + match text { + "true" => Option::Some(true), + "false" => Option::Some(false), + _ => Option::None, + } +} + +// Parses a boolean literal or returns a fallback. pub fn parse_bool_or(value: string, fallback: bool) -> bool { - let out = try_parse_bool(value); - let mut parsed = fallback; - if type(out) == "bool" { - parsed = out; + match try_parse_bool(value) { + Option::Some(value) => value, + Option::None => fallback, } - parsed } diff --git a/stdlib/tests/collections.rss b/stdlib/tests/collections.rss index 84953da4..4f11a938 100644 --- a/stdlib/tests/collections.rss +++ b/stdlib/tests/collections.rss @@ -30,14 +30,15 @@ assert(has::(merged, "missing") == false); assert(get_or::(merged, "y", 0) == 20); assert(get_or::(merged, "missing", 9) == 9); -let mut sparse = {keep: 1, drop: null}; +let mut sparse: map = {keep: 1, drop: 2}; +sparse = sparse.remove("drop"); assert(has::(sparse, "keep")); assert(has::(sparse, "drop") == false); -sparse.keep = null; +sparse = sparse.remove("keep"); assert(has::(sparse, "keep") == false); -let mut pruned = {x: 1, y: 2}; -pruned.y = null; +let mut pruned: map = {x: 1, y: 2}; +pruned = pruned.remove("y"); let mut merged_without = merge::(pruned, {z: 3}); assert(has::(merged_without, "x")); assert(has::(merged_without, "y") == false); diff --git a/stdlib/tests/iter.rss b/stdlib/tests/iter.rss index 29fc79e5..d46c9c97 100644 --- a/stdlib/tests/iter.rss +++ b/stdlib/tests/iter.rss @@ -72,4 +72,4 @@ let mut total = reduce::([1, 2, 3, 4], 0, |acc, value| acc + value); assert(total == 10); let mut each_result = each::([7, 8], |value| value + 1); -assert(type(each_result) == "null"); +assert(each_result == ()); diff --git a/stdlib/tests/lrucache.rss b/stdlib/tests/lrucache.rss index eb553724..dec49e7b 100644 --- a/stdlib/tests/lrucache.rss +++ b/stdlib/tests/lrucache.rss @@ -20,7 +20,7 @@ assert(initial_keys[1] == "b"); let mut peeked = peek::(cache, "a"); assert(peeked.found); -let peeked_value: int = peeked.value; +let peeked_value: int = peeked.value.unwrap_or(0); assert(peeked_value == 1); let mut still_initial = keys::(cache); assert(still_initial[0] == "a"); @@ -28,7 +28,7 @@ assert(still_initial[1] == "b"); let mut accessed = get::(cache, "a"); assert(accessed.found); -let accessed_value: int = accessed.value; +let accessed_value: int = accessed.value.unwrap_or(0); assert(accessed_value == 1); cache = accessed.cache; let mut after_get = keys::(cache); @@ -37,13 +37,13 @@ assert(after_get[1] == "a"); let mut lru_entry = oldest::(cache); assert(lru_entry.found); -assert(lru_entry.key == "b"); -assert(lru_entry.value == 2); +assert(lru_entry.key.unwrap_or("") == "b"); +assert(lru_entry.value.unwrap_or(0) == 2); let mut mru_entry = newest::(cache); assert(mru_entry.found); -assert(mru_entry.key == "a"); -assert(mru_entry.value == 1); +assert(mru_entry.key.unwrap_or("") == "a"); +assert(mru_entry.value.unwrap_or(0) == 1); cache = put::(cache, "c", 3); let mut after_evict = keys::(cache); @@ -64,7 +64,7 @@ assert(after_update[0] == "c"); assert(after_update[1] == "a"); let mut updated = peek::(cache, "a"); assert(updated.found); -assert(updated.value == 10); +assert(updated.value.unwrap_or(0) == 10); let mut missed = get::(cache, "missing"); assert(missed.found == false); @@ -115,13 +115,13 @@ feed_cache = put::(feed_cache, 8, {slug: "beta", score: 20}); let mut cached_feed = peek::(feed_cache, 7); assert(cached_feed.found); -let cached_feed_value = cached_feed.value.copy(); +let cached_feed_value = cached_feed.value.unwrap_or({slug: "", score: 0}).copy(); assert(cached_feed_value.slug == "alpha"); assert(cached_feed_value.score == 10); let mut accessed_feed = get::(feed_cache, 7); assert(accessed_feed.found); -let accessed_feed_value = accessed_feed.value.copy(); +let accessed_feed_value = accessed_feed.value.unwrap_or({slug: "", score: 0}).copy(); assert(accessed_feed_value.slug == "alpha"); feed_cache = accessed_feed.cache; @@ -139,12 +139,12 @@ assert(second_feed_row.value.score == 10); let mut feed_oldest = oldest::(feed_cache); assert(feed_oldest.found); -assert(feed_oldest.key == 8); -let feed_oldest_value = feed_oldest.value.copy(); +assert(feed_oldest.key.unwrap_or(0) == 8); +let feed_oldest_value = feed_oldest.value.unwrap_or({slug: "", score: 0}).copy(); assert(feed_oldest_value.slug == "beta"); let mut feed_newest = newest::(feed_cache); assert(feed_newest.found); -assert(feed_newest.key == 7); -let feed_newest_value = feed_newest.value.copy(); +assert(feed_newest.key.unwrap_or(0) == 7); +let feed_newest_value = feed_newest.value.unwrap_or({slug: "", score: 0}).copy(); assert(feed_newest_value.score == 10); diff --git a/stdlib/tests/parse.rss b/stdlib/tests/parse.rss index 67c41498..97e9efb2 100644 --- a/stdlib/tests/parse.rss +++ b/stdlib/tests/parse.rss @@ -1,21 +1,11 @@ use super::rss::parse::{is_bool_literal, is_int_literal, is_int_literal_base, parse_bool_or, parse_int_base_or, parse_int_or, try_parse_bool, try_parse_int, try_parse_int_base}; -fn assert_some_int(actual: int?, expected: int) -> bool { - let out = if type(actual) == "int" => { - actual == expected - } else => { - false - }; - out +fn assert_some_int(actual: Option, expected: int) -> bool { + actual.unwrap_or(expected + 1) == expected } -fn assert_some_bool(actual: bool?, expected: bool) -> bool { - let out = if type(actual) == "bool" => { - actual == expected - } else => { - false - }; - out +fn assert_some_bool(actual: Option, expected: bool) -> bool { + actual.unwrap_or(expected == false) == expected } assert(is_int_literal("0")); @@ -31,7 +21,7 @@ assert(assert_some_int(try_parse_int("0"), 0)); assert(assert_some_int(try_parse_int(" 42 "), 42)); assert(assert_some_int(try_parse_int("-17"),-17)); assert(assert_some_int(try_parse_int("+9"), 9)); -assert(type(try_parse_int("nope")) == "null"); +assert(try_parse_int("nope").unwrap_or(1) == 1); assert(parse_int_or("nope", 7) == 7); assert(is_int_literal_base("1011", 2)); @@ -45,7 +35,7 @@ assert(assert_some_int(try_parse_int_base("1011", 2), 11)); assert(assert_some_int(try_parse_int_base("7f", 16), 127)); assert(assert_some_int(try_parse_int_base("zz", 36), 1295)); assert(assert_some_int(try_parse_int_base(" -1A ", 16),-26)); -assert(type(try_parse_int_base("19", 8)) == "null"); +assert(try_parse_int_base("19", 8).unwrap_or(1) == 1); assert(parse_int_base_or("19", 8, 99) == 99); assert(is_bool_literal("true")); @@ -53,6 +43,6 @@ assert(is_bool_literal(" false ")); assert(is_bool_literal("TRUE") == false); assert(assert_some_bool(try_parse_bool("true"), true)); assert(assert_some_bool(try_parse_bool(" false "), false)); -assert(type(try_parse_bool("yes")) == "null"); +assert(try_parse_bool("yes").unwrap_or(false) == false); assert(parse_bool_or("yes", true)); assert(parse_bool_or("yes", false) == false); diff --git a/stdlib/tests/re.rss b/stdlib/tests/re.rss index 283650bd..09939543 100644 --- a/stdlib/tests/re.rss +++ b/stdlib/tests/re.rss @@ -1,12 +1,7 @@ use re; -fn assert_some_string(actual: string?, expected: string) -> bool { - let out = if type(actual) == "string" => { - actual == expected - } else => { - false - }; - out +fn assert_some_string(actual: Option, expected: string) -> bool { + actual.unwrap_or("") == expected } assert(re::match("^foo", "foobar")); @@ -16,46 +11,54 @@ assert(re::match("(?i)^foo$", "FoO")); assert(re::match("(?is)^foo.bar$", "FoO\nbar")); assert(re::match("(?m)^bar$", "foo\nbar\nbaz")); -let mut first = re::find("bar+", "foobarr"); +let first = re::find("bar+", "foobarr"); assert(assert_some_string(first, "barr")); -let mut first_icase = re::find("(?i)^foo", "FoO bar"); +let first_icase = re::find("(?i)^foo", "FoO bar"); assert(assert_some_string(first_icase, "FoO")); -let mut missing = re::find("z+", "foobarr"); -assert(type(missing) == "null"); +let missing = re::find("z+", "foobarr"); +let missing_is_none = match missing { + Option::Some(_) => false, + Option::None => true, +}; +assert(missing_is_none); -let mut replaced = re::replace("(foo)+", "foofoo bar", "x"); +let replaced = re::replace("(foo)+", "foofoo bar", "x"); assert(replaced == "x bar"); -let mut replaced_icase = re::replace("(?i)foo", "FoO bar", "x"); +let replaced_icase = re::replace("(?i)foo", "FoO bar", "x"); assert(replaced_icase == "x bar"); -let mut parts = re::split(" +", "a b c"); +let parts = re::split(" +", "a b c"); assert(parts.length == 3); assert(parts[0] == "a"); assert(parts[1] == "b"); assert(parts[2] == "c"); -let mut parts_icase = re::split("(?i)x", "aXb"); +let parts_icase = re::split("(?i)x", "aXb"); assert(parts_icase.length == 2); assert(parts_icase[0] == "a"); assert(parts_icase[1] == "b"); -let mut groups = re::captures("([a-z]+)-([0-9]+)", "item-42"); +let groups = re::captures("([a-z]+)-([0-9]+)", "item-42"); assert(groups.length == 3); -assert(groups[0] == "item-42"); -assert(groups[1] == "item"); -assert(groups[2] == "42"); +assert(groups[0].unwrap_or("") == "item-42"); +assert(groups[1].unwrap_or("") == "item"); +assert(groups[2].unwrap_or("") == "42"); -let mut groups_icase = re::captures("(?i)^(foo)-([0-9]+)$", "FoO-42"); +let groups_icase = re::captures("(?i)^(foo)-([0-9]+)$", "FoO-42"); assert(groups_icase.length == 3); -assert(groups_icase[1] == "FoO"); -assert(groups_icase[2] == "42"); +assert(groups_icase[1].unwrap_or("") == "FoO"); +assert(groups_icase[2].unwrap_or("") == "42"); -let mut no_groups = re::captures("(foo)", "bar"); +let no_groups = re::captures("(foo)", "bar"); assert(no_groups.length == 0); -let mut optional = re::captures("(a)?b", "b"); +let optional = re::captures("(a)?b", "b"); assert(optional.length == 2); -assert(type(optional[1]) == "null"); +let unmatched_is_none = match optional[1] { + Option::Some(_) => false, + Option::None => true, +}; +assert(unmatched_is_none); diff --git a/tests/builtins/sqlite_scope_lifecycle_tests.rs b/tests/builtins/sqlite_scope_lifecycle_tests.rs index aaa6dc66..3bf2bdc8 100644 --- a/tests/builtins/sqlite_scope_lifecycle_tests.rs +++ b/tests/builtins/sqlite_scope_lifecycle_tests.rs @@ -132,7 +132,14 @@ fn start_long_sqlite_query(vm: &mut Vm) { /// Helper: run a SQLite source to completion. Scripts use `assert(...)` for /// value checks; a failed assert surfaces as a host error. fn run_sqlite_source(policy: vm::SqlitePolicy, source: &str) -> Result<(), VmError> { - let wrapped = format!("use sqlite;\n{source}"); + let wrapped = format!( + "use sqlite;\n\ + fn no_int() -> Option {{ Option::None }}\n\ + fn no_float() -> Option {{ Option::None }}\n\ + fn no_string() -> Option {{ Option::None }}\n\ + fn no_bytes() -> Option {{ Option::None }}\n\ + {source}" + ); let catalog = sqlite_host_catalog(); let compiled = compile_source_with_flavor_and_options( &wrapped, @@ -206,7 +213,7 @@ fn policy_for(root: &Path) -> vm::SqlitePolicy { #[test] fn sqlite_async_hosts_require_an_async_bridge() { let compiled = compile_source( - "use sqlite;\nlet db = sqlite::open({ path: \":memory:\", mode: \"memory\", limits: {} });", + "use sqlite;\nlet db = sqlite::open({ path: Option::Some(\":memory:\"), mode: Option::Some(\"memory\"), limits: Option::Some({}) });", ) .expect("source should compile"); let mut vm = Vm::new(compiled.program); @@ -229,23 +236,33 @@ fn sqlite_round_trip_supports_typed_values_and_ordered_transactions() { policy, r#" use bytes; - let db = sqlite::open({ path: "state.db", mode: "read_write_create", limits: { max_rows: 128, max_result_bytes: 65536, max_statements: 16, max_transaction_ms: 5000 } }); - sqlite::execute(&db, "CREATE TABLE values_table (id INTEGER PRIMARY KEY, n INTEGER, r REAL, s TEXT, b BLOB, z TEXT)", []); + let db_limits: SqliteLimits = { max_rows: Option::Some(128), max_result_bytes: Option::Some(65536), max_statements: Option::Some(16), max_transaction_ms: Option::Some(5000) }; + let db_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db_limits) }; + let db = sqlite::open(db_options); + let execute_params1: [SqliteValue] = []; + sqlite::execute(&db, "CREATE TABLE values_table (id INTEGER PRIMARY KEY, n INTEGER, r REAL, s TEXT, b BLOB, z TEXT)", execute_params1); let blob_payload = bytes::from_hex("000102"); - let ins = sqlite::execute(&db, "INSERT INTO values_table (n, r, s, b, z) VALUES (?1, ?2, ?3, ?4, ?5)", [ - { kind: "int", int_value: 7, float_value: null, text_value: null, blob_value: null }, - { kind: "float", int_value: null, float_value: 1.5, text_value: null, blob_value: null }, - { kind: "text", int_value: null, float_value: null, text_value: "hello", blob_value: null }, - { kind: "blob", int_value: null, float_value: null, text_value: null, blob_value: blob_payload }, - { kind: "null", int_value: null, float_value: null, text_value: null, blob_value: null } - ]); + let execute_params2: [SqliteValue] = [ + { kind: "int", int_value: Option::Some(7), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() }, + { kind: "float", int_value: no_int(), float_value: Option::Some(1.5), text_value: no_string(), blob_value: no_bytes() }, + { kind: "text", int_value: no_int(), float_value: no_float(), text_value: Option::Some("hello"), blob_value: no_bytes() }, + { kind: "blob", int_value: no_int(), float_value: no_float(), text_value: no_string(), blob_value: Option::Some(blob_payload) }, + { kind: "null", int_value: no_int(), float_value: no_float(), text_value: no_string(), blob_value: no_bytes() } + ]; + let ins = sqlite::execute(&db, "INSERT INTO values_table (n, r, s, b, z) VALUES (?1, ?2, ?3, ?4, ?5)", execute_params2); let affected = ins.rows_affected; assert(affected == 1); - let rowset = sqlite::query(&db, "SELECT n, r, s, b, z FROM values_table ORDER BY id", [], { max_rows: 8, max_result_bytes: 65536 }); + let query_params3: [SqliteValue] = []; + let query_limits4: SqliteLimits = { max_rows: Option::Some(8), max_result_bytes: Option::Some(65536) }; + let rowset = sqlite::query(&db, "SELECT n, r, s, b, z FROM values_table ORDER BY id", query_params3, query_limits4); let truncated = rowset.truncated; let next_cursor = rowset.next_cursor; assert(truncated == false); - assert(next_cursor == 7); + let cursor_value = match next_cursor { + Option::Some(value) => value, + Option::None => -1, + }; + assert(cursor_value == 7); let columns = rowset.columns; assert(columns == {"n", "r", "s", "b", "z"}); let row = rowset.rows[0]; @@ -254,55 +271,82 @@ fn sqlite_round_trip_supports_typed_values_and_ordered_transactions() { let int_kind = int_cell.kind; let int_value = int_cell.int_value; assert(int_kind == "int"); - assert(int_value == 7); + assert(int_value.unwrap_or(-1) == 7); let float_cell = cells[1]; let float_kind = float_cell.kind; let float_value = float_cell.float_value; assert(float_kind == "float"); - assert(float_value == 1.5); + assert(float_value.unwrap_or(-1.0) == 1.5); let text_cell = cells[2]; let text_kind = text_cell.kind; let text_value = text_cell.text_value; assert(text_kind == "text"); - assert(text_value == "hello"); + assert(text_value.unwrap_or("") == "hello"); let blob_cell = cells[3]; let blob_kind = blob_cell.kind; let blob_value = blob_cell.blob_value; assert(blob_kind == "blob"); - assert(blob_value == blob_payload); + let blob_present = match blob_value { + Option::Some(value) => value, + Option::None => blob_payload.copy(), + }; + assert(blob_present == blob_payload); let null_cell = cells[4]; let null_kind = null_cell.kind; let null_int = null_cell.int_value; assert(null_kind == "null"); - assert(null_int == null); + let null_int_absent = match null_int { + Option::Some(_) => false, + Option::None => true, + }; + assert(null_int_absent); - let results = sqlite::transaction(&db, { - { sql: "INSERT INTO values_table (n) VALUES (?1)", params: [{ kind: "int", int_value: 8 }] }, - { sql: "INSERT INTO values_table (n) VALUES (?1)", params: [{ kind: "int", int_value: 9 }] }, - { sql: "SELECT n FROM values_table ORDER BY id", query: true, limits: { max_rows: 8, max_result_bytes: 65536 } } - }); + let statements5: [SqliteStatement] = { + { sql: "INSERT INTO values_table (n) VALUES (?1)", params: Option::Some([{ kind: "int", int_value: Option::Some(8) }]) }, + { sql: "INSERT INTO values_table (n) VALUES (?1)", params: Option::Some([{ kind: "int", int_value: Option::Some(9) }]) }, + { sql: "SELECT n FROM values_table ORDER BY id", query: Option::Some(true), limits: Option::Some({ max_rows: Option::Some(8), max_result_bytes: Option::Some(65536) }) } + }; + let results = sqlite::transaction(&db, statements5); assert(type(results) == "array"); let first_result = results[0]; let first_kind = first_result.kind; assert(first_kind == "execute"); - assert(first_result.execute.rows_affected == 1); - assert(first_result.query == null); + let first_affected = match first_result.execute { + Option::Some(value) => value.rows_affected, + Option::None => -1, + }; + assert(first_affected == 1); + let first_query_absent = match first_result.query { + Option::Some(value) => false, + Option::None => true, + }; + assert(first_query_absent); let second_result = results[1]; let second_kind = second_result.kind; assert(second_kind == "execute"); let third_result = results[2]; let third_kind = third_result.kind; assert(third_kind == "query"); - assert(third_result.execute == null); - assert(third_result.query.rows[0].cells[0].int_value == 7); - let count = sqlite::query(&db, "SELECT count(*) AS count FROM values_table", [], { max_rows: 8, max_result_bytes: 65536 }); + let third_execute_absent = match third_result.execute { + Option::Some(value) => false, + Option::None => true, + }; + assert(third_execute_absent); + let third_query_value = match third_result.query { + Option::Some(value) => value.rows[0].cells[0].int_value.unwrap_or(-1), + Option::None => -1, + }; + assert(third_query_value == 7); + let query_params6: [SqliteValue] = []; + let query_limits7: SqliteLimits = { max_rows: Option::Some(8), max_result_bytes: Option::Some(65536) }; + let count = sqlite::query(&db, "SELECT count(*) AS count FROM values_table", query_params6, query_limits7); let count_row = count.rows[0]; let count_cells = count_row.cells; let count_cell = count_cells[0]; let count_kind = count_cell.kind; let count_value = count_cell.int_value; assert(count_kind == "int"); - assert(count_value == 3); + assert(count_value.unwrap_or(-1) == 3); sqlite::close(db); "#, ) @@ -318,13 +362,16 @@ fn sqlite_value_discriminators_reject_unknown_missing_and_multiple_payloads() { (r#"{ kind: "unknown" }"#, "unknown SQLite value kind"), (r#"{ kind: "int" }"#, "missing SQLite int_value"), ( - r#"{ kind: "int", int_value: 1, text_value: "extra" }"#, + r#"{ kind: "int", int_value: Option::Some(1), text_value: Option::Some("extra") }"#, "multiple non-null payloads", ), ] { let source = format!( - r#"let db = sqlite::open({{ path: "state.db", mode: "read_write_create", limits: {{}} }}); - sqlite::execute(&db, "SELECT ?", [{value}]);"# + r#"let db_limits: SqliteLimits = {{}}; + let db_options: SqliteOpenOptions = {{ path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db_limits) }}; + let db = sqlite::open(db_options); + let execute_params8: [SqliteValue] = [{value}]; + sqlite::execute(&db, "SELECT ?", execute_params8);"# ); let error = run_sqlite_host_error(policy.clone(), &source); assert!( @@ -337,26 +384,34 @@ fn sqlite_value_discriminators_reject_unknown_missing_and_multiple_payloads() { #[test] fn sqlite_value_mismatched_payload_and_limits_are_rejected() { - let mismatch_root = temporary_root("value-mismatch"); - let mismatch = run_sqlite_builtin_host_error( - policy_for(&mismatch_root), - r#" - let db = sqlite::open({ path: "state.db", mode: "read_write_create", limits: {} }); - sqlite::execute(db, "SELECT ?", [{ kind: "int", int_value: "wrong" }]); - "#, - ); + let mismatch_source = r#"use sqlite; + let db_limits: SqliteLimits = {}; + let db_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db_limits) }; + let db = sqlite::open(db_options); + let execute_params9: [SqliteValue] = [{ kind: "int", int_value: Option::Some("wrong") }]; + sqlite::execute(db, "SELECT ?", execute_params9);"#; + let mismatch = match compile_source_with_flavor_and_options( + mismatch_source, + vm::compiler::SourceFlavor::RustScript, + CompileSourceFileOptions::default().with_host_api_catalog(sqlite_host_catalog()), + ) { + Ok(_) => panic!("mismatched payload type must be rejected at compile time"), + Err(error) => error.to_string(), + }; assert!( - mismatch.contains("SQLite int_value payload"), + mismatch.contains("int") && mismatch.contains("string"), "mismatched payload type must be rejected, got: {mismatch}" ); - fs::remove_dir_all(mismatch_root).expect("temporary mismatch root should be removed"); let count_root = temporary_root("parameter-count-limit"); let count = run_sqlite_host_error( policy_for(&count_root), r#" - let db = sqlite::open({ path: "state.db", mode: "read_write_create", limits: { max_parameters: 1 } }); - sqlite::execute(&db, "SELECT ?1, ?2", [{ kind: "int", int_value: 1 }, { kind: "int", int_value: 2 }]); + let db_limits: SqliteLimits = { max_parameters: Option::Some(1) }; + let db_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db_limits) }; + let db = sqlite::open(db_options); + let execute_params10: [SqliteValue] = [{ kind: "int", int_value: Option::Some(1) }, { kind: "int", int_value: Option::Some(2) }]; + sqlite::execute(&db, "SELECT ?1, ?2", execute_params10); "#, ); assert!( @@ -369,8 +424,11 @@ fn sqlite_value_mismatched_payload_and_limits_are_rejected() { let bytes = run_sqlite_host_error( policy_for(&bytes_root), r#" - let db = sqlite::open({ path: "state.db", mode: "read_write_create", limits: { max_parameter_bytes: 2 } }); - sqlite::execute(&db, "SELECT ?1", [{ kind: "text", text_value: "abc" }]); + let db_limits: SqliteLimits = { max_parameter_bytes: Option::Some(2) }; + let db_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db_limits) }; + let db = sqlite::open(db_options); + let execute_params11: [SqliteValue] = [{ kind: "text", text_value: Option::Some("abc") }]; + sqlite::execute(&db, "SELECT ?1", execute_params11); "#, ); assert!( @@ -388,12 +446,20 @@ fn sqlite_invalid_utf8_text_is_returned_as_blob_value() { policy, r#" use bytes; - let db = sqlite::open({ path: "state.db", mode: "read_write_create", limits: {} }); + let db_limits: SqliteLimits = {}; + let db_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db_limits) }; + let db = sqlite::open(db_options); let payload = bytes::from_hex("80ff"); - let result = sqlite::query(&db, "SELECT CAST(?1 AS TEXT)", [{ kind: "blob", blob_value: payload }], {}); + let query_params12: [SqliteValue] = [{ kind: "blob", blob_value: Option::Some(payload) }]; + let query_limits13: SqliteLimits = {}; + let result = sqlite::query(&db, "SELECT CAST(?1 AS TEXT)", query_params12, query_limits13); let cell = result.rows[0].cells[0]; assert(cell.kind == "blob"); - assert(cell.blob_value == payload); + let blob_present = match cell.blob_value { + Option::Some(value) => value, + Option::None => payload.copy(), + }; + assert(blob_present == payload); sqlite::close(db); "#, ) @@ -409,8 +475,11 @@ fn sqlite_enforces_read_only_vm_local_ids_and_sql_safety() { run_sqlite_source( policy.clone(), r#" - let db = sqlite::open({ path: "state.db", mode: "read_write_create", limits: {} }); - sqlite::execute(&db, "CREATE TABLE items (value INTEGER)", []); + let db_limits: SqliteLimits = {}; + let db_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db_limits) }; + let db = sqlite::open(db_options); + let execute_params14: [SqliteValue] = []; + sqlite::execute(&db, "CREATE TABLE items (value INTEGER)", execute_params14); "#, ) .expect("writer should create the table"); @@ -418,8 +487,11 @@ fn sqlite_enforces_read_only_vm_local_ids_and_sql_safety() { let hazard = run_sqlite_host_error( policy.clone(), r#" - let db = sqlite::open({ path: "state.db", mode: "read_only", limits: {} }); - sqlite::execute(&db, "INSERT INTO items (value) VALUES (1)", []); + let db_limits: SqliteLimits = {}; + let db_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_only"), limits: Option::Some(db_limits) }; + let db = sqlite::open(db_options); + let execute_params15: [SqliteValue] = []; + sqlite::execute(&db, "INSERT INTO items (value) VALUES (1)", execute_params15); "#, ); assert!( @@ -438,7 +510,7 @@ fn sqlite_enforces_read_only_vm_local_ids_and_sql_safety() { let err = run_sqlite_host_error( policy.clone(), &format!( - "let db = sqlite::open({{ path: \"state.db\", mode: \"read_write_create\", limits: {{}} }});\n sqlite::execute(&db, \"{bad}\", []);" + "let db_limits: SqliteLimits = {{}};\n let db_options: SqliteOpenOptions = {{ path: Option::Some(\"state.db\"), mode: Option::Some(\"read_write_create\"), limits: Option::Some(db_limits) }};\n let db = sqlite::open(db_options);\n sqlite::execute(&db, \"{bad}\", []);" ), ); assert!( @@ -468,13 +540,21 @@ fn sqlite_query_reports_row_and_result_byte_truncation() { run_sqlite_source( policy, r#" - let db = sqlite::open({ path: "state.db", mode: "read_write_create", limits: { max_rows: 32, max_result_bytes: 32 } }); - sqlite::execute(&db, "CREATE TABLE items (value TEXT)", []); - sqlite::execute(&db, "INSERT INTO items (value) VALUES (?1)", [{ kind: "text", text_value: "one" }]); - sqlite::execute(&db, "INSERT INTO items (value) VALUES (?1)", [{ kind: "text", text_value: "two" }]); - sqlite::execute(&db, "INSERT INTO items (value) VALUES (?1)", [{ kind: "text", text_value: "three" }]); - - let row_limited = sqlite::query(&db, "SELECT value FROM items ORDER BY rowid", [], { max_rows: 1, max_result_bytes: 65536 }); + let db_limits: SqliteLimits = { max_rows: Option::Some(32), max_result_bytes: Option::Some(32) }; + let db_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db_limits) }; + let db = sqlite::open(db_options); + let execute_params16: [SqliteValue] = []; + sqlite::execute(&db, "CREATE TABLE items (value TEXT)", execute_params16); + let execute_params17: [SqliteValue] = [{ kind: "text", text_value: Option::Some("one") }]; + sqlite::execute(&db, "INSERT INTO items (value) VALUES (?1)", execute_params17); + let execute_params18: [SqliteValue] = [{ kind: "text", text_value: Option::Some("two") }]; + sqlite::execute(&db, "INSERT INTO items (value) VALUES (?1)", execute_params18); + let execute_params19: [SqliteValue] = [{ kind: "text", text_value: Option::Some("three") }]; + sqlite::execute(&db, "INSERT INTO items (value) VALUES (?1)", execute_params19); + + let query_params20: [SqliteValue] = []; + let query_limits21: SqliteLimits = { max_rows: Option::Some(1), max_result_bytes: Option::Some(65536) }; + let row_limited = sqlite::query(&db, "SELECT value FROM items ORDER BY rowid", query_params20, query_limits21); let row_limited_truncated = row_limited.truncated; assert(row_limited_truncated == true); let row_limited_row = row_limited.rows[0]; @@ -483,9 +563,11 @@ fn sqlite_query_reports_row_and_result_byte_truncation() { let row_limited_kind = row_limited_cell.kind; let row_limited_value = row_limited_cell.text_value; assert(row_limited_kind == "text"); - assert(row_limited_value == "one"); + assert(row_limited_value.unwrap_or("") == "one"); - let byte_limited = sqlite::query(&db, "SELECT value FROM items ORDER BY rowid", [], { max_rows: 32, max_result_bytes: 8 }); + let query_params22: [SqliteValue] = []; + let query_limits23: SqliteLimits = { max_rows: Option::Some(32), max_result_bytes: Option::Some(8) }; + let byte_limited = sqlite::query(&db, "SELECT value FROM items ORDER BY rowid", query_params22, query_limits23); let byte_limited_truncated = byte_limited.truncated; assert(byte_limited_truncated == true); "#, @@ -502,9 +584,9 @@ fn sqlite_uses_typed_generation_checked_resource_handles() { let err = run_sqlite_builtin_host_error( policy, r#" - let a = sqlite::open({ path: "handles.db", mode: "read_write_create", limits: {} }); + let a = sqlite::open({ path: Option::Some("handles.db"), mode: Option::Some("read_write_create"), limits: Option::Some({}) }); sqlite::close(a); - let b = sqlite::open({ path: "handles.db", mode: "read_write_create", limits: {} }); + let b = sqlite::open({ path: Option::Some("handles.db"), mode: Option::Some("read_write_create"), limits: Option::Some({}) }); assert(a != b); sqlite::execute(a, "SELECT 1", []); "#, @@ -523,9 +605,15 @@ fn sqlite_connection_limit_is_enforced_by_the_adapter() { let err = run_sqlite_host_error( policy, r#" - let a = sqlite::open({ path: "a.db", mode: "read_write_create", limits: { max_connections: 2 } }); - let b = sqlite::open({ path: "b.db", mode: "read_write_create", limits: { max_connections: 2 } }); - let c = sqlite::open({ path: "c.db", mode: "read_write_create", limits: { max_connections: 2 } }); + let a_limits: SqliteLimits = { max_connections: Option::Some(2) }; + let a_options: SqliteOpenOptions = { path: Option::Some("a.db"), mode: Option::Some("read_write_create"), limits: Option::Some(a_limits) }; + let a = sqlite::open(a_options); + let b_limits: SqliteLimits = { max_connections: Option::Some(2) }; + let b_options: SqliteOpenOptions = { path: Option::Some("b.db"), mode: Option::Some("read_write_create"), limits: Option::Some(b_limits) }; + let b = sqlite::open(b_options); + let c_limits: SqliteLimits = { max_connections: Option::Some(2) }; + let c_options: SqliteOpenOptions = { path: Option::Some("c.db"), mode: Option::Some("read_write_create"), limits: Option::Some(c_limits) }; + let c = sqlite::open(c_options); "#, ); assert!( @@ -544,14 +632,17 @@ fn sqlite_configure_and_clear_own_the_policy() { run_sqlite_source( policy.clone(), r#" - let db = sqlite::open({ path: "state.db", mode: "read_write_create", limits: {} }); - sqlite::execute(&db, "CREATE TABLE items (value INTEGER)", []); + let db_limits: SqliteLimits = {}; + let db_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db_limits) }; + let db = sqlite::open(db_options); + let execute_params25: [SqliteValue] = []; + sqlite::execute(&db, "CREATE TABLE items (value INTEGER)", execute_params25); "#, ) .expect("configured policy should allow file opens"); // clear_sqlite restores the default (no root), so a file open is rejected. - let compiled = compile_source("use sqlite;\nlet db = sqlite::open({ path: \"state.db\", mode: \"read_write_create\", limits: {} });") + let compiled = compile_source("use sqlite;\nlet db = sqlite::open({ path: Option::Some(\"state.db\"), mode: Option::Some(\"read_write_create\"), limits: Option::Some({}) });") .expect("source should compile"); let mut vm = Vm::new(compiled.program); install_host_driver(&mut vm); @@ -576,19 +667,28 @@ fn sqlite_close_and_reset_retire_resources() { run_sqlite_source( policy.clone(), r#" - let db = sqlite::open({ path: "state.db", mode: "read_write_create", limits: { max_transaction_ms: 10000, max_result_bytes: 65536 } }); - sqlite::execute(&db, "CREATE TABLE items (value INTEGER)", []); - let pending = sqlite::query(&db, "WITH RECURSIVE numbers(value) AS (SELECT 1 UNION ALL SELECT value + 1 FROM numbers LIMIT 2000000) SELECT sum(value) FROM numbers", [], { max_rows: 1, max_result_bytes: 65536 }); + let db_limits: SqliteLimits = { max_transaction_ms: Option::Some(10000), max_result_bytes: Option::Some(65536) }; + let db_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db_limits) }; + let db = sqlite::open(db_options); + let execute_params26: [SqliteValue] = []; + sqlite::execute(&db, "CREATE TABLE items (value INTEGER)", execute_params26); + let query_params27: [SqliteValue] = []; + let query_limits28: SqliteLimits = { max_rows: Option::Some(1), max_result_bytes: Option::Some(65536) }; + let pending = sqlite::query(&db, "WITH RECURSIVE numbers(value) AS (SELECT 1 UNION ALL SELECT value + 1 FROM numbers LIMIT 2000000) SELECT sum(value) FROM numbers", query_params27, query_limits28); sqlite::close(db); - let db2 = sqlite::open({ path: "state.db", mode: "read_write_create", limits: { max_transaction_ms: 10000, max_result_bytes: 65536 } }); - let count = sqlite::query(&db2, "SELECT count(*) AS count FROM items", [], {}); + let db2_limits: SqliteLimits = { max_transaction_ms: Option::Some(10000), max_result_bytes: Option::Some(65536) }; + let db2_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db2_limits) }; + let db2 = sqlite::open(db2_options); + let query_params29: [SqliteValue] = []; + let query_limits30: SqliteLimits = {}; + let count = sqlite::query(&db2, "SELECT count(*) AS count FROM items", query_params29, query_limits30); let count_row = count.rows[0]; let count_cells = count_row.cells; let count_cell = count_cells[0]; let count_kind = count_cell.kind; let count_value = count_cell.int_value; assert(count_kind == "int"); - assert(count_value == 0); + assert(count_value.unwrap_or(-1) == 0); sqlite::close(db2); "#, ) @@ -597,7 +697,7 @@ fn sqlite_close_and_reset_retire_resources() { // VM reset retires all pending sqlite operations and closes every open // connection through the generic scope lifecycle. let compiled = compile_source( - "use sqlite;\nlet db = sqlite::open({ path: \"state.db\", mode: \"read_write_create\", limits: { max_transaction_ms: 10000, max_result_bytes: 65536 } });\nlet pending = sqlite::query(&db, \"WITH RECURSIVE numbers(value) AS (SELECT 1 UNION ALL SELECT value + 1 FROM numbers LIMIT 2000000) SELECT sum(value) FROM numbers\", [], { max_rows: 1, max_result_bytes: 65536 });", + "use sqlite;\nlet db = sqlite::open({ path: Option::Some(\"state.db\"), mode: Option::Some(\"read_write_create\"), limits: Option::Some({ max_transaction_ms: Option::Some(10000), max_result_bytes: Option::Some(65536) }) });\nlet pending = sqlite::query(&db, \"WITH RECURSIVE numbers(value) AS (SELECT 1 UNION ALL SELECT value + 1 FROM numbers LIMIT 2000000) SELECT sum(value) FROM numbers\", [], { max_rows: Option::Some(1), max_result_bytes: Option::Some(65536) });", ) .expect("reset source should compile"); let mut vm = Vm::new(compiled.program); @@ -630,7 +730,7 @@ fn sqlite_reset_waits_for_adapter_close_before_reuse_and_prevents_late_writes() .expect("blocking transaction should hold the writer lock"); let compiled = compile_source( - "use sqlite;\nlet db = sqlite::open({ path: \"state.db\", mode: \"read_write_create\", limits: { busy_timeout_ms: 5000 } });\nsqlite::execute(&db, \"INSERT INTO items (value) VALUES (1)\", []);", + "use sqlite;\nlet db = sqlite::open({ path: Option::Some(\"state.db\"), mode: Option::Some(\"read_write_create\"), limits: Option::Some({ busy_timeout_ms: Option::Some(5000) }) });\nsqlite::execute(&db, \"INSERT INTO items (value) VALUES (1)\", []);", ) .expect("reset source should compile"); let mut vm = Vm::new(compiled.program); @@ -682,21 +782,27 @@ fn sqlite_pending_operation_slots_are_reclaimed_after_completion() { run_sqlite_source( policy, r#" - let db = sqlite::open({ path: "state.db", mode: "read_write_create", limits: { max_pending_operations: 4 } }); - sqlite::execute(&db, "CREATE TABLE items (value INTEGER)", []); + let db_limits: SqliteLimits = { max_pending_operations: Option::Some(4) }; + let db_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db_limits) }; + let db = sqlite::open(db_options); + let execute_params31: [SqliteValue] = []; + sqlite::execute(&db, "CREATE TABLE items (value INTEGER)", execute_params31); let mut i = 0; while i < 10 { - sqlite::execute(&db, "INSERT INTO items (value) VALUES (?1)", [{ kind: "int", int_value: i }]); + let execute_params32: [SqliteValue] = [{ kind: "int", int_value: Option::Some(i) }]; + sqlite::execute(&db, "INSERT INTO items (value) VALUES (?1)", execute_params32); i = i + 1; } - let count = sqlite::query(&db, "SELECT count(*) AS count FROM items", [], {}); + let query_params33: [SqliteValue] = []; + let query_limits34: SqliteLimits = {}; + let count = sqlite::query(&db, "SELECT count(*) AS count FROM items", query_params33, query_limits34); let count_row = count.rows[0]; let count_cells = count_row.cells; let count_cell = count_cells[0]; let count_kind = count_cell.kind; let count_value = count_cell.int_value; assert(count_kind == "int"); - assert(count_value == 10); + assert(count_value.unwrap_or(-1) == 10); sqlite::close(db); "#, ) @@ -710,16 +816,20 @@ fn sqlite_transaction_deadline_interrupts_and_rolls_back() { let error = run_sqlite_host_error( policy_for(&root), r#" - let db = sqlite::open({ path: "state.db", mode: "read_write_create", limits: { max_transaction_ms: 500 } }); - sqlite::execute(&db, "CREATE TABLE items (value INTEGER)", []); - sqlite::transaction(&db, { + let db_limits: SqliteLimits = { max_transaction_ms: Option::Some(500) }; + let db_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db_limits) }; + let db = sqlite::open(db_options); + let execute_params35: [SqliteValue] = []; + sqlite::execute(&db, "CREATE TABLE items (value INTEGER)", execute_params35); + let statements36: [SqliteStatement] = { { sql: "INSERT INTO items (value) VALUES (1)" }, { sql: "WITH RECURSIVE numbers(value) AS (SELECT 1 UNION ALL SELECT value + 1 FROM numbers LIMIT 10000000) SELECT sum(value) FROM numbers", - query: true, - limits: { max_rows: 1 } + query: Option::Some(true), + limits: Option::Some({ max_rows: Option::Some(1) }) } - }); + }; + sqlite::transaction(&db, statements36); "#, ); assert!( @@ -729,9 +839,13 @@ fn sqlite_transaction_deadline_interrupts_and_rolls_back() { run_sqlite_source( policy_for(&root), r#" - let db = sqlite::open({ path: "state.db", mode: "read_write_create", limits: {} }); - let result = sqlite::query(&db, "SELECT count(*) FROM items", [], {}); - assert(result.rows[0].cells[0].int_value == 0); + let db_limits: SqliteLimits = {}; + let db_options: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(db_limits) }; + let db = sqlite::open(db_options); + let query_params37: [SqliteValue] = []; + let query_limits38: SqliteLimits = {}; + let result = sqlite::query(&db, "SELECT count(*) FROM items", query_params37, query_limits38); + assert(result.rows[0].cells[0].int_value.unwrap_or(-1) == 0); sqlite::close(db); "#, ) @@ -744,7 +858,7 @@ fn sqlite_pending_reset_repeatedly_keeps_vm_reusable() { let root = temporary_root("reset-stress"); let policy = policy_for(&root); let compiled = compile_source( - "use sqlite;\nlet db = sqlite::open({ path: \"state.db\", mode: \"read_write_create\", limits: { max_transaction_ms: 10000, max_result_bytes: 65536 } });\nlet pending = sqlite::query(&db, \"WITH RECURSIVE numbers(value) AS (SELECT 1 UNION ALL SELECT value + 1 FROM numbers LIMIT 2000000) SELECT sum(value) FROM numbers\", [], { max_rows: 1, max_result_bytes: 65536 });", + "use sqlite;\nlet db = sqlite::open({ path: Option::Some(\"state.db\"), mode: Option::Some(\"read_write_create\"), limits: Option::Some({ max_transaction_ms: Option::Some(10000), max_result_bytes: Option::Some(65536) }) });\nlet pending = sqlite::query(&db, \"WITH RECURSIVE numbers(value) AS (SELECT 1 UNION ALL SELECT value + 1 FROM numbers LIMIT 2000000) SELECT sum(value) FROM numbers\", [], { max_rows: Option::Some(1), max_result_bytes: Option::Some(65536) });", ) .expect("stress source should compile"); let mut vm = Vm::new(compiled.program); diff --git a/tests/canonical_option_unit_tests.rs b/tests/canonical_option_unit_tests.rs new file mode 100644 index 00000000..280b6996 --- /dev/null +++ b/tests/canonical_option_unit_tests.rs @@ -0,0 +1,96 @@ +#![cfg(feature = "runtime")] + +fn compile_catalog( + source: &str, + catalog: &std::sync::Arc, +) -> Result { + vm::compile_source_with_flavor_and_options( + source, + vm::SourceFlavor::RustScript, + vm::CompileSourceFileOptions::default() + .with_host_api_catalog(std::sync::Arc::clone(catalog)), + ) +} + +fn tag_only_unit_catalog() -> std::sync::Arc { + let mut builder = vm::HostApiBuilder::new(); + builder.function(vm::HostFunctionSchema::with_return( + "host::some_unit", + Vec::new(), + vm::HostTypeSchema::Option(Box::new(vm::HostTypeSchema::Unit)), + )); + builder.function(vm::HostFunctionSchema::with_return( + "host::some_bad", + Vec::new(), + vm::HostTypeSchema::Option(Box::new(vm::HostTypeSchema::Int)), + )); + std::sync::Arc::new(builder.build().expect("tag-only unit catalog")) +} + +fn tag_only_some_handler(_: &mut vm::Vm, _: &[vm::Value]) -> vm::VmResult { + Ok(vm::CallOutcome::Return(vm::return_one(vm::Value::map( + vec![(vm::Value::string("tag"), vm::Value::Bool(true))], + )))) +} + +fn run_with_tag_only_unit(source: &str, host_name: &str) -> vm::VmResult { + let catalog = tag_only_unit_catalog(); + let compiled = compile_catalog(source, &catalog) + .unwrap_or_else(|error| panic!("tag-only host source should compile: {source}\n{error}")); + let mut registry = vm::HostFunctionRegistry::new(); + let schema = vm::catalog_import_schemas(&catalog, host_name) + .into_iter() + .next() + .expect("tag-only host schema"); + registry + .register_catalog_static(schema, tag_only_some_handler) + .expect("register tag-only host handler"); + let mut machine = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + let lines = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let recorded = std::sync::Arc::clone(&lines); + machine.set_runtime_print_sink(move |rendered| { + recorded.lock().expect("sink").push(rendered); + }); + registry + .bind_vm_cached(&mut machine) + .expect("bind tag-only host handler"); + let run = machine.run(); + if run.is_ok() { + assert_eq!( + lines.lock().expect("sink").as_slice(), + ["Some(())".to_string()], + "canonical tag-only Some(()) must render logically" + ); + } + run.map(|_| machine.stack().last().cloned().expect("run result")) +} + +#[test] +fn canonical_tag_only_some_unit_prints_and_encodes_a_declared_field() { + let source = r#" + use host; + use json; + struct Row { value: Option<()> } + let unit: Option<()> = host::some_unit(); + print(unit); + let row: Row = {value: unit}; + let text = json::encode(row); + text == "{\"value\":null}"; + "#; + let value = + run_with_tag_only_unit(source, "host::some_unit").expect("canonical Some(()) must run"); + assert_eq!(value, vm::Value::Bool(true)); +} + +#[test] +fn tag_only_some_with_non_unit_schema_still_fails() { + let source = "use host;\nlet value: Option = host::some_bad();\nvalue;\n"; + let error = run_with_tag_only_unit(source, "host::some_bad") + .expect_err("tag-only Some must not satisfy a non-Unit payload schema") + .to_string(); + assert!( + error.contains("Option Some payload") || error.contains("json unit"), + "unexpected non-Unit tag-only failure: {error}" + ); +} diff --git a/tests/capture_origin_certification.rs b/tests/capture_origin_certification.rs index 8181c325..c41a0091 100644 --- a/tests/capture_origin_certification.rs +++ b/tests/capture_origin_certification.rs @@ -46,7 +46,7 @@ fn catalog() -> Arc { HostTypeSchema::Resource(key()), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, )); b.function(HostFunctionSchema::with_return( "neutral::bits", diff --git a/tests/catalog_print_identity_tests.rs b/tests/catalog_print_identity_tests.rs new file mode 100644 index 00000000..e15c7c0a --- /dev/null +++ b/tests/catalog_print_identity_tests.rs @@ -0,0 +1,199 @@ +#![cfg(feature = "runtime")] + +fn compile_catalog( + source: &str, + catalog: &std::sync::Arc, +) -> Result { + vm::compile_source_with_flavor_and_options( + source, + vm::SourceFlavor::RustScript, + vm::CompileSourceFileOptions::default() + .with_host_api_catalog(std::sync::Arc::clone(catalog)), + ) +} + +fn namespaced_print_catalog( + name: &str, + params: Vec, + returns: vm::HostTypeSchema, +) -> std::sync::Arc { + let mut builder = vm::HostApiBuilder::new(); + builder.function(vm::HostFunctionSchema::with_return(name, params, returns)); + std::sync::Arc::new(builder.build().expect("namespaced print catalog")) +} + +fn run_bound_catalog_function( + source: &str, + catalog: &std::sync::Arc, + catalog_name: &str, + handler: vm::StaticHostFunction, + counter: &std::sync::atomic::AtomicUsize, +) -> vm::Value { + counter.store(0, std::sync::atomic::Ordering::SeqCst); + let compiled = compile_catalog(source, catalog).unwrap_or_else(|error| { + panic!("imported catalog call should keep its declared target: {error}") + }); + let mut registry = vm::HostFunctionRegistry::new(); + let schema = vm::catalog_import_schemas(catalog, catalog_name) + .into_iter() + .next() + .expect("catalog schema"); + registry + .register_catalog_static(schema, handler) + .expect("register catalog handler"); + let mut machine = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + let sink_lines = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let recorded = std::sync::Arc::clone(&sink_lines); + machine.set_runtime_print_sink(move |rendered| { + recorded.lock().expect("sink").push(rendered); + }); + registry + .bind_vm_cached(&mut machine) + .expect("bind catalog handler"); + assert_eq!( + machine.run().expect("catalog call should run"), + vm::VmStatus::Halted + ); + assert!( + sink_lines.lock().expect("sink").is_empty(), + "the selected catalog call must not fall through to language print" + ); + assert_eq!( + counter.load(std::sync::atomic::Ordering::SeqCst), + 1, + "selected catalog handler must actually run" + ); + machine + .stack() + .last() + .cloned() + .expect("catalog call should leave its return value") +} + +fn bool_print_catalog_for(catalog_name: &str) -> std::sync::Arc { + namespaced_print_catalog( + catalog_name, + vec![vm::HostParamSchema::value("value", vm::HostTypeSchema::Int)], + vm::HostTypeSchema::Bool, + ) +} + +static EXPLICIT_IMPORTED_PRINT_CALLS: std::sync::atomic::AtomicUsize = + std::sync::atomic::AtomicUsize::new(0); + +fn explicit_imported_bool_print( + _: &mut vm::Vm, + args: &[vm::Value], +) -> vm::VmResult { + EXPLICIT_IMPORTED_PRINT_CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + assert_eq!(args, &[vm::Value::Int(1)]); + Ok(vm::CallOutcome::Return(vm::return_one(vm::Value::Bool( + true, + )))) +} + +#[test] +fn explicit_imported_catalog_print_keeps_handler_identity() { + let catalog = bool_print_catalog_for("demo::print"); + let value = run_bound_catalog_function( + "use demo::{print};\nlet output: bool = print(1);\noutput;", + &catalog, + "demo::print", + explicit_imported_bool_print, + &EXPLICIT_IMPORTED_PRINT_CALLS, + ); + assert_eq!(value, vm::Value::Bool(true)); +} + +static WILDCARD_IMPORTED_PRINT_CALLS: std::sync::atomic::AtomicUsize = + std::sync::atomic::AtomicUsize::new(0); + +fn wildcard_imported_bool_print( + _: &mut vm::Vm, + args: &[vm::Value], +) -> vm::VmResult { + WILDCARD_IMPORTED_PRINT_CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + assert_eq!(args, &[vm::Value::Int(1)]); + Ok(vm::CallOutcome::Return(vm::return_one(vm::Value::Bool( + true, + )))) +} + +#[test] +fn wildcard_imported_catalog_print_keeps_handler_identity() { + let catalog = bool_print_catalog_for("demo::print"); + let value = run_bound_catalog_function( + "use demo::*;\nlet output: bool = print(1);\noutput;", + &catalog, + "demo::print", + wildcard_imported_bool_print, + &WILDCARD_IMPORTED_PRINT_CALLS, + ); + assert_eq!(value, vm::Value::Bool(true)); +} + +static NAMESPACED_PRINTLN_CALLS: std::sync::atomic::AtomicUsize = + std::sync::atomic::AtomicUsize::new(0); + +fn namespaced_bool_println(_: &mut vm::Vm, args: &[vm::Value]) -> vm::VmResult { + NAMESPACED_PRINTLN_CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + assert_eq!(args, &[vm::Value::Int(1)]); + Ok(vm::CallOutcome::Return(vm::return_one(vm::Value::Bool( + true, + )))) +} + +#[test] +fn imported_catalog_println_keeps_handler_identity() { + let catalog = bool_print_catalog_for("demo::println"); + let value = run_bound_catalog_function( + "use demo::{println};\nlet output: bool = println(1);\noutput;", + &catalog, + "demo::println", + namespaced_bool_println, + &NAMESPACED_PRINTLN_CALLS, + ); + assert_eq!(value, vm::Value::Bool(true)); +} + +#[test] +fn explicit_imported_catalog_print_arity_mismatch_is_a_host_error() { + let catalog = namespaced_print_catalog( + "demo::print", + vec![ + vm::HostParamSchema::value("left", vm::HostTypeSchema::Int), + vm::HostParamSchema::value("right", vm::HostTypeSchema::Int), + ], + vm::HostTypeSchema::Bool, + ); + let error = match compile_catalog("use demo::{print};\nprint(1);", &catalog) { + Ok(_) => panic!("an explicit imported print target must keep its declared arity"), + Err(error) => error.to_string(), + }; + assert!( + error.contains("no overload with 1 argument"), + "explicit imported target must report its own arity contract: {error}" + ); +} + +#[test] +fn wildcard_namespace_without_matching_print_keeps_language_print() { + let catalog = namespaced_print_catalog( + "demo::other", + vec![vm::HostParamSchema::value("value", vm::HostTypeSchema::Int)], + vm::HostTypeSchema::Bool, + ); + let compiled = compile_catalog("use demo::*;\nprint(1);", &catalog).unwrap_or_else(|error| { + panic!("wildcard import without print must keep language print: {error}") + }); + let mut machine = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + let lines = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let recorded = std::sync::Arc::clone(&lines); + machine.set_runtime_print_sink(move |rendered| { + recorded.lock().expect("sink").push(rendered); + }); + assert_eq!(machine.run().expect("language print"), vm::VmStatus::Halted); + assert_eq!(lines.lock().expect("sink").as_slice(), ["1".to_string()]); +} diff --git a/tests/cli_embedding_resources.rs b/tests/cli_embedding_resources.rs index 75d223e8..fa184d82 100644 --- a/tests/cli_embedding_resources.rs +++ b/tests/cli_embedding_resources.rs @@ -101,7 +101,7 @@ fn services(audit: Audit, fail_init: bool, reset_probe: bool) -> CliHostServices resource.clone(), HostParamPassing::BorrowMut, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, )); builder.function(HostFunctionSchema::with_return( "counter::read", @@ -115,7 +115,7 @@ fn services(audit: Audit, fail_init: bool, reset_probe: bool) -> CliHostServices builder.function(HostFunctionSchema::with_return( "counter::abort", vec![], - HostTypeSchema::Null, + HostTypeSchema::Unit, )); let catalog = Arc::new(builder.build().unwrap()); let register_catalog = Arc::clone(&catalog); diff --git a/tests/compiler/compiler_rustscript_tests.rs b/tests/compiler/compiler_rustscript_tests.rs index 5aa544df..4d2a40d1 100644 --- a/tests/compiler/compiler_rustscript_tests.rs +++ b/tests/compiler/compiler_rustscript_tests.rs @@ -2,6 +2,19 @@ mod common; use common::*; +#[path = "../support/legacy_nullable_plugin.rs"] +mod legacy_nullable_plugin; + +/// The untyped public JSON encoder/decoder stays available to non-RustScript +/// frontends. These runtime-encoder cases exercise that owner directly. +fn compile_plugin_source(source: &str) -> Result { + vm::compile_source_with_flavor_and_options( + source, + legacy_nullable_plugin::legacy_nullable_flavor(), + legacy_nullable_plugin::legacy_nullable_options(vm::CompileSourceFileOptions::default()), + ) +} + struct BoundRuntimeCase<'a> { case: RuntimeCase<'a>, bindings: Vec>, @@ -205,7 +218,7 @@ fn rustscript_builtin_and_namespace_runtime_cases_work() { if d.length == 2 && d[0] == "a" && d[1] == "b" { score = score + 1; } - if e.length == 3 && e[1] == "FoO" && e[2] == "42" { + if e.length == 3 && e[1].unwrap_or("") == "FoO" && e[2].unwrap_or("") == "42" { score = score + 1; } score; @@ -1138,7 +1151,7 @@ fn closure_mut_capture_is_visible_after_callback_returns() { r#" fn invoke(cb, x) { let _ = cb(x); - null + () } let mut state: string = ""; let sink = |delta| if true => { @@ -1285,9 +1298,9 @@ fn closure_mut_capture_compound_and_write_only_modes_stay_shared() { let mut state: int = 0; let bump = |delta| if true => { state += delta; - null + () } else => { - null + () }; let _ = bump(1); state; @@ -1299,9 +1312,9 @@ fn closure_mut_capture_compound_and_write_only_modes_stay_shared() { let mut state: string = "initial"; let reset = |value| if true => { state = value; - null + () } else => { - null + () }; let _ = reset("after"); state; @@ -1485,19 +1498,17 @@ fn rustscript_move_and_alias_runtime_cases_work() { expected_locals: None, }, RuntimeCase { - name: "bool fields are copy by default and null object entries are omitted", + name: "bool fields are copy by default and unit object entries stay present", source: r#" - let p = { b: true, n: null }; + let p = { b: true, n: () }; let b1 = p.b; let b2 = p.b; - if b1 && b2 && !p.has("n") { - 1; - } else { - 0; - } + let copied = if b1 && b2 => { 1 } else => { 0 }; + let present = if p.has("n") => { 1 } else => { 0 }; + copied + present; "#, flavor: SourceFlavor::RustScript, - expected_stack: vec![Value::Int(1)], + expected_stack: vec![Value::Int(2)], expected_locals: None, }, RuntimeCase { @@ -2871,14 +2882,12 @@ fn rustscript_if_and_match_runtime_cases_work() { let data: Data = { values: [41] }; let present = match data?.values?.[0] { - None => 0, - Some(value) => value + 1, - _ => 0, + Option::None => 0, + Option::Some(value) => value + 1, }; let missing = match data?.values?.[1] { - None => 1, - Some(value) => value, - _ => 0, + Option::None => 1, + Option::Some(value) => value, }; present + missing; @@ -3426,28 +3435,32 @@ fn rustscript_language_runtime_cases_work() { expected_locals: None, }, RuntimeCase { - name: "null literal is supported", + name: "option none is observable", source: r#" - let v = null; - type(v) == "null"; + let v: Option = Option::None; + match v { + Option::None => true, + _ => false, + }; "#, flavor: SourceFlavor::RustScript, expected_stack: vec![Value::Bool(true)], expected_locals: None, }, RuntimeCase { - name: "null values and type patterns are supported", + name: "option none and type patterns are supported", source: r#" let some = 1 + 1; - let none = null; + let none: Option = Option::None; let some2 = 40; let a = match none { - null => 1, + Option::None => 1, _ => 0, }; - let b = match some { - null => 0, + let present: Option = Option::Some(some); + let b = match present { + Option::None => 0, _ => 1, }; let c = match some2 { @@ -3455,8 +3468,11 @@ fn rustscript_language_runtime_cases_work() { _ => 0, }; - let t = type(null); - if t == "null" { + let saw_none = match none { + Option::None => true, + _ => false, + }; + if saw_none { (a + b + c) + some2; } else { 0; @@ -3481,10 +3497,12 @@ fn rustscript_language_runtime_cases_work() { expected_locals: None, }, RuntimeCase { - name: "null literal can be used as map key", + name: "option none can be used as map key", source: r#" - let m = { null: 42 }; - m[null]; + let mut m: map = {}; + let key: Option = Option::None; + m[key] = 42; + m[key]; "#, flavor: SourceFlavor::RustScript, expected_stack: vec![Value::Int(42)], @@ -3509,7 +3527,7 @@ fn rustscript_language_parse_rejection_cases_work() { ParseErrorCase { name: "type name of val alias is rejected", source: r#" - type_name_of_val(null); + type_name_of_val(1); "#, flavor: SourceFlavor::RustScript, expected_contains_all: &["unknown function 'type_name_of_val'"], @@ -3553,7 +3571,7 @@ fn rustscript_referenced_struct_schema_runtime_cases_work() { struct User { age: Age } let user: User = { age: { first: 41 } }; let age = user?.age; - age?.first; + age?.first.unwrap_or(-1); "#, vec![Value::Int(41)], ), @@ -3682,7 +3700,7 @@ fn rustscript_recursive_struct_schema_runtime_cases_work() { let node: Node = { value: 1, next: { value: 41, next: {} }}; let next = node?.next; - next?.value; + next?.value.unwrap_or(-1); "#, vec![Value::Int(41)], )]; @@ -3738,7 +3756,7 @@ fn rustscript_optional_chain_requires_declared_schema_and_explicit_handling() { struct Profile { stats: Stats } let present: Profile = { stats: { score: 41 } }; - let missing: Profile? = null; + let missing: Option = Option::None; let present_score = present?.stats?.score.unwrap_or(0); let missing_score = missing?.stats?.score.unwrap_or(0); @@ -3869,9 +3887,8 @@ fn rustscript_optional_chain_rejects_statistically_mismatched_declared_schemas() let profile: Profile = { stats: { score: "oops" } }; match profile?.stats?.score { - None => 0, - Some(score) => score + 1, - _ => 0, + Option::None => 0, + Option::Some(score) => score + 1, }; "#, flavor: SourceFlavor::RustScript, @@ -3958,10 +3975,10 @@ fn rustscript_explicit_optional_type_annotations_work() { rustscript_runtime_case( "optional local and return annotations preserve concrete inner typing", r#" - fn maybe_score(text: string) -> int? { - let mut out: int? = null; + fn maybe_score(text: string) -> Option { + let mut out: Option = Option::None; if text == "ok" { - out = 41; + out = Option::Some(41); } out } @@ -3983,31 +4000,18 @@ fn rustscript_explicit_optional_type_annotations_work() { let error_cases = vec![ SourceErrorCase { - name: "non optional local rejects null assignment", - source: r#" - let value: int = null; - value; - "#, + name: "non optional local rejects unit assignment", + source: "let value: int = ();\n", flavor: SourceFlavor::RustScript, expected_kind: SourceErrorKind::Compile(CompileErrorKind::InvalidFieldAccess), - expected_contains_all: &[ - "local is declared as schema type 'int' but was assigned null", - ], + expected_contains_all: &["local is declared as schema type 'int' but was assigned ()"], }, SourceErrorCase { - name: "non optional return rejects null result", - source: r#" - fn bad() -> int { - null - } - - bad(); - "#, + name: "non optional declared return rejects unit result", + source: "fn bad() -> int { () } bad();\n", flavor: SourceFlavor::RustScript, expected_kind: SourceErrorKind::Compile(CompileErrorKind::StrictTypingRequired), - expected_contains_all: &[ - "function 'bad' is declared to return 'int' but produced null", - ], + expected_contains_all: &["function 'bad' is declared to return 'int' but produced ()"], }, SourceErrorCase { name: "typed callable locals reject mismatched closure results", @@ -4526,7 +4530,7 @@ fn json_encode_accepts_string_key_runtime_map() { // A runtime map annotated as `map` has schema `map`: key // legality cannot be proven statically, so the compile-time validator // must admit it and the runtime encoder's string-key check decides. - let compiled = compile_source( + let compiled = compile_plugin_source( r#" use json; let request: map = { @@ -4557,7 +4561,7 @@ fn json_encode_accepts_nested_runtime_maps_and_arrays() { // Provider-shaped payload: nested maps and arrays inside a runtime map. // The generated object key order is unspecified, so the assertion parses // the text and compares semantic JSON. - let compiled = compile_source( + let compiled = compile_plugin_source( r#" use json; let request: map = { @@ -4667,10 +4671,7 @@ fn json_encode_runtime_map_rejects_non_string_key() { .expect_err("json::encode must reject non-string map keys"); match err { vm::VmError::HostError(message) => { - assert!( - message.contains("json_encode map keys must be strings"), - "{message}" - ); + assert!(message.contains("map keys must be strings"), "{message}"); } other => panic!("unexpected vm error: {other}"), } @@ -4678,7 +4679,7 @@ fn json_encode_runtime_map_rejects_non_string_key() { #[test] fn json_encode_runtime_map_rejects_nested_bytes() { - let compiled = compile_source( + let compiled = compile_plugin_source( r#" use json; let payload: map = { "data": b"abc" }; @@ -4704,7 +4705,7 @@ fn json_encode_runtime_map_rejects_nested_bytes() { #[test] fn json_encode_runtime_map_rejects_nested_callable() { - let compiled = compile_source( + let compiled = compile_plugin_source( r#" use json; fn handler(value: int) -> int { value + 1 } @@ -4937,22 +4938,20 @@ fn spawn_json_probe(probe_env: &str, test_name: &str, sentinel: &str) -> Result< #[test] fn json_encode_accepts_mutually_recursive_structs_inside_concrete_map() { // A `map` whose inner schema is a mutually recursive struct pair - // (A -> B -> A) must compile and encode: the recursion is structural, - // every runtime value is finite, and the cycle edge itself is - // encodable. The innermost partial literal `{}` (an `A` missing its - // required `b`) is admitted only because the declared-schema check - // allows partial objects at recursive re-entries, so the runtime - // value is exactly the map literal as written and the expected - // encoding is `{"x":{"b":{"a":{"b":{"a":{}}}}}}`. + // (A -> B -> A) must compile and encode. The cycle fields are + // `Option`, so the finite base is `Option::None` rather than an + // omitted required field. `Option::None` is omitted from the + // encoding, so the innermost object is `{}` and the value is + // `{"x":{"b":{"a":{"b":{"a":{}}}}}}`. const PROBE: &str = "RUSTSCRIPT_JSON_PROBE_ACCEPT_RECURSIVE_MAP"; const SENTINEL: &str = "json-probe-entered:RUSTSCRIPT_JSON_PROBE_ACCEPT_RECURSIVE_MAP"; run_json_probe_child(PROBE, SENTINEL, || { let compiled = match compile_source( r#" use json; - struct A { b: B } - struct B { a: A } - let payload: map = { "x": { b: { a: { b: { a: {} } } } } }; + struct A { b: Option } + struct B { a: Option } + let payload: map = { "x": { b: Option::Some({ a: Option::Some({ b: Option::Some({ a: Option::Some({ b: Option::None }) }) }) }) } }; json::encode(payload); "#, ) { @@ -5003,6 +5002,31 @@ fn json_encode_accepts_mutually_recursive_structs_inside_concrete_map() { } } +#[test] +fn json_encode_rejects_missing_required_field_on_recursive_partial() { + // A finite literal that omits a required non-Option field is not a + // legal construction, even when the omission sits on a recursive + // re-entry. Encode must not be reached by skipping that field. + match compile_source( + r#" + use json; + struct A { b: B } + struct B { a: A } + let payload: map = { "x": { b: { a: {} } } }; + json::encode(payload); + "#, + ) { + Err(err) => { + let text = err.to_string(); + assert!( + text.contains("required by the declared schema but is missing"), + "missing required field must be refused, got: {text}" + ); + } + Ok(_) => panic!("omitting required field b must not compile"), + } +} + #[test] fn json_encode_rejects_unsupported_field_in_recursive_struct_inside_concrete_map() { // Negative control for the cycle contract: the cycle edge itself is @@ -5265,8 +5289,8 @@ fn json_encode_accepts_wrapped_recursion_in_concrete_map() { let compiled = match compile_source( r#" use json; - struct Node { child: Node<[T]>?, data: int } - let payload: map> = { "x": { child: null, data: 1 } }; + struct Node { child: Option>, data: int } + let payload: map> = { "x": { child: Option::None, data: 1 } }; json::encode(payload); "#, ) { diff --git a/tests/compiler/diagnostics_tests.rs b/tests/compiler/diagnostics_tests.rs index d0098a17..b5c8d4e0 100644 --- a/tests/compiler/diagnostics_tests.rs +++ b/tests/compiler/diagnostics_tests.rs @@ -1,6 +1,9 @@ use std::fs; use std::time::{SystemTime, UNIX_EPOCH}; +#[path = "../support/legacy_nullable_plugin.rs"] +mod legacy_nullable_plugin; + use vm::{ ParseError, SourceError, SourceFlavor, SourceMap, SourcePathError, Span, collect_inferred_local_type_hints, compile_source, compile_source_file, @@ -348,8 +351,12 @@ let payload_decoded: Profile = json::decode(payload_json); payload_decoded.stats.score; "#; - let hints = collect_inferred_local_type_hints(source, SourceFlavor::RustScript) - .expect("type hints should succeed"); + let hints = vm::collect_inferred_local_type_hints_with_options( + source, + legacy_nullable_plugin::legacy_nullable_flavor(), + legacy_nullable_plugin::legacy_nullable_options(vm::CompileSourceFileOptions::default()), + ) + .expect("type hints should succeed"); let payload_decoded = hints .iter() .find(|hint| hint.name == "payload_decoded") diff --git a/tests/compiler/frontend_plugin_policy_tests.rs b/tests/compiler/frontend_plugin_policy_tests.rs index 6af59a8d..00ea76e4 100644 --- a/tests/compiler/frontend_plugin_policy_tests.rs +++ b/tests/compiler/frontend_plugin_policy_tests.rs @@ -603,6 +603,98 @@ impl SourcePlugin for LogicalSchemaPlugin { } } +struct OptionSyntaxDialect; + +impl ParserDialect for OptionSyntaxDialect { + fn allow_option_syntax(&self) -> bool { + true + } +} + +static OPTION_SYNTAX_DIALECT: OptionSyntaxDialect = OptionSyntaxDialect; + +struct OptionSchemaPlugin; +static OPTION_SCHEMA_PLUGIN: OptionSchemaPlugin = OptionSchemaPlugin; + +impl SourcePlugin for OptionSchemaPlugin { + fn descriptor(&self) -> SourceFrontendDescriptor { + SourceFrontendDescriptor { + id: FrontendId::new("neutral-option-schemas"), + extensions: &["optlogical"], + typing_policy: FrontendTypingPolicy::Strict, + supports_ownership_checking: true, + } + } + + fn parse_source( + &self, + source: &str, + context: &SourceFrontendContext<'_>, + ) -> Result { + parse_source_with_dialect( + source, + &OPTION_SYNTAX_DIALECT, + SharedParserOptions { + source_id: context.source_id, + import_scan_mode: true, + ..SharedParserOptions::default() + }, + ) + } + + fn validate_typed_ir(&self, typed: &FrontendTypedIr<'_>) -> Result<(), CompileError> { + let option = TypeSchema::Option(Box::new(TypeSchema::Int)); + for (name, slot) in &typed.ir.local_bindings { + let expected = match name.as_str() { + "f" => TypeSchema::Callable { + params: vec![], + result: Box::new(TypeSchema::Int), + }, + "g" => TypeSchema::Callable { + params: vec![option.clone()], + result: Box::new(option.clone()), + }, + _ => continue, + }; + assert_eq!( + typed.local_schemas[usize::from(*slot)], + Some(expected.clone()), + "checked local {name}" + ); + if let Some(expr) = typed.ir.stmts.iter().find_map(|statement| match statement { + Stmt::Let { index, expr, .. } if index == slot => Some(expr), + _ => None, + }) { + assert_eq!( + typed.schema_of_expr(&vm::compiler::Expr::Var(*slot)), + Some(expected.clone()), + "variable query {name}" + ); + assert_eq!( + typed.schema_of_expr(expr), + Some(expected), + "initializer query {name}" + ); + } + } + if let Some(function) = typed + .ir + .functions + .iter() + .find(|function| function.name == "maybe") + { + assert_eq!( + typed.schema_of_function(function.index), + Some(TypeSchema::Callable { + params: vec![option.clone()], + result: Box::new(option) + }) + ); + } + Ok(()) + } +} + #[test] fn inferred_function_reference_schema_agrees_with_runtime_hint_and_semantic_query() { let source = "fn answer() { 42 }\nlet f = answer;\nf();\n"; @@ -659,22 +751,22 @@ fn logical_optional_schema_is_complete_in_hints_and_validation_queries() { #[test] fn inferred_optional_callable_results_agree_across_native_and_neutral_queries() { - let optional = TypeSchema::Optional(Box::new(TypeSchema::Int)); + let option = TypeSchema::Option(Box::new(TypeSchema::Int)); let expected = TypeSchema::Callable { - params: vec![optional.clone()], - result: Box::new(optional), + params: vec![option.clone()], + result: Box::new(option), }; for (flavor, path) in [ (SourceFlavor::RustScript, "main.rss"), ( - SourceFlavor::Plugin(FrontendId::new("neutral-logical-schemas")), - "main.logical", + SourceFlavor::Plugin(FrontendId::new("neutral-option-schemas")), + "main.optlogical", ), ] { - let options = || CompileSourceFileOptions::new().with_source_plugin(&LOGICAL_SCHEMA_PLUGIN); + let options = || CompileSourceFileOptions::new().with_source_plugin(&OPTION_SCHEMA_PLUGIN); for source in [ - "fn maybe(value: int?) { value }\nlet g = maybe;\ng(null);\n", - "let g: fn(int?) -> int? = |value| value;\ng(null);\n", + "fn maybe(value: Option) { value }\nlet g = maybe;\ng(Option::None);\n", + "let g: fn(Option) -> Option = |value| value;\ng(Option::None);\n", ] { let hints = collect_inferred_local_type_hints_with_options(source, flavor, options()).unwrap(); @@ -719,7 +811,7 @@ fn inferred_optional_callable_results_agree_across_native_and_neutral_queries() compile_source_with_flavor_and_options(source, flavor, options()).unwrap(); let mut machine = Vm::new(compiled.program); assert_eq!(machine.run().unwrap(), VmStatus::Halted); - assert_eq!(machine.stack(), &[Value::Null]); + assert_eq!(machine.stack(), &[vm::option::none_carrier()]); } } } @@ -789,11 +881,11 @@ fn finalized_named_and_contextual_callable_consumers_keep_authored_generic_decla for (flavor, path) in [ (SourceFlavor::RustScript, "main.rss"), ( - SourceFlavor::Plugin(FrontendId::new("neutral-logical-schemas")), - "main.logical", + SourceFlavor::Plugin(FrontendId::new("neutral-option-schemas")), + "main.optlogical", ), ] { - let options = || CompileSourceFileOptions::new().with_source_plugin(&LOGICAL_SCHEMA_PLUGIN); + let options = || CompileSourceFileOptions::new().with_source_plugin(&OPTION_SCHEMA_PLUGIN); for (source, name, result, local) in [ ( "fn answer() { 42 }\nlet f = answer;\nanswer();\nf();\n", @@ -802,9 +894,9 @@ fn finalized_named_and_contextual_callable_consumers_keep_authored_generic_decla "f", ), ( - "fn maybe(value: int?) { value }\nlet g = maybe;\nmaybe(null);\ng(null);\n", + "fn maybe(value: Option) { value }\nlet g = maybe;\nmaybe(Option::None);\ng(Option::None);\n", "maybe", - TypeSchema::Optional(Box::new(TypeSchema::Int)), + TypeSchema::Option(Box::new(TypeSchema::Int)), "g", ), ] { @@ -860,12 +952,13 @@ fn finalized_named_and_contextual_callable_consumers_keep_authored_generic_decla ); let mut machine = Vm::new(compiled.program); assert_eq!(machine.run().unwrap(), VmStatus::Halted); + let absent = vm::option::none_carrier(); assert_eq!( machine.stack().last(), Some(if name == "answer" { &Value::Int(42) } else { - &Value::Null + &absent }) ); } @@ -913,10 +1006,10 @@ fn finalized_named_and_contextual_callable_consumers_keep_authored_generic_decla let mut machine = Vm::new(compiled.program); assert_eq!(machine.run().unwrap(), VmStatus::Halted); assert_eq!(machine.stack(), &[Value::Int(41)]); - let source = "let g: fn(int?) -> int? = |value| value;\ng(null);\n"; + let source = "let g: fn(Option) -> Option = |value| value;\ng(Option::None);\n"; let expected = TypeSchema::Callable { - params: vec![TypeSchema::Optional(Box::new(TypeSchema::Int))], - result: Box::new(TypeSchema::Optional(Box::new(TypeSchema::Int))), + params: vec![TypeSchema::Option(Box::new(TypeSchema::Int))], + result: Box::new(TypeSchema::Option(Box::new(TypeSchema::Int))), }; let compiled = compile_source_with_flavor_and_options(source, flavor, options()).unwrap(); assert!( diff --git a/tests/compiler/frontend_plugin_tests.rs b/tests/compiler/frontend_plugin_tests.rs index 2c905e1c..9c7a89c9 100644 --- a/tests/compiler/frontend_plugin_tests.rs +++ b/tests/compiler/frontend_plugin_tests.rs @@ -4,10 +4,12 @@ use std::ops::Deref; use std::path::{Path, PathBuf}; use std::sync::Mutex; use std::sync::atomic::{AtomicU64, Ordering}; +use vm::compiler::ir::{ParsedCallSite, ParsedCallTarget, ParsedSemanticIndex, SemanticNodeId}; use vm::{ CompileSourceFileOptions, Expr, FrontendId, FrontendIr, FrontendTypingPolicy, ParseError, - ParsePhase, SourceFlavor, SourceFrontendContext, SourceFrontendDescriptor, SourcePlugin, Stmt, - Value, Vm, VmStatus, compile_source_file_with_options, compile_source_with_flavor_and_options, + ParsePhase, SourceFlavor, SourceFrontendContext, SourceFrontendDescriptor, SourcePlugin, Span, + Stmt, Value, Vm, VmStatus, compile_source, compile_source_file_with_options, + compile_source_with_flavor_and_options, }; #[derive(Clone, Debug, PartialEq, Eq)] @@ -1279,3 +1281,221 @@ fn closure_let_guest_named_expected_schema_survives_canonical_remap() { } } } + +struct NullDeletePlugin; + +impl SourcePlugin for NullDeletePlugin { + fn descriptor(&self) -> SourceFrontendDescriptor { + SourceFrontendDescriptor { + id: FrontendId::new("plugin-null-delete"), + extensions: &["nulldelete"], + typing_policy: FrontendTypingPolicy::Dynamic, + supports_ownership_checking: false, + } + } + + fn parse_source( + &self, + _source: &str, + _context: &SourceFrontendContext<'_>, + ) -> Result { + let stored = builtin_call( + vm::BuiltinFunction::Set, + vec![ + builtin_call(vm::BuiltinFunction::MapNew, Vec::new()), + Expr::String("k".to_string()), + Expr::Int(1), + ], + ); + let cleared = builtin_call( + vm::BuiltinFunction::Set, + vec![stored, Expr::String("k".to_string()), Expr::Null], + ); + let present = builtin_call( + vm::BuiltinFunction::Has, + vec![cleared, Expr::String("k".to_string())], + ); + Ok(FrontendIr { + stmts: vec![Stmt::Expr { + expr: present, + line: 1, + }], + locals: 0, + local_bindings: Vec::new(), + struct_schemas: Default::default(), + unknown_type_spans: Vec::new(), + functions: Vec::new(), + function_impls: Default::default(), + stmt_sources: Vec::new(), + function_sources: Default::default(), + use_declarations: Vec::new(), + implicit_extern_names: Vec::new(), + host_api_metadata: None, + semantic_index: None, + parsed_semantic_index: None, + catalog_visibility: None, + lexer_tokens: Vec::new(), + }) + } +} + +#[test] +fn plugin_null_store_stays_set_and_deletes_key() { + let compiled = compile_source_with_flavor_and_options( + "synthetic null delete", + SourceFlavor::Plugin(FrontendId::new("plugin-null-delete")), + CompileSourceFileOptions::new().with_source_plugin(&NullDeletePlugin), + ) + .expect("plugin null store must keep legacy Set deletion"); + let set_index = vm::BuiltinFunction::Set.call_index().to_le_bytes(); + let edit_index = vm::BuiltinFunction::MapEdit.call_index().to_le_bytes(); + assert!( + compiled + .program + .code + .windows(2) + .any(|pair| pair == set_index), + "plugin null store must remain a Set call" + ); + assert!( + !compiled + .program + .code + .windows(2) + .any(|pair| pair == edit_index), + "plugin null store must not be rewritten to MapEdit" + ); + let mut vm = Vm::new(compiled.program); + assert_eq!( + vm.run().expect("plugin null delete should run"), + VmStatus::Halted + ); + assert_eq!(vm.stack(), &[Value::Bool(false)]); +} + +struct ProvenanceNullDeletePlugin; + +impl SourcePlugin for ProvenanceNullDeletePlugin { + fn descriptor(&self) -> SourceFrontendDescriptor { + SourceFrontendDescriptor { + id: FrontendId::new("plugin-null-provenance"), + extensions: &["nullown"], + typing_policy: FrontendTypingPolicy::Dynamic, + supports_ownership_checking: false, + } + } + + fn parse_source( + &self, + _source: &str, + context: &SourceFrontendContext<'_>, + ) -> Result { + let call_id = SemanticNodeId(1); + let span = Span::new(context.source_id, 1, 8); + let stored = Expr::Call( + vm::BuiltinFunction::Set.call_index(), + Vec::new(), + vec![ + Expr::Call( + vm::BuiltinFunction::MapNew.call_index(), + Vec::new(), + Vec::new(), + None, + None, + ), + Expr::String("k".to_string()), + Expr::Null, + ], + None, + Some(call_id), + ); + let present = Expr::Call( + vm::BuiltinFunction::Has.call_index(), + Vec::new(), + vec![stored, Expr::String("k".to_string())], + None, + None, + ); + let parsed = FrontendIr { + stmts: vec![Stmt::Expr { + expr: present, + line: 1, + }], + locals: 0, + local_bindings: Vec::new(), + struct_schemas: Default::default(), + unknown_type_spans: Vec::new(), + functions: Vec::new(), + function_impls: Default::default(), + stmt_sources: Vec::new(), + function_sources: Default::default(), + use_declarations: Vec::new(), + implicit_extern_names: Vec::new(), + host_api_metadata: None, + semantic_index: None, + parsed_semantic_index: Some(ParsedSemanticIndex { + call_sites: vec![ParsedCallSite { + id: call_id, + callee_span: span, + expr_span: span, + target: ParsedCallTarget::Function(vm::BuiltinFunction::Set.call_index()), + name: "set".to_string(), + scope_id: 0, + is_namespace_call: false, + }], + next_node_id: 2, + ..ParsedSemanticIndex::default() + }), + catalog_visibility: None, + lexer_tokens: Vec::new(), + }; + Ok(parsed) + } +} + +fn builtin_call_count(program: &vm::Program, builtin: vm::BuiltinFunction) -> usize { + let index = builtin.call_index().to_le_bytes(); + program + .code + .windows(3) + .filter(|window| window[0] == vm::OpCode::Call as u8 && window[1..3] == index) + .count() +} + +#[test] +fn plugin_provenance_keeps_null_set_while_rss_unit_store_rewrites() { + let path = plugin_source_path("nullown"); + let plugin = compile_source_file_with_options( + &path, + CompileSourceFileOptions::new().with_source_plugin(&ProvenanceNullDeletePlugin), + ) + .expect("provenanced plugin null store must compile"); + assert!( + builtin_call_count(&plugin.program, vm::BuiltinFunction::Set) >= 1, + "plugin null store must remain Set" + ); + assert_eq!( + builtin_call_count(&plugin.program, vm::BuiltinFunction::MapEdit), + 0, + "plugin source id must stay outside RustScript map-store policy" + ); + let mut plugin_vm = Vm::new(plugin.program); + assert_eq!( + plugin_vm.run().expect("plugin null delete should run"), + VmStatus::Halted + ); + assert_eq!(plugin_vm.stack(), &[Value::Bool(false)]); + + let rss = compile_source(r#"let mut values = {}; values["k"] = (); values.has("k");"#) + .expect("rss unit store must compile"); + assert!( + builtin_call_count(&rss.program, vm::BuiltinFunction::MapEdit) >= 1, + "rss unit store must be rewritten to MapEdit" + ); + let mut rss_vm = Vm::new(rss.program.with_local_count(rss.locals)); + assert_eq!( + rss_vm.run().expect("rss unit store should run"), + VmStatus::Halted + ); + assert_eq!(rss_vm.stack().last().cloned(), Some(Value::Bool(true))); +} diff --git a/tests/compiler/module_import_tests.rs b/tests/compiler/module_import_tests.rs index 0826684e..2b348f27 100644 --- a/tests/compiler/module_import_tests.rs +++ b/tests/compiler/module_import_tests.rs @@ -71,6 +71,99 @@ fn compile_source_file_module_override_path_redirects_import_spec() { remove_module_root(&root); } +#[test] +fn module_call_omitted_option_field_argument_materializes() { + let root = temp_module_root("vm_rustscript_module_omitted_option_test"); + let module_path = root.join("dep.rss"); + write_source( + &module_path, + r#" + struct Entry { name: string, note: Option } + pub fn take(entry: Entry) -> int { + match entry.note { + Option::Some(value) => value, + Option::None => 7, + } + } + "#, + "module source", + ); + let main_path = root.join("main.rss"); + write_source( + &main_path, + r#" + use self::dep as dep; + dep::take({ name: "x" }); + "#, + "main source", + ); + + let compiled = compile_source_file(&main_path).expect("module argument should compile"); + let mut vm = Vm::new(compiled.program); + assert_eq!( + vm.run().expect("module argument should run"), + VmStatus::Halted + ); + assert_eq!(vm.stack(), &[Value::Int(7)]); + + remove_module_root(&root); +} + +#[test] +fn rss_module_import_rejects_legacy_nullable_export() { + let root = temp_module_root("vm_rustscript_module_nullable_import_test"); + let module_path = root.join("dep.rss"); + write_source( + &module_path, + r#" + pub fn value() -> int? { 1 } + "#, + "nullable module source", + ); + let main_path = root.join("main.rss"); + let main_source = r#" + use self::dep as dep; + dep::value(); + "#; + write_source(&main_path, main_source, "main source"); + + let error = match compile_source_file(&main_path) { + Ok(_) => { + panic!("a legacy nullable .rss export must be rejected at its own import boundary") + } + Err(error) => error, + }; + match error { + vm::SourcePathError::SourceWithMap { + error: vm::SourceError::Parse(parse), + .. + } => { + assert!( + parse.message.contains("RSS rejects T?") && parse.message.contains("dep.rss"), + "expected the .rss nullable rejection with module path, got {parse}" + ); + } + error => panic!("expected a source-aware module parse error, got {error:?}"), + } + + write_source( + &module_path, + r#" + pub fn value() -> int { 1 } + "#, + "concrete module source", + ); + let compiled = compile_source_file(&main_path).expect("concrete .rss import should compile"); + let mut vm = Vm::new(compiled.program); + assert_eq!( + vm.run().expect("concrete .rss import should run"), + VmStatus::Halted + ); + assert_eq!(vm.stack(), &[Value::Int(1)]); + + remove_module_root(&root); +} + #[test] fn nested_module_override_parse_error_preserves_source_text_and_path() { let root = temp_module_root("vm_rustscript_nested_override_error_test"); @@ -2474,7 +2567,7 @@ fn nested_local_call_in_call_arg_optional_key_and_unwrap_fallback_stays_compact( "struct Payload { values: [int] }\n\ fn helper(x: int) -> int { x + 1 }\n\ pub fn run(context: map) -> map {\n\ - let f = |x| x + 1;\n\ + let f: fn(int) -> int = |x| x + 1;\n\ let payload: Payload = { values: [1, 2, 3] };\n\ let a: int = helper(f(10));\n\ let b: int = payload?.values?.[f(20)].unwrap_or(-1);\n\ diff --git a/tests/compiler/type_inference_tests.rs b/tests/compiler/type_inference_tests.rs index 2afa3ec1..810b29a4 100644 --- a/tests/compiler/type_inference_tests.rs +++ b/tests/compiler/type_inference_tests.rs @@ -549,17 +549,14 @@ fn compiler_type_inference_runtime_cases_cover_schema_and_optional_flows() { let profile: Profile = { stats: { score: 41 } }; let score = profile?.stats?.score; - if score != null { - score + 1; - } else { - 0; - } + let narrowed = match score { + Option::Some(value) => value + 1, + Option::None => 0, + }; + narrowed; "#, vec![Value::Int(42)], - &[TypeMetadataExpectation::LastExact { - opcode: OpCode::Add, - expected: (ValueType::Int, ValueType::Int), - }], + &[], ), rustscript_type_inference_runtime_case( "Some match bindings expose the concrete inner type", @@ -567,17 +564,13 @@ fn compiler_type_inference_runtime_cases_cover_schema_and_optional_flows() { struct Data { values: [int] } let data: Data = { values: [41] }; let result = match data?.values?.[0] { - None => 0, - Some(value) => value + 1, - _ => 0, + Option::None => 0, + Option::Some(value) => value + 1, }; result; "#, vec![Value::Int(42)], - &[TypeMetadataExpectation::LastExact { - opcode: OpCode::Add, - expected: (ValueType::Int, ValueType::Int), - }], + &[], ), rustscript_type_inference_runtime_case( "local let bindings accept homogeneous array schemas", diff --git a/tests/dynamic_host_output_admission_tests.rs b/tests/dynamic_host_output_admission_tests.rs new file mode 100644 index 00000000..e23379e1 --- /dev/null +++ b/tests/dynamic_host_output_admission_tests.rs @@ -0,0 +1,62 @@ +#![cfg(feature = "runtime")] + +fn dynamic_output_catalog() -> std::sync::Arc { + let mut builder = vm::HostApiBuilder::new(); + builder.function(vm::HostFunctionSchema::with_return( + "demo::missing", + Vec::new(), + vm::HostTypeSchema::Null, + )); + builder.function(vm::HostFunctionSchema::with_return( + "demo::flag", + Vec::new(), + vm::HostTypeSchema::Bool, + )); + std::sync::Arc::new(builder.build().expect("dynamic output catalog")) +} + +fn reject_nested_dynamic_output(source: &str) { + let rejected = vm::compile_source_with_flavor_and_options( + source, + vm::SourceFlavor::RustScript, + vm::CompileSourceFileOptions::default().with_host_api_catalog(dynamic_output_catalog()), + ); + let message = match rejected { + Ok(_) => { + panic!("dynamic host output must be rejected in every structured position: {source}") + } + Err(error) => error.to_string(), + }; + assert!( + message.contains("dynamic or legacy-null"), + "expected the host-output admission diagnostic for {source}, got {message}" + ); +} + +#[test] +fn rss_rejects_dynamic_host_output_inside_match_arm() { + reject_nested_dynamic_output( + "use demo;\nlet value = match 1 { 1 => if true => { demo::missing(); 1 } else => { 2 }, _ => 2 };\n1;", + ); +} + +#[test] +fn rss_rejects_dynamic_host_output_inside_option_payload() { + reject_nested_dynamic_output( + "use demo;\nlet value = Option::Some(if true => { demo::missing(); 1 } else => { 2 });\nvalue;", + ); +} + +#[test] +fn rss_rejects_dynamic_host_output_inside_result_arm() { + reject_nested_dynamic_output( + "use demo;\nlet seed: Result = Result::Ok(1);\nlet value = match seed { Result::Ok(v) => if true => { demo::missing(); v } else => { 0 }, Result::Err(e) => 0 };\nvalue;", + ); +} + +#[test] +fn rss_rejects_dynamic_host_output_inside_method_callback_body() { + reject_nested_dynamic_output( + "use demo;\nlet value = Option::Some(1).map(|item| if true => { demo::missing(); item } else => { 0 });\nvalue;", + ); +} diff --git a/tests/example_tests.rs b/tests/example_tests.rs index 3fb42bbb..29fd49aa 100644 --- a/tests/example_tests.rs +++ b/tests/example_tests.rs @@ -96,7 +96,7 @@ struct Inner { c: int } struct Outer { b: Inner } let present: Outer = { b: { c: 7 } }; -let missing: Outer? = null; +let missing: Option = Option::None; present?.b?.c.unwrap_or(0); missing?.b?.c.unwrap_or(0); diff --git a/tests/fixtures/map_json/generic_unit_insert.rss b/tests/fixtures/map_json/generic_unit_insert.rss new file mode 100644 index 00000000..e21a47fc --- /dev/null +++ b/tests/fixtures/map_json/generic_unit_insert.rss @@ -0,0 +1,9 @@ +fn insert_unit(values: map, key: string, value: T) -> map { + let mut values = values; + values[key] = value; + values +} + +let mut values: map<()> = {}; +values = insert_unit::<()>(values, "present", ()); +values.has("present"); diff --git a/tests/fixtures/map_json/option_unit_field_encode.rss b/tests/fixtures/map_json/option_unit_field_encode.rss new file mode 100644 index 00000000..163ebe7c --- /dev/null +++ b/tests/fixtures/map_json/option_unit_field_encode.rss @@ -0,0 +1 @@ +use json; struct Payload { value: Option<()> } let payload: Payload = {value: Option::Some(())}; json::encode(payload) == "{\"value\":null}"; diff --git a/tests/fixtures/map_json/optional_named_missing.rss b/tests/fixtures/map_json/optional_named_missing.rss new file mode 100644 index 00000000..5283d85a --- /dev/null +++ b/tests/fixtures/map_json/optional_named_missing.rss @@ -0,0 +1 @@ +use json; struct Node { next: Option } let decoded: Node = json::decode::("{\"next\":{}}"); let ok = match decoded.next { Option::Some(inner) => match inner.next { Option::None => true, Option::Some(_) => false }, Option::None => false }; ok; diff --git a/tests/fixtures/map_json/recursive_named_decode.rss b/tests/fixtures/map_json/recursive_named_decode.rss new file mode 100644 index 00000000..c954a54a --- /dev/null +++ b/tests/fixtures/map_json/recursive_named_decode.rss @@ -0,0 +1,2 @@ +use json; +struct Node { next: Option } let decoded: Node = json::decode::("{\"next\":{\"next\":null}}"); let ok = match decoded.next { Option::Some(inner) => match inner.next { Option::None => true, Option::Some(_) => false }, Option::None => false }; ok; diff --git a/tests/fixtures/map_json/required_named_missing.rss b/tests/fixtures/map_json/required_named_missing.rss new file mode 100644 index 00000000..85e3051a --- /dev/null +++ b/tests/fixtures/map_json/required_named_missing.rss @@ -0,0 +1 @@ +use json; struct Node { id: int, next: Option } let decoded: Node = json::decode::("{\"next\":{\"next\":null}}"); decoded.id; diff --git a/tests/fixtures/map_json/unit_field_encode.rss b/tests/fixtures/map_json/unit_field_encode.rss new file mode 100644 index 00000000..c6c36fd1 --- /dev/null +++ b/tests/fixtures/map_json/unit_field_encode.rss @@ -0,0 +1,2 @@ +use json; +struct UnitField { value: () } let payload: UnitField = {value: ()}; json::encode(payload) == "{\"value\":null}"; diff --git a/tests/fixtures/option_sdk_signature.rs b/tests/fixtures/option_sdk_signature.rs new file mode 100644 index 00000000..50029320 --- /dev/null +++ b/tests/fixtures/option_sdk_signature.rs @@ -0,0 +1,135 @@ +/// Echoes a logical guest Option with a typed array payload. +#[pd_host_function(name = "option_sdk::fixture")] +fn fixture(value: GuestOption>) -> VmResult>> { + Ok(value) +} + +/// Returns every nested Option state, including an unselected None. +#[pd_host_function(name = "option_sdk::nested")] +fn nested(mode: i64) -> VmResult>> { + Ok(match mode { + 0 => GuestOption::None, + 1 => GuestOption::Some(GuestOption::None), + _ => GuestOption::Some(GuestOption::Some(SdkBox { value: 7 })), + }) +} + +/// Returns the logical Unit value through the typed host boundary. +#[pd_host_function(name = "option_sdk::unit")] +fn unit() -> VmResult<()> { + Ok(()) +} + +/// Observes the selected Option payload without erasing its guest schema. +#[pd_host_function(name = "option_sdk::inspect")] +fn inspect(vm: &mut Vm, value: GuestOption>) -> i64 { + let calls = vm + .host_context() + .module_state::() + .copied() + .unwrap_or(0); + vm.host_context().set_module_state(calls + 1); + match value { + GuestOption::Some(values) => values.into_iter().sum(), + GuestOption::None => -1, + } +} + +/// Echoes an Option of a guest Result. +#[pd_host_function(name = "option_sdk::option_result")] +fn option_result( + value: GuestOption>, +) -> VmResult>> { + Ok(value) +} + +/// Echoes a guest Result containing a guest Option. +#[pd_host_function(name = "option_sdk::result_option")] +fn result_option( + value: GuestResult, String>, +) -> VmResult, String>> { + Ok(value) +} + +/// Echoes a typed callable payload nested inside a logical guest Option. +#[pd_host_function(name = "option_sdk::callable_echo")] +fn callable_echo( + value: GuestOption i64>>, +) -> VmResult i64>>> { + Ok(value) +} + +/// Returns an exported callable as `Some`, or `None` without resolving it. +#[pd_host_function(name = "option_sdk::callable_pick")] +fn callable_pick( + vm: &mut Vm, + present: bool, +) -> VmResult i64>>> { + if !present { + return Ok(GuestOption::None); + } + let value = vm.resolve_exported_callable("double")?; + let callable = i64> as FromVmValue>::from_vm_value(&value, "callable")?; + Ok(GuestOption::Some(callable)) +} + +/// Reports whether the Option carries a callable without invoking it. +#[pd_host_function(name = "option_sdk::callable_count")] +fn callable_count(value: GuestOption i64>>) -> i64 { + match value { + GuestOption::Some(_) => 1, + GuestOption::None => 0, + } +} + +/// Echoes a Unit-returning callable payload nested in a logical Option. +#[pd_host_function(name = "option_sdk::callable_unit_echo")] +fn callable_unit_echo( + value: GuestOption ()>>, +) -> VmResult ()>>> { + Ok(value) +} + +/// Echoes a Unit callable declared with the omitted Rust return spelling. +#[pd_host_function(name = "option_sdk::callable_unit_default_echo")] +fn callable_unit_default_echo( + value: GuestOption>, +) -> VmResult>> { + Ok(value) +} + +/// Echoes a multi-argument callable payload with a Boolean parameter. +#[pd_host_function(name = "option_sdk::callable_pair_echo")] +fn callable_pair_echo( + value: GuestOption i64>>, +) -> VmResult i64>>> { + Ok(value) +} + +/// Publishes only the selected owned resource. +#[pd_host_function(name = "option_sdk::resource_make")] +fn resource_make(present: bool) -> VmResult>> { + Ok(if present { + CREATED.fetch_add(1, Ordering::SeqCst); + GuestOption::Some(ResourceOwned::new(OptionSuccess(23))) + } else { + GuestOption::None + }) +} + +/// Takes precisely the active owned resource. +#[pd_host_function(name = "option_sdk::resource_take")] +fn resource_take(value: GuestOption>) -> i64 { + match value { + GuestOption::Some(value) => value.into_inner().0, + GuestOption::None => -1, + } +} + +/// Reports the live resource count before VM teardown. +#[pd_host_function(name = "option_sdk::resource_observe")] +fn resource_observe(vm: &mut Vm) -> i64 { + let count = vm.host_context().resource_count(); + OBSERVED_RESOURCE_COUNT.store(count, Ordering::SeqCst); + count as i64 +} diff --git a/tests/fixtures/result_sdk_signature.rs b/tests/fixtures/result_sdk_signature.rs index 0467b4cc..a5f8ce20 100644 --- a/tests/fixtures/result_sdk_signature.rs +++ b/tests/fixtures/result_sdk_signature.rs @@ -1,8 +1,8 @@ -/// Echo a nullable typed array guest union. +/// Echo a public guest Option array inside a guest Result. #[pd_host_function(name = "result_sdk::fixture")] fn fixture( - value: GuestResult>, String>, -) -> VmResult>, String>> { + value: GuestResult>, String>, +) -> VmResult>, String>> { Ok(value) } @@ -16,9 +16,9 @@ fn named(ok: bool) -> VmResult>> { }) } -/// Observes the selected optional array without losing its guest schema. +/// Observes the selected guest Option array without losing its guest schema. #[pd_host_function(name = "result_sdk::inspect")] -fn inspect(vm: &mut Vm, value: GuestResult>, String>) -> i64 { +fn inspect(vm: &mut Vm, value: GuestResult>, String>) -> i64 { let calls = vm .host_context() .module_state::() @@ -26,8 +26,8 @@ fn inspect(vm: &mut Vm, value: GuestResult>, String>) -> i64 { .unwrap_or(0); vm.host_context().set_module_state(calls + 1); match value { - GuestResult::Ok(Some(values)) => values.into_iter().sum(), - GuestResult::Ok(None) => -1, + GuestResult::Ok(GuestOption::Some(values)) => values.into_iter().sum(), + GuestResult::Ok(GuestOption::None) => -1, GuestResult::Err(_) => -2, } } diff --git a/tests/frontend_logical_scope_tests.rs b/tests/frontend_logical_scope_tests.rs new file mode 100644 index 00000000..87328f99 --- /dev/null +++ b/tests/frontend_logical_scope_tests.rs @@ -0,0 +1,67 @@ +#![cfg(feature = "runtime")] + +use vm::compile_source; + +#[path = "support/legacy_nullable_plugin.rs"] +mod legacy_nullable_plugin; + +fn plugin_print_sink(source: &str) -> Vec { + let compiled = vm::compile_source_with_flavor_and_options( + source, + legacy_nullable_plugin::legacy_nullable_flavor(), + legacy_nullable_plugin::legacy_nullable_options(vm::CompileSourceFileOptions::default()), + ) + .unwrap_or_else(|error| panic!("plugin source should compile: {source}\n{error}")); + let mut machine = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + let lines = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let recorded = std::sync::Arc::clone(&lines); + machine.set_runtime_print_sink(move |rendered| { + recorded.lock().expect("sink").push(rendered); + }); + assert_eq!( + machine.run().expect("plugin source run"), + vm::VmStatus::Halted + ); + lines.lock().expect("sink").clone() +} + +#[test] +fn plugin_root_print_keeps_plain_legacy_null_display() { + assert_eq!(plugin_print_sink("print(());"), ["null".to_string()]); +} + +#[test] +fn rss_root_print_unit_keeps_logical_display() { + let source = "print(());"; + let compiled = compile_source(source).expect("RSS unit print should compile"); + let mut machine = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + let lines = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let recorded = std::sync::Arc::clone(&lines); + machine.set_runtime_print_sink(move |rendered| { + recorded.lock().expect("sink").push(rendered); + }); + assert_eq!(machine.run().expect("RSS unit print"), vm::VmStatus::Halted); + assert_eq!(lines.lock().expect("sink").as_slice(), ["()".to_string()]); +} + +#[test] +fn plugin_root_keeps_legacy_typed_json_decode_route() { + let source = "use json;\nlet decoded: int? = json::decode::(\"null\");\ndecoded;\n"; + let compiled = vm::compile_source_with_flavor_and_options( + source, + legacy_nullable_plugin::legacy_nullable_flavor(), + legacy_nullable_plugin::legacy_nullable_options(vm::CompileSourceFileOptions::default()), + ) + .unwrap_or_else(|error| { + panic!("plugin legacy nullable decode should compile: {source}\n{error}") + }); + let mut machine = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + assert_eq!( + machine.run().expect("plugin legacy decode should run"), + vm::VmStatus::Halted + ); + assert_eq!(machine.stack().last(), Some(&vm::Value::Null)); +} diff --git a/tests/host_option_compile_parity_tests.rs b/tests/host_option_compile_parity_tests.rs new file mode 100644 index 00000000..40cfd240 --- /dev/null +++ b/tests/host_option_compile_parity_tests.rs @@ -0,0 +1,126 @@ +#![cfg(all(feature = "runtime", feature = "sqlite"))] + +use vm::{ + CompileSourceFileOptions, SourceFlavor, analyze_source_from_string_with_options, + compile_source_with_flavor_and_options, standard_host_catalog, +}; + +const OMITTED: &str = r#"use sqlite; +fn main() { + let db = sqlite::open({}); + sqlite::query(&db, "SELECT 1", [], {}); + true; +} +"#; + +const EXPLICIT: &str = r#"use sqlite; +fn main() { + let db = sqlite::open({ + path: Option::Some(""), + mode: Option::Some(""), + root: Option::Some(""), + limits: Option::Some({ max_rows: Option::Some(0), busy_timeout_ms: Option::Some(0) }) + }); + sqlite::query(&db, "SELECT 1", [], { max_rows: Option::Some(0), busy_timeout_ms: Option::Some(0) }); + let statement: SqliteStatement = { + sql: "SELECT 1", + query: Option::Some(false), + params: Option::None, + limits: Option::None + }; + statement.sql == "SELECT 1"; +} +"#; + +const WRONG_TYPE: &str = r#"use sqlite; +fn main() { + sqlite::open({ path: Option::Some(false) }); + true; +} +"#; + +fn catalog_options() -> CompileSourceFileOptions { + CompileSourceFileOptions::new().with_host_api_catalog(standard_host_catalog()) +} + +fn analyze_messages(source: &str) -> Vec { + let model = analyze_source_from_string_with_options("parity.rss", source, catalog_options()) + .expect("configured catalog analysis should parse"); + model + .diagnostics() + .into_iter() + .map(|diagnostic| diagnostic.message) + .collect() +} + +fn compile_message(source: &str) -> Result<(), String> { + compile_source_with_flavor_and_options(source, SourceFlavor::RustScript, catalog_options()) + .map(|_| ()) + .map_err(|error| error.to_string()) +} + +#[test] +fn sqlite_omitted_and_explicit_options_match_across_compile_and_analyze() { + let omitted_diagnostics = analyze_messages(OMITTED); + assert!( + omitted_diagnostics.is_empty(), + "analyze must accept omitted sqlite options: {omitted_diagnostics:?}" + ); + compile_message(OMITTED).expect("compile must accept the same omitted sqlite options"); + + let explicit_diagnostics = analyze_messages(EXPLICIT); + assert!( + explicit_diagnostics.is_empty(), + "analyze must accept Some(0)/Some(empty)/Some(false): {explicit_diagnostics:?}" + ); + compile_message(EXPLICIT) + .expect("compile must accept the same explicit Some(0)/Some(empty)/Some(false)"); +} + +#[test] +fn sqlite_wrong_option_type_fails_on_compile_and_analyze() { + let analyzed = analyze_messages(WRONG_TYPE); + assert!( + !analyzed.is_empty(), + "analyze must reject Some(false) for a string option" + ); + let compiled = + compile_message(WRONG_TYPE).expect_err("compile must reject the same wrong type"); + assert!( + compiled.contains("string") || compiled.contains("Option") || compiled.contains("path"), + "compile rejection should name the option type, got {compiled}" + ); +} + +#[test] +fn sqlite_close_takes_owned_connection_and_query_borrows() { + let owned = r#"use sqlite; +fn main() { + let db = sqlite::open({}); + sqlite::query(&db, "SELECT 1", [], {}); + sqlite::close(db); + true; +} +"#; + assert!( + analyze_messages(owned).is_empty(), + "owned close should analyze" + ); + compile_message(owned).expect("owned close should compile"); + + let borrowed_close = r#"use sqlite; +fn main() { + let db = sqlite::open({}); + sqlite::close(&db); + true; +} +"#; + assert!( + !analyze_messages(borrowed_close).is_empty(), + "close must not accept a borrow" + ); + assert!( + compile_message(borrowed_close).is_err(), + "compile must reject borrowed close" + ); +} diff --git a/tests/http_buffered_resource_tests.rs b/tests/http_buffered_resource_tests.rs index 6a291699..fbb3f1f1 100644 --- a/tests/http_buffered_resource_tests.rs +++ b/tests/http_buffered_resource_tests.rs @@ -267,6 +267,90 @@ fn buffered_resource_request_reads_status_body_and_repeated_headers() { assert_eq!(vm.host_context().resource_count(), 0); } +#[test] +fn buffered_resource_distinguishes_a_missing_header_from_a_present_empty_value() { + let listener = TcpListener::bind(("127.0.0.1", 0)).expect("bind loopback"); + let port = listener.local_addr().unwrap().port(); + let server = thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + stream + .set_read_timeout(Some(std::time::Duration::from_secs(5))) + .unwrap(); + let mut request = Vec::new(); + let mut chunk = [0; 1024]; + loop { + let read = stream.read(&mut chunk).unwrap(); + if read == 0 { + break; + } + request.extend_from_slice(&chunk[..read]); + if request.windows(4).any(|window| window == b"\r\n\r\n") { + break; + } + } + stream + .write_all(b"HTTP/1.1 200 OK\r\nX-Empty: \r\nContent-Length: 0\r\n\r\n") + .unwrap(); + }); + let source = format!( + r#" + use http; + let req = http::request::new("GET", "http://127.0.0.1:{port}/"); + let response = http::client::request(req); + [http::response::header_values(&response, "X-Missing"), + http::response::header_values(&response, "X-Empty"), + http::response::header_names(&response)];"# + ); + let program = compile_source(&source) + .expect("header presence program compiles against the standard catalog") + .program; + let mut vm = Vm::new(program); + http_test_bridge::install(&mut vm); + vm.configure_http(HttpConfig { + allowed_schemes: vec!["http".to_string()], + allowed_hosts: vec!["127.0.0.1".to_string()], + allowed_ports: vec![port], + allow_private_ips: true, + ..Default::default() + }) + .unwrap(); + HostFunctionRegistry::new().bind_vm_cached(&mut vm).unwrap(); + let mut state = vm.run().expect("run request"); + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + loop { + state = match state { + VmStatus::Halted => break, + VmStatus::Yielded => vm.resume().unwrap(), + VmStatus::Waiting(_) => { + runtime + .block_on(vm.await_waiting_host_op()) + .expect("await worker"); + vm.resume().expect("resume worker") + } + }; + } + server.join().unwrap(); + assert_eq!( + vm.stack().last(), + Some(&Value::array(vec![ + Value::array(Vec::new()), + Value::array(vec![Value::string("")]), + Value::array(vec![ + Value::string("content-length"), + Value::string("x-empty") + ]) + ])), + "stack: {:?}", + vm.stack() + ); + assert_eq!(vm.host_context().resource_count(), 1); + vm.reset_for_reuse().unwrap(); + assert_eq!(vm.host_context().resource_count(), 0); +} + #[test] fn buffered_resource_header_values_recover_raw_response_octets() { let listener = TcpListener::bind(("127.0.0.1", 0)).expect("bind loopback"); diff --git a/tests/http_named_struct_contract_tests.rs b/tests/http_named_struct_contract_tests.rs index c0940fff..e2dfd98f 100644 --- a/tests/http_named_struct_contract_tests.rs +++ b/tests/http_named_struct_contract_tests.rs @@ -146,6 +146,74 @@ fn http_catalog_declares_resources_and_no_named_structs() { } } +fn assert_no_public_nullable_schema(schema: &HostTypeSchema, owner: &str) { + match schema { + HostTypeSchema::Null + | HostTypeSchema::Optional(_) + | HostTypeSchema::Option(_) + | HostTypeSchema::Unknown + | HostTypeSchema::Map(_) => panic!( + "HTTP public surface must not publish a nullable/dynamic schema, {owner} uses {schema:?}" + ), + HostTypeSchema::Array(inner) => assert_no_public_nullable_schema(inner, owner), + HostTypeSchema::Callable { params, result } => { + for param in params { + assert_no_public_nullable_schema(param, owner); + } + assert_no_public_nullable_schema(result, owner); + } + HostTypeSchema::Named { fields, .. } => { + for field in fields { + assert_no_public_nullable_schema(&field.ty, owner); + } + } + HostTypeSchema::Result { ok, err } => { + assert_no_public_nullable_schema(ok, owner); + assert_no_public_nullable_schema(err, owner); + } + HostTypeSchema::Unit + | HostTypeSchema::Int + | HostTypeSchema::Float + | HostTypeSchema::Number + | HostTypeSchema::Bool + | HostTypeSchema::String + | HostTypeSchema::Bytes + | HostTypeSchema::Resource(_) => {} + } +} + +#[test] +fn public_http_catalog_uses_unit_mutators_and_no_nullable_carriers() { + let catalog = http_host_catalog(); + assert_eq!( + catalog.functions().len(), + 23, + "the installed HTTP catalog must publish 23 descriptor declarations" + ); + + for name in [ + "http::request::set_header", + "http::request::set_body_text", + "http::request::set_body_bytes", + ] { + let schema = catalog + .function(name) + .unwrap_or_else(|| panic!("missing {name}")); + assert_eq!( + schema.return_type, + HostTypeSchema::Unit, + "{name} must publish the logical Unit result, not Null or a nullable schema" + ); + } + + for function in catalog.functions() { + for param in &function.params { + assert_no_public_nullable_schema(¶m.ty, &function.name); + } + assert_no_public_nullable_schema(&function.return_type, &function.name); + } +} + #[test] fn buffered_request_takes_an_owned_builder_and_returns_a_response_resource() { let catalog = http_host_catalog(); diff --git a/tests/io_descriptor_install_tests.rs b/tests/io_descriptor_install_tests.rs index 966e35a3..371551ed 100644 --- a/tests/io_descriptor_install_tests.rs +++ b/tests/io_descriptor_install_tests.rs @@ -98,6 +98,50 @@ fn host_error(error: VmError) -> String { } } +/// The IO guest contract has no nullable or void route: absence is a domain +/// value (`false`, an empty string, or a host error), never an implicit +/// `Option`/`Optional`/`Null` carrier, and every void-free call keeps its +/// concrete return type. +fn assert_no_nullable_or_void_route(path: &str, schema: &HostTypeSchema) { + match schema { + HostTypeSchema::Null | HostTypeSchema::Unit => { + panic!("io public schema {path} exposes a void/null route: {schema:?}") + } + HostTypeSchema::Optional(_) | HostTypeSchema::Option(_) => { + panic!("io public schema {path} exposes a nullable route: {schema:?}") + } + HostTypeSchema::Array(inner) => { + assert_no_nullable_or_void_route(&format!("{path}[]"), inner) + } + HostTypeSchema::Map(inner) => { + assert_no_nullable_or_void_route(&format!("{path}{{}}"), inner) + } + HostTypeSchema::Callable { params, result } => { + for (index, param) in params.iter().enumerate() { + assert_no_nullable_or_void_route(&format!("{path}.callback_param[{index}]"), param); + } + assert_no_nullable_or_void_route(&format!("{path}.callback_result"), result); + } + HostTypeSchema::Result { ok, err } => { + assert_no_nullable_or_void_route(&format!("{path}.ok"), ok); + assert_no_nullable_or_void_route(&format!("{path}.err"), err); + } + HostTypeSchema::Named { fields, .. } => { + for field in fields { + assert_no_nullable_or_void_route(&format!("{path}.{}", field.name), &field.ty); + } + } + HostTypeSchema::Unknown + | HostTypeSchema::Int + | HostTypeSchema::Float + | HostTypeSchema::Number + | HostTypeSchema::Bool + | HostTypeSchema::String + | HostTypeSchema::Bytes + | HostTypeSchema::Resource(_) => {} + } +} + /// A unique, empty scratch directory for one test run. fn scratch_dir(label: &str) -> PathBuf { let nonce = SystemTime::now() @@ -227,6 +271,33 @@ fn io_module_descriptors_carry_coherent_schema_binding_and_effects() { // -------------------------------------------------------------------- install +#[test] +fn io_owned_public_schemas_have_no_nullable_or_void_route() { + for descriptor in owned_descriptors().values() { + assert_no_nullable_or_void_route( + &format!("{}(return)", descriptor.schema.name), + &descriptor.schema.return_type, + ); + for param in &descriptor.schema.params { + assert_no_nullable_or_void_route( + &format!("{}({})", descriptor.schema.name, param.name), + ¶m.ty, + ); + } + } + + // The domain keeps its actual present/absent policy: a missing path is + // `false`, an empty file is an empty string, and close reports its Boolean + // result. None of these is a logical nullable route. + let owned = owned_descriptors(); + assert_eq!(owned["io::exists"].schema.return_type, HostTypeSchema::Bool); + assert_eq!( + owned["io::read_all"].schema.return_type, + HostTypeSchema::String + ); + assert_eq!(owned["io::close"].schema.return_type, HostTypeSchema::Bool); +} + #[test] fn io_module_install_grants_its_owned_functions_on_a_restricted_registry() { let names: Vec = owned_descriptors().keys().cloned().collect(); @@ -287,7 +358,6 @@ fn io_module_install_rejects_a_descriptor_list_whose_adapters_disagree() { /// The IO calls the round-trip script makes. Each one is dispatched through the /// installed descriptor and must be driven to completion as a pending host /// operation. -#[cfg(feature = "async")] const ROUND_TRIP_IO_CALLS: usize = 7; /// Submitted-future count of the async host driver; stays zero on the inline @@ -452,13 +522,17 @@ fn install_async_driver(_vm: &mut Vm) -> SubmittedOps { /// backend that is compiled in: the async backend resolves each one through the /// submitted-future bridge, while the synchronous backend yields no pending /// operation. -fn assert_pending_ops_resolved_by_backend(submitted: &SubmittedOps, driven_ops: usize) { +fn assert_pending_ops_resolved_by_backend( + submitted: &SubmittedOps, + driven_ops: usize, + _minimum_driven: usize, +) { use std::sync::atomic::Ordering; let submitted = submitted.load(Ordering::SeqCst); #[cfg(feature = "async")] { assert!( - driven_ops >= ROUND_TRIP_IO_CALLS, + driven_ops >= _minimum_driven, "the async backend must yield a pending operation per IO call, drove {driven_ops}" ); assert_eq!( @@ -557,7 +631,7 @@ fn installed_io_descriptors_execute_open_write_read_close() { fs::read_to_string(&inside).expect("the written file must exist"), "descriptor-installed" ); - assert_pending_ops_resolved_by_backend(&submitted, run.driven_ops); + assert_pending_ops_resolved_by_backend(&submitted, run.driven_ops, ROUND_TRIP_IO_CALLS); assert_eq!( vm.host_context().resource_count(), 0, @@ -570,6 +644,52 @@ fn installed_io_descriptors_execute_open_write_read_close() { let _ = fs::remove_dir_all(&dir); } +#[test] +fn installed_io_descriptors_preserve_absent_false_and_empty_file_string() { + let dir = scratch_dir("absent-empty"); + let empty = dir.join("empty.txt"); + let missing = dir.join("missing.txt"); + fs::write(&empty, "").expect("the empty fixture should be writable"); + let (mut vm, submitted) = vm_with_installed_io(&format!( + r#" + let present = io::exists("{empty}"); + let absent = io::exists("{missing}"); + let handle = io::open("{empty}", "r"); + let contents = io::read_all(handle); + let closed = io::close(handle); + present && !absent && contents == "" && closed; + "#, + empty = empty.display(), + missing = missing.display() + )); + vm.configure_io(IoPolicy { + allowed_roots: vec![dir.display().to_string()], + ..IoPolicy::default() + }); + + let run = run_driven(&mut vm).expect("the installed absent/empty IO path must run"); + assert_eq!( + run.stack.last(), + Some(&Value::Bool(true)), + "a present empty file stays an empty string, a missing path stays false, and close stays Boolean" + ); + assert_pending_ops_resolved_by_backend(&submitted, run.driven_ops, 5); + assert_eq!( + vm.host_context().resource_count(), + 0, + "the explicitly closed file resource must be retired" + ); + assert!( + vm.execution_scope().operations().is_empty(), + "a completed absent/empty IO script must leave no pending operation behind" + ); + assert!( + !missing.exists(), + "reading absence must not create the missing path" + ); + let _ = fs::remove_dir_all(&dir); +} + #[test] fn installed_io_descriptors_reject_a_stale_handle_after_close() { let dir = scratch_dir("stale"); diff --git a/tests/jit/jit_tests.rs b/tests/jit/jit_tests.rs index c62d3b17..8af21445 100644 --- a/tests/jit/jit_tests.rs +++ b/tests/jit/jit_tests.rs @@ -3,9 +3,12 @@ use std::{cell::Cell, sync::Arc}; use vm::{ BytecodeBuilder, CallOutcome, CallReturn, HostFunction, JitConfig, JitTraceTerminal, OpCode, Program, RegexCacheVmExt, Value, ValueType, Vm, VmStatus, VmYieldReason, builtin_call_index, - compile_source, disassemble_program, + compile_source, compile_source_with_flavor_and_options, disassemble_program, }; +#[path = "../support/legacy_nullable_plugin.rs"] +mod legacy_nullable_plugin; + fn native_jit_supported() -> bool { (cfg!(target_arch = "x86_64") && (cfg!(target_os = "windows") || (cfg!(unix) && !cfg!(target_os = "macos")))) @@ -3912,7 +3915,12 @@ fn trace_jit_specializes_same_local_map_set_through_loop_back() { values["key"]; "#; - let compiled = compile_source(source).expect("map-set trace compile should succeed"); + let compiled = compile_source_with_flavor_and_options( + source, + legacy_nullable_plugin::legacy_nullable_flavor(), + legacy_nullable_plugin::legacy_nullable_options(vm::CompileSourceFileOptions::new()), + ) + .expect("map-set trace compile should succeed"); let program = force_local_types( compiled.program, &[(0, ValueType::Map), (1, ValueType::Int)], diff --git a/tests/logical_presentation_tests.rs b/tests/logical_presentation_tests.rs new file mode 100644 index 00000000..b5081a5c --- /dev/null +++ b/tests/logical_presentation_tests.rs @@ -0,0 +1,186 @@ +#![cfg(feature = "runtime")] + +use std::sync::{Arc, Mutex}; +use vm::{TypeSchema, Value, Vm, VmStatus, compile_source, compile_source_for_repl_with_state}; + +fn run_prints(source: &str) -> (Vec, Vec) { + let compiled = compile_source(source).unwrap_or_else(|error| panic!("{source}\n{error}")); + let mut vm = Vm::new(compiled.program.with_local_count(compiled.locals)); + vm.set_fuel(100_000); + let lines = Arc::new(Mutex::new(Vec::::new())); + let sink = Arc::clone(&lines); + vm.set_runtime_print_sink(move |rendered| { + sink.lock().expect("print sink").push(rendered); + }); + assert_eq!(vm.run().expect(source), VmStatus::Halted); + (lines.lock().expect("printed").clone(), vm.stack().to_vec()) +} + +#[test] +fn direct_option_print_uses_the_expression_schema() { + let (printed, stack) = run_prints( + r#" + fn none() -> Option { Option::None; } + print(none()); + "#, + ); + assert_eq!(printed, ["None".to_string()]); + assert!(!stack.is_empty(), "print keeps the original value"); +} + +#[test] +fn reassigned_option_prints_the_later_value() { + let (printed, _) = run_prints( + r#" + let mut value: Option = Option::None; + value = Option::Some(7); + print(value); + "#, + ); + assert_eq!(printed, ["Some(7)".to_string()]); +} + +#[test] +fn function_frame_prints_the_parameter_schema() { + let (printed, _) = run_prints( + r#" + fn show(value: Option) { print(value); } + show(Option::Some(3)); + "#, + ); + assert_eq!(printed, ["Some(3)".to_string()]); +} + +#[test] +fn captured_option_prints_the_captured_schema() { + let (printed, _) = run_prints( + r#" + let value: Option = Option::Some(4); + fn show() { print(value); } + show(); + "#, + ); + assert_eq!(printed, ["Some(4)".to_string()]); +} + +#[test] +fn print_evaluates_a_logical_input_once() { + let (printed, stack) = run_prints( + r#" + let mut n = 0; + fn bump() -> int { n = n + 1; n; } + let printed: Option = print(Option::Some(bump())); + match printed { Option::Some(value) => value, Option::None => 0 }; + "#, + ); + assert_eq!(printed, ["Some(1)".to_string()]); + assert_eq!(stack.last(), Some(&Value::Int(1))); +} + +#[test] +fn type_of_option_and_result_uses_the_logical_label() { + let (_, option_stack) = run_prints("let missing: Option = Option::None; type(missing);"); + assert_eq!(option_stack.last(), Some(&Value::string("Option"))); + let (_, result_stack) = + run_prints("let value: Result = Result::Ok(1); type(value);"); + assert_eq!(result_stack.last(), Some(&Value::string("Result"))); +} + +#[test] +fn format_print_renders_option_payload() { + let (printed, _) = + run_prints("let missing: Option = Option::None; print(\"{}\", missing);"); + assert_eq!(printed, ["None".to_string()]); +} + +#[test] +fn user_print_keeps_its_own_dispatch() { + let (printed, stack) = run_prints( + r#" + fn print(value: int) -> int { value + 1; } + print(1); + "#, + ); + assert!(printed.is_empty(), "{printed:?}"); + assert_eq!(stack.last(), Some(&Value::Int(2))); +} + +#[test] +fn repl_result_distinguishes_absence_unit_and_option() { + let absent = compile_source_for_repl_with_state("let x = 1;", &[]).expect("let"); + assert!(absent.result_schema.is_none()); + + let unit = compile_source_for_repl_with_state("();", &[]).expect("unit"); + assert!(matches!(unit.result_schema, Some(TypeSchema::Unit))); + + let option = compile_source_for_repl_with_state( + "let missing: Option = Option::None; missing;", + &[], + ) + .expect("option"); + assert!(matches!(option.result_schema, Some(TypeSchema::Option(_)))); + + let trailing = compile_source_for_repl_with_state( + "let missing: Option = Option::None; missing; let x = 1;", + &[], + ) + .expect("trailing let"); + assert!( + matches!(trailing.result_schema, Some(TypeSchema::Option(_))), + "trailing let must not clear the previous expression schema: {:?}", + trailing.result_schema + ); +} + +#[test] +fn print_operand_keeps_checked_source_position_schema() { + let source = "\ +let value: Option = Option::Some(1); +print(value); +let plain = { tag: false }; +print(plain); +fn show(item: Option) { print(item); } +show(value); +"; + let model = vm::analyze_source_from_string_with_options( + "main.rss", + source, + vm::CompileSourceFileOptions::default(), + ) + .unwrap_or_else(|error| panic!("presentation source should analyze: {error}")); + assert!( + model.diagnostics().is_empty(), + "checked presentation source has diagnostics: {:?}", + model.diagnostics() + ); + let parsed = model + .ir() + .parsed_semantic_index + .as_ref() + .expect("parser semantic index"); + for name in ["value", "plain", "item"] { + let site = parsed + .local_refs + .iter() + .find(|site| site.name == name) + .unwrap_or_else(|| panic!("missing source ref for {name}")); + assert_eq!( + &source[site.ident_span.lo..site.ident_span.hi], + name, + "{name} source span must be the identifier" + ); + let schema = model + .inferred_schema_at(vm::SourcePosition::new(0, site.ident_span.lo)) + .unwrap_or_else(|| panic!("missing checked schema at {name}")); + match name { + "plain" => assert!( + matches!(schema, TypeSchema::Object(_)), + "ordinary object must stay an object at its source position, got {schema:?}" + ), + _ => assert!( + matches!(schema, TypeSchema::Option(_)), + "{name} must stay Option at its print source position, got {schema:?}" + ), + } + } +} diff --git a/tests/map_json_behavior_tests.rs b/tests/map_json_behavior_tests.rs new file mode 100644 index 00000000..506c7cec --- /dev/null +++ b/tests/map_json_behavior_tests.rs @@ -0,0 +1,733 @@ +#![cfg(feature = "runtime")] + +use vm::{Value, Vm, VmStatus, compile_source}; + +fn eval(source: &str) -> Value { + let compiled = compile_source(source).expect("source should compile"); + let mut vm = Vm::new(compiled.program.with_local_count(compiled.locals)); + vm.set_fuel(100_000); + assert_eq!( + vm.run().expect("program should run"), + VmStatus::Halted, + "program should halt" + ); + vm.stack() + .last() + .cloned() + .expect("program should leave a value") +} + +#[test] +fn inferred_empty_map_keeps_unit_entry() { + let source = r#" + let mut values = {}; + values["present"] = (); + values.has("present"); + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn option_map_callback_encodes_nested_payload_as_natural_json() { + let source = r#" + use json; + let src: Option> = Option::Some(Option::Some(1)); + let encoded = src.map(|item| json::encode(item)); + match encoded { + Option::Some(text) => text == "1", + Option::None => false, + }; + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn result_map_callback_encodes_nested_payload_as_natural_json() { + let source = r#" + use json; + let src: Result, string> = Result::Ok(Option::Some(1)); + let encoded = src.map(|item| json::encode(item)); + match encoded { + Result::Ok(text) => text == "1", + Result::Err(error) => error == "", + }; + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn option_and_then_callback_encodes_nested_payload_as_natural_json() { + let source = r#" + use json; + let src: Option> = Option::Some(Option::Some(1)); + let encoded = src.and_then(|item| Option::Some(json::encode(item))); + match encoded { + Option::Some(text) => text == "1", + Option::None => false, + }; + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn record_factory_remove_is_rejected_at_admission() { + let source = r#" + struct Entry { value: int } + fn make() -> Entry { {value: 7} } + let broken = make().remove("value"); + broken.value; + "#; + let message = match compile_source(source) { + Err(error) => error.to_string(), + Ok(_) => panic!("record remove must be rejected"), + }; + assert!( + message.contains("E_MAP_REMOVE_RECORD"), + "expected typed map removal guidance, got {message}" + ); + assert!( + message.contains("map<"), + "expected map guidance, got {message}" + ); +} + +#[test] +fn recursive_named_decode_keeps_finite_option_chain() { + let source = include_str!("fixtures/map_json/recursive_named_decode.rss"); + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn missing_optional_named_field_decodes_as_none() { + let source = include_str!("fixtures/map_json/optional_named_missing.rss"); + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn missing_required_named_field_is_rejected() { + let source = include_str!("fixtures/map_json/required_named_missing.rss"); + let compiled = compile_source(source).expect("required-field source should compile"); + let mut vm = Vm::new(compiled.program.with_local_count(compiled.locals)); + vm.set_fuel(100_000); + let error = vm.run().expect_err("missing required id must fail"); + let message = error.to_string(); + assert!( + message.contains("missing a required field"), + "expected required-field rejection, got {message}" + ); +} + +#[test] +fn unit_field_encodes_as_null() { + let source = include_str!("fixtures/map_json/unit_field_encode.rss"); + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn option_unit_field_some_encodes_present_null() { + let source = include_str!("fixtures/map_json/option_unit_field_encode.rss"); + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn generic_unit_insert_keeps_entry() { + let source = include_str!("fixtures/map_json/generic_unit_insert.rss"); + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn inferred_unit_insert_survives_vmbc_reload() { + let source = "let mut values = {}; values[\"present\"] = (); values.has(\"present\");"; + let compiled = compile_source(source).expect("source should compile"); + let wire = vm::vmbc::encode_program(&compiled.program).expect("program should encode"); + let reloaded = vm::vmbc::decode_program(&wire).expect("program should reload"); + let mut vm = Vm::new(reloaded.with_local_count(compiled.locals)); + vm.set_fuel(100_000); + assert_eq!( + vm.run().expect("reloaded program should run"), + VmStatus::Halted + ); + assert_eq!(vm.stack().last().cloned(), Some(Value::Bool(true))); +} + +#[test] +fn closure_contextual_unit_map_store_keeps_entry() { + let source = r#" + let set: fn(map<()>) -> bool = |m| if true => { m["x"] = (); m.has("x") } else => { false }; + set({}); + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn pattern_moved_unit_map_store_keeps_entry() { + let source = r#"let maybe: Option> = Option::Some({}); let ok = match maybe { Option::Some(m) => if true => { let mut values = m; values["x"] = (); values.has("x") } else => { false }, Option::None => false }; ok;"#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn option_carrier_index_store_is_rejected() { + let source = r#"let mut o: Option = Option::Some(1); o["x"] = ();"#; + let message = match compile_source(source) { + Err(error) => error.to_string(), + Ok(_) => panic!("Option index store must be rejected"), + }; + assert!( + message.contains("cannot store into an Option or Result"), + "expected carrier refusal, got {message}" + ); +} + +#[test] +fn result_carrier_index_store_is_rejected() { + let source = r#"let mut r: Result = Result::Ok(1); r["x"] = 2;"#; + let message = match compile_source(source) { + Err(error) => error.to_string(), + Ok(_) => panic!("Result index store must be rejected"), + }; + assert!( + message.contains("cannot store into an Option or Result"), + "expected carrier refusal, got {message}" + ); +} + +#[test] +fn option_carrier_stays_unmodified_without_store() { + let source = r#"let o: Option = Option::Some(1); o.has("x");"#; + assert_eq!(eval(source), Value::Bool(false)); +} + +#[test] +fn result_pattern_moved_unit_map_store_keeps_entry() { + let source = r#"let maybe: Result, string> = Result::Ok({}); let ok = match maybe { Result::Ok(m) => if true => { let mut values = m; values["x"] = (); values.has("x") } else => { false }, Result::Err(_) => false }; ok;"#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn loop_unit_store_keeps_entry_after_iterations() { + let source = r#"let mut values: map<()> = {}; let mut n = 0; while n < 2 { values["x"] = (); n = n + 1; } values.has("x");"#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn zero_iteration_loop_does_not_invent_unit_entry() { + let source = r#"let mut values: map<()> = {}; let mut n = 0; while n < 0 { values["x"] = (); n = n + 1; } values.has("x");"#; + assert_eq!(eval(source), Value::Bool(false)); +} + +#[test] +fn typed_map_remove_drops_key() { + let source = r#"let mut values: map = {}; values["x"] = 1; let updated = values.remove("x"); updated.has("x");"#; + assert_eq!(eval(source), Value::Bool(false)); +} + +fn reject(source: &str, needle: &str) { + let message = match compile_source(source) { + Err(error) => error.to_string(), + Ok(_) => panic!("source must be rejected: {source}"), + }; + assert!( + message.contains(needle), + "expected {needle} in diagnostic, got {message}" + ); +} + +fn call_indexes(program: &vm::Program, builtin: vm::BuiltinFunction) -> usize { + let index = builtin.call_index().to_le_bytes(); + let mut count = 0; + let code = &program.code; + let mut offset = 0; + while offset + 2 < code.len() { + if code[offset] == vm::OpCode::Call as u8 && code[offset + 1..offset + 3] == index { + count += 1; + } + offset += 1; + } + count +} + +#[test] +fn factory_returned_map_keeps_unit_entry() { + let source = r#" + fn make() { + let mut values = {}; + values + } + let mut values = make(); + values["present"] = (); + values.has("present"); + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn object_field_update_keeps_unit_entry() { + let source = r#" + let mut row = {}; + row.present = (); + row.has("present"); + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn named_field_update_keeps_unit_entry() { + let source = r#" + struct Entry { value: () } + let mut row: Entry = {value: ()}; + row.value = (); + row.has("value"); + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn closure_capture_unit_store_keeps_entry() { + let source = r#" + let mut values = {}; + let write = |flag| if flag => { values["captured"] = (); values.has("captured") } else => { false }; + write(true); + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn source_order_reassignment_keeps_latest_value() { + let source = r#" + let mut values: map = {}; + values["k"] = 1; + values["k"] = 2; + values["k"]; + "#; + assert_eq!(eval(source), Value::Int(2)); +} + +#[test] +fn branch_join_keeps_only_taken_unit_entry() { + let source = r#" + let mut values = {}; + let flag = true; + if flag { + values["taken"] = (); + } else { + values["other"] = (); + } + values.has("taken") && values.has("other") == false; + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn inferred_loop_unit_store_selects_checked_insert() { + let source = r#" + let mut values = {}; + let mut n = 0; + while n < 2 { + values["x"] = (); + n = n + 1; + } + values.has("x"); + "#; + let compiled = compile_source(source).expect("inferred loop should compile"); + assert!( + call_indexes(&compiled.program, vm::BuiltinFunction::MapEdit) >= 1, + "unannotated unit loop must be rewritten by the checked collector" + ); + let mut vm = Vm::new(compiled.program.with_local_count(compiled.locals)); + vm.set_fuel(100_000); + assert_eq!( + vm.run().expect("inferred loop should run"), + VmStatus::Halted + ); + assert_eq!(vm.stack().last().cloned(), Some(Value::Bool(true))); +} + +#[test] +fn inferred_zero_iteration_loop_does_not_invent_unit_entry() { + let source = r#" + let mut values = {}; + let mut n = 0; + while n < 0 { + values["x"] = (); + n = n + 1; + } + values.has("x"); + "#; + assert_eq!(eval(source), Value::Bool(false)); +} + +#[test] +fn annotated_int_loop_store_keeps_set_fast_path() { + let source = r#" + let mut values: map = {}; + let mut n = 0; + while n < 2 { + values["k"] = n; + n = n + 1; + } + values["k"]; + "#; + let compiled = compile_source(source).expect("int map loop should compile"); + assert_eq!( + call_indexes(&compiled.program, vm::BuiltinFunction::MapEdit), + 0, + "proven int store must stay Set, not parser MapEdit" + ); + assert!( + call_indexes(&compiled.program, vm::BuiltinFunction::Set) >= 1, + "proven int store must emit Set" + ); + let mut vm = Vm::new(compiled.program.with_local_count(compiled.locals)); + vm.set_fuel(100_000); + assert_eq!(vm.run().expect("int map loop should run"), VmStatus::Halted); + assert_eq!(vm.stack().last().cloned(), Some(Value::Int(1))); +} + +#[test] +fn keys_length_and_readback_follow_unit_insert() { + let source = r#" + let mut values = {}; + values["a"] = (); + values["b"] = (); + let count = values.length; + let keys = values.keys; + let key_count = keys.length; + count == 2 && key_count == 2 && values.has("a") && values.has("b"); + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn missing_remove_is_noop_and_alias_stays_independent() { + let source = r#" + let mut values: map = {keep: 1, drop: 2}; + let alias = values.copy(); + let updated = values.remove("missing"); + let removed = updated.remove("drop"); + removed.has("drop") == false && removed.has("keep") && alias.has("drop") && alias.has("keep"); + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn discarded_remove_leaves_original_alias() { + let source = r#" + let mut values: map = {drop: 1}; + let alias = values.copy(); + values.remove("drop"); + alias.has("drop"); + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn factory_map_remove_returns_updated_map() { + let source = r#" + fn make() -> map { + let mut values: map = {k: 1}; + values + } + let updated = make().remove("k"); + updated.has("k"); + "#; + assert_eq!(eval(source), Value::Bool(false)); +} + +#[test] +fn object_literal_remove_is_rejected() { + reject( + r#"let row = {value: 1}; row.remove("value");"#, + "E_MAP_REMOVE_RECORD", + ); +} + +#[test] +fn option_and_result_remove_are_rejected() { + reject( + r#"let carrier: Option = Option::Some(1); carrier.remove("x");"#, + "E_MAP_REMOVE_RECORD", + ); + reject( + r#"let carrier: Result = Result::Ok(1); carrier.remove("x");"#, + "E_MAP_REMOVE_RECORD", + ); +} + +#[test] +fn option_non_unit_and_result_unit_index_stores_are_rejected() { + reject( + r#"let mut carrier: Option = Option::Some(1); carrier["x"] = 2;"#, + "cannot store into an Option or Result", + ); + reject( + r#"let mut carrier: Result = Result::Ok(1); carrier["x"] = ();"#, + "cannot store into an Option or Result", + ); +} + +#[test] +fn option_and_result_dot_stores_are_rejected() { + reject( + r#"let mut carrier: Option = Option::Some(1); carrier.x = ();"#, + "cannot store into an Option or Result", + ); + reject( + r#"let mut carrier: Result = Result::Ok(1); carrier.x = 2;"#, + "cannot store into an Option or Result", + ); +} + +#[test] +fn option_and_result_match_leave_payload_unmodified() { + let source = r#" + let option_value: Option = Option::Some(4); + let result_value: Result = Result::Ok(5); + let option_ok = match option_value { + Option::Some(value) => value, + Option::None => 0 + }; + let result_ok = match result_value { + Result::Ok(value) => value, + Result::Err(_) => 0 + }; + option_ok == 4 && result_ok == 5; + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn option_map_combinator_keeps_payload() { + let source = r#" + let mapped = Option::Some(3).map(|value| value + 1); + let shown = match mapped { + Option::Some(value) => value, + Option::None => 0 + }; + shown; + "#; + assert_eq!(eval(source), Value::Int(4)); +} + +#[test] +fn array_and_int_controls_are_not_map_unit_stores() { + let source = r#" + let mut items = [1, 2]; + items[0] = 9; + let mut n = 1; + n = n + 1; + items[0] == 9 && items.length == 2 && n == 2; + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn index_key_and_value_evaluate_once() { + let source = r#" + let mut values: map = {}; + let mut hits = 0; + fn key() -> string { hits = hits + 1; "k" } + fn payload() -> int { hits = hits + 1; 7 } + values[key()] = payload(); + hits == 2 && values["k"] == 7; + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +fn fail(source: &str) -> String { + let compiled = compile_source(source).expect("source should compile"); + let mut vm = Vm::new(compiled.program.with_local_count(compiled.locals)); + vm.set_fuel(2_000_000); + vm.run().expect_err("program should fail").to_string() +} + +fn reload_eval(source: &str) -> Value { + let compiled = compile_source(source).expect("source should compile"); + let wire = vm::vmbc::encode_program(&compiled.program).expect("program should encode"); + let reloaded = vm::vmbc::decode_program(&wire).expect("program should reload"); + let mut vm = Vm::new(reloaded.with_local_count(compiled.locals)); + vm.set_fuel(100_000); + assert_eq!( + vm.run().expect("reloaded program should run"), + VmStatus::Halted + ); + vm.stack().last().cloned().expect("reloaded value") +} + +#[test] +fn unit_scalar_array_and_map_round_trip() { + let source = r#" + use json; + let scalar: () = (); + let items: [()] = [()]; + let mut values: map<()> = {}; + values["k"] = (); + json::encode(scalar) == "null" + && json::encode(items) == "[null]" + && json::encode(values) == "{\"k\":null}" + && json::decode::<()>("null") == () + && json::decode::<[()]>("[null]").length == 1 + && json::decode::>("{\"k\":null}").has("k"); + "#; + assert_eq!(eval(source), Value::Bool(true)); + assert_eq!(reload_eval(source), Value::Bool(true)); +} + +#[test] +fn option_unit_field_none_omits_and_some_encodes_null() { + let source = r#" + use json; + struct Payload { value: Option<()> } + let absent: Payload = {value: Option::None}; + let present: Payload = {value: Option::Some(())}; + let absent_text = json::encode(absent); + let present_text = json::encode(present); + let absent_back: Payload = json::decode::(absent_text); + let present_back: Payload = json::decode::(present_text); + let absent_ok = match absent_back.value { Option::None => absent_text == "{}", Option::Some(_) => false }; + let present_ok = match present_back.value { Option::Some(_) => present_text == "{\"value\":null}", Option::None => false }; + absent_ok && present_ok; + "#; + assert_eq!(eval(source), Value::Bool(true)); + assert_eq!(reload_eval(source), Value::Bool(true)); +} + +#[test] +fn option_int_field_distinguishes_none_zero_false_and_empty() { + let source = r#" + use json; + struct Row { n: Option, flag: Option, text: Option } + let missing: Row = json::decode::("{}"); + let explicit: Row = json::decode::("{\"n\":0,\"flag\":false,\"text\":\"\"}"); + let encoded_missing = json::encode(json::decode::("{}")); + let encoded_explicit = json::encode(json::decode::("{\"n\":0,\"flag\":false,\"text\":\"\"}")); + let missing_ok = match missing.n { + Option::None => match missing.flag { + Option::None => match missing.text { Option::None => true, Option::Some(_) => false }, + Option::Some(_) => false + }, + Option::Some(_) => false + }; + let explicit_ok = match explicit.n { + Option::Some(number) => number == 0 && match explicit.flag { + Option::Some(flag) => flag == false && match explicit.text { Option::Some(text) => text == "", _ => false }, + _ => false + }, + _ => false + }; + missing_ok && explicit_ok && encoded_missing == "{}" && encoded_explicit == "{\"flag\":false,\"n\":0,\"text\":\"\"}"; + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn extra_named_key_drops_and_typed_map_keeps_keys() { + let source = r#" + use json; + struct Row { id: int } + let encoded = json::encode(json::decode::("{\"id\":1,\"extra\":9}")); + let row: Row = json::decode::("{\"id\":1,\"extra\":9}"); + let values: map = json::decode::>("{\"keep\":1,\"also\":2}"); + row.id == 1 && encoded == "{\"id\":1}" && values.has("keep") && values.has("also") && values["keep"] == 1; + "#; + assert_eq!(eval(source), Value::Bool(true)); +} + +#[test] +fn mutual_recursion_and_generic_substitution_round_trip() { + let source = r#" + use json; + struct Left { n: int, right: Option } + struct Right { n: int, left: Option } + struct Box { value: T } + let decoded: Left = json::decode::("{\"n\":1,\"right\":{\"n\":2,\"left\":null}}"); + let encoded = json::encode(decoded); + let again: Left = json::decode::(encoded); + let left_ok = match again.right { + Option::Some(right) => right.n == 2 && match right.left { Option::None => true, Option::Some(_) => false }, + Option::None => false + }; + let boxed: Box = json::decode::>("{\"value\":4}"); + left_ok && again.n == 1 && boxed.value == 4 && encoded == "{\"n\":1,\"right\":{\"n\":2}}"; + "#; + assert_eq!(eval(source), Value::Bool(true)); + assert_eq!(reload_eval(source), Value::Bool(true)); +} + +#[test] +fn required_nested_field_fails_when_root_fields_are_present() { + let source = r#" + use json; + struct Inner { id: int } + struct Outer { ok: int, inner: Inner } + let decoded: Outer = json::decode::("{\"ok\":1,\"inner\":{}}"); + decoded.ok; + "#; + let message = fail(source); + assert!( + message.contains("missing a required field"), + "expected nested required-field rejection, got {message}" + ); +} + +#[test] +fn value_position_option_unit_and_nested_option_reject_before_payload() { + let unit = + fail(r#"use json; let decoded: Option<()> = json::decode::>("null"); decoded;"#); + let nested = fail( + r#"use json; let decoded: Option> = json::decode::>>("null"); decoded;"#, + ); + let empty_map = fail( + r#"use json; let decoded: map> = json::decode::>>("{}"); decoded;"#, + ); + for message in [unit, nested, empty_map] { + assert!( + message.contains("value-position Option") || message.contains("null-ambiguous"), + "expected payload-independent refusal, got {message}" + ); + } +} + +#[test] +fn duplicate_malformed_and_nonfinite_json_are_rejected() { + let duplicate = fail( + r#"use json; let row: map = json::decode::>("{\"k\":1,\"k\":2}"); row;"#, + ); + let malformed = fail( + r#"use json; let value: Option = json::decode::>("{\"value\":1}"); value;"#, + ); + let nonfinite = fail(r#"use json; let value: float = 1.0 / 0.0; json::encode(value);"#); + assert!(duplicate.contains("duplicate"), "got {duplicate}"); + assert!( + malformed.contains("json") + || malformed.contains("TypeMismatch") + || malformed.contains("bool") + || malformed.contains("int"), + "malformed carrier must not become Some(0), got {malformed}" + ); + assert!( + nonfinite.contains("NaN") || nonfinite.contains("infinity") || nonfinite.contains("number"), + "got {nonfinite}" + ); +} + +#[test] +fn json_depth_and_node_budgets_fail() { + let mut nested = "null".to_string(); + for _ in 0..70 { + nested = format!("{{\\\"next\\\":{nested}}}"); + } + let deep = format!( + "use json; struct Node {{ next: Option }} let decoded: Node = json::decode::(\"{nested}\"); decoded;" + ); + let deep_message = fail(&deep); + assert!( + deep_message.contains("nesting budget"), + "got {deep_message}" + ); + + let wide = format!("[{}]", vec!["0"; 20_000].join(",")); + let wide_source = + format!("use json; let values: [int] = json::decode::<[int]>(\"{wide}\"); values.length;"); + let wide_message = fail(&wide_source); + assert!(wide_message.contains("node budget"), "got {wide_message}"); +} diff --git a/tests/match_arm_availability_tests.rs b/tests/match_arm_availability_tests.rs new file mode 100644 index 00000000..cfa7a92f --- /dev/null +++ b/tests/match_arm_availability_tests.rs @@ -0,0 +1,281 @@ +#![cfg(feature = "runtime")] + +use vm::{Value, Vm, VmStatus, compile_source}; + +fn run(source: &str) -> Vec { + let compiled = compile_source(source) + .unwrap_or_else(|error| panic!("source should compile, got {error}\n{source}")); + let mut machine = Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + let status = machine + .run() + .unwrap_or_else(|error| panic!("runtime error {error}\n{source}")); + assert_eq!(status, VmStatus::Halted, "{source}"); + machine.stack().to_vec() +} + +fn reject(source: &str) -> String { + match compile_source(source) { + Ok(_) => panic!("source should be rejected:\n{source}"), + Err(error) => error.to_string(), + } +} + +/// Supplied function parameters are already initialized. A match arm must read +/// them on every applicable branch, for both Copy and non-copy payloads. +#[test] +fn match_arms_read_supplied_outer_parameters() { + let source = r#" + fn option_int(actual: Option, expected: int) -> bool { + match actual { + Option::Some(value) => value == expected, + Option::None => false + } + } + fn option_string(actual: Option, expected: string) -> bool { + match actual { + Option::Some(value) => value == expected, + Option::None => false + } + } + fn result_string(actual: Result, expected: string) -> bool { + match actual { + Result::Ok(value) => value == expected, + Result::Err(_) => false + } + } + fn result_error(actual: Result, expected: string) -> bool { + match actual { + Result::Ok(_) => false, + Result::Err(value) => value == expected + } + } + option_int(Option::Some(7), 7); + option_int(Option::Some(7), 8); + option_int(Option::None, 7); + option_string(Option::Some("same"), "same"); + option_string(Option::Some("same"), "other"); + option_string(Option::None, "same"); + result_string(Result::Ok("same"), "same"); + result_string(Result::Ok("same"), "other"); + result_string(Result::Err("missing"), "same"); + result_error(Result::Err("same"), "same"); + result_error(Result::Err("same"), "other"); + result_error(Result::Ok("same"), "same"); + "#; + assert_eq!( + run(source), + vec![ + Value::Bool(true), + Value::Bool(false), + Value::Bool(false), + Value::Bool(true), + Value::Bool(false), + Value::Bool(false), + Value::Bool(true), + Value::Bool(false), + Value::Bool(false), + Value::Bool(true), + Value::Bool(false), + Value::Bool(false), + ] + ); +} + +#[test] +fn match_arms_read_same_frame_lets_and_nested_matches() { + let source = r#" + fn same_frame(actual: Option, expected: int) -> bool { + let probe = expected; + match actual { + Option::Some(value) => value == probe, + Option::None => false + } + } + fn nested(actual: Option>, expected: int) -> bool { + match actual { + Option::Some(inner) => match inner { + Option::Some(value) => value == expected, + Option::None => false + }, + Option::None => false + } + } + fn both_arms(actual: Option, expected: int) -> int { + match actual { + Option::Some(value) => value + expected, + Option::None => expected + } + } + same_frame(Option::Some(7), 7); + same_frame(Option::Some(7), 8); + same_frame(Option::None, 7); + nested(Option::Some(Option::Some(7)), 7); + nested(Option::Some(Option::Some(7)), 8); + nested(Option::Some(Option::None), 7); + nested(Option::None, 7); + both_arms(Option::Some(7), 1); + both_arms(Option::None, 4); + "#; + assert_eq!( + run(source), + vec![ + Value::Bool(true), + Value::Bool(false), + Value::Bool(false), + Value::Bool(true), + Value::Bool(false), + Value::Bool(false), + Value::Bool(false), + Value::Int(8), + Value::Int(4), + ] + ); +} + +#[test] +fn match_arm_keeps_real_captures_separate_from_same_frame_locals() { + let source = r#" + let outer = "same"; + fn captured_root(actual: Option) -> bool { + match actual { + Option::Some(value) => value == outer, + Option::None => false + } + } + fn closure_over_parameter(actual: Option, expected: string) -> bool { + let check = |probe| probe == expected; + match actual { + Option::Some(value) => check(value), + Option::None => false + } + } + captured_root(Option::Some("same")); + captured_root(Option::Some("other")); + captured_root(Option::None); + closure_over_parameter(Option::Some("same"), "same"); + closure_over_parameter(Option::Some("same"), "other"); + closure_over_parameter(Option::None, "same"); + "#; + assert_eq!( + run(source), + vec![ + Value::Bool(true), + Value::Bool(false), + Value::Bool(false), + Value::Bool(true), + Value::Bool(false), + Value::Bool(false), + ] + ); +} + +/// Equality does not consume a string parameter. A match arm must follow that +/// contract rather than inventing a move. +#[test] +fn string_equality_reuse_matches_the_non_match_baseline() { + let source = r#" + fn plain(actual: string, expected: string) -> bool { + let first = actual == expected; + let second = actual == expected; + first && second + } + fn matched(actual: Option, expected: string) -> bool { + let first = match actual { + Option::Some(value) => value == expected, + Option::None => false + }; + let second = match actual { + Option::Some(value) => value == expected, + Option::None => false + }; + first && second + } + plain("same", "same"); + plain("same", "other"); + matched(Option::Some("same"), "same"); + matched(Option::Some("same"), "other"); + matched(Option::None, "same"); + "#; + assert_eq!( + run(source), + vec![ + Value::Bool(true), + Value::Bool(false), + Value::Bool(true), + Value::Bool(false), + Value::Bool(false), + ] + ); +} + +#[test] +fn match_arm_availability_still_rejects_illegal_uses() { + let escaped = reject( + r#" + fn compare(actual: Option) -> int { + match actual { + Option::Some(value) => value, + Option::None => 0 + }; + value + } + compare(Option::Some(7)); + "#, + ); + assert!( + escaped.contains("unknown local 'value'"), + "arm binding must not escape, got {escaped}" + ); + + let maybe = reject( + r#" + fn compare(actual: Option, flag: bool) -> bool { + if flag => { + let expected = 7; + expected + } else => { + 0 + }; + match actual { + Option::Some(value) => value == expected, + Option::None => false + } + } + compare(Option::Some(7), true); + "#, + ); + assert!( + maybe.contains("may be unavailable"), + "conditionally initialized local must stay unavailable, got {maybe}" + ); + + let wrong_scrutinee = reject( + r#" + fn compare(actual: int, expected: int) -> bool { + match actual { + Option::Some(value) => value == expected, + Option::None => false + } + } + compare(7, 7); + "#, + ); + assert!( + wrong_scrutinee.contains("expected Option"), + "wrong scrutinee must stay rejected, got {wrong_scrutinee}" + ); + + let wrong_payload = reject( + r#" + fn bad() -> Option { + Option::Some("no") + } + bad(); + "#, + ); + assert!( + wrong_payload.contains("assigned string"), + "wrong Option payload must stay rejected, got {wrong_payload}" + ); +} diff --git a/tests/option_combinator_tests.rs b/tests/option_combinator_tests.rs new file mode 100644 index 00000000..7cde052f --- /dev/null +++ b/tests/option_combinator_tests.rs @@ -0,0 +1,830 @@ +#![cfg(feature = "runtime")] + +use vm::{Value, Vm, VmStatus, compile_source}; + +fn eval(source: &str) -> Value { + let compiled = compile_source(source).expect("Option combinator source should compile"); + let mut machine = Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + assert_eq!( + machine.run().expect("Option combinator program should run"), + VmStatus::Halted + ); + machine + .stack() + .last() + .cloned() + .expect("Option combinator program should leave a value") +} + +fn reject(source: &str) -> String { + match compile_source(source) { + Ok(_) => panic!("Option combinator source should be rejected: {source}"), + Err(error) => error.to_string(), + } +} + +#[test] +fn map_wraps_exactly_the_callback_value_and_skips_none() { + let source = r#" + let present: Option = Option::Some(2); + let absent: Option = Option::None; + let mapped_present = present.map(|value| value + 1); + let mapped_absent = absent.map(|value| value + 1); + let some_value = match mapped_present { Option::Some(v) => v, Option::None => -1 }; + let none_value = match mapped_absent { Option::Some(v) => 100, Option::None => 3 }; + some_value + none_value; + "#; + // Some(2) -> Some(3); None stays None. + assert_eq!(eval(source), Value::Int(6)); +} + +#[test] +fn map_does_not_flatten_nested_option_or_result_payloads() { + let source = r#" + let nested: Option> = Option::Some(Option::Some(7)); + let mapped = nested.map(|inner| inner); + let observed = match mapped { + Option::Some(inner) => match inner { Option::Some(v) => v, Option::None => -1 }, + Option::None => -2, + }; + let result: Result = Result::Ok(9); + let wrapped = Option::Some(result).map(|value| match value { + Result::Ok(v) => v + 1, + Result::Err(e) => -3, + }); + let second = match wrapped { Option::Some(v) => v, Option::None => -4 }; + observed + second; + "#; + // map(|inner| inner) keeps Some(Some(7)); map over Result yields Some(10). + assert_eq!(eval(source), Value::Int(17)); +} + +#[test] +fn map_calls_the_selected_callback_body_once_and_never_the_unselected_body() { + let source = r#" + let mut some_calls = [0]; + let mut none_calls = [0]; + let present: Option = Option::Some(5); + let absent: Option = Option::None; + let mapped_present = present.map(|value| if true => { + some_calls[0] = some_calls[0].copy() + 1; + value + 1 + } else => { 0 }); + let mapped_absent = absent.map(|value| if true => { + none_calls[0] = none_calls[0].copy() + 1; + value + 1 + } else => { 0 }); + let observed = match mapped_present { Option::Some(v) => v, Option::None => 0 }; + let absent_observed = match mapped_absent { Option::Some(v) => 1, Option::None => 0 }; + observed + some_calls[0].copy() + none_calls[0].copy() + absent_observed; + "#; + // Some path body runs once (6), None path body never runs (0 calls). + assert_eq!(eval(source), Value::Int(7)); +} + +#[test] +fn and_then_flattens_only_the_explicitly_returned_option() { + let source = r#" + fn keep(values: [Option], index: int) -> Option { values[index] } + let present: Option = Option::Some(4); + let absent: Option = Option::None; + let nested: Option> = Option::Some(Option::Some(6)); + let kept = present.and_then(|value| keep([Option::Some(value + 1), Option::None], 0)); + let skipped = absent.and_then(|value| Option::Some(value + 1)); + // and_then flattens exactly one returned layer even for an Option payload. + let double = nested.and_then(|inner| inner); + let a = match kept { Option::Some(v) => v, Option::None => -1 }; + let b = match skipped { Option::Some(v) => 100, Option::None => 2 }; + let c = match double { Option::Some(v) => v, Option::None => 3 }; + a + b + c; + "#; + // kept = Some(5); skipped = None; double = Some(Some(6)) flattened once to Some(6). + assert_eq!(eval(source), Value::Int(13)); +} + +#[test] +fn and_then_rejects_a_callback_that_returns_a_bare_payload() { + let diagnostic = + reject("let value: Option = Option::Some(1); value.and_then(|item| item + 1);"); + assert!( + diagnostic.contains("and_then") && diagnostic.contains("Option"), + "and_then must require an Option-returning callback: {diagnostic}" + ); +} + +#[test] +fn or_else_uses_a_zero_argument_lazy_fallback_and_keeps_the_payload() { + let source = r#" + let mut fallback_calls = [0]; + let present: Option = Option::Some(8); + let absent: Option = Option::None; + let present_value = present.or_else(|| if true => { + fallback_calls[0] = fallback_calls[0].copy() + 1; + Option::Some(99) + } else => { Option::None }); + let absent_value = absent.or_else(|| if true => { + fallback_calls[0] = fallback_calls[0].copy() + 1; + Option::Some(9) + } else => { Option::None }); + let a = match present_value { Option::Some(v) => v, Option::None => -1 }; + let b = match absent_value { Option::Some(v) => v, Option::None => -2 }; + a + b + fallback_calls[0].copy(); + "#; + // Some keeps 8 without invoking the fallback; None invokes it once. + assert_eq!(eval(source), Value::Int(18)); +} + +#[test] +fn or_else_requires_a_zero_argument_callback() { + let diagnostic = reject( + "let value: Option = Option::None; value.or_else(|error| Option::Some(error));", + ); + assert!( + diagnostic.contains("or_else") && diagnostic.contains("zero"), + "or_else must reject a Result-shaped callback: {diagnostic}" + ); +} + +#[test] +fn unwrap_or_is_eager_and_returns_the_exact_payload_type() { + let source = r#" + let mut fallback_calls = [0]; + let bump = || if true => { + fallback_calls[0] = fallback_calls[0].copy() + 1; + 11 + } else => { 0 }; + let present: Option = Option::Some(5); + let absent: Option = Option::None; + let a = present.unwrap_or(bump()); + let b = absent.unwrap_or(bump()); + let nested: Option> = Option::Some(Option::Some(3)); + let c = nested.unwrap_or(Option::Some(0)); + let c_value = match c { Option::Some(v) => v, Option::None => -1 }; + a + b + c_value + fallback_calls[0].copy(); + "#; + // The eager fallback runs once even when Some is selected, and once for None. + assert_eq!(eval(source), Value::Int(21)); +} + +#[test] +fn unwrap_or_else_runs_the_body_only_for_none() { + let source = r#" + let mut fallback_calls = [0]; + let present: Option = Option::Some(6); + let absent: Option = Option::None; + let a = present.unwrap_or_else(|| if true => { + fallback_calls[0] = fallback_calls[0].copy() + 1; + 100 + } else => { 0 }); + let b = absent.unwrap_or_else(|| if true => { + fallback_calls[0] = fallback_calls[0].copy() + 1; + 7 + } else => { 0 }); + a + b + fallback_calls[0].copy(); + "#; + // Lazy body runs only for None: 6 + 7 + 1. + assert_eq!(eval(source), Value::Int(14)); +} + +#[test] +fn ok_or_is_eager_and_converts_some_none_to_ok_err() { + let source = r#" + let mut error_calls = [0]; + let bump = || if true => { + error_calls[0] = error_calls[0].copy() + 1; + "missing" + } else => { "unused" }; + let present: Option = Option::Some(12); + let absent: Option = Option::None; + let ok = present.ok_or(bump()); + let err = absent.ok_or(bump()); + let a = match ok { Result::Ok(v) => v, Result::Err(e) => -1 }; + let b = match err { Result::Ok(v) => -2, Result::Err(e) => 10 }; + a + b + error_calls[0].copy(); + "#; + // Eager error argument is created on both selections. + assert_eq!(eval(source), Value::Int(24)); +} + +#[test] +fn ok_or_else_runs_the_error_body_only_for_none() { + let source = r#" + let mut error_calls = [0]; + let present: Option = Option::Some(3); + let absent: Option = Option::None; + let ok = present.ok_or_else(|| if true => { + error_calls[0] = error_calls[0].copy() + 1; + "no present" + } else => { "unused" }); + let err = absent.ok_or_else(|| if true => { + error_calls[0] = error_calls[0].copy() + 1; + "no absent" + } else => { "unused" }); + let a = match ok { Result::Ok(v) => v, Result::Err(e) => -1 }; + let b = match err { Result::Ok(v) => -2, Result::Err(e) => 8 }; + a + b + error_calls[0].copy(); + "#; + // Only the selected None path runs its callback. + assert_eq!(eval(source), Value::Int(12)); +} + +#[test] +fn result_ok_and_err_select_and_discard_the_matching_payload() { + let source = r#" + let ok_value: Result = Result::Ok(21); + let err_value: Result = Result::Err("bad"); + let ok_option = ok_value.ok(); + let err_option = err_value.ok(); + let ok_err_option = ok_value.err(); + let err_err_option = err_value.err(); + let a = match ok_option { Option::Some(v) => v, Option::None => -1 }; + let b = match err_option { Option::Some(v) => 100, Option::None => 1 }; + let c = match ok_err_option { Option::Some(e) => 100, Option::None => 2 }; + let d = match err_err_option { Option::Some(e) => 3, Option::None => -2 }; + a + b + c + d; + "#; + // Ok(21) -> Some(21)/None; Err("bad") -> None/Some("bad"). + assert_eq!(eval(source), Value::Int(27)); +} + +#[test] +fn result_ok_and_err_require_a_result_receiver() { + let diagnostic = reject("let value: Option = Option::Some(1); value.ok();"); + assert!( + diagnostic.contains("Result") && diagnostic.contains("ok"), + "ok on an Option receiver must require Result: {diagnostic}" + ); + let diagnostic = reject("let value: Option = Option::Some(1); value.err();"); + assert!( + diagnostic.contains("Result") && diagnostic.contains("err"), + "err on an Option receiver must require Result: {diagnostic}" + ); +} + +#[test] +fn option_methods_reject_wrong_callback_shapes_and_return_types() { + let wrong_map_return = reject( + "let value: Option = Option::Some(1); let out: Option = value.map(|item| item + 1);", + ); + assert!( + wrong_map_return.contains("map") && wrong_map_return.contains("expects"), + "map output type mismatch must be focused: {wrong_map_return}" + ); + let wrong_unwrap = + reject("let value: Option = Option::Some(1); let out: string = value.unwrap_or(0);"); + assert!( + wrong_unwrap.contains("unwrap_or") && wrong_unwrap.contains("string"), + "unwrap_or output type mismatch must be focused: {wrong_unwrap}" + ); + let wrong_ok_or = reject( + "let value: Option = Option::Some(1); let out: Result = value.ok_or(\"bad\");", + ); + assert!( + wrong_ok_or.contains("ok_or") && wrong_ok_or.contains("int"), + "ok_or error type mismatch must be focused: {wrong_ok_or}" + ); + let map_err_on_option = + reject("let value: Option = Option::Some(1); value.map_err(|error| error);"); + assert!( + map_err_on_option.contains("map_err") && map_err_on_option.contains("Result"), + "map_err must remain Result-only: {map_err_on_option}" + ); +} + +#[test] +fn map_preserves_known_named_return_schema_and_materializes_omitted_option_field() { + let source = r#" + struct Entry { name: string, note: Option } + let flag: Option = Option::Some(1); + let item: Option = flag.map(|n| { name: "x" }); + let out = match item { + Option::Some(entry) => match entry.note { + Option::Some(value) => value, + Option::None => 0, + }, + Option::None => -1, + }; + out; + "#; + assert_eq!(eval(source), Value::Int(0)); +} + +#[test] +fn unwrap_or_preserves_known_named_payload_schema_and_materializes_omitted_option_field() { + let source = r#" + struct Entry { name: string, note: Option } + let flag: Option = Option::None; + let item: Entry = flag.unwrap_or({ name: "x" }); + let projected = item?.note; + let out = match projected { + Option::Some(inner) => match inner { + Option::Some(value) => value, + Option::None => 1, + }, + Option::None => 2, + }; + out; + "#; + assert_eq!(eval(source), Value::Int(1)); +} + +#[test] +fn methods_on_a_non_option_receiver_report_the_option_requirement() { + let diagnostic = reject("let value: int = 1; value.map(|item| item + 1);"); + assert!( + diagnostic.contains("Option") || diagnostic.contains("Result"), + "non-container receiver must name the required owner: {diagnostic}" + ); +} + +#[test] +fn map_function_return_known_named_schema_materializes_omitted_option_field() { + let source = r#" + struct Entry { name: string, note: Option } + fn make(flag: Option) -> Option { + flag.map(|n| { name: "x" }) + } + let flag: Option = Option::Some(1); + let item = make(flag); + let out = match item { + Option::Some(entry) => match entry.note { + Option::Some(value) => value, + Option::None => 0, + }, + Option::None => -1, + }; + out; + "#; + assert_eq!(eval(source), Value::Int(0)); +} + +#[test] +fn map_generic_nested_named_payload_materializes_all_omitted_option_fields() { + let source = r#" + struct Entry { name: string, note: Option } + struct Box { value: T, note: Option } + fn make(flag: Option) -> Option> { + flag.map(|n| { value: { name: "x" } }) + } + fn observe_note(note: Option>) -> int { + match note { + Option::Some(inner_note) => match inner_note { + Option::Some(value) => value, + Option::None => 1, + }, + Option::None => -2, + } + } + fn observe_box(item: Option>) -> int { + match item { + Option::Some(inner) => observe_note(inner?.note), + Option::None => -1, + } + } + fn entry_note(value: Entry) -> Option> { + value?.note + } + fn observe_entry(item: Option>) -> int { + match item { + Option::Some(inner) => observe_note(entry_note(inner.value)), + Option::None => -1, + } + } + observe_box(make(Option::Some(1))) + observe_entry(make(Option::Some(1))); + "#; + // Both the Box.note and nested Entry.note materialize as tagged None. + assert_eq!(eval(source), Value::Int(2)); +} + +#[test] +fn map_conditional_and_block_callbacks_keep_the_named_return_context() { + let source = r#" + struct Entry { name: string, note: Option } + let flag: Option = Option::Some(1); + let item: Option = flag.map(if true => { + |n| { name: "chosen" } + } else => { + |n| { name: "other" } + }); + let out = match item { + Option::Some(entry) => match entry.note { + Option::Some(value) => value, + Option::None => 0, + }, + Option::None => -1, + }; + out; + "#; + assert_eq!(eval(source), Value::Int(0)); +} + +#[test] +fn map_named_function_callback_keeps_its_declared_return_context() { + let source = r#" + struct Entry { name: string, note: Option } + fn build(n: int) -> Entry { + { name: "built" } + } + let flag: Option = Option::Some(1); + let item: Option = flag.map(build); + let out = match item { + Option::Some(entry) => match entry.note { + Option::Some(value) => value, + Option::None => 0, + }, + Option::None => -1, + }; + out; + "#; + assert_eq!(eval(source), Value::Int(0)); +} + +#[test] +fn map_prebound_annotated_callback_keeps_its_declared_return_context() { + let source = r#" + struct Entry { name: string, note: Option } + let callback: fn(int) -> Entry = |n| { name: "bound" }; + let flag: Option = Option::Some(1); + let item: Option = flag.map(callback); + let out = match item { + Option::Some(entry) => match entry.note { + Option::Some(value) => value, + Option::None => 0, + }, + Option::None => -1, + }; + out; + "#; + assert_eq!(eval(source), Value::Int(0)); +} + +#[test] +fn map_known_return_schema_preserves_callback_captures_and_single_body_run() { + let source = r#" + struct Entry { name: string, note: Option } + let mut calls = [0]; + let flag: Option = Option::Some(1); + let item: Option = flag.map(|n| if true => { + calls[0] = calls[0].copy() + 1; + { name: "x" } + } else => { + { name: "y" } + }); + let out = match item { + Option::Some(entry) => match entry.note { + Option::Some(value) => value, + Option::None => 0, + }, + Option::None => -1, + }; + out + calls[0].copy(); + "#; + assert_eq!(eval(source), Value::Int(1)); +} + +#[test] +fn unwrap_or_else_named_payload_keeps_the_body_lazy_and_materializes_none() { + let source = r#" + struct Entry { name: string, note: Option } + let mut calls = [0]; + let present: Option = Option::Some({ name: "p", note: Option::Some(1) }); + let absent: Option = Option::None; + let a: Entry = present.unwrap_or_else(|| if true => { + calls[0] = calls[0].copy() + 1; + { name: "a" } + } else => { + { name: "unused" } + }); + let b: Entry = absent.unwrap_or_else(|| if true => { + calls[0] = calls[0].copy() + 1; + { name: "b" } + } else => { + { name: "unused" } + }); + let a_note = match a.note { Option::Some(value) => value, Option::None => -1 }; + let b_note = match b?.note { + Option::Some(inner_note) => match inner_note { + Option::Some(value) => value, + Option::None => 1, + }, + Option::None => -2, + }; + a_note + b_note + calls[0].copy(); + "#; + // The Some branch keeps its payload and skips the lazy body; the None + // branch runs it once and materializes Entry.note as tagged None. + assert_eq!(eval(source), Value::Int(3)); +} + +#[test] +fn and_then_and_or_else_named_option_payloads_materialize_omitted_option_fields() { + let source = r#" + struct Entry { name: string, note: Option } + fn observed(item: Option) -> int { + match item { + Option::Some(entry) => match entry?.note { + Option::Some(inner_note) => match inner_note { + Option::Some(value) => value, + Option::None => 1, + }, + Option::None => -2, + }, + Option::None => -1, + } + } + let flag: Option = Option::Some(1); + let chained: Option = flag.and_then(|n| Option::Some({ name: "x" })); + let absent: Option = Option::None; + let recovered: Option = absent.or_else(|| Option::Some({ name: "y" })); + observed(chained) + observed(recovered); + "#; + assert_eq!(eval(source), Value::Int(2)); +} + +#[test] +fn ok_or_and_ok_or_else_named_errors_materialize_omitted_option_fields() { + let source = r#" + struct Failure { code: int, detail: Option } + fn observed(result: Result) -> int { + match result { + Result::Ok(value) => -1, + Result::Err(error) => match error?.detail { + Option::Some(inner_detail) => match inner_detail { + Option::Some(value) => value, + Option::None => 1, + }, + Option::None => -2, + }, + } + } + let present: Option = Option::Some(1); + let absent: Option = Option::None; + let eager: Result = absent.ok_or({ code: 7 }); + let lazy: Result = absent.ok_or_else(|| { code: 8 }); + observed(eager) + observed(lazy) + match present.ok_or({ code: 9 }) { + Result::Ok(value) => value, + Result::Err(error) => -1, + }; + "#; + assert_eq!(eval(source), Value::Int(3)); +} + +#[test] +fn nonconstruction_sparse_object_cannot_be_annotated_as_named_record() { + let diagnostic = reject( + r#" + struct Entry { name: string, note: Option } + let raw = { name: "x" }; + let item: Entry = raw; + item.name; + "#, + ); + assert!( + diagnostic.contains("field 'note'") + && diagnostic.contains("non-construction") + && diagnostic.contains("must be materialized"), + "the missing-field obligation must be asserted at the compile boundary: {diagnostic}" + ); +} + +#[test] +fn nonconstruction_sparse_object_cannot_be_an_ordinary_record_argument() { + let diagnostic = reject( + r#" + struct Entry { name: string, note: Option } + fn read(entry: Entry) -> string { entry.name } + let raw = { name: "x" }; + read(raw); + "#, + ); + assert!( + diagnostic.contains("field 'note'") && diagnostic.contains("must be materialized"), + "an ordinary record argument must carry the same missing-field obligation: {diagnostic}" + ); +} + +#[test] +fn nonconstruction_sparse_object_cannot_be_an_eager_unwrap_or_fallback() { + let diagnostic = reject( + r#" + struct Entry { name: string, note: Option } + let flag: Option = Option::None; + let raw = { name: "x" }; + let item: Entry = flag.unwrap_or(raw); + item.name; + "#, + ); + assert!( + diagnostic.contains("unwrap_or") + && diagnostic.contains("field 'note'") + && diagnostic.contains("must be materialized"), + "the eager fallback must be rejected before the named label is installed: {diagnostic}" + ); +} + +#[test] +fn nonconstruction_sparse_object_cannot_be_a_map_callback_payload() { + let diagnostic = reject( + r#" + struct Entry { name: string, note: Option } + let flag: Option = Option::Some(1); + let raw = { name: "x" }; + let item: Option = flag.map(|n| raw); + item; + "#, + ); + assert!( + diagnostic.contains("map") + && diagnostic.contains("field 'note'") + && diagnostic.contains("must be materialized"), + "a callback payload that is a plain variable must not be relabelled: {diagnostic}" + ); +} + +#[test] +fn nested_nonconstruction_sparse_object_is_not_excused_by_an_ancestor_literal() { + let diagnostic = reject( + r#" + struct Inner { name: string, note: Option } + struct Outer { inner: Option } + let raw = { name: "x" }; + let outer: Outer = { inner: Option::Some(raw) }; + outer; + "#, + ); + assert!( + diagnostic.contains("inner.note") && diagnostic.contains("must be materialized"), + "the nested value position, not the ancestor literal, owns the obligation: {diagnostic}" + ); +} + +#[test] +fn unwrap_or_keeps_a_previously_materialized_named_fallback() { + let source = r#" + struct Entry { name: string, note: Option } + let fallback: Entry = { name: "x" }; + let flag: Option = Option::None; + let item: Entry = flag.unwrap_or(fallback); + match item.note { Option::Some(value) => value, Option::None => 0 }; + "#; + assert_eq!(eval(source), Value::Int(0)); +} + +#[test] +fn conditional_callbacks_with_distinct_present_key_shapes_materialize_the_known_record() { + let source = r#" + struct Entry { name: string, note: Option } + fn make(flag: Option, select: bool) -> Option { + flag.map(if select => { + |n| { name: "x" } + } else => { + |n| { name: "y", note: Option::None } + }) + } + let item: Entry = make(Option::Some(1), true).unwrap_or({ name: "fallback" }); + match item.note { Option::Some(value) => value, Option::None => 0 }; + "#; + assert_eq!(eval(source), Value::Int(0)); +} + +#[test] +fn match_callbacks_with_distinct_present_key_shapes_materialize_the_known_record() { + let source = r#" + struct Entry { name: string, note: Option } + fn make(flag: Option, select: int) -> Option { + flag.map(match select { + 1 => |n| { name: "x" }, + _ => |n| { name: "y", note: Option::Some(1) }, + }) + } + let item: Entry = make(Option::Some(1), 1).unwrap_or({ name: "fallback" }); + match item.note { Option::Some(value) => value, Option::None => 0 }; + "#; + assert_eq!(eval(source), Value::Int(0)); +} + +#[test] +fn conditional_callback_missing_a_required_field_is_rejected() { + let diagnostic = reject( + r#" + struct Entry { name: string, note: Option } + fn make(flag: Option, select: bool) -> Option { + flag.map(if select => { + |n| { name: "x" } + } else => { + |n| { note: Option::None } + }) + } + make(Option::Some(1), true); + "#, + ); + assert!( + diagnostic.contains("field 'name'") && diagnostic.contains("required"), + "a branch missing a required field must keep the focused missing-field rejection: {diagnostic}" + ); +} + +#[test] +fn conditional_callback_with_a_wrong_field_type_is_rejected() { + let diagnostic = reject( + r#" + struct Entry { name: string, note: Option } + fn make(flag: Option, select: bool) -> Option { + flag.map(if select => { + |n| { name: "x" } + } else => { + |n| { name: 2 } + }) + } + make(Option::Some(1), true); + "#, + ); + assert!( + diagnostic.contains("field 'name'") && diagnostic.contains("string"), + "a wrong branch field type must keep the focused schema rejection: {diagnostic}" + ); +} + +#[test] +fn expression_block_local_sparse_object_cannot_be_a_declared_return() { + let diagnostic = reject( + r#" + struct Entry { name: string, note: Option } + fn make(flag: bool) -> Entry { + if flag => { + let raw = { name: "x" }; + raw + } else => { + { name: "y" } + } + } + let item: Entry = make(true); + match item.note { Option::Some(value) => value, Option::None => 0 }; + "#, + ); + assert!( + diagnostic.contains("field 'note'") && diagnostic.contains("must be materialized"), + "a sparse object bound inside the expression block and returned as a declared record must keep the missing-field obligation: {diagnostic}" + ); +} + +#[test] +fn expression_block_local_sparse_alias_cannot_be_a_declared_return() { + let diagnostic = reject( + r#" + struct Entry { name: string, note: Option } + fn make(flag: bool) -> Entry { + if flag => { + let raw = { name: "x" }; + let alias = raw; + alias + } else => { + { name: "y" } + } + } + let item: Entry = make(true); + match item.note { Option::Some(value) => value, Option::None => 0 }; + "#, + ); + assert!( + diagnostic.contains("field 'note'") && diagnostic.contains("must be materialized"), + "a block-local alias must not hide the missing-field obligation: {diagnostic}" + ); +} + +#[test] +fn expression_block_literal_tail_materializes_the_omitted_option_field() { + let source = r#" + struct Entry { name: string, note: Option } + fn make(flag: bool) -> Entry { + if flag => { + let name = "x"; + { name: name } + } else => { + { name: "y" } + } + } + let item: Entry = make(true); + match item.note { Option::Some(value) => value, Option::None => 0 }; + "#; + assert_eq!(eval(source), Value::Int(0)); +} + +#[test] +fn expression_block_materialized_variable_tail_keeps_the_named_record() { + let source = r#" + struct Entry { name: string, note: Option } + fn make(flag: bool) -> Entry { + if flag => { + let entry: Entry = { name: "x" }; + entry + } else => { + { name: "y" } + } + } + let item: Entry = make(true); + match item.note { Option::Some(value) => value, Option::None => 0 }; + "#; + assert_eq!(eval(source), Value::Int(0)); +} diff --git a/tests/option_host_tests.rs b/tests/option_host_tests.rs new file mode 100644 index 00000000..733339a7 --- /dev/null +++ b/tests/option_host_tests.rs @@ -0,0 +1,1062 @@ +#![cfg(feature = "runtime")] + +use pd_host_function::pd_host_function; +use std::sync::{ + Arc, + atomic::{AtomicUsize, Ordering}, +}; +use vm::resource::{CloseProgress, HostResource, ResourceCloseReason, ResourceResult}; +use vm::{ + FromVmValue, GuestOption, GuestResult, HostNamedStruct, HostResourceType, ResourceOwned, + ResourceTypeKey, Value, Vm, VmCallable, VmResult, VmStatus, +}; + +static CREATED: AtomicUsize = AtomicUsize::new(0); +static CLOSED: AtomicUsize = AtomicUsize::new(0); +static OBSERVED_RESOURCE_COUNT: AtomicUsize = AtomicUsize::new(0); +static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +#[derive(Clone, Debug, PartialEq)] +struct SdkBox { + value: i64, +} +impl HostNamedStruct for SdkBox { + const NAME: &'static str = "OptionSdkBox"; + fn host_struct_fields() -> Vec { + vec![vm::HostStructField::new("value", vm::HostTypeSchema::Int)] + } +} +impl vm::IntoVmValue for SdkBox { + fn into_vm_value(self) -> Value { + Value::map(vec![(Value::string("value"), Value::Int(self.value))]) + } +} + +#[derive(Debug)] +struct OptionSuccess(i64); +impl HostResource for OptionSuccess { + fn resource_type_key() -> Option { + Some(ResourceTypeKey::new("option.success").unwrap()) + } + fn begin_close(&mut self, _: ResourceCloseReason) -> ResourceResult { + CLOSED.fetch_add(1, Ordering::SeqCst); + Ok(CloseProgress::Ready) + } +} +impl HostResourceType for OptionSuccess { + const KEY: &'static str = "option.success"; + const DESCRIPTION: &'static str = "option sdk success resource"; +} + +#[derive(Debug)] +struct OtherOptionSuccess; +impl HostResource for OtherOptionSuccess { + fn resource_type_key() -> Option { + Some(ResourceTypeKey::new("option.other").unwrap()) + } + fn begin_close(&mut self, _: ResourceCloseReason) -> ResourceResult { + Ok(CloseProgress::Ready) + } +} +impl HostResourceType for OtherOptionSuccess { + const KEY: &'static str = "option.other"; + const DESCRIPTION: &'static str = "option sdk other resource"; +} + +mod generated { + pub use vm::borrow_arg; + // The host scanner needs the explicit nested callable signatures in this + // fixture; aliases would hide them from the macro. + #[allow(clippy::type_complexity)] + pub mod functions { + use super::super::*; + include!("fixtures/option_sdk_signature.rs"); + } +} +include!(concat!(env!("OUT_DIR"), "/option_sdk_build_schemas.rs")); + +fn descriptors() -> Vec { + vec![ + generated::functions::fixture_descriptor(), + generated::functions::nested_descriptor(), + generated::functions::unit_descriptor(), + generated::functions::inspect_descriptor(), + generated::functions::option_result_descriptor(), + generated::functions::result_option_descriptor(), + generated::functions::callable_echo_descriptor(), + generated::functions::callable_pick_descriptor(), + generated::functions::callable_count_descriptor(), + generated::functions::callable_unit_echo_descriptor(), + generated::functions::callable_unit_default_echo_descriptor(), + generated::functions::callable_pair_echo_descriptor(), + generated::functions::resource_make_descriptor(), + generated::functions::resource_take_descriptor(), + generated::functions::resource_observe_descriptor(), + ] +} + +fn machine(source: &str) -> Vm { + let descriptors = descriptors(); + let catalog = Arc::new(vm::HostFunctionDescriptor::collect_catalog(&descriptors).unwrap()); + let compiled = vm::compile_source_with_flavor_and_options( + source, + vm::SourceFlavor::RustScript, + vm::CompileSourceFileOptions::default().with_host_api_catalog(catalog), + ) + .unwrap(); + let mut machine = Vm::new(compiled.program.with_local_count(compiled.locals)); + bind_registry(&mut machine); + machine.set_fuel(100_000); + machine +} + +fn bind_registry(machine: &mut Vm) { + let descriptors = descriptors(); + let mut registry = vm::HostFunctionRegistry::empty(); + registry.set_capability_profile(vm::CapabilityProfile::deny_all()); + vm::HostModuleDescriptor::install_descriptors(&mut registry, &descriptors).unwrap(); + for descriptor in &descriptors { + registry.authorize_registered_builtin_import(&descriptor.schema.name); + } + registry.bind_vm_cached(machine).unwrap(); +} + +fn eval(source: &str) -> Value { + let mut machine = machine(source); + assert_eq!(machine.run().unwrap(), VmStatus::Halted); + machine.stack().last().cloned().expect("value on stack") +} + +fn empty_vm() -> Vm { + Vm::new(vm::Program::new(Vec::new(), vec![vm::OpCode::Ret as u8])) +} + +#[test] +fn build_fixture_and_proc_macro_descriptors_are_identical() { + for (descriptor, (schema, effects, binding)) in [ + ( + generated::functions::fixture_descriptor(), + result_sdk_build_fixture(), + ), + ( + generated::functions::nested_descriptor(), + result_sdk_build_nested(), + ), + ( + generated::functions::unit_descriptor(), + result_sdk_build_unit(), + ), + ( + generated::functions::inspect_descriptor(), + result_sdk_build_inspect(), + ), + ( + generated::functions::option_result_descriptor(), + result_sdk_build_option_result(), + ), + ( + generated::functions::result_option_descriptor(), + result_sdk_build_result_option(), + ), + ( + generated::functions::callable_echo_descriptor(), + result_sdk_build_callable_echo(), + ), + ( + generated::functions::callable_pick_descriptor(), + result_sdk_build_callable_pick(), + ), + ( + generated::functions::callable_count_descriptor(), + result_sdk_build_callable_count(), + ), + ( + generated::functions::callable_unit_echo_descriptor(), + result_sdk_build_callable_unit_echo(), + ), + ( + generated::functions::callable_unit_default_echo_descriptor(), + result_sdk_build_callable_unit_default_echo(), + ), + ( + generated::functions::callable_pair_echo_descriptor(), + result_sdk_build_callable_pair_echo(), + ), + ( + generated::functions::resource_make_descriptor(), + result_sdk_build_resource_make(), + ), + ( + generated::functions::resource_take_descriptor(), + result_sdk_build_resource_take(), + ), + ( + generated::functions::resource_observe_descriptor(), + result_sdk_build_resource_observe(), + ), + ] { + assert_eq!(schema, descriptor.schema); + assert_eq!(effects, descriptor.effects); + assert_eq!(binding, descriptor.binding.kind); + } + assert!(matches!( + generated::functions::fixture_descriptor().schema.params[0].ty, + vm::HostTypeSchema::Option(_) + )); + assert!(matches!( + generated::functions::unit_descriptor().schema.return_type, + vm::HostTypeSchema::Unit + )); + assert!(matches!( + generated::functions::resource_take_descriptor() + .schema + .params[0] + .ty, + vm::HostTypeSchema::Option(_) + )); + assert!(matches!( + generated::functions::resource_make_descriptor() + .schema + .return_type, + vm::HostTypeSchema::Option(_) + )); + let callable_schema = &generated::functions::callable_echo_descriptor() + .schema + .params[0] + .ty; + assert_eq!( + generated::functions::callable_echo_descriptor() + .schema + .params[0] + .passing, + vm::HostParamPassing::Value, + "a callable payload owns no resource and uses value passing" + ); + let vm::HostTypeSchema::Option(callable) = callable_schema else { + panic!("callable Option parameter schema: {callable_schema:?}"); + }; + let vm::HostTypeSchema::Callable { params, result } = callable.as_ref() else { + panic!("callable Option payload must carry the Callable schema"); + }; + assert_eq!(params, &vec![vm::HostTypeSchema::Int]); + assert!(matches!(result.as_ref(), vm::HostTypeSchema::Int)); + + let unit_schema = &generated::functions::callable_unit_echo_descriptor() + .schema + .params[0] + .ty; + let vm::HostTypeSchema::Option(unit) = unit_schema else { + panic!("Unit callable Option parameter schema: {unit_schema:?}"); + }; + let vm::HostTypeSchema::Callable { params, result } = unit.as_ref() else { + panic!("Unit callable payload must carry the Callable schema"); + }; + assert!(params.is_empty()); + assert!(matches!(result.as_ref(), vm::HostTypeSchema::Unit)); + + let defaulted_unit_schema = &generated::functions::callable_unit_default_echo_descriptor() + .schema + .params[0] + .ty; + assert_eq!( + defaulted_unit_schema, unit_schema, + "omitted and explicit Unit callable spellings must publish the same schema" + ); + assert_eq!( + generated::functions::callable_unit_default_echo_descriptor() + .schema + .params[0] + .passing, + vm::HostParamPassing::Value + ); + + let pair_schema = &generated::functions::callable_pair_echo_descriptor() + .schema + .params[0] + .ty; + let vm::HostTypeSchema::Option(pair) = pair_schema else { + panic!("pair callable Option parameter schema: {pair_schema:?}"); + }; + let vm::HostTypeSchema::Callable { params, result } = pair.as_ref() else { + panic!("pair callable payload must carry the Callable schema"); + }; + assert_eq!( + params, + &vec![vm::HostTypeSchema::Int, vm::HostTypeSchema::Bool] + ); + assert!(matches!(result.as_ref(), vm::HostTypeSchema::Int)); +} + +#[test] +fn logical_option_and_result_signatures_do_not_interchange() { + let catalog = Arc::new(vm::HostFunctionDescriptor::collect_catalog(&descriptors()).unwrap()); + let error = match vm::compile_source_with_flavor_and_options( + "use option_sdk;\n\ + let value: Result = Result::Ok([3, 4]);\n\ + option_sdk::fixture(value);", + vm::SourceFlavor::RustScript, + vm::CompileSourceFileOptions::default().with_host_api_catalog(Arc::clone(&catalog)), + ) { + Ok(_) => panic!("Option parameter must reject an identical-shaped Result carrier"), + Err(error) => error, + }; + let message = error.to_string(); + assert!( + message.contains("Option") && message.contains("Result"), + "Option/Result signature mismatch was {message}" + ); + + let mut builder = vm::HostApiBuilder::new(); + builder.function(vm::HostFunctionSchema::with_return( + "mismatch::result", + vec![vm::HostParamSchema::value( + "value", + vm::HostTypeSchema::Result { + ok: Box::new(vm::HostTypeSchema::Array(Box::new(vm::HostTypeSchema::Int))), + err: Box::new(vm::HostTypeSchema::String), + }, + )], + vm::HostTypeSchema::Int, + )); + let result_catalog = Arc::new(builder.build().expect("result catalog")); + let error = match vm::compile_source_with_flavor_and_options( + "use mismatch;\n\ + let value: Option = Option::Some([3, 4]);\n\ + mismatch::result(value);", + vm::SourceFlavor::RustScript, + vm::CompileSourceFileOptions::default().with_host_api_catalog(result_catalog), + ) { + Ok(_) => panic!("Result parameter must reject an identical-shaped Option carrier"), + Err(error) => error, + }; + let message = error.to_string(); + assert!( + message.contains("Option") && message.contains("Result"), + "Result/Option signature mismatch was {message}" + ); +} + +#[test] +fn rust_option_roundtrips_through_generated_host_and_rss() { + for (source, expected) in [ + ( + "use option_sdk; let value: Option = option_sdk::fixture(Option::Some([3, 4])); match value { Option::Some(items) => items[0] + items[1], Option::None => -1, };", + 7, + ), + ( + "use option_sdk; let value: Option = option_sdk::fixture(Option::None); match value { Option::Some(items) => 100, Option::None => -1, };", + -1, + ), + ] { + let value = eval(source); + match value { + Value::Int(value) => assert_eq!(value, expected), + other => panic!("unexpected value {other:?}"), + } + } + // `Some([])` stays a selected Some with an empty payload; `inspect` proves + // it is distinct from None without indexing an empty array. + let value = eval("use option_sdk; option_sdk::inspect(Option::Some([]));"); + assert_eq!(value, Value::Int(0)); +} + +#[test] +fn nested_option_states_cross_the_generated_host_boundary() { + for (mode, expected) in [(0, 0), (1, 1), (2, 107)] { + let value = eval(&format!( + "use option_sdk; let nested: Option> = option_sdk::nested({mode}); match nested {{ Option::Some(inner) => match inner {{ Option::Some(boxed) => boxed.value + 100, Option::None => 1 }}, Option::None => 0 }};" + )); + assert_eq!(value, Value::Int(expected), "mode {mode}"); + } +} + +#[test] +fn unit_host_returns_carry_logical_unit_to_rss() { + let value = eval("use option_sdk; let unit: () = option_sdk::unit(); unit;"); + assert_eq!(value, Value::Null); + assert_eq!( + generated::functions::unit_descriptor().schema.return_type, + vm::HostTypeSchema::Unit + ); +} + +#[test] +fn generated_host_roundtrips_callable_option_payloads() { + let source = r#" + use option_sdk; + pub fn double(value: int) -> int { value + value } + fn announce() { } + fn invoke_unit(f: fn() -> ()) -> int { f(); 5 } + fn pick(value: int, scale: bool) -> int { if scale => { value * 2 } else => { value } } + let echoed: Option int> = option_sdk::callable_echo(Option::Some(double)); + let doubled = match echoed { Option::Some(f) => f(4), Option::None => -1 }; + let absent: Option int> = option_sdk::callable_echo(Option::None); + let skipped = match absent { Option::Some(f) => f(4), Option::None => -2 }; + let picked: Option int> = option_sdk::callable_pick(true); + let from_host = match picked { Option::Some(f) => f(5), Option::None => -3 }; + let empty: Option int> = option_sdk::callable_pick(false); + let missing = match empty { Option::Some(f) => f(5), Option::None => -4 }; + let offset = 10; + let add: fn(int) -> int = |value| value + offset; + let carried: Option int> = option_sdk::callable_echo(Option::Some(add)); + let captured = match carried { Option::Some(f) => f(6) + f(7), Option::None => -5 }; + let unit_some: Option ()> = option_sdk::callable_unit_echo(Option::Some(announce)); + let unit_result = match unit_some { Option::Some(f) => invoke_unit(f), Option::None => -6 }; + let unit_none: Option ()> = option_sdk::callable_unit_echo(Option::None); + let unit_skipped = match unit_none { Option::Some(f) => invoke_unit(f), Option::None => -7 }; + let pair: Option int> = option_sdk::callable_pair_echo(Option::Some(pick)); + let pair_result = match pair { Option::Some(f) => f(9, true), Option::None => -8 }; + doubled + skipped + from_host + missing + captured + unit_result + unit_skipped + pair_result; + "#; + assert_eq!(eval(source), Value::Int(61)); + + let none = eval("use option_sdk; option_sdk::callable_count(Option::None);"); + assert_eq!(none, Value::Int(0)); + let some = eval( + "use option_sdk; fn double(value: int) -> int { value + value } option_sdk::callable_count(Option::Some(double));", + ); + assert_eq!(some, Value::Int(1)); +} + +#[test] +fn generated_host_admits_and_invokes_bare_rust_callable_unit() { + let source = r#" + use option_sdk; + fn tick() { let total: int = option_sdk::inspect(Option::Some([2, 3])); } + fn invoke_unit(f: fn() -> ()) -> int { f(); 5 } + let selected: Option ()> = option_sdk::callable_unit_default_echo(Option::Some(tick)); + let called = match selected { Option::Some(f) => invoke_unit(f), Option::None => -1 }; + let absent: Option ()> = option_sdk::callable_unit_default_echo(Option::None); + let skipped = match absent { Option::Some(f) => invoke_unit(f), Option::None => -6 }; + called + skipped; + "#; + let mut runtime = machine(source); + assert_eq!(runtime.run().unwrap(), VmStatus::Halted); + assert_eq!(runtime.stack().last(), Some(&Value::Int(-1))); + assert_eq!( + runtime.host_context().module_state::().copied(), + Some(1), + "Some must invoke the no-tail callable body once; None must stay uncalled" + ); +} + +#[test] +fn typed_rust_callback_accepts_returns_and_invokes_guest_option_callable() { + let compiled = vm::compile_source( + "fn double(value: int) -> int { value + value }\n\ + fn announce() { }\n\ + fn pick(value: int, scale: bool) -> int { if scale => { value * 2 } else => { value } }\n\ + pub fn make(present: bool) -> Option int> { let offset = 1; let add: fn(int) -> int = |value| value + offset; if present => { Option::Some(add) } else => { Option::None } }\n\ + pub fn make_unit(present: bool) -> Option ()> { if present => { Option::Some(announce) } else => { Option::None } }\n\ + pub fn make_pair(present: bool) -> Option int> { if present => { Option::Some(pick) } else => { Option::None } }\n\ + pub fn apply(value: Option int>) -> int { match value { Option::Some(f) => f(3), Option::None => -1 } }", + ) + .unwrap(); + let mut store = vm::Store::new( + Vm::new(compiled.program.with_local_count(compiled.locals)), + (), + ); + store.vm_mut().set_fuel(100_000); + assert_eq!(store.run().unwrap(), VmStatus::Halted); + + let make = store + .script_callback_by_name::<(bool,), GuestOption i64>>>("make") + .unwrap(); + let present = make.call(&mut store, (true,)).unwrap(); + let GuestOption::Some(callable) = present else { + panic!("present selection must carry a typed callable"); + }; + let invoke = store + .script_callback::<(i64,), i64>(callable.into_value()) + .unwrap(); + assert_eq!(invoke.call(&mut store, (7,)).unwrap(), 8); + assert_eq!(invoke.call(&mut store, (8,)).unwrap(), 9); + assert!(matches!( + make.call(&mut store, (false,)).unwrap(), + GuestOption::None + )); + + let apply = store + .script_callback_by_name::<(GuestOption i64>>,), i64>("apply") + .unwrap(); + let held = match make.call(&mut store, (true,)).unwrap() { + GuestOption::Some(value) => value, + GuestOption::None => panic!("second present selection must carry a callable"), + }; + assert_eq!( + apply.call(&mut store, (GuestOption::Some(held),)).unwrap(), + 4 + ); + assert_eq!(apply.call(&mut store, (GuestOption::None,)).unwrap(), -1); + + let make_unit = store + .script_callback_by_name::<(bool,), GuestOption ()>>>("make_unit") + .unwrap(); + let unit_selected = match make_unit.call(&mut store, (true,)).unwrap() { + GuestOption::Some(value) => value, + GuestOption::None => panic!("present Unit selection must carry a callable"), + }; + let unit_invoke = store + .script_callback::<(), ()>(unit_selected.into_value()) + .unwrap(); + assert_eq!(unit_invoke.call(&mut store, ()).unwrap(), ()); + assert!(matches!( + make_unit.call(&mut store, (false,)).unwrap(), + GuestOption::None + )); + + // The omitted Rust spelling is the same public callable type and must + // admit the no-tail RSS function through the typed Store path too. + let make_unit_bare = store + .script_callback_by_name::<(bool,), GuestOption>>("make_unit") + .unwrap(); + let bare_selected = match make_unit_bare.call(&mut store, (true,)).unwrap() { + GuestOption::Some(value) => value, + GuestOption::None => panic!("bare Unit selection must carry a callable"), + }; + let bare_invoke = store + .script_callback::<(), ()>(bare_selected.into_value()) + .unwrap(); + assert_eq!(bare_invoke.call(&mut store, ()).unwrap(), ()); + assert!(matches!( + make_unit_bare.call(&mut store, (false,)).unwrap(), + GuestOption::None + )); + + let make_pair = store + .script_callback_by_name::<(bool,), GuestOption i64>>>( + "make_pair", + ) + .unwrap(); + let pair_selected = match make_pair.call(&mut store, (true,)).unwrap() { + GuestOption::Some(value) => value, + GuestOption::None => panic!("present pair selection must carry a callable"), + }; + let pair_invoke = store + .script_callback::<(i64, bool), i64>(pair_selected.into_value()) + .unwrap(); + assert_eq!(pair_invoke.call(&mut store, (4, true)).unwrap(), 8); + assert_eq!(pair_invoke.call(&mut store, (4, false)).unwrap(), 4); + + let cross = match make.call(&mut store, (true,)).unwrap() { + GuestOption::Some(value) => value.into_value(), + GuestOption::None => panic!("third present selection must carry a callable"), + }; + let mut other = vm::Store::new( + Vm::new(vm::Program::new(Vec::new(), vec![vm::OpCode::Ret as u8])), + (), + ); + assert!( + other.script_callback::<(i64,), i64>(cross.clone()).is_err(), + "callable from another store must be rejected" + ); + + store.vm_mut().reset_for_reuse().unwrap(); + let mut context = std::task::Context::from_waker(std::task::Waker::noop()); + assert!(matches!( + store.vm_mut().poll_reset_for_reuse(&mut context), + std::task::Poll::Ready(Ok(())) + )); + assert!( + store.script_callback::<(i64,), i64>(cross).is_err(), + "reset must invalidate a previously returned callable" + ); +} + +#[test] +fn callable_option_signature_mismatch_rejects_before_guest_side_effects() { + let descriptors = descriptors(); + let catalog = Arc::new(vm::HostFunctionDescriptor::collect_catalog(&descriptors).unwrap()); + let error = match vm::compile_source_with_flavor_and_options( + "use option_sdk;\n\ + fn text(value: string) -> string { value }\n\ + option_sdk::callable_echo(Option::Some(text));", + vm::SourceFlavor::RustScript, + vm::CompileSourceFileOptions::default().with_host_api_catalog(catalog), + ) { + Ok(_) => panic!("a string callable must not satisfy the int callable schema"), + Err(error) => error, + }; + let message = error.to_string(); + assert!( + message.contains("callable") || message.contains("Option"), + "callable signature mismatch was {message}" + ); + + let compiled = vm::compile_source( + "fn text(value: string) -> string { value }\n\ + pub fn text_fn() -> fn(string) -> string { text }", + ) + .unwrap(); + let mut machine = Vm::new(compiled.program.with_local_count(compiled.locals)); + assert_eq!(machine.run().unwrap(), VmStatus::Halted); + let text_fn = machine.resolve_exported_callable("text_fn").unwrap(); + let returned = machine.invoke_callable(text_fn, &[]).unwrap(); + let wrapped = + i64> as FromVmValue>::from_vm_value(&returned, "callable").unwrap(); + let carrier = vm::option::some_carrier(wrapped.into_value()); + let schema = generated::functions::callable_echo_descriptor() + .schema + .params[0] + .ty + .clone(); + let error = machine + .validate_guest_result_value(&carrier, &schema) + .expect_err("runtime admission must reject the mismatched callable signature"); + assert!(error.to_string().contains("callable"), "{error}"); +} + +#[test] +fn result_and_option_nest_across_the_host_boundary() { + let value = eval( + "use option_sdk; let some: Option> = option_sdk::option_result(Option::Some(Result::Ok(5))); match some { Option::Some(result) => match result { Result::Ok(v) => v, Result::Err(e) => -10 }, Option::None => -20 };", + ); + assert_eq!(value, Value::Int(5)); + let value = eval( + "use option_sdk; let some: Option> = option_sdk::option_result(Option::Some(Result::Err(\"bad\"))); match some { Option::Some(result) => match result { Result::Ok(v) => v, Result::Err(e) => 2 }, Option::None => -20 };", + ); + assert_eq!(value, Value::Int(2)); + let value = eval( + "use option_sdk; let absent: Option> = option_sdk::option_result(Option::None); match absent { Option::Some(result) => 10, Option::None => 3 };", + ); + assert_eq!(value, Value::Int(3)); + let value = eval( + "use option_sdk; let nested: Result, string> = option_sdk::result_option(Result::Ok(Option::Some(6))); match nested { Result::Ok(option) => match option { Option::Some(v) => v, Option::None => -1 }, Result::Err(e) => -2 };", + ); + assert_eq!(value, Value::Int(6)); + let value = eval( + "use option_sdk; let nested: Result, string> = option_sdk::result_option(Result::Ok(Option::None)); match nested { Result::Ok(option) => match option { Option::Some(v) => 11, Option::None => 4 }, Result::Err(e) => -2 };", + ); + assert_eq!(value, Value::Int(4)); +} + +#[test] +fn inspect_observes_selected_option_without_collapsing_states() { + let mut machine = machine( + "use option_sdk; option_sdk::inspect(Option::None); option_sdk::inspect(Option::Some([])); option_sdk::inspect(Option::Some([9]));", + ); + assert_eq!(machine.run().unwrap(), VmStatus::Halted); + assert_eq!(machine.host_context().module_state::(), Some(&3)); +} + +#[test] +fn typed_conversions_preserve_some_none_unit_and_empty_containers() { + use vm::{FromVmValue, IntoVmValue, TakeVmValue}; + let zero = GuestOption::::Some(0).into_vm_value(); + assert_eq!( + GuestOption::::from_vm_value(&zero, "option").unwrap(), + GuestOption::Some(0) + ); + let mut zero = zero; + assert_eq!( + GuestOption::::take_vm_value(&mut zero, "option").unwrap(), + GuestOption::Some(0) + ); + assert_eq!(zero, Value::Null); + let none = GuestOption::::None.into_vm_value(); + assert_eq!( + GuestOption::::from_vm_value(&none, "option").unwrap(), + GuestOption::None + ); + let false_some = GuestOption::::Some(false).into_vm_value(); + assert_eq!( + GuestOption::::from_vm_value(&false_some, "option").unwrap(), + GuestOption::Some(false) + ); + let empty_string = GuestOption::::Some(String::new()).into_vm_value(); + assert_eq!( + GuestOption::::from_vm_value(&empty_string, "option").unwrap(), + GuestOption::Some(String::new()) + ); + let empty_array = GuestOption::>::Some(Vec::new()).into_vm_value(); + assert_eq!( + GuestOption::>::from_vm_value(&empty_array, "option").unwrap(), + GuestOption::Some(Vec::new()) + ); + let unit_some = GuestOption::<()>::Some(()).into_vm_value(); + assert_eq!( + GuestOption::<()>::from_vm_value(&unit_some, "option").unwrap(), + GuestOption::Some(()) + ); + // A canonical Some(()) writer is allowed to omit the Null-valued payload; + // only the proven Unit schema may restore the missing entry. + let unit_tag_only = Value::map(vec![(Value::string("tag"), Value::Bool(true))]); + assert_eq!( + GuestOption::<()>::from_vm_value(&unit_tag_only, "option").unwrap(), + GuestOption::Some(()) + ); + assert!(GuestOption::::from_vm_value(&unit_tag_only, "option").is_err()); + for value in [ + Value::map(vec![ + (Value::string("tag"), Value::Int(1)), + (Value::string("payload"), Value::Int(0)), + ]), + Value::map(vec![(Value::string("payload"), Value::Int(0))]), + Value::map(vec![ + (Value::string("tag"), Value::Bool(false)), + (Value::string("payload"), Value::Int(0)), + ]), + Value::map(vec![ + (Value::string("tag"), Value::Bool(true)), + (Value::string("payload"), Value::Int(0)), + (Value::string("extra"), Value::Int(1)), + ]), + ] { + assert!( + GuestOption::::from_vm_value(&value, "option").is_err(), + "{value:?}" + ); + } +} + +#[test] +fn ordinary_and_vm_aware_callbacks_preserve_option_and_unit() { + let compiled = vm::compile_source( + "pub fn echo(value: Option) -> Option { value }\n\ + pub fn choose(mode: int) -> Option { if mode == 0 => { Option::Some(0) } else => { Option::None } }\n\ + pub fn unit() -> () { () }", + ) + .unwrap(); + let mut store = vm::Store::new( + Vm::new(compiled.program.with_local_count(compiled.locals)), + (), + ); + store.vm_mut().set_fuel(100_000); + assert_eq!(store.run().unwrap(), VmStatus::Halted); + + let ordinary = store + .script_callback_by_name::<(GuestOption,), GuestOption>("echo") + .unwrap(); + for value in [GuestOption::Some(0), GuestOption::None] { + assert_eq!(ordinary.call(&mut store, (value,)).unwrap(), value); + } + let vm_aware = store + .vm_script_callback_by_name::<(GuestOption,), GuestOption>("echo") + .unwrap(); + for value in [GuestOption::Some(7), GuestOption::None] { + assert_eq!(vm_aware.call(&mut store, (value,)).unwrap(), value); + } + + let choose = store + .script_callback_by_name::<(i64,), GuestOption>("choose") + .unwrap(); + for (mode, expected) in [(0, GuestOption::Some(0)), (1, GuestOption::None)] { + assert_eq!(choose.call(&mut store, (mode,)).unwrap(), expected); + } + + let ordinary_unit = store.script_callback_by_name::<(), ()>("unit").unwrap(); + assert_eq!(ordinary_unit.call(&mut store, ()).unwrap(), ()); + let vm_unit = store.vm_script_callback_by_name::<(), ()>("unit").unwrap(); + assert_eq!(vm_unit.call(&mut store, ()).unwrap(), ()); + + let mut other = vm::Store::new( + Vm::new(vm::Program::new(Vec::new(), vec![vm::OpCode::Ret as u8])), + (), + ); + assert!(ordinary.call(&mut other, (GuestOption::Some(1),)).is_err()); +} + +#[test] +fn generated_catalog_rejects_malformed_option_carriers_before_handler() { + let descriptor = generated::functions::inspect_descriptor(); + let descriptors = std::slice::from_ref(&descriptor); + let catalog = vm::HostFunctionDescriptor::collect_catalog(descriptors).unwrap(); + for value in [ + Value::Null, + Value::map(vec![ + (Value::string("tag"), Value::Int(1)), + (Value::string("payload"), Value::array(vec![])), + ]), + Value::map(vec![(Value::string("payload"), Value::array(vec![]))]), + Value::map(vec![ + (Value::string("tag"), Value::Bool(false)), + (Value::string("payload"), Value::array(vec![])), + ]), + Value::map(vec![(Value::string("tag"), Value::Bool(true))]), + Value::map(vec![ + (Value::string("tag"), Value::Bool(true)), + ( + Value::string("payload"), + Value::array(vec![Value::Bool(false)]), + ), + ]), + Value::map(vec![ + (Value::string("tag"), Value::Bool(true)), + (Value::string("payload"), Value::array(vec![])), + (Value::string("extra"), Value::Int(1)), + ]), + ] { + let import = vm::catalog_import_schemas(&catalog, "option_sdk::inspect").remove(0); + let mut code = vm::BytecodeBuilder::new(); + code.ldc(0); + code.call(0, 1); + code.ret(); + let program = vm::Program::with_imports_and_debug( + vec![value], + code.finish(), + vec![vm::HostImport { + name: "option_sdk::inspect".into(), + arity: 1, + return_type: vm::ValueType::Int, + }], + None, + ) + .with_host_import_schemas(vec![import]) + .unwrap(); + let mut machine = Vm::new(program); + let mut registry = vm::HostFunctionRegistry::empty(); + registry.set_capability_profile(vm::CapabilityProfile::deny_all()); + vm::HostModuleDescriptor::install_descriptors(&mut registry, descriptors).unwrap(); + registry.authorize_registered_builtin_import("option_sdk::inspect"); + registry.bind_vm_cached(&mut machine).unwrap(); + machine.set_fuel(100_000); + assert!(machine.run().is_err()); + assert_eq!(machine.host_context().module_state::(), None); + assert_eq!(machine.host_context().resource_count(), 0); + } +} + +#[test] +fn owned_option_resources_publish_take_and_leave_none_owner_free() { + let _lock = LOCK.lock().unwrap_or_else(|p| p.into_inner()); + let mut machine = empty_vm(); + let closed = CLOSED.load(Ordering::SeqCst); + + let none = generated::functions::resource_make(&mut machine, &[Value::Bool(false)]).unwrap(); + assert!(matches!( + none, + Value::Map(ref map) if matches!(map.get(&Value::string("tag")), Some(Value::Bool(false))) + )); + assert_eq!(machine.host_context().resource_count(), 0); + assert_eq!( + generated::functions::resource_observe(&mut machine, &[]).unwrap(), + 0 + ); + assert_eq!( + generated::functions::resource_take(&mut machine, std::slice::from_ref(&none)).unwrap(), + -1 + ); + assert_eq!(machine.host_context().resource_count(), 0); + + let some = generated::functions::resource_make(&mut machine, &[Value::Bool(true)]).unwrap(); + assert_eq!(machine.host_context().resource_count(), 1); + assert_eq!( + generated::functions::resource_observe(&mut machine, &[]).unwrap(), + 1 + ); + assert_eq!( + generated::functions::resource_take(&mut machine, std::slice::from_ref(&some)).unwrap(), + 23 + ); + assert_eq!(machine.host_context().resource_count(), 0); + assert!(generated::functions::resource_take(&mut machine, &[some]).is_err()); + assert_eq!(machine.host_context().resource_count(), 0); + + // Cross-VM ownership is rejected; the origin VM keeps its owner. + let mut origin = empty_vm(); + let mut other = empty_vm(); + let value = generated::functions::resource_make(&mut origin, &[Value::Bool(true)]).unwrap(); + assert_eq!(origin.host_context().resource_count(), 1); + assert!(generated::functions::resource_take(&mut other, &[value]).is_err()); + assert_eq!(other.host_context().resource_count(), 0); + assert_eq!(origin.host_context().resource_count(), 1); + let replacement = + generated::functions::resource_make(&mut origin, &[Value::Bool(true)]).unwrap(); + generated::functions::resource_take(&mut origin, &[replacement]).unwrap(); + assert_eq!(origin.host_context().resource_count(), 1); + drop(origin); + assert!(CLOSED.load(Ordering::SeqCst) > closed); +} + +#[test] +fn owned_option_resources_reject_wrong_resource_kind() { + let _lock = LOCK.lock().unwrap_or_else(|p| p.into_inner()); + let mut machine = empty_vm(); + let some = generated::functions::resource_make(&mut machine, &[Value::Bool(true)]).unwrap(); + assert_eq!(machine.host_context().resource_count(), 1); + let error = > as vm::VmScriptValue>::from_value( + &mut machine, + some.clone(), + ) + .expect_err("an option.success handle must not decode as option.other"); + assert!( + error.to_string().contains("resource") || error.to_string().contains("option.other"), + "wrong option resource kind was {error}" + ); + let value = generated::functions::resource_take(&mut machine, &[some]).unwrap(); + assert_eq!(value, 23); + assert_eq!(machine.host_context().resource_count(), 0); +} + +#[test] +fn owned_option_publication_rolls_back_after_insertion() { + let _lock = LOCK.lock().unwrap_or_else(|p| p.into_inner()); + let mut machine = empty_vm(); + let schema = generated::functions::resource_make_descriptor() + .schema + .return_type; + let closed = CLOSED.load(Ordering::SeqCst); + let error = vm::result::publish_owned( + &mut machine, + ResourceOwned::new(OptionSuccess(41)), + &schema, + |_| Err(vm::VmError::HostError("option assembly denied".into())), + ) + .unwrap_err(); + assert!(error.to_string().contains("option assembly denied")); + assert_eq!(machine.host_context().resource_count(), 0); + assert_eq!(CLOSED.load(Ordering::SeqCst), closed + 1); + + let error = vm::result::publish_owned( + &mut machine, + ResourceOwned::new(OptionSuccess(42)), + &schema, + |_| Ok(vm::result::carrier(true, Value::Bool(true))), + ) + .unwrap_err(); + assert!(error.to_string().contains("resource")); + assert_eq!(machine.host_context().resource_count(), 0); + assert_eq!(CLOSED.load(Ordering::SeqCst), closed + 2); +} + +#[test] +fn rss_constructs_and_transfers_owned_option_resources() { + let source = r#" + use option_sdk; + let made: Option> = option_sdk::resource_make(true); + let before = option_sdk::resource_observe(); + let taken = match made { + Option::Some(owner) => option_sdk::resource_take(Option::Some(owner)), + Option::None => -1, + }; + let after = option_sdk::resource_observe(); + let absent: Option> = option_sdk::resource_make(false); + let none_take = option_sdk::resource_take(absent); + before + after + taken + none_take; + "#; + let mut machine = machine(source); + assert_eq!(machine.run().unwrap(), VmStatus::Halted); + assert_eq!(machine.stack().last(), Some(&Value::Int(23))); + assert_eq!(machine.host_context().resource_count(), 0); + assert_eq!(OBSERVED_RESOURCE_COUNT.load(Ordering::SeqCst), 0); +} + +#[cfg(feature = "cranelift-jit")] +#[test] +fn option_roundtrip_executes_in_native_aot() { + let mut machine = machine( + "use option_sdk; let value: Option = option_sdk::fixture(Option::Some([3, 4])); option_sdk::inspect(value);", + ); + machine.compile_aot().unwrap(); + let dump = machine.dump_aot_info(); + assert!(dump.contains("lowering=ssa"), "{dump}"); + assert!(!dump.contains("interpreter-boundary"), "{dump}"); + assert_eq!(machine.run().unwrap(), VmStatus::Halted); + assert_eq!(machine.stack().last(), Some(&Value::Int(7))); + assert!(machine.aot_exec_count() > 0, "{dump}"); +} + +#[test] +fn option_roundtrip_executes_from_loaded_vmbc() { + let source = "use option_sdk; fn double(value: int) -> int { value + value } let value: Option = option_sdk::fixture(Option::Some([3, 4])); let callable: Option int> = option_sdk::callable_echo(Option::Some(double)); let applied = match callable { Option::Some(f) => f(4), Option::None => -1 }; match value { Option::Some(items) => items[0] + items[1] + applied, Option::None => -1 };"; + let descriptors = descriptors(); + let catalog = Arc::new(vm::HostFunctionDescriptor::collect_catalog(&descriptors).unwrap()); + let compiled = vm::compile_source_with_flavor_and_options( + source, + vm::SourceFlavor::RustScript, + vm::CompileSourceFileOptions::default().with_host_api_catalog(catalog), + ) + .unwrap(); + let bytes = vm::encode_program(&compiled.program.with_local_count(compiled.locals)).unwrap(); + let decoded = vm::decode_program(&bytes).unwrap(); + let mut loaded = Vm::new(decoded.with_local_count(compiled.locals)); + bind_registry(&mut loaded); + loaded.set_fuel(100_000); + assert_eq!(loaded.run().unwrap(), VmStatus::Halted); + assert_eq!(loaded.stack().last(), Some(&Value::Int(15))); +} + +#[cfg(feature = "cranelift-jit")] +#[test] +fn option_states_execute_in_trace_jit() { + let source = r#" + fn choose(index: int) -> Option { + if index == 64 => { Option::None } else => { Option::Some(index) } + } + let mut index = 0; + let mut total = 0; + while index < 128 { + let value: Option = choose(index); + let selected = match value { Option::Some(inner) => inner, Option::None => 1 }; + total = total + selected; + index = index + 1; + } + total; + "#; + let mut machine = machine(source); + machine.set_jit_config(vm::JitConfig { + enabled: true, + hot_loop_threshold: 1, + max_trace_len: 512, + }); + assert_eq!(machine.run().unwrap(), VmStatus::Halted); + assert_eq!(machine.stack().last(), Some(&Value::Int(8065))); + assert!( + machine.jit_native_exec_count() > 0, + "Option states must execute in a native trace: {}", + machine.dump_jit_info() + ); +} + +#[cfg(feature = "cranelift-jit")] +#[test] +fn option_combinators_and_projection_execute_in_trace_jit() { + let source = r#" + struct Row { value: int } + let row: Row = { value: 2 }; + let mut index = 0; + let mut total = 0; + while index < 128 { + let base: Option = Option::Some(index); + let mapped = base.map(|value| value + 1).and_then(|value| Option::Some(value + 1)); + let selected = mapped.unwrap_or(0); + let projected = row?.value.unwrap_or(0); + total = total + selected + projected; + index = index + 1; + } + total; + "#; + let mut machine = machine(source); + machine.set_jit_config(vm::JitConfig { + enabled: true, + hot_loop_threshold: 1, + max_trace_len: 1024, + }); + assert_eq!(machine.run().unwrap(), VmStatus::Halted); + assert_eq!(machine.stack().last(), Some(&Value::Int(8640))); + assert!( + machine.jit_native_exec_count() > 0, + "Option combinators and projection must execute in a native trace: {}", + machine.dump_jit_info() + ); +} + +#[cfg(feature = "cranelift-jit")] +#[test] +fn option_roundtrip_executes_from_loaded_aot_artifact() { + let source = "use option_sdk; let value: Option = option_sdk::fixture(Option::Some([3, 4])); option_sdk::inspect(value);"; + let mut source_machine = machine(source); + source_machine.compile_aot().unwrap(); + let artifact = source_machine.encode_aot_artifact().unwrap(); + let program = source_machine.program().clone(); + let mut loaded = Vm::new(program); + bind_registry(&mut loaded); + loaded.set_fuel(100_000); + loaded.load_aot_artifact(&artifact).unwrap(); + assert_eq!(loaded.run().unwrap(), VmStatus::Halted); + assert_eq!(loaded.stack().last(), Some(&Value::Int(7))); + assert!( + loaded.aot_exec_count() > 0, + "loaded AOT execution must use the native artifact" + ); +} diff --git a/tests/option_language_tests.rs b/tests/option_language_tests.rs new file mode 100644 index 00000000..54d1b3e7 --- /dev/null +++ b/tests/option_language_tests.rs @@ -0,0 +1,390 @@ +#![cfg(feature = "runtime")] + +use vm::{Vm, VmStatus, compile_source}; + +fn eval(source: &str) -> vm::Value { + let compiled = compile_source(source).expect("Option source should compile"); + let mut vm = Vm::new(compiled.program.with_local_count(compiled.locals)); + vm.set_fuel(100_000); + assert_eq!( + vm.run().expect("Option program should run"), + VmStatus::Halted + ); + vm.stack() + .last() + .cloned() + .expect("Option program should leave a value") +} + +fn reject(source: &str) -> String { + match compile_source(source) { + Ok(_) => panic!("Option source should be rejected: {source}"), + Err(error) => error.to_string(), + } +} + +#[test] +fn nested_option_states_are_constructed_matched_and_distinct() { + let source = r#" + let none: Option> = Option::None; + let some_none: Option> = Option::Some(Option::None); + let some_some: Option> = Option::Some(Option::Some(0)); + let output = match none { + Option::Some(inner) => match inner { Option::Some(v) => 10, Option::None => 11 }, + Option::None => 0, + }; + let output2 = match some_none { + Option::Some(inner) => match inner { Option::Some(v) => 20 + v, Option::None => 21 }, + Option::None => 30, + }; + let output3 = match some_some { + Option::Some(inner) => match inner { Option::Some(v) => 40 + v, Option::None => 41 }, + Option::None => 50, + }; + output + output2 + output3; + "#; + assert_eq!(eval(source), vm::Value::Int(61)); +} + +#[test] +fn unit_none_some_unit_false_zero_and_empty_string_are_distinct() { + let source = r#" + let unit_none: Option<()> = Option::None; + let unit_some: Option<()> = Option::Some(()); + let none_bool: Option = Option::None; + let some_false: Option = Option::Some(false); + let none_int: Option = Option::None; + let some_zero: Option = Option::Some(0); + let none_string: Option = Option::None; + let some_empty: Option = Option::Some(""); + let a = match unit_none { Option::Some(u) => 1, Option::None => 2 }; + let b = match unit_some { Option::Some(u) => 4, Option::None => 8 }; + let c = match none_bool { Option::Some(v) => 16, Option::None => 32 }; + let d = match some_false { Option::Some(v) => 64, Option::None => 128 }; + let e = match none_int { Option::Some(v) => 1, Option::None => 2 }; + let f = match some_zero { Option::Some(v) => 4, Option::None => 8 }; + let g = match none_string { Option::Some(v) => 16, Option::None => 32 }; + let h = match some_empty { Option::Some(v) => 64, Option::None => 128 }; + a + b + c + d + e + f + g + h; + "#; + assert_eq!(eval(source), vm::Value::Int(204)); +} + +#[test] +fn nested_result_option_and_option_result_compose() { + let source = r#" + let option_result: Option> = Option::Some(Result::Ok(3)); + let absent: Option> = Option::None; + let result_option: Result, string> = Result::Ok(Option::Some(4)); + let result_absent: Result, string> = Result::Ok(Option::None); + let first = match option_result { + Option::Some(result) => match result { Result::Ok(value) => value, Result::Err(error) => -1 }, + Option::None => -2, + }; + let second = match absent { + Option::Some(result) => 100, + Option::None => 10, + }; + let third = match result_option { + Result::Ok(option) => match option { Option::Some(value) => value, Option::None => -4 }, + Result::Err(error) => -8, + }; + let fourth = match result_absent { + Result::Ok(option) => match option { Option::Some(value) => 100, Option::None => 20 }, + Result::Err(error) => -16, + }; + first + second + third + fourth; + "#; + assert_eq!(eval(source), vm::Value::Int(37)); +} + +#[test] +fn named_array_tuple_and_callable_payloads_keep_their_schemas() { + let source = r#" + struct Point { x: int } + fn add1(value: int) -> int { value + 1 } + let named: Option = Option::Some({ x: 5 }); + let array: Option<[Option, string]> = Option::Some([Option::Some(1), "tag"]); + let callable: Option int> = Option::Some(add1); + let x = match named { Option::Some(value) => value.x, Option::None => -1 }; + let inner = match array { + Option::Some(values) => match values[0] { Option::Some(v) => v, Option::None => -2 }, + Option::None => -4, + }; + let applied = match callable { Option::Some(f) => f(6), Option::None => -8 }; + x + inner + applied; + "#; + assert_eq!(eval(source), vm::Value::Int(13)); +} + +#[test] +fn some_payload_is_evaluated_exactly_once() { + let source = r#" + let mut calls = [0]; + let bump: fn() -> int = || if true => { calls[0] = calls[0].copy() + 1; calls[0].copy() } else => { 0 }; + let some: Option = Option::Some(bump()); + let observed = match some { + Option::Some(value) => value, + Option::None => 0, + }; + observed + calls[0].copy(); + "#; + assert_eq!(eval(source), vm::Value::Int(2)); +} + +#[test] +fn option_constructors_and_matches_require_explicit_context() { + let missing_none = reject("Option::None;"); + assert!( + missing_none.contains("Option annotation"), + "missing None context error was {missing_none}" + ); + let missing_inner = reject("let value = Option::Some(Option::None);"); + assert!( + missing_inner.contains("Option annotation") || missing_inner.contains("Option::None"), + "missing inner context error was {missing_inner}" + ); + let wrong_context = reject("let value: int = Option::Some(1);"); + assert!( + wrong_context.contains("Option") && wrong_context.contains("int"), + "wrong context error was {wrong_context}" + ); + let wrong_payload = reject("let value: Option = Option::Some(\"text\");"); + assert!( + wrong_payload.contains("schema") || wrong_payload.contains("assigned"), + "wrong payload error was {wrong_payload}" + ); + compile_source("let value: Option = Option::Some(1); value;") + .expect("Some payload inference should compile"); +} + +#[test] +fn option_match_must_be_exhaustive_and_unambiguous() { + let missing_none = + reject("let value: Option = Option::Some(1); match value { Option::Some(v) => v };"); + assert!( + missing_none.contains("missing Option::None"), + "missing None arm error was {missing_none}" + ); + let missing_some = + reject("let value: Option = Option::Some(1); match value { Option::None => 0 };"); + assert!( + missing_some.contains("missing Option::Some"), + "missing Some arm error was {missing_some}" + ); + let duplicate = reject( + "let value: Option = Option::Some(1); match value { Option::Some(v) => v, Option::Some(v) => v, Option::None => 0 };", + ); + assert!( + duplicate.contains("duplicate Option::Some"), + "duplicate arm error was {duplicate}" + ); + let wildcard = reject( + "let value: Option = Option::Some(1); match value { Option::Some(v) => v, Option::None => 0, _ => 1 };", + ); + assert!( + wildcard.contains("wildcard") || wildcard.contains("both"), + "wildcard error was {wildcard}" + ); + let mixed = reject( + "let value: Option> = Option::Some(Result::Ok(1)); match value { Option::Some(v) => 1, Option::None => 2, Result::Ok(v) => 3, Result::Err(e) => 4 };", + ); + assert!( + mixed.contains("mix") || mixed.contains("Result"), + "mixed arm error was {mixed}" + ); + let wildcard_none = + "let value: Option = Option::Some(1); match value { Option::Some(v) => v, _ => 0 };"; + assert_eq!(eval(wildcard_none), vm::Value::Int(1)); +} + +#[test] +fn unit_literal_blocks_and_callable_returns_carry_unit_facts() { + let source = r#" + fn empty() { } + fn explicit() -> () { () } + fn block() -> () { let local = 1; () } + fn branch(flag: bool) -> () { if flag => { () } else => { () } } + let a = empty(); + let b = explicit(); + let c = block(); + let d = branch(true); + let unit: () = (); + unit; + "#; + assert_eq!(eval(source), vm::Value::Null); +} + +#[test] +fn empty_blocks_and_no_tail_closures_carry_unit_facts() { + let source = r#" + fn no_tail(value: int) { let local = value + 1; } + let closure: fn() -> () = || if true => { } else => { }; + let from_function: () = no_tail(1); + let from_closure: () = closure(); + let some: Option<()> = Option::Some(from_closure); + let matched = match some { Option::Some(unit) => 7, Option::None => -1 }; + matched + 1; + "#; + assert_eq!(eval(source), vm::Value::Int(8)); +} + +#[test] +fn option_operations_cover_supported_methods_and_remaining_negatives() { + let source = r#" + let value: Option = Option::Some(1); + let mapped = value.map(|item| item + 1); + let unwrapped = value.unwrap_or(0); + let a = match mapped { Option::Some(v) => v, Option::None => -1 }; + a + unwrapped; + "#; + assert_eq!(eval(source), vm::Value::Int(3)); + + let absent = r#" + fn relay() -> Option { + let value: Option = Option::None; + let unwrapped = value?; + Option::Some(unwrapped) + } + match relay() { Option::Some(v) => v, Option::None => 7 }; + "#; + assert_eq!(eval(absent), vm::Value::Int(7)); + + let map_err_on_option = + reject("let value: Option = Option::Some(1); value.map_err(|error| error);"); + assert!( + map_err_on_option.contains("map_err") && map_err_on_option.contains("Result"), + "Option.map_err remains Result-only: {map_err_on_option}" + ); + let unwrap = reject("let value: Option = Option::Some(1); value.unwrap();"); + assert!( + unwrap.contains("unwrap") && unwrap.contains("not supported"), + "bare unwrap remains rejected: {unwrap}" + ); + let mixed = reject( + "fn out() -> Result { let value: Option = Option::None; let item = value?; Result::Ok(item) } out();", + ); + assert!( + mixed.contains("Option") && mixed.contains("Result"), + "mixed propagation requires an explicit conversion: {mixed}" + ); +} + +#[test] +fn option_methods_and_projection_format_twice_and_publish_hints() { + let source = r#" + struct Entry { note: Option } + fn relay(flag: bool) -> Option { + let value: Option = if flag => { Option::Some(2) } else => { Option::None }; + let item = value?; + Option::Some(item + 1) + } + let entry: Entry = { note: Option::Some(2) }; + let projected = entry?.note.unwrap_or(Option::None); + let mapped = projected.map(|value| value + 1); + let converted = mapped.ok_or("bad"); + match converted { Result::Ok(value) => value, Result::Err(error) => -1 }; + "#; + let once = vm::format_source(source).expect("Option formatting succeeds"); + let twice = vm::format_source(&once).expect("second Option formatting succeeds"); + assert_eq!(once, twice, "Option formatter must be idempotent"); + + let options = vm::CompileSourceFileOptions::default(); + let model = + vm::analyze_source_from_string_with_options("main.rss", source, options.clone()).unwrap(); + assert!(model.diagnostics().is_empty(), "{:?}", model.diagnostics()); + let hints = vm::collect_inferred_local_type_hints_with_options( + source, + vm::SourceFlavor::RustScript, + options, + ) + .unwrap(); + let projected = hints + .iter() + .find(|hint| hint.name == "projected") + .expect("projected local hint"); + let schema = projected.schema.as_ref().expect("projected schema"); + assert!( + format!("{schema:?}").contains("Option"), + "logical projection hint must publish Option, found {schema:?}" + ); + let span = projected.span.unwrap(); + assert_eq!(&source[span.lo..span.hi], "projected"); +} + +#[test] +fn explicit_null_does_not_become_logical_unit_or_option() { + let null_return = reject("fn bad() -> () { null }"); + assert!( + null_return.contains("null") || null_return.contains("Unit") || null_return.contains("()"), + "explicit null return error was {null_return}" + ); + let null_unit = reject("let value: () = null;"); + assert!( + null_unit.contains("null") || null_unit.contains("()"), + "explicit null unit error was {null_unit}" + ); + let unit_int = reject("let value: int = ();"); + assert!( + unit_int.contains("()") || unit_int.contains("int"), + "unit as int error was {unit_int}" + ); + let legacy = reject("let value: int? = null; value;"); + assert!( + legacy.contains("RSS rejects T?") && legacy.contains("Option"), + "legacy nullable annotation must give an actionable migration diagnostic, got {legacy}" + ); +} + +#[test] +fn option_types_are_reserved_and_not_struct_declarations() { + let reserved = reject("struct Option { value: int }"); + assert!( + reserved.contains("built-in") || reserved.contains("Option"), + "reserved Option struct error was {reserved}" + ); + let arity = reject("let value: Option = Option::None;"); + assert!( + arity.contains("Option") || arity.contains("type argument"), + "Option arity error was {arity}" + ); +} + +#[test] +fn option_generic_instantiation_keeps_distinct_payloads() { + let source = r#" + struct Box { value: T } + let inner: Option> = Option::Some({ value: 9 }); + let outer: Option>> = Option::Some(Option::Some({ value: 4 })); + let a = match inner { Option::Some(b) => b.value, Option::None => -1 }; + let b = match outer { + Option::Some(option) => match option { Option::Some(boxed) => boxed.value, Option::None => -2 }, + Option::None => -4, + }; + a + b; + "#; + assert_eq!(eval(source), vm::Value::Int(13)); +} + +#[test] +fn conditional_option_values_keep_both_branches_typed() { + let source = r#" + fn choose(flag: bool) -> Option { + if flag => { Option::Some(7) } else => { Option::None } + } + let present = match choose(true) { Option::Some(v) => v, Option::None => 0 }; + let absent = match choose(false) { Option::Some(v) => 10, Option::None => 1 }; + present + absent; + "#; + assert_eq!(eval(source), vm::Value::Int(8)); +} + +#[test] +fn option_payload_keeps_typed_value_in_a_match_arm() { + let source = r#" + fn identity(value: Option) -> Option { value } + let typed: Option = identity(Option::Some(11)); + match typed { Option::Some(value) => value, Option::None => 0 }; + "#; + assert_eq!(eval(source), vm::Value::Int(11)); +} diff --git a/tests/option_try_tests.rs b/tests/option_try_tests.rs new file mode 100644 index 00000000..8363bdf8 --- /dev/null +++ b/tests/option_try_tests.rs @@ -0,0 +1,729 @@ +#![cfg(feature = "runtime")] + +use vm::{ + CompileSourceFileOptions, SourceFlavor, TypeSchema, Value, Vm, VmStatus, + collect_inferred_local_type_hints_with_options, compile_source, +}; + +fn eval(source: &str) -> Value { + let compiled = compile_source(source).expect("Option try source should compile"); + let mut machine = Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + assert_eq!( + machine.run().expect("Option try program should run"), + VmStatus::Halted + ); + machine + .stack() + .last() + .cloned() + .expect("Option try program should leave a value") +} + +fn reject(source: &str) -> String { + match compile_source(source) { + Ok(_) => panic!("Option try source should be rejected: {source}"), + Err(error) => error.to_string(), + } +} + +#[test] +fn question_mark_passes_some_payload_and_stops_after_none() { + let source = r#" + fn load(flag: bool) -> Option { + if flag => { Option::Some(21) } else => { Option::None } + } + fn relay(flag: bool) -> Option { + let value = load(flag)?; + Option::Some(value + 1) + } + let present = match relay(true) { Option::Some(v) => v, Option::None => -1 }; + let absent = match relay(false) { Option::Some(v) => 100, Option::None => 2 }; + present + absent; + "#; + // Some(21) continues as 22; None exits only this Option-returning function. + assert_eq!(eval(source), Value::Int(24)); +} + +#[test] +fn none_stops_later_arguments_and_side_effects() { + let source = r#" + fn relay(flag: bool) -> Option { + let mut step_calls = [0]; + let mut continuation = [0]; + let step: fn() -> Option = || if flag => { + step_calls[0] = step_calls[0].copy() + 1; + Option::Some(1) + } else => { Option::None }; + let first = step()?; + continuation[0] = continuation[0].copy() + 10; + let second = step()?; + continuation[0] = continuation[0].copy() + 100; + Option::Some(first + second + continuation[0].copy() + step_calls[0].copy()) + } + let present = match relay(true) { Option::Some(v) => v, Option::None => -1 }; + let absent = match relay(false) { Option::Some(v) => 100, Option::None => 2 }; + present + absent; + "#; + // Present: 1 + 1 + 110 + 2 = 114; absent exits before any continuation. + assert_eq!(eval(source), Value::Int(116)); +} + +#[test] +fn none_in_a_closure_returns_only_from_that_closure() { + let source = r#" + let mut calls = [0]; + let inner: fn() -> Option = || if true => { + let absent: Option = Option::None; + let value = absent?; + calls[0] = calls[0].copy() + 100; + Option::Some(value) + } else => { Option::None }; + let outer: fn() -> Option = || if true => { + let inside = inner(); + calls[0] = calls[0].copy() + 1; + inside + } else => { Option::None }; + let observed = match outer() { Option::Some(v) => v, Option::None => -1 }; + observed + calls[0].copy(); + "#; + // The caller continues after the inner closure's None: -1 + 1 == 0. + assert_eq!(eval(source), Value::Int(0)); +} + +#[test] +fn question_mark_covers_named_functions_aliases_and_finite_recursion() { + let source = r#" + fn countdown(value: int) -> Option { + if value <= 0 => { Option::Some(0) } + else => { + let next = countdown(value - 1)?; + Option::Some(next + 1) + } + } + fn relay(value: int) -> Option { + let alias = countdown; + let forwarded = alias(value)?; + Option::Some(forwarded + 1) + } + match relay(4) { Option::Some(v) => v, Option::None => -1 }; + "#; + // Four recursive steps plus the relay increment. + assert_eq!(eval(source), Value::Int(5)); +} + +#[test] +fn option_try_accepts_a_wider_option_return_payload() { + let source = r#" + fn widen() -> Option { + let absent: Option = Option::None; + let value = absent?; + Option::Some("done") + } + match widen() { Option::Some(v) => 4, Option::None => 7 }; + "#; + // None carries no payload, so the enclosing Option receives None. + assert_eq!(eval(source), Value::Int(7)); +} + +#[test] +fn explicit_ok_or_and_ok_or_else_convert_before_result_propagation() { + let source = r#" + fn load(flag: bool) -> Option { + if flag => { Option::Some(4) } else => { Option::None } + } + fn runtime_error() -> string { "missing" } + fn relay(flag: bool) -> Result { + let value = load(flag).ok_or_else(runtime_error)?; + let fallback_checked = load(flag).ok_or("second missing")?; + Result::Ok(value + fallback_checked) + } + let present = match relay(true) { Result::Ok(v) => v, Result::Err(e) => -1 }; + let absent = match relay(false) { Result::Ok(v) => 100, Result::Err(e) => 2 }; + present + absent; + "#; + // Some(4) runs both conversions and continues as 8; None stops at Err. + assert_eq!(eval(source), Value::Int(10)); +} + +#[test] +fn explicit_result_ok_converts_before_option_propagation() { + let source = r#" + fn load(flag: bool) -> Result { + if flag => { Result::Ok(3) } else => { Result::Err("bad") } + } + fn relay(flag: bool) -> Option { + let value = load(flag).ok()?; + Option::Some(value + 1) + } + let present = match relay(true) { Option::Some(v) => v, Option::None => -1 }; + let absent = match relay(false) { Option::Some(v) => 100, Option::None => 5 }; + present + absent; + "#; + // Ok(3) -> Some(4); Err -> None. + assert_eq!(eval(source), Value::Int(9)); +} + +#[test] +fn map_err_before_result_propagation_remains_exact() { + let source = r#" + fn load() -> Result { Result::Err("source") } + fn convert(error: string) -> int { 42 } + fn relay() -> Result { + let value = load().map_err(convert)?; + Result::Ok(value) + } + match relay() { Result::Ok(v) => v, Result::Err(e) => e }; + "#; + assert_eq!(eval(source), Value::Int(42)); +} + +#[test] +fn mixed_option_and_result_propagation_without_conversion_is_rejected() { + let diagnostic = reject( + r#" + fn bad() -> Result { + let value: Option = Option::None; + let unwrapped = value?; + Result::Ok(unwrapped) + } + bad(); + "#, + ); + assert!( + diagnostic.contains("Option") && diagnostic.contains("Result"), + "mixed propagation must explain the explicit conversion: {diagnostic}" + ); + let diagnostic = reject( + r#" + fn bad() -> Option { + let value: Result = Result::Err("bad"); + let unwrapped = value?; + Option::Some(unwrapped) + } + bad(); + "#, + ); + assert!( + diagnostic.contains("Option") && diagnostic.contains("Result"), + "Result in an Option-returning callable must stay explicit: {diagnostic}" + ); +} + +#[test] +fn question_mark_outside_option_or_result_context_reports_the_owner() { + let diagnostic = reject( + r#" + fn plain() -> int { + let value: Option = Option::None; + let unwrapped = value?; + unwrapped + } + plain(); + "#, + ); + assert!( + diagnostic.contains("Option") && diagnostic.contains("enclosing"), + "a plain function must reject Option try: {diagnostic}" + ); +} + +#[test] +fn unwrap_returns_the_nested_payload_not_an_outer_option() { + let source = r#" + fn relay() -> Option { + let nested: Option> = Option::Some(Option::None); + let inner = nested.unwrap_or(Option::None); + let value = inner?; + Option::Some(value + 1) + } + match relay() { Option::Some(v) => 8, Option::None => 9 }; + "#; + assert_eq!(eval(source), Value::Int(9)); +} + +// ---- Fallible `?.` projection semantics ---- + +#[test] +fn option_methods_and_projection_execute_from_loaded_vmbc() { + let source = r#" + struct Entry { note: Option } + let entry: Entry = { note: Option::Some(4) }; + let mapped = entry?.note.unwrap_or(Option::None); + let converted = mapped.ok_or("missing").map_err(|error| error.length); + let recovered = match converted { Result::Ok(value) => value, Result::Err(length) => length }; + let absent: Option = Option::None; + let fallback = absent.or_else(|| Option::Some(3)); + let final_value = match fallback { Option::Some(value) => value, Option::None => -1 }; + recovered + final_value; + "#; + let compiled = compile_source(source).expect("loaded-VMBC Option source should compile"); + let bytes = vm::encode_program(&compiled.program.with_local_count(compiled.locals)).unwrap(); + let decoded = vm::decode_program(&bytes).unwrap(); + let mut loaded = Vm::new(decoded.with_local_count(compiled.locals)); + loaded.set_fuel(100_000); + assert_eq!(loaded.run().unwrap(), VmStatus::Halted); + assert_eq!(loaded.stack().last(), Some(&Value::Int(7))); +} + +#[test] +fn optional_projection_preserves_none_some_none_and_some_some() { + let source = r#" + struct Entry { name: string, note: Option } + let present: Entry = { name: "first", note: Option::Some(0) }; + let explicit_none: Entry = { name: "second", note: Option::None }; + let omitted: Entry = { name: "third" }; + + let projected_some = present?.note; + let a = match projected_some { + Option::Some(inner) => match inner { + Option::Some(value) => value + 10, + Option::None => 11, + }, + Option::None => 12, + }; + let projected_none = explicit_none?.note; + let b = match projected_none { + Option::Some(inner) => match inner { + Option::Some(value) => 100, + Option::None => 13, + }, + Option::None => 14, + }; + let projected_missing = omitted?.note; + let c = match projected_missing { + Option::Some(inner) => match inner { + Option::Some(value) => 200, + Option::None => 15, + }, + Option::None => 16, + }; + a + b + c; + "#; + // Some(Some(0)) keeps two layers, explicit None stays Some(None), and an + // omitted permitted Option field normalizes to an explicit None. + assert_eq!(eval(source), Value::Int(38)); +} + +#[test] +fn omitted_option_field_direct_access_observes_explicit_none() { + let source = r#" + struct Entry { name: string, note: Option } + let entry: Entry = { name: "x" }; + let note = entry.note; + match note { Option::Some(value) => value, Option::None => 7 }; + "#; + assert_eq!(eval(source), Value::Int(7)); +} + +#[test] +fn optional_projection_distinguishes_absent_dynamic_key_from_present_none() { + let source = r#" + let values: map> = { inner: Option::None, some: Option::Some(3) }; + let present_none = values?.["inner"]; + let present_some = values?.["some"]; + let absent = values?.["absent"]; + let a = match present_none { + Option::Some(inner) => match inner { Option::Some(v) => 100, Option::None => 1 }, + Option::None => 1000, + }; + let b = match present_some { + Option::Some(inner) => match inner { Option::Some(v) => v, Option::None => 200 }, + Option::None => 300, + }; + let c = match absent { Option::Some(inner) => 400, Option::None => 10 }; + a + b + c; + "#; + // present None = 1, present Some(3) = 3, absent = None -> 10. + assert_eq!(eval(source), Value::Int(14)); +} + +#[test] +fn optional_projection_of_unit_and_out_of_range_indexes_stays_typed() { + let source = r#" + struct UnitEntry { value: () } + let units: UnitEntry = { value: () }; + let projected_unit = units?.value; + let unit_state = match projected_unit { Option::Some(value) => 1, Option::None => 2 }; + let text: string = "ab"; + let outside = text?.[10]; + let outside_state = match outside { Option::Some(value) => 100, Option::None => 3 }; + let inside = text?.[1]; + let inside_state = match inside { Option::Some(value) => if value == "b" => { 4 } else => { 5 }, Option::None => 6 }; + unit_state + outside_state + inside_state; + "#; + // Unit payload is Some(Unit), an out-of-range index is None, and a valid + // index keeps the exact projected string. + assert_eq!(eval(source), Value::Int(8)); +} + +#[test] +fn optional_receiver_and_then_projects_one_layer_without_flattening_payload_options() { + let source = r#" + struct Entry { note: Option } + let some_entry: Option = Option::Some({ note: Option::Some(7) }); + let none_entry: Option = Option::None; + let from_some = some_entry?.note; + let from_none = none_entry?.note; + let a = match from_some { + Option::Some(inner) => match inner { Option::Some(v) => v, Option::None => -1 }, + Option::None => -2, + }; + let b = match from_none { Option::Some(inner) => 100, Option::None => 3 }; + a + b; + "#; + // Some(Entry) projects its child without an extra outer layer; None + // receiver projects to None. + assert_eq!(eval(source), Value::Int(10)); +} + +#[test] +fn optional_projection_keeps_struct_required_field_checks() { + let diagnostic = match compile_source( + r#"struct Entry { name: string, note: Option } +let entry: Entry = { note: Option::None }; +entry?.name;"#, + ) { + Ok(_) => panic!("missing required field must still reject"), + Err(error) => error.to_string(), + }; + assert!( + diagnostic.contains("required") && diagnostic.contains("name"), + "required-field check must remain: {diagnostic}" + ); +} + +#[test] +fn optional_projection_does_not_implicitly_convert_option_and_result() { + let diagnostic = match compile_source( + r#"fn out() -> Option { +let value: Result = Result::Ok(1); +let projected = value?; +Option::Some(projected) +} +out();"#, + ) { + Ok(_) => panic!("mixed projection propagation must reject"), + Err(error) => error.to_string(), + }; + assert!( + diagnostic.contains("Option") && diagnostic.contains("Result"), + "explicit conversion requirement must stay visible: {diagnostic}" + ); +} + +#[test] +fn typed_construction_generic_named_omitted_option_field_materializes() { + let source = r#" + struct Box { value: Option } + fn probe() -> int { + let item: Box = {}; + let observed = item.value; + match observed { + Option::Some(value) => value, + Option::None => 7, + } + } + probe(); + "#; + assert_eq!(eval(source), Value::Int(7)); + let hints = collect_inferred_local_type_hints_with_options( + source, + SourceFlavor::RustScript, + CompileSourceFileOptions::new(), + ) + .expect("typed construction hints should resolve"); + let observed = hints + .iter() + .find(|hint| hint.name == "observed") + .expect("observed local hint"); + assert_eq!( + observed.schema, + Some(TypeSchema::Option(Box::new(TypeSchema::Int))), + "generic field must stay Option, not Unknown" + ); +} + +#[test] +fn typed_construction_materialized_generic_field_projects_as_some_none() { + let source = r#" + struct Box { value: Option } + let item: Box = {}; + let projected = item?.value; + match projected { + Option::Some(inner) => match inner { + Option::Some(value) => value, + Option::None => 7, + }, + Option::None => 100, + }; + "#; + assert_eq!(eval(source), Value::Int(7)); +} + +#[test] +fn typed_construction_function_argument_omitted_option_field_materializes() { + let source = r#" + struct Entry { name: string, note: Option } + fn take(entry: Entry) -> int { + match entry.note { + Option::Some(value) => value, + Option::None => 7, + } + } + take({ name: "x" }); + "#; + assert_eq!(eval(source), Value::Int(7)); +} + +#[test] +fn typed_construction_assignment_omitted_option_field_materializes() { + let source = r#" + struct Entry { name: string, note: Option } + let mut entry: Entry = { name: "first", note: Option::Some(3) }; + entry = { name: "second" }; + match entry.note { + Option::Some(value) => value, + Option::None => 7, + }; + "#; + assert_eq!(eval(source), Value::Int(7)); +} + +#[test] +fn typed_construction_nested_record_field_materializes() { + let source = r#" + struct Inner { note: Option } + struct Outer { name: string, inner: Inner } + let outer: Outer = { name: "x", inner: {} }; + match outer.inner.note { + Option::Some(value) => value, + Option::None => 7, + }; + "#; + assert_eq!(eval(source), Value::Int(7)); +} + +#[test] +fn typed_construction_option_and_result_payloads_materialize() { + let source = r#" + struct Entry { name: string, note: Option } + let maybe: Option = Option::Some({ name: "some" }); + let ok: Result = Result::Ok({ name: "ok" }); + let err: Result = Result::Err({ name: "err" }); + let a = match maybe { + Option::Some(entry) => match entry.note { + Option::Some(value) => value, + Option::None => 7, + }, + Option::None => 100, + }; + let b = match ok { + Result::Ok(entry) => match entry.note { + Option::Some(value) => value, + Option::None => 7, + }, + Result::Err(_) => 200, + }; + let c = match err { + Result::Err(entry) => match entry.note { + Option::Some(value) => value, + Option::None => 7, + }, + Result::Ok(_) => 300, + }; + a + b + c; + "#; + assert_eq!(eval(source), Value::Int(21)); +} + +#[test] +fn typed_construction_local_callable_argument_materializes() { + let source = r#" + struct Entry { name: string, note: Option } + fn take(entry: Entry) -> int { + match entry.note { + Option::Some(value) => value, + Option::None => 7, + } + } + let callable: fn(Entry) -> int = take; + callable({ name: "x" }); + "#; + assert_eq!(eval(source), Value::Int(7)); +} + +#[test] +fn typed_construction_array_and_tuple_elements_materialize() { + let source = r#" + struct Entry { name: string, note: Option } + let entries: [Entry] = [{ name: "array" }]; + let pair: [int, Entry] = [1, { name: "tuple" }]; + let from_array = match entries[0].note { + Option::Some(value) => value, + Option::None => 7, + }; + let from_tuple = match pair[1].note { + Option::Some(value) => value, + Option::None => 7, + }; + from_array + from_tuple; + "#; + assert_eq!(eval(source), Value::Int(14)); +} + +#[test] +fn typed_construction_return_branch_and_closure_tails_materialize() { + let source = r#" + struct Entry { name: string, note: Option } + fn make(flag: bool) -> Entry { + if flag => { { name: "first" } } else => { { name: "second" } } + } + let direct = make(true); + let state_a = match direct.note { + Option::Some(value) => value, + Option::None => 7, + }; + let make_closure: fn() -> Entry = || if true => { { name: "closure" } } else => { { name: "other" } }; + let state_b = match make_closure().note { + Option::Some(value) => value, + Option::None => 7, + }; + state_a + state_b; + "#; + assert_eq!(eval(source), Value::Int(14)); +} + +#[test] +fn typed_construction_required_field_and_wrong_option_payload_still_reject() { + let required = reject( + r#"struct Box { name: string, value: Option } let item: Box = {}; item.name;"#, + ); + assert!( + required.contains("required") && required.contains("name"), + "generic required-field check must remain: {required}" + ); + let wrong = reject( + r#"struct Box { value: Option } let item: Box = { value: Option::Some("text") }; item.value;"#, + ); + assert!( + wrong.contains("Option::Some payload"), + "wrong generic Option payload must reject at that obligation: {wrong}" + ); +} + +#[test] +fn named_function_capture_counter_matches_plain_int_baseline() { + let source = r#" + let mut n = 0; + fn next() -> int { n = n + 1; n } + let first = next(); + let second = next(); + first * 10 + second; + "#; + assert_eq!(eval(source), Value::Int(12)); +} + +#[test] +fn option_some_from_captured_int_matches_plain_capture_counter() { + let source = r#" + let mut n = 0; + fn next() -> Option { n = n + 1; Option::Some(n) } + let first = match next() { Option::Some(v) => v, Option::None => -1 }; + let second = match next() { Option::Some(v) => v, Option::None => -1 }; + first * 10 + second; + "#; + assert_eq!(eval(source), Value::Int(12)); +} + +#[test] +fn option_some_from_captured_string_keeps_source_identity() { + let source = r#" + let mut label = "a"; + fn next() -> Option { + label = label + "b"; + Option::Some(label) + } + let flag = match next() { + Option::Some(v) => if v == "ab" => { 1 } else => { 0 }, + Option::None => -1, + }; + flag; + "#; + assert_eq!(eval(source), Value::Int(1)); +} + +#[test] +fn option_some_capture_keeps_param_and_root_slots_distinct() { + let source = r#" + let mut n = 1; + fn from_param(n: int) -> Option { Option::Some(n) } + fn from_capture() -> Option { Option::Some(n) } + let param_value = match from_param(9) { Option::Some(v) => v, Option::None => -1 }; + let captured = match from_capture() { Option::Some(v) => v, Option::None => -1 }; + param_value * 10 + captured; + "#; + assert_eq!(eval(source), Value::Int(91)); +} + +#[test] +fn nested_function_option_some_uses_enclosing_capture_schema() { + let source = r#" + fn outer() -> int { + let mut n = 0; + fn next() -> Option { n = n + 1; Option::Some(n) } + let first = match next() { Option::Some(v) => v, Option::None => -1 }; + let second = match next() { Option::Some(v) => v, Option::None => -1 }; + first * 10 + second + } + outer(); + "#; + assert_eq!(eval(source), Value::Int(12)); +} + +#[test] +fn captured_option_constructor_in_method_position_uses_capture_schema() { + let source = r#" + let base = 1; + fn next() -> Option { + Option::Some(base).map(|v| v + 1) + } + match next() { Option::Some(v) => v, Option::None => -1 }; + "#; + assert_eq!(eval(source), Value::Int(2)); +} + +#[test] +fn captured_record_schema_materializes_omitted_option_field_in_assignment() { + let source = r#" + struct Record { value: Option } + let seed: Record = {value: Option::None}; + fn build() -> int { + let mut target = seed; + target = {}; + match target.value { Option::Some(v) => v, Option::None => 0 } + } + build(); + "#; + assert_eq!(eval(source), Value::Int(0)); +} + +#[test] +fn nested_owner_local_captured_record_materializes_omitted_option_field() { + let source = r#" + struct Record { value: Option } + fn outer() -> int { + let mut seed: Record = { value: Option::Some(7) }; + fn clear() -> int { + seed = {}; + match seed.value { + Option::None => 0, + Option::Some(n) => n + } + } + clear() + } + outer(); + "#; + assert_eq!(eval(source), Value::Int(0)); +} diff --git a/tests/optional_projection_ownership_tests.rs b/tests/optional_projection_ownership_tests.rs index 36c23b3d..95c46254 100644 --- a/tests/optional_projection_ownership_tests.rs +++ b/tests/optional_projection_ownership_tests.rs @@ -420,7 +420,7 @@ fn resource_catalog() -> vm::HostApiCatalog { HostTypeSchema::Resource(key), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, )); builder.build().unwrap() } @@ -449,7 +449,7 @@ fn resource_record_borrow_and_mutable_borrow_cannot_escape() { #[test] fn native_value_lookup_still_permits_repeated_required_field_reads() { - let source = "struct Entry { name: string }\nlet entry: Entry = { name: \"first\" };\nentry?.name;\nentry?.name;"; + let source = "struct Entry { name: string }\nlet entry: Entry = { name: \"first\" };\nentry?.name.unwrap_or(\"\");\nentry?.name.unwrap_or(\"\");"; let compiled = compile_source_with_flavor_and_options( source, SourceFlavor::RustScript, @@ -484,14 +484,14 @@ fn native_temporary_dynamic_key_value_lookup_evaluates_each_once() { builder.function(vm::HostFunctionSchema::with_return( "neutral::tick", vec![], - vm::HostTypeSchema::Null, + vm::HostTypeSchema::Unit, )); builder.function(vm::HostFunctionSchema::with_return( "neutral::key", vec![], vm::HostTypeSchema::String, )); - let source = "use neutral;\nlet entry: map = { note: 42 };\n(if true => { let ignored = neutral::tick(); entry } else => { entry })?.[neutral::key()];"; + let source = "use neutral;\nlet entry: map = { note: 42 };\n(if true => { let ignored = neutral::tick(); entry } else => { entry })?.[neutral::key()].unwrap_or(0);"; let compiled = compile_source_with_flavor_and_options( source, SourceFlavor::RustScript, @@ -601,6 +601,33 @@ fn optional_resource_projection_transfers_one_handle_without_record_clone() { } } +#[test] +fn native_logical_projection_moves_one_resource_child_and_closes_once() { + let source = "use neutral;\nstruct Entry { note: resource }\nlet entry: Entry = { note: neutral::make() };\nlet projected = entry?.note;\nmatch projected { Option::Some(handle) => neutral::close(handle), Option::None => 0 };\n42;"; + let compiled = compile_source_with_flavor_and_options( + source, + SourceFlavor::RustScript, + CompileSourceFileOptions::new() + .with_host_api_catalog(std::sync::Arc::new(resource_catalog())), + ) + .unwrap(); + let creates = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let closes = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let mut machine = Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.bind_function( + "neutral::make", + Box::new(MakeResource { + creates: creates.clone(), + closes: closes.clone(), + }), + ); + machine.bind_function("neutral::close", Box::new(CloseResource)); + assert_eq!(machine.run().unwrap(), VmStatus::Halted); + assert_eq!(machine.stack(), &[Value::Int(42)]); + assert_eq!(creates.load(std::sync::atomic::Ordering::SeqCst), 1); + assert_eq!(closes.load(std::sync::atomic::Ordering::SeqCst), 1); +} + #[test] fn integer_literal_projection_keeps_other_indexes_available() { for plugin in [&A, &B] { @@ -676,7 +703,7 @@ fn imported_projection_preserves_intent_through_linking_and_slot_compaction() { fn native_borrowed_resource_receiver_still_rejects_escape_in_value_mode() { for receiver in ["(&entry)", "(&mut entry)"] { let source = format!( - "use neutral;\nstruct Entry {{ note: resource? }}\nlet mut entry: Entry = {{ note: neutral::make() }};\n{receiver}?.note;" + "use neutral;\nstruct Entry {{ note: Option> }}\nlet mut entry: Entry = {{ note: Option::Some(neutral::make()) }};\n{receiver}.note;" ); let error = compile_source_with_flavor_and_options( &source, diff --git a/tests/optional_record_schema_tests.rs b/tests/optional_record_schema_tests.rs index 836cbde1..ff398a5e 100644 --- a/tests/optional_record_schema_tests.rs +++ b/tests/optional_record_schema_tests.rs @@ -9,7 +9,11 @@ use vm::{ }; struct RecordDialect; -impl ParserDialect for RecordDialect {} +impl ParserDialect for RecordDialect { + fn allow_option_syntax(&self) -> bool { + true + } +} struct RecordPlugin { id: &'static str, @@ -73,8 +77,7 @@ fn source_path(plugin: Option<&'static RecordPlugin>) -> String { ) } -const OMITTED: &str = - "struct Entry { count: int, note: string? }\nlet entry: Entry = { count: 42 };\nentry.count;\n"; +const OMITTED: &str = "struct Entry { count: int, note: Option }\nlet entry: Entry = { count: 42 };\nentry.count;\n"; fn assert_checked_run(source: &str, plugin: Option<&'static RecordPlugin>) { let compiled = compile_source_with_flavor_and_options(source, flavor(plugin), options(plugin)) @@ -124,7 +127,10 @@ fn all_frontends() -> [Option<&'static RecordPlugin>; 3] { #[test] fn present_valid_optional_record_field_runs_across_frontends() { - let source = OMITTED.replace("{ count: 42 }", "{ count: 42, note: \"ready\" }"); + let source = OMITTED.replace( + "{ count: 42 }", + "{ count: 42, note: Option::Some(\"ready\") }", + ); for plugin in all_frontends() { assert_checked_run(&source, plugin); } @@ -132,7 +138,7 @@ fn present_valid_optional_record_field_runs_across_frontends() { #[test] fn present_null_optional_record_field_runs_across_frontends() { - let source = OMITTED.replace("{ count: 42 }", "{ count: 42, note: null }"); + let source = OMITTED.replace("{ count: 42 }", "{ count: 42, note: Option::None }"); for plugin in all_frontends() { assert_checked_run(&source, plugin); } @@ -140,7 +146,7 @@ fn present_null_optional_record_field_runs_across_frontends() { #[test] fn nested_named_generic_optional_fields_run_across_frontends() { - let source = "struct Parcel { value: T, extra: T? }\nstruct Entry { parcel: Parcel, note: string? }\nlet entry: Entry = { parcel: { value: 42 } };\nentry.parcel.value;\n"; + let source = "struct Parcel { value: T, extra: Option }\nstruct Entry { parcel: Parcel, note: Option }\nlet entry: Entry = { parcel: { value: 42 } };\nentry.parcel.value;\n"; for plugin in all_frontends() { assert_checked_run(source, plugin); } @@ -148,7 +154,7 @@ fn nested_named_generic_optional_fields_run_across_frontends() { #[test] fn optional_record_omission_in_function_return_and_argument_runs_across_frontends() { - let source = "struct Entry { count: int, note: string? }\nfn make() -> Entry { { count: 42 } }\nfn read(value: Entry) -> int { value.count }\nlet entry: Entry = make();\nread({ count: entry.count });\n"; + let source = "struct Entry { count: int, note: Option }\nfn make() -> Entry { { count: 42 } }\nfn read(value: Entry) -> int { value.count }\nlet entry: Entry = make();\nread({ count: entry.count });\n"; for plugin in all_frontends() { assert_checked_run(source, plugin); } @@ -172,7 +178,7 @@ fn assert_rejected(source: &str, detail: &str, plugin: Option<&'static RecordPlu #[test] fn omitted_required_record_field_remains_rejected_across_frontends() { - let source = OMITTED.replace("{ count: 42 }", "{ note: null }"); + let source = OMITTED.replace("{ count: 42 }", "{ note: Option::None }"); for plugin in all_frontends() { assert_rejected( &source, @@ -188,7 +194,7 @@ fn wrong_present_optional_field_remains_rejected_across_frontends() { for plugin in all_frontends() { assert_rejected( &source, - "field 'note' is declared as schema type 'string' but was assigned int", + "field 'note' is declared as schema type 'Option' but was assigned int", plugin, ); } @@ -196,7 +202,7 @@ fn wrong_present_optional_field_remains_rejected_across_frontends() { #[test] fn nested_required_generic_field_remains_rejected_across_frontends() { - let source = "struct Parcel { value: T, extra: T? }\nstruct Entry { parcel: Parcel, note: string? }\nlet entry: Entry = { parcel: { extra: 7 } };\nentry.parcel.value;\n"; + let source = "struct Parcel { value: T, extra: Option }\nstruct Entry { parcel: Parcel, note: Option }\nlet entry: Entry = { parcel: { extra: Option::Some(7) } };\nentry.parcel.value;\n"; for plugin in all_frontends() { assert_rejected( source, @@ -208,11 +214,11 @@ fn nested_required_generic_field_remains_rejected_across_frontends() { #[test] fn nested_wrong_present_generic_optional_field_remains_rejected_across_frontends() { - let source = "struct Parcel { value: T, extra: T? }\nstruct Entry { parcel: Parcel, note: string? }\nlet entry: Entry = { parcel: { value: 42, extra: \"wrong\" } };\nentry.parcel.value;\n"; + let source = "struct Parcel { value: T, extra: Option }\nstruct Entry { parcel: Parcel, note: Option }\nlet entry: Entry = { parcel: { value: 42, extra: Option::Some(\"wrong\") } };\nentry.parcel.value;\n"; for plugin in all_frontends() { assert_rejected( source, - "field 'parcel.extra' is declared as schema type 'int' but was assigned string", + "Option::Some payload: value is declared as schema type 'int' but was assigned string", plugin, ); } @@ -220,7 +226,7 @@ fn nested_wrong_present_generic_optional_field_remains_rejected_across_frontends #[test] fn omitted_required_resource_field_remains_rejected_across_frontends() { - let source = "struct Entry { handle: resource, note: string? }\nlet entry: Entry = { note: null };\n42;\n"; + let source = "struct Entry { handle: resource, note: Option }\nlet entry: Entry = { note: Option::None };\n42;\n"; for plugin in all_frontends() { assert_rejected( source, @@ -244,7 +250,7 @@ fn independent_strict_unknown_rejection_survives_optional_omission() { #[test] fn optional_field_does_not_make_callable_argument_optional() { - let source = "struct Entry { count: int, note: string? }\nfn read(value: Entry) -> int { value.count }\nread();\n"; + let source = "struct Entry { count: int, note: Option }\nfn read(value: Entry) -> int { value.count }\nread();\n"; for plugin in all_frontends() { assert_rejected(source, "argument", plugin); } diff --git a/tests/pr24_resource_tests.rs b/tests/pr24_resource_tests.rs index 2fedd768..f6aa74bd 100644 --- a/tests/pr24_resource_tests.rs +++ b/tests/pr24_resource_tests.rs @@ -110,7 +110,7 @@ fn standard_catalog_contains_exact_resource_close_schemas() { HostParamPassing::TakeOwned, )] ); - assert_eq!(sqlite_close.return_type, HostTypeSchema::Null); + assert_eq!(sqlite_close.return_type, HostTypeSchema::Unit); } #[cfg(not(all(feature = "sqlite", not(target_family = "wasm"))))] assert!(catalog.function("sqlite::close").is_none()); @@ -152,7 +152,7 @@ fn standard_catalog_resolves_open_then_close_with_nominal_ownership() { ) .expect("standard sqlite::close resolves"); assert_eq!(sqlite_close.passing, vec![HostParamPassing::TakeOwned]); - assert_eq!(sqlite_close.return_type, TypeSchema::Null); + assert_eq!(sqlite_close.return_type, TypeSchema::Unit); } #[cfg(not(all(feature = "sqlite", not(target_family = "wasm"))))] assert!( @@ -402,7 +402,7 @@ fn closure_catalog() -> Arc { HostTypeSchema::Resource(resource_key.clone()), HostParamPassing::Borrow, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, )); builder.function(HostFunctionSchema::with_return( "test::close_closure", @@ -411,7 +411,7 @@ fn closure_catalog() -> Arc { HostTypeSchema::Resource(resource_key), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, )); Arc::new(builder.build().expect("closure catalog should build")) } @@ -430,12 +430,12 @@ fn block_catalog() -> Arc { builder.function(HostFunctionSchema::with_return( "test::make_optional", Vec::new(), - HostTypeSchema::Optional(Box::new(HostTypeSchema::Resource(resource_key.clone()))), + HostTypeSchema::Option(Box::new(HostTypeSchema::Resource(resource_key.clone()))), )); builder.function(HostFunctionSchema::with_return( "test::make_optional_map", Vec::new(), - HostTypeSchema::Optional(Box::new(HostTypeSchema::Map(Box::new( + HostTypeSchema::Option(Box::new(HostTypeSchema::Map(Box::new( HostTypeSchema::Resource(resource_key.clone()), )))), )); @@ -446,7 +446,7 @@ fn block_catalog() -> Arc { HostTypeSchema::Resource(resource_key), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, )); Arc::new(builder.build().expect("catalog should build")) } @@ -555,7 +555,7 @@ fn unwrap_or_consumes_owned_optional_source_for_later_use_check() { let options = CompileSourceFileOptions::new().with_host_api_catalog(block_catalog()); let source = r#" let maybe = test::make_optional(); - let handle = maybe.unwrap_or(test::make_block()); + let handle = maybe.unwrap_or_else(|| test::make_block()); test::close_block(handle); maybe; "#; @@ -571,12 +571,55 @@ fn unwrap_or_consumes_owned_optional_source_for_later_use_check() { ); } +#[test] +fn result_err_discards_resource_ok_owner_with_rejection() { + let options = CompileSourceFileOptions::new().with_host_api_catalog(block_catalog()); + let source = r#" + let value: Result, string> = Result::Ok(test::make_block()); + let discarded = value.err(); + 42; + "#; + let error = + match compile_source_with_flavor_and_options(source, vm::SourceFlavor::RustScript, options) + { + Ok(_) => panic!(".err() cannot discard a resource-typed Ok payload"), + Err(error) => error, + }; + assert!( + error.to_string().contains("cannot discard") && error.to_string().contains("Ok"), + "resource conversion rejection must be precise: {error}" + ); +} + +#[test] +fn result_ok_discards_resource_err_owner_with_rejection() { + let options = CompileSourceFileOptions::new().with_host_api_catalog(block_catalog()); + let source = r#" + let value: Result> = Result::Err(test::make_block()); + let discarded = value.ok(); + 42; + "#; + let error = + match compile_source_with_flavor_and_options(source, vm::SourceFlavor::RustScript, options) + { + Ok(_) => panic!(".ok() cannot discard a resource-typed Err payload"), + Err(error) => error, + }; + assert!( + error.to_string().contains("cannot discard") && error.to_string().contains("Err"), + "resource conversion rejection must be precise: {error}" + ); +} + #[test] fn optional_get_then_unwrap_consumes_owned_container_for_later_use_check() { let options = CompileSourceFileOptions::new().with_host_api_catalog(block_catalog()); let source = r#" - let maybe: map>? = test::make_optional_map(); - let handle = maybe?.["resource"].unwrap_or(test::make_block()); + let maybe: Option>> = test::make_optional_map(); + let handle = match maybe { + Option::Some(items) => items["resource"], + Option::None => test::make_block(), + }; test::close_block(handle); maybe; "#; @@ -587,7 +630,7 @@ fn optional_get_then_unwrap_consumes_owned_container_for_later_use_check() { Err(error) => error, }; assert!( - error.to_string().contains("moved"), + error.to_string().contains("moved") || error.to_string().contains("eager fallback owner"), "unexpected compiler error: {error}" ); } @@ -598,8 +641,8 @@ fn match_consumes_owned_scrutinee_for_later_use_check() { let source = r#" let maybe = test::make_optional(); let handle = match maybe { - Some(value) => value, - _ => test::make_block(), + Option::Some(value) => value, + Option::None => test::make_block(), }; test::close_block(handle); maybe; @@ -710,7 +753,7 @@ fn result_resource_catalog() -> Arc { HostTypeSchema::Resource(resource_key.clone()), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, )); builder.function(HostFunctionSchema::with_return( "test::use_owned", @@ -775,7 +818,7 @@ fn result_resource_catalog() -> Arc { HostTypeSchema::Resource(resource_key), HostParamPassing::TakeOwned, )], - HostTypeSchema::Optional(Box::new(HostTypeSchema::Int)), + HostTypeSchema::Option(Box::new(HostTypeSchema::Int)), )); builder.function(HostFunctionSchema::with_return( "test::make_err_int", @@ -974,7 +1017,9 @@ impl HostFunction for TakeNull { fn call(&mut self, vm: &mut Vm, args: &[Value]) -> VmResult { POLEX_EVENTS.lock().unwrap().push("close"); CloseClosureResource.call(vm, args)?; - Ok(CallOutcome::Return(CallReturn::one(Value::Null))) + Ok(CallOutcome::Return(CallReturn::one( + vm::option::none_carrier(), + ))) } } @@ -1271,7 +1316,7 @@ fn assert_polex_err_backends( #[test] fn observation_statement_if_keeps_owner_for_explicit_close() { let _guard = CLOSURE_TEST_LOCK.lock().unwrap_or_else(|p| p.into_inner()); - let source = "fn relay() -> int { let owned: resource = test::make_local(); let mut observed = 0; if owned == null { observed = 99; } else { observed = 1; } test::close_closure(owned); observed } relay();"; + let source = "fn relay() -> int { let owned: resource = test::make_local(); let mut observed = 0; if test::touch_borrow(owned) == 0 { observed = 99; } else { observed = 1; } test::close_closure(owned); observed } relay();"; for backend in [0, 1] { reset_exit_observers(); let mut vm = run_result_source_backend(source, backend); @@ -1291,7 +1336,7 @@ fn observation_statement_if_keeps_owner_for_explicit_close() { #[test] fn observation_while_keeps_owner_across_multiple_condition_checks() { let _guard = CLOSURE_TEST_LOCK.lock().unwrap_or_else(|p| p.into_inner()); - let source = "fn relay() -> int { let owned: resource = test::make_local(); let mut n = 0; while owned != null && n < 2 { n = n + 1; } test::close_closure(owned); n } relay();"; + let source = "fn relay() -> int { let owned: resource = test::make_local(); let mut n = 0; while test::touch_borrow(owned) != 0 && n < 2 { n = n + 1; } test::close_closure(owned); n } relay();"; for backend in [0, 1] { reset_exit_observers(); let mut vm = run_result_source_backend(source, backend); @@ -1312,9 +1357,9 @@ fn observation_while_keeps_owner_across_multiple_condition_checks() { fn observation_expression_if_and_nested_logical_keep_err_cleanup_owner() { let _guard = CLOSURE_TEST_LOCK.lock().unwrap_or_else(|p| p.into_inner()); for body in [ - "let observed = if owned == null => { 99 } else => { 1 }; let v = test::make_err_result()?;", - "if (owned != null && true) || owned == null { test::later_arg(); } let v = test::make_err_result()?;", - "for i in 0..(if owned != null => { 2 } else => { 0 }) { test::later_arg(); } let v = test::make_err_result()?;", + "let observed = if test::touch_borrow(owned) == 0 => { 99 } else => { 1 }; let v = test::make_err_result()?;", + "if (test::touch_borrow(owned) != 0 && true) || test::touch_borrow(owned) == 0 { test::later_arg(); } let v = test::make_err_result()?;", + "for i in 0..(if test::touch_borrow(owned) != 0 => { 2 } else => { 0 }) { test::later_arg(); } let v = test::make_err_result()?;", ] { // Resource-vs-Null Ceq remains outside the admitted AOT instruction set. assert_polex_err_backends(body, 1, 1, &[0, 1]); @@ -1326,7 +1371,7 @@ fn polex_reached_and_rhs_uses_post_lhs_owner_state() { let _guard = CLOSURE_TEST_LOCK.lock().unwrap_or_else(|p| p.into_inner()); reset_exit_observers(); assert_polex_err( - "let flag = test::close_closure(owned) == null && test::make_err_result()? == 9;", + "let flag = test::close_closure(owned) == () && test::make_err_result()? == 9;", 1, 1, ); @@ -1366,7 +1411,7 @@ macro_rules! polex_position_test { polex_position_test!( polex_reached_or_rhs, - "let flag = !(test::close_closure(owned) == null) || test::make_err_result()? == 9;" + "let flag = !(test::close_closure(owned) == ()) || test::make_err_result()? == 9;" ); polex_position_test!( polex_eager_add_rhs, @@ -1374,7 +1419,7 @@ polex_position_test!( ); polex_position_test!( polex_nested_expression_if, - "let x = 1 + (if test::close_closure(owned) == null => { test::make_err_result()? } else => { 0 });" + "let x = 1 + (if test::close_closure(owned) == () => { test::make_err_result()? } else => { 0 });" ); #[test] fn polex_unwrap_fallback() { @@ -1393,7 +1438,7 @@ fn polex_aot_optional_null_baseline_probe() { let _guard = CLOSURE_TEST_LOCK.lock().unwrap_or_else(|p| p.into_inner()); reset_exit_observers(); let mut vm = run_result_source_backend( - "fn absent() -> int? { null } fn relay() -> Result { let x = absent().unwrap_or(test::make_err_int()?); Result::Ok(0) } let carried = relay(); carried;", + "fn absent() -> Option { Option::None } fn relay() -> Result { let x = absent().unwrap_or(test::make_err_int()?); Result::Ok(0) } let carried = relay(); carried;", 2, ); assert_eq!(err_payload_raw(vm.stack().last().unwrap()), 9); @@ -1414,7 +1459,7 @@ fn aotnull_exact_scalar_interpreter_and_decoded_control() { for backend in [0, 1] { reset_exit_observers(); let mut vm = run_result_source_backend( - "fn absent() -> int? { null } fn relay() -> Result { let x = absent().unwrap_or(test::make_err_int()?); Result::Ok(0) } let carried = relay(); carried;", + "fn absent() -> Option { Option::None } fn relay() -> Result { let x = absent().unwrap_or(test::make_err_int()?); Result::Ok(0) } let carried = relay(); carried;", backend, ); assert_eq!(err_payload_raw(vm.stack().last().unwrap()), 9); @@ -1456,28 +1501,35 @@ fn aotnull_nullable_payloads_and_fallback_effects() { Value::Float(2.5), ), ] { + let absent = expression == "null"; + let rss_value = if absent { + "Option::None".to_string() + } else { + format!("Option::Some({expression})") + }; + let present = if absent { + match ty { + "int" => "0", + "bool" => "false", + "float" => "0.0", + _ => unreachable!(), + } + } else { + expression + }; for backend in [0, 1, 2] { for source in [ - format!("fn pick() -> {ty}? {{ {expression} }} pick().unwrap_or({fallback});"), format!( - "fn relay(x:{ty}?) -> {ty} {{ x.unwrap_or({fallback}) }} relay({expression});" + "fn pick() -> Option<{ty}> {{ {rss_value} }} pick().unwrap_or_else(|| {fallback});" + ), + format!( + "fn relay(x: Option<{ty}>) -> {ty} {{ x.unwrap_or_else(|| {fallback}) }} relay({rss_value});" ), format!( - "fn pick() -> {ty}? {{ {expression} }} let f = pick; f().unwrap_or({fallback});" + "fn pick() -> Option<{ty}> {{ {rss_value} }} let f = pick; f().unwrap_or_else(|| {fallback});" ), format!( - "let f: fn() -> {ty}? = || if {} => {{ null }} else => {{ {} }}; let value: {ty}? = f(); value.unwrap_or({fallback});", - expression == "null", - if expression == "null" { - match ty { - "int" => "0", - "bool" => "false", - "float" => "0.0", - _ => unreachable!(), - } - } else { - expression - } + "let f: fn() -> Option<{ty}> = || if {absent} => {{ Option::None }} else => {{ Option::Some({present}) }}; let value: Option<{ty}> = f(); value.unwrap_or_else(|| {fallback});" ), ] { reset_exit_observers(); @@ -1511,10 +1563,15 @@ fn aotnull_fatal_fallback_is_not_guest_err() { } } for expression in ["null", "0", "7"] { + let rss_value = if expression == "null" { + "Option::None".to_string() + } else { + format!("Option::Some({expression})") + }; for backend in [0, 1, 2] { reset_exit_observers(); let source = format!( - "fn pick() -> int? {{ {expression} }} fn relay() -> Result {{ let x = pick().unwrap_or(test::make_err_int()?); Result::Ok(x) }} relay();" + "fn pick() -> Option {{ {rss_value} }} fn relay() -> Result {{ let x = match pick() {{ Option::Some(v) => v, Option::None => test::make_err_int()?, }}; Result::Ok(x) }} relay();" ); let options = CompileSourceFileOptions::new().with_host_api_catalog(result_resource_catalog()); @@ -1566,7 +1623,7 @@ fn aotnull_fatal_fallback_is_not_guest_err() { #[test] fn aotnull_loop_jit_and_aot_fallback_effect_parity() { let _guard = CLOSURE_TEST_LOCK.lock().unwrap_or_else(|p| p.into_inner()); - let source = "fn pick(i:int) -> int? { if i < 50 => { null } else => { 0 } } fn relay() -> int { let mut total = 0; for i in 0..100 { total = total + pick(i).unwrap_or(test::later_arg()); } total } relay();"; + let source = "fn pick(i:int) -> Option { if i < 50 => { Option::None } else => { Option::Some(0) } } fn relay() -> int { let mut total = 0; for i in 0..100 { total = total + pick(i).unwrap_or_else(|| test::later_arg()); } total } relay();"; for backend in [0, 1, 2, 4] { reset_exit_observers(); let vm = run_result_source_backend(source, backend); @@ -1585,8 +1642,8 @@ fn aotnull_loop_jit_and_aot_fallback_effect_parity() { assert!(vm.jit_snapshot().traces.iter().any(|trace| { let ssa = trace.ssa_text(); trace.executions > 0 - && ssa.contains("type_of") - && ssa.contains("String(\"null\")") + && ssa.contains("String(\"tag\")") + && ssa.contains("map_get") && ssa.contains("branch_bool") })); } @@ -1598,7 +1655,7 @@ fn polex_optional_index() { let _guard = CLOSURE_TEST_LOCK.lock().unwrap_or_else(|p| p.into_inner()); // AOT does not support this Optional index's Get(String) instruction. assert_polex_err_backends( - "let arr: array? = [1]; let x = arr?.[(if test::close_closure(owned) == null => { test::make_err_result()? } else => { 0 })];", + "let arr: Option> = Option::Some([1]); let items: array = match arr { Option::Some(value) => value, Option::None => [0] }; let x = items[(if test::close_closure(owned) == () => { test::make_err_result()? } else => { 0 })];", 1, 1, &[0, 1], @@ -1618,7 +1675,7 @@ fn polex_nested_match() { let _guard = CLOSURE_TEST_LOCK.lock().unwrap_or_else(|p| p.into_inner()); // Optional-pattern Ceq is outside AOT's admitted instruction set. assert_polex_err_backends( - "let x = 1 + (match test::take_null(owned) { Some(v) => v, _ => test::make_err_result()?, });", + "let x = 1 + (match test::take_null(owned) { Option::Some(v) => v, Option::None => test::make_err_result()?, });", 1, 1, &[0, 1], @@ -1655,7 +1712,7 @@ fn polex_reached_and_skipped_try_both_operators() { for flag in [false, true] { assert_polex_err( &format!( - "let x = ((test::close_closure(owned) == null) == {flag}) {op} (test::make_err_result()? == 9); test::later_arg(); let v = test::make_err_result()?;" + "let x = ((test::close_closure(owned) == ()) == {flag}) {op} (test::make_err_result()? == 9); test::later_arg(); let v = test::make_err_result()?;" ), 1, 1, @@ -1690,7 +1747,7 @@ fn polex_optional_fallback_skipped_owner_is_not_definitely_moved() { for flag in [false, true] { assert_polex_err( &format!( - "let maybe: int? = if {flag} => {{ 1 }} else => {{ null }}; let x = maybe.unwrap_or(if test::close_closure(owned) == null => {{ 2 }} else => {{ 3 }}); let v = test::make_err_result()?;" + "let maybe: Option = if {flag} => {{ Option::Some(1) }} else => {{ Option::None }}; let x = match maybe {{ Option::Some(v) => v, Option::None => if test::close_closure(owned) == () => {{ 2 }} else => {{ 3 }} }}; let v = test::make_err_result()?;" ), 1, 1, @@ -1712,7 +1769,7 @@ fn polex_short_circuit_skipped_rhs_retains_owner_for_later_try() { reset_exit_observers(); assert_polex_err( &format!( - "let flag = {flag} {op} (test::close_closure(owned) == null); let value = test::make_err_result()?;" + "let flag = {flag} {op} (test::close_closure(owned) == ()); let value = test::make_err_result()?;" ), 1, 1, @@ -1744,7 +1801,7 @@ fn polex_loop_backedge_fresh_owner_is_in_condition_cleanup() { fn polex_scalar_error_conditions_close_before_vm_drop() { let _guard = CLOSURE_TEST_LOCK.lock().unwrap_or_else(|p| p.into_inner()); for body in [ - "let x = test::close_closure(owned) == null && test::make_err_int()? == 9;", + "let x = test::close_closure(owned) == () && test::make_err_int()? == 9;", "if test::make_err_int()? == 9 { 1; } else { 0; }", "while test::make_err_int()? == 9 { break; }", "for i in 0..test::make_err_int()? { break; }", @@ -1853,9 +1910,8 @@ fn run_result_source_backend(source: &str, backend: u8) -> Vm { vm.set_fuel(100_000); bind_result_hosts(&mut vm); if backend < 3 { - // The legacy fixture's Null host returns zero values. These tests bind - // its explicit Null value so a let consumes exactly one value and the - // AOT branch shapes match the host signature. + // Unit close returns one internal Null carrier so a let consumes exactly + // one value and AOT branch shapes match the host signature. struct ExplicitClose; impl HostFunction for ExplicitClose { fn call(&mut self, vm: &mut Vm, args: &[Value]) -> VmResult { @@ -1987,7 +2043,7 @@ fn result_try_aot_artifact_closes_live_local_before_vm_drop() { bind_result_hosts(&mut producer); let artifact = producer .encode_aot_artifact() - .expect("AOT artifact should embed the current v13 cleanup section"); + .expect("AOT artifact should embed the current v14 cleanup section"); drop(producer); reset_exit_observers(); let mut vm = Vm::new_from_aot_artifact_with_jit_config(&artifact, jit) diff --git a/tests/regex_host_arch_tests.rs b/tests/regex_host_arch_tests.rs index fc46da9a..ac7ea749 100644 --- a/tests/regex_host_arch_tests.rs +++ b/tests/regex_host_arch_tests.rs @@ -225,3 +225,333 @@ fn generic_host_state_machinery_stays_available_and_regex_free() { "the generic host-state table must not know about regex" ); } + +/// Executable proof of the logical `Option` public contract for `re::find`, +/// `re::captures` and `re::split`. +/// +/// The descriptor tests read the installed standard `regex` module rather than +/// a parallel schema table; the consumer tests compile and run real RustScript +/// against the same builtins, including through an encoded/decoded VMBC +/// program. The regex cache stays private and untouched. +#[cfg(feature = "runtime")] +mod logical_option_contract { + use vm::{ + CompileSourceFileOptions, HostTypeSchema, SourceFlavor, TypeSchema, Value, Vm, VmError, + VmStatus, collect_inferred_local_type_hints_with_options, compile_source, decode_program, + encode_program, standard_host_modules, + }; + + fn regex_descriptor(name: &str) -> vm::HostFunctionDescriptor { + let module = standard_host_modules() + .iter() + .find(|module| module.name == "regex") + .expect("the standard host modules include regex"); + module + .owned_descriptors() + .into_iter() + .find(|descriptor| descriptor.schema.name == name) + .unwrap_or_else(|| panic!("the regex module owns {name}")) + } + + fn eval(source: &str) -> Value { + let compiled = compile_source(source).expect("regex source should compile"); + let mut machine = Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + assert_eq!( + machine.run().expect("regex program should run"), + VmStatus::Halted + ); + machine + .stack() + .last() + .cloned() + .expect("regex program should leave a value") + } + + fn logical_string() -> HostTypeSchema { + HostTypeSchema::Option(Box::new(HostTypeSchema::String)) + } + + fn logical_string_array() -> HostTypeSchema { + HostTypeSchema::Array(Box::new(logical_string())) + } + + #[test] + fn re_find_declares_logical_option_return_metadata() { + let descriptor = regex_descriptor("re::find"); + assert_eq!(descriptor.schema.params.len(), 2); + assert_eq!(descriptor.schema.return_type, logical_string()); + } + + #[test] + fn re_captures_declares_array_of_logical_option_metadata() { + let descriptor = regex_descriptor("re::captures"); + assert_eq!(descriptor.schema.params.len(), 2); + assert_eq!(descriptor.schema.return_type, logical_string_array()); + } + + #[test] + fn re_split_declares_array_string_metadata() { + let descriptor = regex_descriptor("re::split"); + assert_eq!(descriptor.schema.params.len(), 2); + assert_eq!( + descriptor.schema.return_type, + HostTypeSchema::Array(Box::new(HostTypeSchema::String)) + ); + } + + #[test] + fn re_find_distinguishes_some_empty_from_none() { + let source = r#" + use re; + let present = match re::find("b+", "abbc") { + Option::Some(value) => value, + Option::None => "missing", + }; + let absent = match re::find("z", "abc") { + Option::Some(value) => value, + Option::None => "none", + }; + let empty = match re::find("b*", "ac") { + Option::Some(value) => value, + Option::None => "fallback", + }; + present + "|" + absent + "|" + empty; + "#; + assert_eq!(eval(source), Value::string("bb|none|")); + } + + #[test] + fn re_captures_distinguishes_present_unmatched_empty_and_no_match() { + let source = r#" + use re; + let groups = re::captures("(a)?(b)", "b"); + let group_zero = match groups[0] { + Option::Some(value) => value, + Option::None => "missing", + }; + let unmatched = match groups[1] { + Option::Some(value) => value, + Option::None => "unmatched", + }; + let present = match groups[2] { + Option::Some(value) => value, + Option::None => "missing", + }; + let empty_groups = re::captures("(a)(b?)", "a"); + let empty = match empty_groups[2] { + Option::Some(value) => value, + Option::None => "missing", + }; + let no_match = re::captures("(a)", "zzz"); + if group_zero == "b" + && unmatched == "unmatched" + && present == "b" + && empty == "" + && no_match.length == 0 + { + 1; + } else { + 0; + } + "#; + assert_eq!(eval(source), Value::Int(1)); + } + + #[test] + fn re_split_keeps_array_string_elements_including_empty_parts() { + let source = r#" + use re; + let parts = re::split(",", "a,,b"); + let leading = re::split(",", ",lead"); + if parts.length == 3 + && parts[0] == "a" + && parts[1] == "" + && parts[2] == "b" + && leading.length == 2 + && leading[0] == "" + && leading[1] == "lead" + { + 1; + } else { + 0; + } + "#; + assert_eq!(eval(source), Value::Int(1)); + } + + #[test] + fn re_find_keeps_logical_option_through_direct_named_and_closure_calls() { + let source = r#" + use re; + fn find_first(pattern: string, text: string) -> Option { + re::find(pattern, text) + } + let direct = re::find("b", "abc"); + let aliased = find_first("b", "abc"); + let via_closure: fn(string, string) -> Option = find_first; + let through_closure = via_closure("b", "abc"); + let direct_value = match direct { + Option::Some(value) => value, + Option::None => "none", + }; + let aliased_value = match aliased { + Option::Some(value) => value, + Option::None => "none", + }; + let closure_value = match through_closure { + Option::Some(value) => value, + Option::None => "none", + }; + if direct_value == "b" && aliased_value == "b" && closure_value == "b" { + 1; + } else { + 0; + } + "#; + assert_eq!(eval(source), Value::Int(1)); + } + + #[test] + fn regex_producer_options_expose_matching_type_hints() { + let source = r#" + use re; + let found = re::find("b", "abc"); + let groups = re::captures("(a)?(b)", "b"); + let parts = re::split(",", "a,b"); + 0; + "#; + let hints = collect_inferred_local_type_hints_with_options( + source, + SourceFlavor::RustScript, + CompileSourceFileOptions::default(), + ) + .expect("regex type hints should resolve"); + let found = hints + .iter() + .find(|hint| hint.name == "found") + .expect("the found local is visible"); + assert_eq!( + found.schema, + Some(TypeSchema::Option(Box::new(TypeSchema::String))) + ); + let groups = hints + .iter() + .find(|hint| hint.name == "groups") + .expect("the groups local is visible"); + assert_eq!( + groups.schema, + Some(TypeSchema::Array(Box::new(TypeSchema::Option(Box::new( + TypeSchema::String + ))))) + ); + let parts = hints + .iter() + .find(|hint| hint.name == "parts") + .expect("the parts local is visible"); + assert_eq!( + parts.schema, + Some(TypeSchema::Array(Box::new(TypeSchema::String))) + ); + } + + #[test] + fn re_logical_option_must_be_unwrapped_before_binary_use() { + let source = r#" + use re; + let found = re::find("b", "abc"); + found + "tail"; + "#; + let error = match compile_source(source) { + Ok(_) => panic!("a raw logical Option cannot join a string"), + Err(error) => error, + }; + assert!( + error + .to_string() + .contains("optional value must be unwrapped"), + "unexpected compiler diagnostic: {error}" + ); + } + + #[test] + fn re_match_bool_and_replace_string_contracts_stay_unchanged() { + let source = r#" + use re; + let matched = re::match("a+", "baa"); + let replaced = re::replace("a+", "baa", "x"); + if matched && replaced == "bx" { + 1; + } else { + 0; + } + "#; + assert_eq!(eval(source), Value::Int(1)); + } + + #[test] + fn regex_invalid_patterns_remain_host_errors_for_every_producer() { + for (operation, source) in [ + ( + "re_find", + r#"use re; let value = re::find("(", "text"); value;"#, + ), + ( + "re_captures", + r#"use re; let value = re::captures("(", "text"); value;"#, + ), + ( + "re_split", + r#"use re; let value = re::split("(", "text"); value;"#, + ), + ] { + let compiled = compile_source(source).expect("invalid patterns compile"); + let mut machine = Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + let error = machine + .run() + .expect_err("invalid pattern must fail at runtime"); + assert!( + matches!( + error, + VmError::HostError(ref detail) + if detail.contains(operation) && detail.contains("invalid pattern") + ), + "unexpected {operation} error: {error:?}" + ); + } + } + + #[test] + fn regex_logical_carriers_survive_an_loaded_vmbc_program() { + let source = r#" + use re; + let found = re::find("b+", "abbc"); + let groups = re::captures("(a)(b?)", "a"); + let parts = re::split(",", "a,b"); + let found_value = match found { + Option::Some(value) => value, + Option::None => "none", + }; + let group_value = match groups[2] { + Option::Some(value) => value, + Option::None => "missing", + }; + if found_value == "bb" && group_value == "" && parts.length == 2 { + 1; + } else { + 0; + } + "#; + let compiled = compile_source(source).expect("regex source should compile"); + let bytes = encode_program(&compiled.program).expect("program should encode"); + let decoded = decode_program(&bytes).expect("program should decode"); + let mut machine = Vm::new(decoded.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + assert_eq!( + machine.run().expect("loaded program should run"), + VmStatus::Halted + ); + assert_eq!(machine.stack(), &[Value::Int(1)]); + } +} diff --git a/tests/repl_public_api.rs b/tests/repl_public_api.rs index aeaea0c4..613f34b1 100644 --- a/tests/repl_public_api.rs +++ b/tests/repl_public_api.rs @@ -4,7 +4,7 @@ use vm::{ReplLocalBinding, ReplLocalState, compile_source_for_repl_with_state}; #[test] fn root_result_join_optional_float_uses_expression_schema() { let compiled = vm::compile_source_for_repl_with_state_and_options( - "let maybe: float? = 2.5; let y = if true => { maybe } else => { null };", + "let maybe: Option = Option::Some(2.5); let y = if true => { maybe } else => { Option::None };", &[], vm::CompileSourceFileOptions::default(), ) @@ -16,14 +16,20 @@ fn root_result_join_optional_float_uses_expression_schema() { let mut machine = vm::Vm::new(compiled.compiled.program.clone()); assert_eq!(machine.run().unwrap(), vm::VmStatus::Halted); let slot = machine.debug_info().unwrap().local_index("y").unwrap(); - assert_eq!(machine.locals()[slot as usize], vm::Value::Float(2.5)); + assert_eq!( + machine.locals()[slot as usize], + vm::option::some_carrier(vm::Value::Float(2.5)) + ); println!( "root result join: runtime y={:?}", machine.locals()[slot as usize] ); } - assert_eq!(binding.schema, Some(TypeSchema::Float)); - assert!(binding.optional); + assert_eq!( + binding.schema, + Some(TypeSchema::Option(Box::new(TypeSchema::Float))) + ); + assert!(!binding.optional); let states = compiled .bindings .into_iter() @@ -43,18 +49,21 @@ fn root_result_join_optional_float_uses_expression_schema() { .iter() .find(|b| b.name == "following") .unwrap(); - assert_eq!(following.schema, Some(TypeSchema::Float)); - assert!(following.optional); + assert_eq!( + following.schema, + Some(TypeSchema::Option(Box::new(TypeSchema::Float))) + ); + assert!(!following.optional); #[cfg(feature = "runtime")] assert_eq!( execute_persistent_snippets( &[ - "let maybe: float? = 2.5; let y = if true => { maybe } else => { null };", + "let maybe: Option = Option::Some(2.5); let y = if true => { maybe } else => { Option::None };", "let following = y; following;", ], vm::CompileSourceFileOptions::default(), ), - vec![vm::Value::Float(2.5)] + vec![vm::option::some_carrier(vm::Value::Float(2.5))] ); } @@ -64,7 +73,7 @@ fn root_result_join_optional_named_retains_checked_host_identity() { "ResultJoinRoot", vec![vm::HostStructField::new("value", vm::HostTypeSchema::Int)], ); - let optional_named = vm::HostTypeSchema::Optional(Box::new(named.as_type())); + let optional_named = vm::HostTypeSchema::Option(Box::new(named.as_type())); let mut builder = vm::HostApiBuilder::new(); builder.named_struct(named.clone()); builder.function(vm::HostFunctionSchema::with_return( @@ -78,9 +87,12 @@ fn root_result_join_optional_named_retains_checked_host_identity() { #[cfg(feature = "runtime")] let registry = { fn origin(_: &mut vm::Vm, _: &[vm::Value]) -> vm::VmResult { - Ok(vm::CallOutcome::Return(vm::return_one(vm::Value::map( - vec![(vm::Value::string("value"), vm::Value::Int(42))], - )))) + Ok(vm::CallOutcome::Return(vm::return_one( + vm::option::some_carrier(vm::Value::map(vec![( + vm::Value::string("value"), + vm::Value::Int(42), + )])), + ))) } let mut registry = vm::HostFunctionRegistry::new(); registry @@ -92,17 +104,17 @@ fn root_result_join_optional_named_retains_checked_host_identity() { registry }; for (expression, flag, expected) in [ - ("if flag => { maybe } else => { null }", true, 42), - ("if flag => { null } else => { maybe }", false, 42), - ("if flag => { maybe } else => { null }", false, 0), - ("if flag => { null } else => { maybe }", true, 0), - ("match choice { 1 => maybe, _ => null }", true, 42), - ("match choice { 1 => null, _ => maybe }", false, 42), - ("match choice { 1 => maybe, _ => null }", false, 0), - ("match choice { 1 => null, _ => maybe }", true, 0), + ("if flag => { maybe } else => { Option::None }", true, 42), + ("if flag => { Option::None } else => { maybe }", false, 42), + ("if flag => { maybe } else => { Option::None }", false, 0), + ("if flag => { Option::None } else => { maybe }", true, 0), + ("match choice { 1 => maybe, _ => Option::None }", true, 42), + ("match choice { 1 => Option::None, _ => maybe }", false, 42), + ("match choice { 1 => maybe, _ => Option::None }", false, 0), + ("match choice { 1 => Option::None, _ => maybe }", true, 0), ] { let source = format!( - "let flag = {flag}; let choice = if flag => {{ 1 }} else => {{ 2 }}; let maybe: ResultJoinRoot? = resultjoin::origin(); let y = {expression};" + "let flag = {flag}; let choice = if flag => {{ 1 }} else => {{ 2 }}; let maybe: Option = resultjoin::origin(); let y = {expression};" ); let first = vm::compile_source_for_repl_with_state_and_options(&source, &[], options.clone()) @@ -111,9 +123,12 @@ fn root_result_join_optional_named_retains_checked_host_identity() { println!("root result join: {expression}, y={y:?}"); assert_eq!( y.schema, - Some(TypeSchema::Named("ResultJoinRoot".into(), vec![])) + Some(TypeSchema::Option(Box::new(TypeSchema::Named( + "ResultJoinRoot".into(), + vec![], + )))) ); - assert!(y.optional); + assert!(!y.optional); let states = first .bindings .iter() @@ -124,7 +139,7 @@ fn root_result_join_optional_named_retains_checked_host_identity() { }) .collect::>(); let second = vm::compile_source_for_repl_with_state_and_options( - "let following = y; following == null;", + "let following = y; match following { Option::None => true, Option::Some(_) => false };", &states, options.clone(), ) @@ -135,7 +150,7 @@ fn root_result_join_optional_named_retains_checked_host_identity() { .find(|b| b.name == "following") .unwrap(); assert_eq!(following.schema, y.schema); - assert!(following.optional); + assert!(!following.optional); #[cfg(feature = "runtime")] { let mut machine = vm::Vm::new(first.compiled.program); @@ -159,9 +174,15 @@ fn root_result_join_optional_named_retains_checked_host_identity() { } let y_slot = machine.debug_info().unwrap().local_index("y").unwrap(); if expected == 0 { - assert_eq!(machine.locals()[y_slot as usize], vm::Value::Null); + assert_eq!( + machine.locals()[y_slot as usize], + vm::option::none_carrier() + ); } else { - let vm::Value::Map(fields) = &machine.locals()[y_slot as usize] else { + let (some, payload) = vm::option::payload(&machine.locals()[y_slot as usize]) + .expect("named option carrier"); + assert!(some); + let vm::Value::Map(fields) = payload.expect("some payload") else { panic!("host result carrier") }; assert_eq!( @@ -180,10 +201,10 @@ fn root_result_join_optional_named_retains_checked_host_identity() { #[test] fn root_result_join_match_uses_existing_expression_alternatives() { for expression in [ - "match 1 { 1 => maybe, _ => null }", - "match 2 { 1 => null, _ => maybe }", + "match 1 { 1 => maybe, _ => Option::None }", + "match 2 { 1 => Option::None, _ => maybe }", ] { - let source = format!("let maybe: float? = 2.5; let y = {expression};"); + let source = format!("let maybe: Option = Option::Some(2.5); let y = {expression};"); let compiled = vm::compile_source_for_repl_with_state_and_options( &source, &[], @@ -192,15 +213,18 @@ fn root_result_join_match_uses_existing_expression_alternatives() { .unwrap(); let y = compiled.bindings.iter().find(|b| b.name == "y").unwrap(); println!("root result join: {expression}, y={y:?}"); - assert_eq!(y.schema, Some(TypeSchema::Float)); - assert!(y.optional); + assert_eq!( + y.schema, + Some(TypeSchema::Option(Box::new(TypeSchema::Float))) + ); + assert!(!y.optional); #[cfg(feature = "runtime")] assert_eq!( execute_persistent_snippets( &[&source, "let following = y; following;"], vm::CompileSourceFileOptions::default() ), - vec![vm::Value::Float(2.5)], + vec![vm::option::some_carrier(vm::Value::Float(2.5))], ); } } @@ -209,13 +233,13 @@ fn root_result_join_match_uses_existing_expression_alternatives() { fn root_result_join_conservative_guards_preserve_optional_flags() { for (source, schema, optional) in [ ( - "let y = if true => { null } else => { null };", - Some(TypeSchema::Null), + "let y = if true => { () } else => { () };", + Some(TypeSchema::Unit), false, ), ( - "let y = match 1 { 1 => null, _ => null };", - Some(TypeSchema::Null), + "let y = match 1 { 1 => (), _ => () };", + Some(TypeSchema::Unit), false, ), ] { @@ -235,7 +259,7 @@ fn root_result_join_conservative_guards_preserve_optional_flags() { "match choice { 1 => left, _ => right }", ] { let source = format!( - "struct Left {{ value: int }} struct Right {{ value: int }} let flag = true; let choice = 1; let left: Left? = {{ value: 1 }}; let right: Right? = {{ value: 2 }}; let y = {expression};" + "struct Left {{ value: int }} struct Right {{ value: int }} let flag = true; let choice = 1; let left: Option = Option::Some({{ value: 1 }}); let right: Option = Option::Some({{ value: 2 }}); let y = {expression};" ); let error = vm::compile_source_for_repl_with_state_and_options( &source, @@ -257,11 +281,11 @@ fn root_result_join_conservative_guards_preserve_optional_flags() { #[test] fn root_result_join_nullable_result_keeps_binding_effects_separate() { for expression in [ - "if flag => { x = 2.5; maybe } else => { x = 2.5; null }", - "match choice { 1 => if flag => { x = 2.5; maybe } else => { x = 2.5; maybe }, _ => if flag => { x = 2.5; null } else => { x = 2.5; null } }", + "if flag => { x = 2.5; maybe } else => { x = 2.5; Option::None }", + "match choice { 1 => if flag => { x = 2.5; maybe } else => { x = 2.5; maybe }, _ => if flag => { x = 2.5; Option::None } else => { x = 2.5; Option::None } }", ] { let source = format!( - "let flag = true; let choice = 1; let mut x = 1; let maybe: float? = 3.5; let y = {expression};" + "let flag = true; let choice = 1; let mut x = 1; let maybe: Option = Option::Some(3.5); let y = {expression};" ); let compiled = vm::compile_source_for_repl_with_state_and_options( &source, @@ -273,15 +297,21 @@ fn root_result_join_nullable_result_keeps_binding_effects_separate() { let y = compiled.bindings.iter().find(|b| b.name == "y").unwrap(); assert_eq!(x.schema, Some(TypeSchema::Float)); assert!(!x.optional); - assert_eq!(y.schema, Some(TypeSchema::Float)); - assert!(y.optional); + assert_eq!( + y.schema, + Some(TypeSchema::Option(Box::new(TypeSchema::Float))) + ); + assert!(!y.optional); #[cfg(feature = "runtime")] assert_eq!( execute_persistent_snippets( &[&source, "let following: float = x; following; y;"], vm::CompileSourceFileOptions::default() ), - vec![vm::Value::Float(2.5), vm::Value::Float(3.5)], + vec![ + vm::Value::Float(2.5), + vm::option::some_carrier(vm::Value::Float(3.5)) + ], ); } } @@ -545,18 +575,29 @@ fn final_layout_named_optional_entry_seeds_follow_final_debug_slots() { schema, TypeSchema::Named("RootNamedOptional".into(), vec![]) ); - let states = [false, true].map(|optional| ReplLocalState { - binding: ReplLocalBinding { - name: if optional { "maybe" } else { "root" }.into(), - mutable: false, - schema: Some(schema.clone()), - optional, + let states = [ + ReplLocalState { + binding: ReplLocalBinding { + name: "root".into(), + mutable: false, + schema: Some(schema.clone()), + optional: false, + }, + moved: false, }, - moved: false, - }); + ReplLocalState { + binding: ReplLocalBinding { + name: "maybe".into(), + mutable: false, + schema: Some(TypeSchema::Option(Box::new(schema.clone()))), + optional: false, + }, + moved: false, + }, + ]; let large = format!("use calibration; {}", sequential_callee_source()); for source in [ - "use calibration; root.value; maybe == null; calibration::value();", + "use calibration; root.value; match maybe { Option::None => true, Option::Some(_) => false }; calibration::value();", "use calibration; calibration::value();", large.as_str(), "use calibration; fn recurse(n: int) -> int { if n == 0 => { 0 } else => { 1 + recurse(n - 1) } } fn capture() -> int { let value = root.value; let add = |n| n + value; add(recurse(3)) } capture();", @@ -578,8 +619,7 @@ fn final_layout_named_optional_entry_seeds_follow_final_debug_slots() { .find(|binding| binding.name == state.binding.name) .unwrap(); assert_eq!( - binding.schema, - Some(schema.clone()), + binding.schema, state.binding.schema, "{} in {source}, final debug slot {slot}", state.binding.name ); @@ -588,10 +628,11 @@ fn final_layout_named_optional_entry_seeds_follow_final_debug_slots() { "{} in {source}, final debug slot {slot}", state.binding.name ); - // No-callee-conflict controls retain the previous backend map. + // No-callee-conflict controls retain the checked root schema, + // including Option, rather than a shared Named plus optional bit. if !source.contains("fn ") { let map = program.type_map.as_ref().unwrap(); - assert_eq!(map.local_schemas[slot], Some(schema.clone())); + assert_eq!(map.local_schemas[slot], state.binding.schema); assert_eq!(map.optional_slots[slot], state.binding.optional); } } diff --git a/tests/result_combinator_tests.rs b/tests/result_combinator_tests.rs index 972032f5..1697926e 100644 --- a/tests/result_combinator_tests.rs +++ b/tests/result_combinator_tests.rs @@ -378,8 +378,8 @@ fn named_payload_and_named_error_keep_their_declarations() { #[test] fn nullable_unwrap_or_keeps_the_existing_nullable_semantics() { let source = r#" - let present: int? = 3; - let missing: int? = null; + let present: Option = Option::Some(3); + let missing: Option = Option::None; let present_out = present.unwrap_or(9); let missing_out = missing.unwrap_or(8); present_out + missing_out; @@ -1039,6 +1039,113 @@ fn result_returning_combinators_still_reject_non_result_annotations() { } } +#[test] +fn result_map_known_named_ok_schema_materializes_omitted_option_field() { + let source = r#" + struct Entry { name: string, note: Option } + fn observe_note(note: Option>) -> int { + match note { + Option::Some(inner_note) => match inner_note { + Option::Some(value) => value, + Option::None => 1, + }, + Option::None => -2, + } + } + fn observe_entry(value: Entry) -> int { + observe_note(value?.note) + } + let flag: Result = Result::Ok(1); + let item: Result = flag.map(|n| { name: "x" }); + match item { + Result::Ok(entry) => observe_entry(entry), + Result::Err(error) => -1, + }; + "#; + assert_eq!(eval(source), Value::Int(1)); +} + +#[test] +fn result_map_err_known_named_error_schema_materializes_omitted_option_field() { + let source = r#" + struct Failure { code: int, detail: Option } + fn observe_note(note: Option>) -> int { + match note { + Option::Some(inner_note) => match inner_note { + Option::Some(value) => value, + Option::None => 1, + }, + Option::None => -2, + } + } + fn observe_failure(error: Failure) -> int { + observe_note(error?.detail) + } + let flag: Result = Result::Err(7); + let item: Result = flag.map_err(|error| { code: error }); + match item { + Result::Ok(value) => -1, + Result::Err(error) => observe_failure(error), + }; + "#; + assert_eq!(eval(source), Value::Int(1)); +} + +#[test] +fn result_unwrap_or_and_unwrap_or_else_known_named_payload_materialize_none() { + let source = r#" + struct Entry { name: string, note: Option } + fn observe_note(note: Option>) -> int { + match note { + Option::Some(inner_note) => match inner_note { + Option::Some(value) => value, + Option::None => 1, + }, + Option::None => -2, + } + } + fn observe_entry(value: Entry) -> int { + observe_note(value?.note) + } + let absent: Result = Result::Err(3); + let eager: Entry = absent.unwrap_or({ name: "eager" }); + let lazy: Entry = absent.unwrap_or_else(|error| { name: "lazy" }); + observe_entry(eager) + observe_entry(lazy); + "#; + assert_eq!(eval(source), Value::Int(2)); +} + +#[test] +fn result_and_then_and_or_else_known_named_payloads_materialize_none() { + let source = r#" + struct Entry { name: string, note: Option } + fn observe_note(note: Option>) -> int { + match note { + Option::Some(inner_note) => match inner_note { + Option::Some(value) => value, + Option::None => 1, + }, + Option::None => -2, + } + } + fn observe_entry(value: Entry) -> int { + observe_note(value?.note) + } + let ok_value: Result = Result::Ok(2); + let chained: Result = ok_value.and_then(|n| Result::Ok({ name: "x" })); + let err_value: Result = Result::Err(5); + let recovered: Result = err_value.or_else(|error| Result::Ok({ name: "y" })); + match chained { + Result::Ok(entry) => observe_entry(entry), + Result::Err(error) => -1, + } + match recovered { + Result::Ok(entry) => observe_entry(entry), + Result::Err(error) => -1, + }; + "#; + assert_eq!(eval(source), Value::Int(2)); +} + #[test] fn typed_unwrap_payload_still_requires_the_payload_type() { for (label, source) in [ @@ -1066,3 +1173,35 @@ fn typed_unwrap_payload_still_requires_the_payload_type() { ); } } + +#[test] +fn result_unwrap_or_rejects_nonconstruction_sparse_fallback() { + let diagnostic = reject( + r#" + struct Entry { name: string, note: Option } + fn make(flag: Result) -> Entry { + let raw = { name: "x" }; + flag.unwrap_or(raw) + } + make(Result::Err(3)).name; + "#, + ); + assert!( + diagnostic.contains("unwrap_or") + && diagnostic.contains("field 'note'") + && diagnostic.contains("must be materialized"), + "the Result eager fallback must be rejected before the named label is installed: {diagnostic}" + ); +} + +#[test] +fn result_unwrap_or_keeps_a_previously_materialized_named_fallback() { + let source = r#" + struct Entry { name: string, note: Option } + let fallback: Entry = { name: "x" }; + let absent: Result = Result::Err(3); + let item: Entry = absent.unwrap_or(fallback); + match item.note { Option::Some(value) => value, Option::None => 0 }; + "#; + assert_eq!(eval(source), Value::Int(0)); +} diff --git a/tests/result_host_tests.rs b/tests/result_host_tests.rs index 0942442e..41f60e46 100644 --- a/tests/result_host_tests.rs +++ b/tests/result_host_tests.rs @@ -2,7 +2,7 @@ use pd_host_function::pd_host_function; use std::sync::Arc; -use vm::{GuestResult, Value, Vm, VmError, VmResult, VmStatus}; +use vm::{GuestOption, GuestResult, Value, Vm, VmError, VmResult, VmStatus}; #[derive(Clone, Debug, PartialEq)] struct SdkFailure { @@ -68,7 +68,12 @@ fn actual_build_fixture_and_proc_macro_have_identical_typed_descriptors() { } assert!(matches!( generated::functions::fixture_descriptor().schema.params[0].ty, - vm::HostTypeSchema::Result { .. } + vm::HostTypeSchema::Result { ref ok, .. } + if matches!( + ok.as_ref(), + vm::HostTypeSchema::Option(inner) + if matches!(inner.as_ref(), vm::HostTypeSchema::Array(_)) + ) )); } @@ -103,8 +108,8 @@ fn generated_named_generic_result_catalog_declares_payload() { #[test] fn typed_array_optional_generated_roundtrip() { for (value, expected) in [ - ("Result::Ok(null)", -1), - ("Result::Ok([3, 4])", 7), + ("Result::Ok(Option::None)", -1), + ("Result::Ok(Option::Some([3, 4]))", 7), ("Result::Err(\"typed error\")", -2), ] { let descriptors = vec![ @@ -113,7 +118,7 @@ fn typed_array_optional_generated_roundtrip() { ]; let catalog = Arc::new(vm::HostFunctionDescriptor::collect_catalog(&descriptors).unwrap()); let source = format!( - "use result_sdk; let input: Result = {value}; let value: Result = result_sdk::fixture(input); result_sdk::inspect(value);" + "use result_sdk; let input: Result, string> = {value}; let value: Result, string> = result_sdk::fixture(input); result_sdk::inspect(value);" ); let compiled = vm::compile_source_with_flavor_and_options( &source, @@ -132,6 +137,7 @@ fn typed_array_optional_generated_roundtrip() { machine.set_fuel(100_000); assert_eq!(machine.run().unwrap(), VmStatus::Halted); assert_eq!(machine.stack().last(), Some(&Value::Int(expected))); + assert_eq!(machine.host_context().module_state::(), Some(&1)); } } diff --git a/tests/result_language_tests.rs b/tests/result_language_tests.rs index 9a53138c..26dcb3a8 100644 --- a/tests/result_language_tests.rs +++ b/tests/result_language_tests.rs @@ -67,7 +67,7 @@ fn false_and_null_payloads_follow_the_active_branch() { let source = r#" let ok_false: Result = Result::Ok(false); let err_false: Result = Result::Err(false); - let ok_null: Result = Result::Ok(null); + let ok_null: Result, string> = Result::Ok(Option::None); let flag = match ok_false { Result::Ok(v) => v, Result::Err(e) => true, @@ -78,18 +78,21 @@ fn false_and_null_payloads_follow_the_active_branch() { }; let missing = match ok_null { Result::Ok(v) => v, - Result::Err(e) => 9, + Result::Err(e) => Option::Some(-1), }; - let err_null: Result = Result::Err(null); - let result = if flag || err_flag => { -1 } else => { missing }; + let err_null: Result> = Result::Err(Option::None); + let result = if flag || err_flag => { Option::Some(-1) } else => { missing }; let err_observed = match err_null { - Result::Ok(v) => 2, + Result::Ok(v) => Option::Some(2), Result::Err(e) => e, }; - let observed = if result == null => { err_observed } else => { result }; + let observed = match result { + Option::None => err_observed, + Option::Some(v) => result, + }; observed; "#; - assert_eq!(eval(source), vm::Value::Null); + assert_eq!(eval(source), vm::option::none_carrier()); } #[test] @@ -372,7 +375,7 @@ fn nested_result_array_constructors_keep_inactive_branches() { let rows: Result>, string> = Result::Ok([Result::Ok(1), Result::Err("e")]); let outer: Result>, string> = Result::Err("outer"); let deep: Result, string>>, string> = Result::Ok([Result::Ok(Result::Err(false))]); - let nullable: Result>, string> = Result::Ok([Result::Ok(null)]); + let nullable: Result, string>>, string> = Result::Ok([Result::Ok(Option::None)]); let first = match rows { Result::Ok(items) => match items[0] { Result::Ok(n) => n, @@ -403,7 +406,10 @@ fn nested_result_array_constructors_keep_inactive_branches() { }; let null_flag = match nullable { Result::Ok(items) => match items[0] { - Result::Ok(n) => if n == null => { 16 } else => { -1 }, + Result::Ok(n) => match n { + Option::None => 16, + Option::Some(v) => -1, + }, Result::Err(e) => -1, }, Result::Err(e) => -1, diff --git a/tests/result_native_tests.rs b/tests/result_native_tests.rs index 3d082b94..cbf3b534 100644 --- a/tests/result_native_tests.rs +++ b/tests/result_native_tests.rs @@ -6,6 +6,9 @@ use std::sync::{ }; use vm::{CallOutcome, CallReturn, HostFunction, HostTypeSchema, Value, Vm, VmResult, VmStatus}; +#[path = "support/legacy_nullable_plugin.rs"] +mod legacy_nullable_plugin; + struct Input(Value, Arc); impl HostFunction for Input { fn call(&mut self, _: &mut Vm, _: &[Value]) -> VmResult { @@ -44,9 +47,11 @@ fn dynamic_payload(ty: &str, schema: HostTypeSchema, payload: Value, ok: bool, m )); let compiled = vm::compile_source_with_flavor_and_options( &source, - vm::SourceFlavor::RustScript, - vm::CompileSourceFileOptions::new() - .with_host_api_catalog(Arc::new(builder.build().unwrap())), + legacy_nullable_plugin::legacy_nullable_flavor(), + legacy_nullable_plugin::legacy_nullable_options( + vm::CompileSourceFileOptions::new() + .with_host_api_catalog(Arc::new(builder.build().unwrap())), + ), ) .unwrap(); let mut machine = Vm::new_with_jit_config( diff --git a/tests/result_sdk_callback_tests.rs b/tests/result_sdk_callback_tests.rs index db6ac988..33f70c0c 100644 --- a/tests/result_sdk_callback_tests.rs +++ b/tests/result_sdk_callback_tests.rs @@ -1,6 +1,7 @@ #![cfg(feature = "runtime")] use vm::{ - GuestResult, IntoScriptValue, ScriptResult, TypeSchema, Value, Vm, VmError, VmResult, VmStatus, + GuestOption, GuestResult, IntoScriptValue, ScriptResult, TypeSchema, Value, Vm, VmError, + VmResult, VmStatus, }; #[derive(Clone, Debug, PartialEq)] @@ -33,19 +34,27 @@ impl ScriptResult for Packet { #[test] fn generic_named_optional_arrays_callback_both_variants() { - let compiled = vm::compile_source("struct Packet { value: T } pub fn echo(value: Result, string>) -> Result, string> { value }").unwrap(); + let compiled = vm::compile_source("struct Packet { value: T } pub fn echo(value: Result>, string>) -> Result>, string> { value }").unwrap(); let mut store = vm::Store::from_vm(Vm::new(compiled.program.with_local_count(compiled.locals))); store.vm_mut().set_fuel(100_000); assert_eq!(store.run().unwrap(), VmStatus::Halted); - type R = GuestResult>>, String>; + type R = GuestResult>>, String>; let callback = store.script_callback_by_name::<(R,), R>("echo").unwrap(); + let mut calls = 0; for value in [ - GuestResult::Ok(Packet(Some(vec![0, 7]))), - GuestResult::Ok(Packet(None)), + GuestResult::Ok(Packet(GuestOption::Some(vec![0, 7]))), + GuestResult::Ok(Packet(GuestOption::None)), GuestResult::Err("error".into()), ] { assert_eq!(callback.call(&mut store, (value.clone(),)).unwrap(), value); + calls += 1; } + assert_eq!(calls, 3); + assert!( + store + .script_callback_by_name::<(R, i64), R>("echo") + .is_err() + ); assert!( store .script_callback_by_name::<(GuestResult,), R>("echo") @@ -64,6 +73,26 @@ fn generic_named_optional_arrays_callback_both_variants() { ); } +#[test] +fn standard_rust_option_codec_stays_independent_of_guest_option() { + let array = TypeSchema::Array(Box::new(TypeSchema::Int)); + assert_eq!( + > as IntoScriptValue>::schema(), + Some(TypeSchema::Optional(Box::new(array.clone()))) + ); + assert_eq!( + > as IntoScriptValue>::schema(), + Some(TypeSchema::Option(Box::new(array))) + ); + assert_eq!(Option::>::None.into_script_value(), Value::Null); + assert_eq!( + GuestOption::>::None.into_script_value(), + vm::option::none_carrier() + ); + assert!(> as ScriptResult>::from_value(vm::option::none_carrier()).is_err()); + assert!(> as ScriptResult>::from_value(Value::Null).is_err()); +} + #[test] fn ordinary_result_callback_prepare_queue_start_and_vm_aware_scalar_controls() { type R = GuestResult; diff --git a/tests/result_sdk_resource_tests.rs b/tests/result_sdk_resource_tests.rs index bca49930..76d44b17 100644 --- a/tests/result_sdk_resource_tests.rs +++ b/tests/result_sdk_resource_tests.rs @@ -204,7 +204,7 @@ fn callback_store() -> vm::Store { pub fn fresh_return(value: Result, resource>, flag: bool) -> Result, resource> { result_resource::observe(); result_resource::make(flag) } pub fn return_first_three(first: Result, resource>, second: Result, resource>, third: Result, resource>) -> Result, resource> { result_resource::observe(); first } pub fn retain_success(value: Result, resource>) -> int { result_resource::retain(value) } - pub fn nullable(value: Result?, resource>) -> Result?, resource> { result_resource::observe(); value } + pub fn nullable(value: Result>, resource>) -> Result>, resource> { result_resource::observe(); value } pub fn map_echo(value: Result, resource>) -> Result, resource> { result_resource::observe(); let mapped = value.map(|item| item); result_resource::observe(); mapped } pub fn map_err_echo(value: Result, resource>) -> Result, resource> { result_resource::observe(); let mapped = value.map_err(|error| error); result_resource::observe(); mapped } pub fn and_then_echo(value: Result, resource>) -> Result, resource> { result_resource::observe(); let mapped = value.and_then(|item| Result::Ok(item)); result_resource::observe(); mapped } @@ -530,23 +530,23 @@ fn vm_aware_owned_callback_fatal_transport_after_take_is_not_guest_err() { #[test] fn vm_aware_nullable_owned_callback_allocates_only_present_selected_payload() { let _lock = LOCK.lock().unwrap_or_else(|p| p.into_inner()); - type Nullable = GuestResult>, ResourceOwned>; + type Nullable = GuestResult>, ResourceOwned>; let mut store = callback_store(); let callback = store .vm_script_callback_by_name::<(Nullable,), Nullable>("nullable") .unwrap(); let returned = callback - .call_with_vm(&mut store, (GuestResult::Ok(None),)) + .call_with_vm(&mut store, (GuestResult::Ok(vm::GuestOption::None),)) .unwrap(); - assert!(matches!(returned, GuestResult::Ok(None))); + assert!(matches!(returned, GuestResult::Ok(vm::GuestOption::None))); assert_eq!(OBSERVED_RESOURCE_COUNT.load(Ordering::SeqCst), 0); for value in [ - GuestResult::Ok(Some(ResourceOwned::new(Success(81)))), + GuestResult::Ok(vm::GuestOption::Some(ResourceOwned::new(Success(81)))), GuestResult::Err(ResourceOwned::new(Failure(93))), ] { let returned = callback.call_with_vm(&mut store, (value,)).unwrap(); assert!( - matches!(returned, GuestResult::Ok(Some(ref resource)) if resource.as_ref().0 == 81) + matches!(returned, GuestResult::Ok(vm::GuestOption::Some(ref resource)) if resource.as_ref().0 == 81) || matches!(returned, GuestResult::Err(ref resource) if resource.as_ref().0 == 93) ); assert_eq!(OBSERVED_RESOURCE_COUNT.load(Ordering::SeqCst), 1); diff --git a/tests/result_sdk_try_integration_tests.rs b/tests/result_sdk_try_integration_tests.rs index e1c6db9f..351c1862 100644 --- a/tests/result_sdk_try_integration_tests.rs +++ b/tests/result_sdk_try_integration_tests.rs @@ -129,7 +129,7 @@ fn machine(source: &str, decoded: bool) -> Vm { let program = compiled.program.with_local_count(compiled.locals); let program = if decoded { let bytes = vm::encode_program(&program).unwrap(); - assert_eq!(u16::from_le_bytes([bytes[4], bytes[5]]), 13); + assert_eq!(u16::from_le_bytes([bytes[4], bytes[5]]), 14); let decoded = vm::decode_program(&bytes).unwrap(); assert_eq!(decoded.host_import_schemas(), program.host_import_schemas()); assert_eq!(vm::encode_program(&decoded).unwrap(), bytes); @@ -269,7 +269,7 @@ fn dynamic_resource_try_hot_loop_before_teardown_all_backends() { } #[test] -fn generated_host_try_is_once_only_function_local_and_fatal_across_v13() { +fn generated_host_try_is_once_only_function_local_and_fatal_across_current_wire() { let _lock = LOCK.lock().unwrap_or_else(|p| p.into_inner()); for decoded in [false, true] { for mode in [0, 1, 2] { @@ -359,7 +359,7 @@ pub fn host_relay(ok: bool) -> Result, resource = Option::Some(1); maybe.unwrap_or(0); "#; assert_eq!(eval(optional_type), vm::Value::Int(1)); @@ -578,7 +578,7 @@ fn try_inside_executed_if_arm_and_loop_exits_the_function() { #[test] fn optional_success_payload_is_not_a_definite_scalar() { let arithmetic = r#" - fn maybe() -> Result { Result::Ok(null) } + fn maybe() -> Result, string> { Result::Ok(Option::None) } fn relay() -> Result { Result::Ok(maybe()? + 1) } @@ -587,7 +587,11 @@ fn optional_success_payload_is_not_a_definite_scalar() { let error = reject(arithmetic); let text = detail(&error); assert!( - text.contains("optional") || text.contains("null"), + text.contains("optional") + || text.contains("null") + || text.contains("Option") + || text.contains("schema") + || text.contains("cannot check"), "selected optional payload must not be used as a definite int, got {error:?}" ); } @@ -595,9 +599,9 @@ fn optional_success_payload_is_not_a_definite_scalar() { #[test] fn optional_success_payload_unwrap_keeps_null_and_nonnull() { let nonnull = r#" - fn maybe() -> Result { Result::Ok(4) } + fn maybe() -> Result, string> { Result::Ok(Option::Some(4)) } fn relay() -> Result { - let inner: int? = maybe()?; + let inner: Option = maybe()?; Result::Ok(inner.unwrap_or(0) + 1) } let value = match relay() { @@ -608,9 +612,9 @@ fn optional_success_payload_unwrap_keeps_null_and_nonnull() { "#; assert_eq!(eval(nonnull), vm::Value::Int(5)); let null_ok = r#" - fn maybe() -> Result { Result::Ok(null) } + fn maybe() -> Result, string> { Result::Ok(Option::None) } fn relay() -> Result { - let inner: int? = maybe()?; + let inner: Option = maybe()?; Result::Ok(inner.unwrap_or(9)) } let value = match relay() { @@ -625,7 +629,7 @@ fn optional_success_payload_unwrap_keeps_null_and_nonnull() { #[test] fn optional_result_operand_is_rejected_at_try_boundary() { let source = r#" - fn relay(value: Result?) -> Result { + fn relay(value: Option>) -> Result { Result::Ok(value?) } relay; @@ -640,8 +644,7 @@ fn optional_result_operand_is_rejected_at_try_boundary() { #[test] fn optional_try_payload_cannot_flow_to_required_let_or_argument() { - let head = - "fn maybe() -> Result { Result::Ok(null) }\nfn take(n: int) -> int { n }\n"; + let head = "fn maybe() -> Result, string> { Result::Ok(Option::None) }\nfn take(n: int) -> int { n }\n"; for body in [ "let required: int = maybe()?; Result::Ok(required)", "Result::Ok(take(maybe()?))", @@ -656,6 +659,7 @@ fn optional_try_payload_cannot_flow_to_required_let_or_argument() { assert!( text.contains("optional") || text.contains("null") + || text.contains("Option") || text.contains("annotation") || text.contains("schema"), "{body}: {error:?}" @@ -665,13 +669,13 @@ fn optional_try_payload_cannot_flow_to_required_let_or_argument() { #[test] fn optional_try_refinement_keeps_both_payload_states() { - for (payload, expected) in [("null", 9), ("4", 5)] { + for (payload, expected) in [("Option::None", 9), ("Option::Some(4)", 5)] { let source = format!( r#" - fn maybe() -> Result {{ Result::Ok({payload}) }} + fn maybe() -> Result, string> {{ Result::Ok({payload}) }} fn relay() -> Result {{ - let selected = maybe()?; - let n = if selected != null => {{ selected + 1 }} else => {{ 9 }}; + let selected: Option = maybe()?; + let n: int = match selected {{ Option::Some(v) => v + 1, Option::None => 9 }}; Result::Ok(n) }} let n = match relay() {{ Result::Ok(v) => v, Result::Err(_) => 0, }}; @@ -761,25 +765,28 @@ fn trace_jit_try_in_hot_region_preserves_payload_and_order() { #[test] fn optional_entry_parameter_stays_optional_across_try_selection() { let negative = r#" - fn relay(value: int?) -> Result { - let carrier: Result = Result::Ok(value); + fn relay(value: Option) -> Result { + let carrier: Result, string> = Result::Ok(value); let required: int = carrier?; Result::Ok(required) } - relay(null); + relay(Option::None); "#; let error = reject(negative); assert!( - detail(&error).contains("optional") || detail(&error).contains("annotation"), + detail(&error).contains("optional") + || detail(&error).contains("annotation") + || detail(&error).contains("Option") + || detail(&error).contains("schema"), "{error:?}" ); - for (payload, expected) in [("null", 9), ("4", 5)] { + for (payload, expected) in [("Option::None", 9), ("Option::Some(4)", 5)] { let source = format!( r#" - fn relay(value: int?) -> Result {{ - let carrier: Result = Result::Ok(value); - let selected = carrier?; - let n = if selected != null => {{ selected + 1 }} else => {{ 9 }}; + fn relay(value: Option) -> Result {{ + let carrier: Result, string> = Result::Ok(value); + let selected: Option = carrier?; + let n: int = match selected {{ Option::Some(v) => v + 1, Option::None => 9 }}; Result::Ok(n) }} let n = match relay({payload}) {{ Result::Ok(v) => v, Result::Err(_) => 0, }}; @@ -1275,3 +1282,25 @@ fn fuel_yields_inside_combinator_callback_then_completes() { ); assert_eq!(machine.stack().last(), Some(&vm::Value::Int(101))); } + +#[test] +fn result_ok_and_err_from_captured_values_match_plain_capture() { + let ok = r#" + let mut n = 0; + fn next() -> Result { n = n + 1; Result::Ok(n) } + let first = match next() { Result::Ok(v) => v, Result::Err(_) => -1 }; + let second = match next() { Result::Ok(v) => v, Result::Err(_) => -1 }; + first * 10 + second; + "#; + assert_eq!(eval(ok), vm::Value::Int(12)); + let err = r#" + let label = "e"; + fn next() -> Result { Result::Err(label) } + let flag = match next() { + Result::Ok(_) => 0, + Result::Err(e) => if e == "e" => { 1 } else => { 2 }, + }; + flag; + "#; + assert_eq!(eval(err), vm::Value::Int(1)); +} diff --git a/tests/rss_no_null_boundary_tests.rs b/tests/rss_no_null_boundary_tests.rs new file mode 100644 index 00000000..3c34eb7e --- /dev/null +++ b/tests/rss_no_null_boundary_tests.rs @@ -0,0 +1,508 @@ +#![cfg(feature = "runtime")] + +use vm::compile_source; + +fn rejection(source: &str) -> String { + match compile_source(source) { + Ok(_) => panic!("RSS should reject legacy null syntax: {source}"), + Err(error) => error.to_string(), + } +} + +fn span_text<'a>(source: &'a str, error: &str) -> &'a str { + let open = error + .rfind('[') + .unwrap_or_else(|| panic!("missing span in {error}")); + let dots = error[open..] + .find("..") + .unwrap_or_else(|| panic!("missing span range in {error}")); + let close = error[open + dots..] + .find(']') + .unwrap_or_else(|| panic!("missing span end in {error}")); + let lo: usize = error[open + 1..open + dots] + .parse() + .unwrap_or_else(|_| panic!("bad span lo in {error}")); + let hi: usize = error[open + dots + 2..open + dots + close] + .parse() + .unwrap_or_else(|_| panic!("bad span hi in {error}")); + &source[lo..hi] +} + +#[test] +fn rss_rejects_null_literal_with_migration_and_span() { + let source = "null;"; + let error = rejection(source); + assert!( + error.contains("Option::None") && error.contains("()"), + "{error}" + ); + assert_eq!(span_text(source, &error), "null"); +} + +#[test] +fn rss_rejects_null_pattern_with_migration_and_span() { + let source = "let value: Option = Option::None; match value { null => 1, _ => 2 };"; + let error = rejection(source); + assert!( + error.contains("Option::None") && error.contains("()"), + "{error}" + ); + assert_eq!(span_text(source, &error), "null"); +} + +#[test] +fn rss_rejects_null_type_with_option_migration_and_span() { + let source = "fn take(value: null) { 1; }"; + let error = rejection(source); + assert!(error.contains("Option"), "{error}"); + assert_eq!(span_text(source, &error), "null"); +} + +#[test] +fn rss_rejects_nullable_suffix_with_option_migration_and_span() { + let source = "fn take(value: int?) { 1; }"; + let error = rejection(source); + assert!(error.contains("Option"), "{error}"); + assert_eq!(span_text(source, &error), "?"); +} + +#[test] +fn rss_rejects_null_map_key_with_span() { + let source = "{ null: 1 };"; + let error = rejection(source); + assert!( + error.contains("Option::None") && error.contains("()"), + "{error}" + ); + assert_eq!(span_text(source, &error), "null"); +} + +fn eval(source: &str) -> vm::Value { + let compiled = compile_source(source) + .unwrap_or_else(|error| panic!("RSS JSON source should compile: {source}\n{error}")); + let mut vm = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + vm.set_fuel(100_000); + assert_eq!( + vm.run().expect("JSON program should run"), + vm::VmStatus::Halted + ); + vm.stack() + .last() + .cloned() + .expect("JSON program should leave a value") +} + +#[test] +fn json_option_null_decodes_as_none() { + let value = eval( + r#" + use json; + let decoded: Option = json::decode::>("null"); + match decoded { Option::None => 0, Option::Some(value) => 1 + value }; + "#, + ); + assert_eq!(value, vm::Value::Int(0)); +} + +#[test] +fn json_option_none_encodes_as_null() { + let value = eval( + r#" + use json; + let missing: Option = Option::None; + let encoded: string = json::encode::>(missing); + encoded; + "#, + ); + assert_eq!(value, vm::Value::string("null")); +} + +#[test] +fn println_option_returns_original_and_renders_none() { + let source = r#" + use json; + let decoded: Option = json::decode::>("null"); + let printed = println(decoded); + match printed { Option::None => 0, Option::Some(value) => 1 + value }; + "#; + let compiled = compile_source(source) + .unwrap_or_else(|error| panic!("logical print should compile: {source}\n{error}")); + let mut vm = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + vm.set_fuel(100_000); + let lines = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let sink_lines = std::sync::Arc::clone(&lines); + vm.set_runtime_print_sink(move |rendered| { + sink_lines + .lock() + .expect("sink should be lockable") + .push(rendered); + }); + assert_eq!( + vm.run().expect("logical print should run"), + vm::VmStatus::Halted + ); + let value = vm.stack().last().cloned().expect("print result"); + assert_eq!(value, vm::Value::Int(0)); + assert_eq!( + lines.lock().expect("printed lines").as_slice(), + ["None\n".to_string()] + ); +} + +#[test] +fn rss_rejects_custom_host_unknown_return() { + use std::sync::Arc; + let catalog = Arc::new({ + let mut builder = vm::HostApiBuilder::new(); + builder.function(vm::HostFunctionSchema::with_return( + "demo::unknown", + Vec::new(), + vm::HostTypeSchema::Unknown, + )); + builder.function(vm::HostFunctionSchema::with_return( + "demo::flag", + Vec::new(), + vm::HostTypeSchema::Bool, + )); + builder.build().expect("catalog") + }); + let rejected = vm::compile_source_with_flavor_and_options( + "use demo; demo::unknown();", + vm::SourceFlavor::RustScript, + vm::CompileSourceFileOptions::default().with_host_api_catalog(Arc::clone(&catalog)), + ); + let message = match rejected { + Ok(_) => panic!("unknown host return must be rejected"), + Err(error) => error.to_string(), + }; + assert!(message.contains("dynamic or legacy-null"), "{message}"); + vm::compile_source_with_flavor_and_options( + "use demo; demo::flag();", + vm::SourceFlavor::RustScript, + vm::CompileSourceFileOptions::default().with_host_api_catalog(catalog), + ) + .expect("concrete host return"); +} + +#[test] +fn format_template_renders_option_from_checked_schema() { + let source = r#" + let value: Option = Option::None; + println("{}", value); + 1; + "#; + let compiled = compile_source(source) + .unwrap_or_else(|error| panic!("format template should compile: {source}\n{error}")); + let mut vm = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + vm.set_fuel(100_000); + let lines = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let sink_lines = std::sync::Arc::clone(&lines); + vm.set_runtime_print_sink(move |rendered| { + sink_lines + .lock() + .expect("sink should be lockable") + .push(rendered); + }); + assert_eq!( + vm.run().expect("format template should run"), + vm::VmStatus::Halted + ); + assert_eq!( + lines.lock().expect("printed lines").as_slice(), + ["None\n".to_string()] + ); +} + +#[test] +fn type_of_option_is_shallow_option_label() { + let source = r#" + use json; + let decoded: Option = json::decode::>("null"); + let label: string = type(decoded); + label; + "#; + let value = eval(source); + assert_eq!(value, vm::Value::string("Option")); +} + +#[test] +fn type_of_unit_is_unit_label() { + let source = r#" + let value: () = (); + let label: string = type(value); + label; + "#; + let value = eval(source); + assert_eq!(value, vm::Value::string("()")); +} + +#[test] +fn unannotated_decode_keeps_explicit_type_argument() { + let source = r#" + use json; + let decoded = json::decode::>("null"); + let typed: Option = decoded; + typed; + "#; + let value = eval(source); + assert!(!vm::option::payload(&value).expect("option").0); +} + +#[test] +fn encode_infers_option_schema_without_type_arguments() { + let source = r#" + use json; + let value: Option = Option::None; + let encoded = json::encode(value); + encoded; + "#; + let value = eval(source); + assert_eq!(value, vm::Value::string("null")); +} + +#[test] +fn repl_result_schema_comes_from_the_expression_not_an_equal_local() { + let compiled = vm::compile_source_for_repl_with_locals( + "let other: Option = Option::None; let result: Option = Option::None; result;", + &[], + ) + .expect("repl compile"); + assert_eq!( + compiled.result_schema, + Some(vm::TypeSchema::Option(Box::new(vm::TypeSchema::Int))) + ); +} + +#[test] +fn unit_map_insert_keeps_the_key() { + let value = eval( + r#" + let mut values: map<()> = {}; + values["present"] = (); + values.has("present"); + "#, + ); + assert_eq!(value, vm::Value::Bool(true)); +} + +#[test] +fn typed_map_remove_returns_map_without_the_key() { + let value = eval( + r#" + let mut values: map = {keep: 1, drop: 2}; + let updated = values.remove("drop"); + updated.has("drop"); + "#, + ); + assert_eq!(value, vm::Value::Bool(false)); +} + +fn print_catalog( + params: Vec, + returns: vm::HostTypeSchema, +) -> std::sync::Arc { + let mut builder = vm::HostApiBuilder::new(); + builder.function(vm::HostFunctionSchema::with_return( + "print", params, returns, + )); + std::sync::Arc::new(builder.build().expect("print catalog")) +} + +fn compile_catalog( + source: &str, + catalog: &std::sync::Arc, +) -> Result { + vm::compile_source_with_flavor_and_options( + source, + vm::SourceFlavor::RustScript, + vm::CompileSourceFileOptions::default() + .with_host_api_catalog(std::sync::Arc::clone(catalog)), + ) +} + +fn assert_matching_print_rejected(returns: vm::HostTypeSchema) { + let catalog = print_catalog( + vec![vm::HostParamSchema::value("value", vm::HostTypeSchema::Int)], + returns, + ); + let message = match compile_catalog("print(1);", &catalog) { + Ok(_) => panic!("matching catalog print must not become the language intrinsic"), + Err(error) => error.to_string(), + }; + assert!( + message.contains("dynamic or legacy-null"), + "expected dynamic output rejection, got {message}" + ); + assert!( + !message.contains("print formatting requires"), + "catalog identity must be resolved before intrinsic formatting sugar: {message}" + ); +} + +#[test] +fn matching_catalog_print_unknown_is_rejected() { + assert_matching_print_rejected(vm::HostTypeSchema::Unknown); +} + +#[test] +fn matching_catalog_print_null_is_rejected() { + assert_matching_print_rejected(vm::HostTypeSchema::Null); +} + +#[test] +fn matching_catalog_print_optional_is_rejected() { + assert_matching_print_rejected(vm::HostTypeSchema::Optional(Box::new( + vm::HostTypeSchema::Int, + ))); +} + +static MATCHING_BOOL_PRINT_CALLS: std::sync::atomic::AtomicUsize = + std::sync::atomic::AtomicUsize::new(0); + +fn matching_bool_print(_: &mut vm::Vm, args: &[vm::Value]) -> vm::VmResult { + MATCHING_BOOL_PRINT_CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + assert_eq!(args, &[vm::Value::Int(1)]); + Ok(vm::CallOutcome::Return(vm::return_one(vm::Value::Bool( + true, + )))) +} + +static TWO_INT_PRINT_CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +fn two_int_print(_: &mut vm::Vm, args: &[vm::Value]) -> vm::VmResult { + TWO_INT_PRINT_CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + assert_eq!(args, &[vm::Value::Int(1), vm::Value::Int(2)]); + Ok(vm::CallOutcome::Return(vm::return_one(vm::Value::Bool( + false, + )))) +} + +fn run_bound_print( + source: &str, + catalog: &std::sync::Arc, + handler: vm::StaticHostFunction, + counter: &std::sync::atomic::AtomicUsize, +) -> vm::Value { + counter.store(0, std::sync::atomic::Ordering::SeqCst); + let compiled = compile_catalog(source, catalog).unwrap_or_else(|error| { + panic!("matching catalog print should keep its declared return: {error}") + }); + let mut registry = vm::HostFunctionRegistry::new(); + let schema = vm::catalog_import_schemas(catalog, "print") + .into_iter() + .next() + .expect("catalog print schema"); + registry + .register_catalog_static(schema, handler) + .expect("register matching print handler"); + let mut machine = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + let sink_lines = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let recorded = std::sync::Arc::clone(&sink_lines); + machine.set_runtime_print_sink(move |rendered| { + recorded.lock().expect("sink").push(rendered); + }); + registry + .bind_vm_cached(&mut machine) + .expect("bind matching print handler"); + assert_eq!( + machine.run().expect("matching print should run"), + vm::VmStatus::Halted + ); + assert!( + sink_lines.lock().expect("sink").is_empty(), + "custom print must not fall through to the language print sink" + ); + assert_eq!( + counter.load(std::sync::atomic::Ordering::SeqCst), + 1, + "custom print handler must actually run" + ); + machine + .stack() + .last() + .cloned() + .expect("matching print should leave its return value") +} + +#[test] +fn matching_catalog_print_bool_keeps_handler_and_return() { + let catalog = print_catalog( + vec![vm::HostParamSchema::value("value", vm::HostTypeSchema::Int)], + vm::HostTypeSchema::Bool, + ); + let value = run_bound_print( + "let output: bool = print(1); output;", + &catalog, + matching_bool_print, + &MATCHING_BOOL_PRINT_CALLS, + ); + assert_eq!(value, vm::Value::Bool(true)); +} + +static EXPLICIT_BOOL_PRINT_CALLS: std::sync::atomic::AtomicUsize = + std::sync::atomic::AtomicUsize::new(0); + +fn explicit_bool_print(_: &mut vm::Vm, args: &[vm::Value]) -> vm::VmResult { + EXPLICIT_BOOL_PRINT_CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + assert_eq!(args, &[vm::Value::Int(1)]); + Ok(vm::CallOutcome::Return(vm::return_one(vm::Value::Bool( + true, + )))) +} + +#[test] +fn explicit_print_declaration_keeps_catalog_bool_identity() { + let catalog = print_catalog( + vec![vm::HostParamSchema::value("value", vm::HostTypeSchema::Int)], + vm::HostTypeSchema::Bool, + ); + let value = run_bound_print( + "fn print(value: int) -> bool; let output: bool = print(1); output;", + &catalog, + explicit_bool_print, + &EXPLICIT_BOOL_PRINT_CALLS, + ); + assert_eq!(value, vm::Value::Bool(true)); +} + +#[test] +fn matching_two_int_catalog_print_is_not_format_sugar() { + let catalog = print_catalog( + vec![ + vm::HostParamSchema::value("left", vm::HostTypeSchema::Int), + vm::HostParamSchema::value("right", vm::HostTypeSchema::Int), + ], + vm::HostTypeSchema::Bool, + ); + let value = run_bound_print( + "let output: bool = print(1, 2); output;", + &catalog, + two_int_print, + &TWO_INT_PRINT_CALLS, + ); + assert_eq!(value, vm::Value::Bool(false)); +} + +#[test] +fn nonmatching_catalog_print_keeps_language_print() { + let catalog = print_catalog( + vec![ + vm::HostParamSchema::value("left", vm::HostTypeSchema::Int), + vm::HostParamSchema::value("right", vm::HostTypeSchema::Int), + ], + vm::HostTypeSchema::Bool, + ); + let compiled = compile_catalog("print(1);", &catalog).unwrap_or_else(|error| { + panic!("a nonmatching catalog overload must not ban language print: {error}") + }); + let mut machine = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + let lines = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let recorded = std::sync::Arc::clone(&lines); + machine.set_runtime_print_sink(move |rendered| { + recorded.lock().expect("sink").push(rendered); + }); + assert_eq!(machine.run().expect("language print"), vm::VmStatus::Halted); + assert_eq!(lines.lock().expect("sink").as_slice(), ["1".to_string()]); +} diff --git a/tests/semantic_host_overload_tests.rs b/tests/semantic_host_overload_tests.rs index 3c04edab..25bc221d 100644 --- a/tests/semantic_host_overload_tests.rs +++ b/tests/semantic_host_overload_tests.rs @@ -60,7 +60,7 @@ fn overload_catalog() -> Arc { HostTypeSchema::Resource(database_key), HostParamPassing::TakeOwned, )], - HostTypeSchema::Null, + HostTypeSchema::Unit, ) .with_description("close the adapter database"), ); @@ -120,7 +120,7 @@ fn semantic_host_metadata_selects_documentation_for_exact_overload() { .callable_signature_at(SourcePosition::new(0, second + 2)) .expect("database close signature"); assert_eq!(database_signature.description, "close the adapter database"); - assert_eq!(database_signature.return_type, HostTypeSchema::Null); + assert_eq!(database_signature.return_type, HostTypeSchema::Unit); assert_eq!(database_signature.params[0].name, "connection"); assert_eq!( database_signature.params[0].passing, diff --git a/tests/sqlite_feature_gating_tests.rs b/tests/sqlite_feature_gating_tests.rs new file mode 100644 index 00000000..c5f790ea --- /dev/null +++ b/tests/sqlite_feature_gating_tests.rs @@ -0,0 +1,87 @@ +//! Feature-disabled and feature-enabled admission of the standard SQLite +//! surface. +//! +//! The native SQLite catalog is optional: a build without `sqlite` must not +//! publish any `sqlite::*` function or `Sqlite*` named struct, while a build +//! with the feature publishes the complete typed surface. This target is +//! deliberately separate from the behavioral SQLite tags so its only job is +//! the real `standard_host_catalog` admission boundary. + +#![cfg(feature = "runtime")] + +use vm::standard_host_catalog; + +#[cfg(not(feature = "sqlite"))] +#[test] +fn standard_catalog_omits_sqlite_surface_without_the_feature() { + let catalog = standard_host_catalog(); + assert!( + catalog + .functions() + .iter() + .all(|function| !function.name.starts_with("sqlite::")), + "a non-sqlite build must publish no sqlite host function" + ); + assert!( + catalog + .structs() + .iter() + .all(|schema| !schema.name.starts_with("Sqlite")), + "a non-sqlite build must publish no Sqlite named struct" + ); + assert!( + catalog + .resources() + .iter() + .all(|resource| resource.key.as_str() != "sqlite.connection"), + "a non-sqlite build must publish no sqlite.connection resource" + ); +} + +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +#[test] +fn standard_catalog_publishes_the_typed_sqlite_surface_with_the_feature() { + let catalog = standard_host_catalog(); + for name in [ + "sqlite::open", + "sqlite::execute", + "sqlite::query", + "sqlite::transaction", + "sqlite::close", + ] { + assert!( + catalog.function(name).is_some(), + "an sqlite build must publish `{name}`" + ); + } + assert_eq!( + catalog + .function("sqlite::close") + .expect("sqlite::close") + .return_type, + vm::HostTypeSchema::Unit, + "sqlite::close exposes logical Unit" + ); + for name in [ + "SqliteOpenOptions", + "SqliteLimits", + "SqliteExecuteResult", + "SqliteValue", + "SqliteRow", + "SqliteQueryResult", + "SqliteStatement", + "SqliteTransactionResult", + ] { + assert!( + catalog.struct_named(name).is_some(), + "an sqlite build must publish `{name}`" + ); + } + assert!( + catalog + .resources() + .iter() + .any(|resource| resource.key.as_str() == "sqlite.connection"), + "an sqlite build must publish the sqlite.connection resource" + ); +} diff --git a/tests/sqlite_named_struct_tests.rs b/tests/sqlite_named_struct_tests.rs index 33e77296..f8dce8a3 100644 --- a/tests/sqlite_named_struct_tests.rs +++ b/tests/sqlite_named_struct_tests.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; use std::sync::Arc; -use std::task::{Context, Poll, Wake, Waker}; +use std::task::{Context, Poll, Waker}; use vm::compiler::{ CompileSourceFileOptions, SourceFlavor, compile_source_with_flavor_and_options, @@ -72,10 +72,6 @@ impl HostAsyncBridge for TokioHostDriver { } } -fn opt(inner: HostTypeSchema) -> HostTypeSchema { - HostTypeSchema::Optional(Box::new(inner)) -} - fn array(inner: HostTypeSchema) -> HostTypeSchema { HostTypeSchema::Array(Box::new(inner)) } @@ -99,16 +95,16 @@ fn limits_fields() -> Vec { "busy_timeout_ms", ] .into_iter() - .map(|name| HostStructField::new(name, opt(HostTypeSchema::Int))) + .map(|name| HostStructField::new(name, option(HostTypeSchema::Int))) .collect() } fn open_options_fields() -> Vec { vec![ - HostStructField::new("path", opt(HostTypeSchema::String)), - HostStructField::new("mode", opt(HostTypeSchema::String)), - HostStructField::new("root", opt(HostTypeSchema::String)), - HostStructField::new("limits", opt(named("SqliteLimits", limits_fields()))), + HostStructField::new("path", option(HostTypeSchema::String)), + HostStructField::new("mode", option(HostTypeSchema::String)), + HostStructField::new("root", option(HostTypeSchema::String)), + HostStructField::new("limits", option(named("SqliteLimits", limits_fields()))), ] } @@ -122,10 +118,10 @@ fn execute_result_fields() -> Vec { fn sqlite_value_fields() -> Vec { vec![ HostStructField::new("kind", HostTypeSchema::String), - HostStructField::new("int_value", opt(HostTypeSchema::Int)), - HostStructField::new("float_value", opt(HostTypeSchema::Float)), - HostStructField::new("text_value", opt(HostTypeSchema::String)), - HostStructField::new("blob_value", opt(HostTypeSchema::Bytes)), + HostStructField::new("int_value", option(HostTypeSchema::Int)), + HostStructField::new("float_value", option(HostTypeSchema::Float)), + HostStructField::new("text_value", option(HostTypeSchema::String)), + HostStructField::new("blob_value", option(HostTypeSchema::Bytes)), ] } @@ -141,7 +137,7 @@ fn query_result_fields() -> Vec { HostStructField::new("columns", array(HostTypeSchema::String)), HostStructField::new("rows", array(named("SqliteRow", row_fields()))), HostStructField::new("truncated", HostTypeSchema::Bool), - HostStructField::new("next_cursor", opt(HostTypeSchema::Int)), + HostStructField::new("next_cursor", option(HostTypeSchema::Int)), ] } @@ -150,10 +146,10 @@ fn statement_fields() -> Vec { HostStructField::new("sql", HostTypeSchema::String), HostStructField::new( "params", - opt(array(named("SqliteValue", sqlite_value_fields()))), + option(array(named("SqliteValue", sqlite_value_fields()))), ), - HostStructField::new("query", opt(HostTypeSchema::Bool)), - HostStructField::new("limits", opt(named("SqliteLimits", limits_fields()))), + HostStructField::new("query", option(HostTypeSchema::Bool)), + HostStructField::new("limits", option(named("SqliteLimits", limits_fields()))), ] } @@ -162,11 +158,11 @@ fn transaction_result_fields() -> Vec { HostStructField::new("kind", HostTypeSchema::String), HostStructField::new( "execute", - opt(named("SqliteExecuteResult", execute_result_fields())), + option(named("SqliteExecuteResult", execute_result_fields())), ), HostStructField::new( "query", - opt(named("SqliteQueryResult", query_result_fields())), + option(named("SqliteQueryResult", query_result_fields())), ), ] } @@ -211,9 +207,9 @@ fn function_named<'a>( fn schema_contains_map(schema: &HostTypeSchema) -> bool { match schema { HostTypeSchema::Map(_) | HostTypeSchema::Unknown => true, - HostTypeSchema::Array(inner) | HostTypeSchema::Optional(inner) => { - schema_contains_map(inner) - } + HostTypeSchema::Array(inner) + | HostTypeSchema::Optional(inner) + | HostTypeSchema::Option(inner) => schema_contains_map(inner), HostTypeSchema::Named { fields, .. } => { fields.iter().any(|field| schema_contains_map(&field.ty)) } @@ -320,10 +316,17 @@ fn object_literal_open_query_and_statement_params_compile() { compile( r#" use sqlite; - let db = sqlite::open({ path: ":memory:", mode: "memory", limits: {} }); + let options: SqliteOpenOptions = { + path: Option::Some(":memory:"), + mode: Option::Some("memory"), + }; + let db = sqlite::open(options); sqlite::execute(&db, "CREATE TABLE t (a INTEGER)", []); - sqlite::query(&db, "SELECT a FROM t", [], { max_rows: 8 }); - sqlite::transaction(&db, [{ sql: "INSERT INTO t VALUES (1)", query: false }]); + let query_limits: SqliteLimits = { max_rows: Option::Some(8) }; + sqlite::query(&db, "SELECT a FROM t", [], query_limits); + let statement: SqliteStatement = { sql: "INSERT INTO t VALUES (1)", query: Option::Some(false) }; + let statements: [SqliteStatement] = [statement]; + sqlite::transaction(&db, statements); sqlite::close(db); "#, ) @@ -335,8 +338,10 @@ fn empty_open_and_limits_objects_compile() { compile( r#" use sqlite; - let db = sqlite::open({}); - sqlite::query(&db, "SELECT 1", [], {}); + let options: SqliteOpenOptions = {}; + let db = sqlite::open(options); + let limits: SqliteLimits = {}; + sqlite::query(&db, "SELECT 1", [], limits); sqlite::close(db); "#, ) @@ -348,7 +353,7 @@ fn extra_field_on_open_options_is_rejected() { let message = compile_err( r#" use sqlite; - sqlite::open({ path: ":memory:", extra: 1 }); + sqlite::open({ path: Option::Some(":memory:"), extra: 1 }); "#, ); assert!( @@ -362,21 +367,22 @@ fn extra_field_on_open_options_is_rejected() { } #[test] -fn extra_field_on_limits_is_rejected() { - let message = compile_err( +fn extra_field_on_a_sqlite_value_is_rejected_at_the_native_boundary() { + let compiled = compile_standard( r#" use sqlite; - let db = sqlite::open({ path: ":memory:", mode: "memory" }); - sqlite::query(&db, "SELECT 1", [], { max_rows: 1, nope: 2 }); + let options: SqliteOpenOptions = { path: Option::Some(":memory:"), mode: Option::Some("memory") }; + let db = sqlite::open(options); + let value: SqliteValue = { kind: "null", nope: 2 }; + let values: [SqliteValue] = [value]; + sqlite::execute(&db, "SELECT 1", values); "#, - ); - assert!( - message.contains("sqlite::query") && message.contains("SqliteLimits"), - "extra limits field diagnostic should name the function and struct, got {message}" - ); + ) + .expect("the extra payload field should compile into the typed value"); + let message = run_compiled_sqlite_host_error(compiled); assert!( - message.contains("nope"), - "extra limits field diagnostic should name the extra key, got {message}" + message.contains("nope") && message.contains("unknown field"), + "the native boundary must reject extra public value fields, got {message}" ); } @@ -385,13 +391,16 @@ fn missing_sql_on_transaction_statement_is_rejected() { let message = compile_err( r#" use sqlite; - let db = sqlite::open({ path: ":memory:", mode: "memory" }); - sqlite::transaction(&db, [{ params: [] }]); + let options: SqliteOpenOptions = { path: Option::Some(":memory:"), mode: Option::Some("memory") }; + let db = sqlite::open(options); + let statement: SqliteStatement = {}; + let statements: [SqliteStatement] = [statement]; + sqlite::transaction(&db, statements); "#, ); assert!( - message.contains("sqlite::transaction") && message.contains("SqliteStatement"), - "missing sql diagnostic should name the function and struct, got {message}" + message.contains("sql") && message.contains("required"), + "missing sql diagnostic should name the missing required field, got {message}" ); } @@ -418,11 +427,13 @@ fn typed_field_access_on_execute_and_query_results_compiles() { compile( r#" use sqlite; - let db = sqlite::open({ path: ":memory:", mode: "memory", limits: {} }); + let options: SqliteOpenOptions = { path: Option::Some(":memory:"), mode: Option::Some("memory") }; + let db = sqlite::open(options); let created = sqlite::execute(&db, "CREATE TABLE t (a INTEGER)", []); let affected = created.rows_affected; let rowid = created.last_insert_rowid; - let queried = sqlite::query(&db, "SELECT a FROM t", [], {}); + let limits: SqliteLimits = {}; + let queried = sqlite::query(&db, "SELECT a FROM t", [], limits); let columns = queried.columns; let rows = queried.rows; let truncated = queried.truncated; @@ -438,17 +449,33 @@ fn typed_params_rows_and_transaction_results_are_structured() { compile( r#" use sqlite; - let db = sqlite::open({ path: ":memory:", mode: "memory" }); + let options: SqliteOpenOptions = { path: Option::Some(":memory:"), mode: Option::Some("memory") }; + let db = sqlite::open(options); sqlite::execute(&db, "CREATE TABLE t (a INTEGER)", []); - sqlite::execute(&db, "INSERT INTO t VALUES (?)", [{ kind: "int", int_value: 7 }]); - let queried = sqlite::query(&db, "SELECT a FROM t", [], {}); + let params: [SqliteValue] = [{ kind: "int", int_value: Option::Some(7) }]; + sqlite::execute(&db, "INSERT INTO t VALUES (?)", params); + let limits: SqliteLimits = {}; + let queried = sqlite::query(&db, "SELECT a FROM t", [], limits); let first = queried.rows[0].cells[0]; assert(first.kind == "int"); - assert(first.int_value == 7); - let results = sqlite::transaction(&db, [{ sql: "SELECT a FROM t", query: true }]); + let first_value = match first.int_value { + Option::Some(value) => value, + Option::None => -1, + }; + assert(first_value == 7); + let statement: SqliteStatement = { sql: "SELECT a FROM t", query: Option::Some(true) }; + let statements: [SqliteStatement] = [statement]; + let results = sqlite::transaction(&db, statements); let first_result = results[0]; assert(first_result.kind == "query"); - assert(first_result.query.rows[0].cells[0].int_value == 7); + let present_value = match first_result.query { + Option::Some(query_result) => match query_result.rows[0].cells[0].int_value { + Option::Some(value) => value, + Option::None => -1, + }, + Option::None => -1, + }; + assert(present_value == 7); sqlite::close(db); queried.truncated; "#, @@ -461,7 +488,8 @@ fn raw_sqlite_scalar_params_are_rejected() { let message = compile_err( r#" use sqlite; - let db = sqlite::open({ path: ":memory:", mode: "memory" }); + let options: SqliteOpenOptions = { path: Option::Some(":memory:"), mode: Option::Some("memory") }; + let db = sqlite::open(options); sqlite::execute(&db, "SELECT ?", [7]); "#, ); @@ -476,22 +504,135 @@ fn wrong_nested_sqlite_value_shapes_are_rejected() { let message = compile_err( r#" use sqlite; - let db = sqlite::open({ path: ":memory:", mode: "memory" }); - sqlite::query(&db, "SELECT ?", [{ kind: "int", int_value: "wrong" }], {}); + let options: SqliteOpenOptions = { path: Option::Some(":memory:"), mode: Option::Some("memory") }; + let db = sqlite::open(options); + let values: [SqliteValue] = [{ kind: "int", int_value: Option::Some("wrong") }]; + let limits: SqliteLimits = {}; + sqlite::query(&db, "SELECT ?", values, limits); "#, ); assert!( - message.contains("sqlite::query") && message.contains("int_value"), - "wrong nested payload diagnostic should name the typed field, got {message}" + message.contains("int") && message.contains("string"), + "wrong logical Option payload diagnostic should name the declared and actual types, got {message}" + ); +} + +// ------------------------------------------------- logical Option/Unit RED + +fn option(inner: HostTypeSchema) -> HostTypeSchema { + HostTypeSchema::Option(Box::new(inner)) +} + +#[test] +fn sqlite_nullable_fields_are_logical_option() { + let catalog = sqlite_host_catalog(); + let query = catalog + .struct_named("SqliteQueryResult") + .expect("SqliteQueryResult"); + assert_eq!( + query.fields[3].ty, + option(HostTypeSchema::Int), + "next_cursor must be a logical Option" + ); + let transaction = catalog + .struct_named("SqliteTransactionResult") + .expect("SqliteTransactionResult"); + assert_eq!( + transaction.fields[1].ty, + option(named("SqliteExecuteResult", execute_result_fields())), + "the unselected execute branch must be a logical Option" + ); + assert_eq!( + transaction.fields[2].ty, + option(named("SqliteQueryResult", query_result_fields())), + "the unselected query branch must be a logical Option" + ); + + // SQL NULL cells stay the tagged `kind: "null"` business value with the + // existing optional payload entries; they are not folded into Option. + let value = catalog.struct_named("SqliteValue").expect("SqliteValue"); + assert_eq!( + value.fields[1].ty, + option(HostTypeSchema::Int), + "SqliteValue keeps its tagged SQL NULL payload contract" + ); + let statement = catalog + .struct_named("SqliteStatement") + .expect("SqliteStatement"); + assert_eq!( + statement.fields[1].ty, + option(array(named("SqliteValue", sqlite_value_fields()))), + "SqliteStatement keeps its optional-entry params contract" + ); +} + +#[test] +fn sqlite_close_contract_is_logical_unit() { + let catalog = sqlite_host_catalog(); + assert_eq!( + function_named(&catalog, "sqlite::close").return_type, + HostTypeSchema::Unit, + "sqlite::close is a void host call and must expose logical Unit" ); } +#[test] +fn logical_option_sqlite_consumer_compiles_and_runs() { + let compiled = compile_standard( + r#" + use sqlite; + let open_limits: SqliteLimits = { max_rows: Option::Some(8) }; + let options: SqliteOpenOptions = { + path: Option::Some(":memory:"), + mode: Option::Some("memory"), + limits: Option::Some(open_limits), + }; + let db = sqlite::open(options); + sqlite::execute(&db, "CREATE TABLE t (a INTEGER, z TEXT)", []); + let insert_params: [SqliteValue] = [ + { kind: "int", int_value: Option::Some(7) }, + { kind: "null" } + ]; + sqlite::execute(&db, "INSERT INTO t (a, z) VALUES (?1, ?2)", insert_params); + let query_limits: SqliteLimits = { max_rows: Option::Some(8) }; + let q = sqlite::query(&db, "SELECT a, z FROM t", [], query_limits); + let cursor = match q.next_cursor { + Option::Some(value) => value, + Option::None => -1, + }; + assert(cursor == 7); + let cell = q.rows[0].cells[1]; + assert(cell.kind == "null"); + let text_absent = match cell.text_value { + Option::Some(_) => false, + Option::None => true, + }; + assert(text_absent); + let insert_statement_params: [SqliteValue] = [{ kind: "int", int_value: Option::Some(8) }]; + let insert_statement: SqliteStatement = { sql: "INSERT INTO t (a) VALUES (?1)", params: Option::Some(insert_statement_params) }; + let query_statement: SqliteStatement = { sql: "SELECT a FROM t", query: Option::Some(true) }; + let statements: [SqliteStatement] = [insert_statement, query_statement]; + let results = sqlite::transaction(&db, statements); + let execute_absent = match results[0].query { + Option::Some(result) => false, + Option::None => true, + }; + assert(execute_absent); + let query_present = match results[1].query { + Option::Some(result) => true, + Option::None => false, + }; + assert(query_present); + let closed: () = sqlite::close(db); + closed; +"#, + ) + .expect("logical Option SQLite consumer should compile"); + run_compiled_sqlite(compiled); +} + fn noop_waker() -> Waker { - struct LocalNoop; - impl Wake for LocalNoop { - fn wake(self: Arc) {} - } - Waker::from(Arc::new(LocalNoop)) + Waker::noop().clone() } fn drive_to_halt(vm: &mut vm::vm::Vm) { @@ -534,15 +675,58 @@ fn run_compiled_sqlite(compiled: CompiledProgram) { drive_to_halt(&mut vm); } +fn run_compiled_sqlite_host_error(compiled: CompiledProgram) -> String { + let mut vm = vm::vm::Vm::try_new(compiled.program).expect("vm"); + vm.set_async_bridge(Box::::default()) + .expect("test async bridge should install"); + let mut registry = HostFunctionRegistry::empty(); + register_sqlite_builtin_module(&mut registry) + .expect("sqlite exact registration should succeed"); + registry + .bind_vm_cached(&mut vm) + .expect("sqlite exact host imports should bind"); + vm.configure_sqlite(SqlitePolicy::default()); + let mut status = match vm.run() { + Ok(status) => status, + Err(VmError::HostError(message)) => return message, + Err(other) => panic!("unexpected VM error: {other:?}"), + }; + loop { + status = match status { + vm::vm::VmStatus::Halted => panic!("expected host error, got success"), + vm::vm::VmStatus::Yielded => match vm.resume() { + Ok(status) => status, + Err(VmError::HostError(message)) => return message, + Err(other) => panic!("unexpected VM error: {other:?}"), + }, + vm::vm::VmStatus::Waiting(_) => { + if let Err(error) = vm.wait_for_host_op_blocking() { + return match error { + VmError::HostError(message) => message, + other => format!("{other:?}"), + }; + } + match vm.resume() { + Ok(status) => status, + Err(VmError::HostError(message)) => return message, + Err(other) => panic!("unexpected VM error: {other:?}"), + } + } + }; + } +} + #[test] fn runtime_execute_and_query_results_remain_maps_with_typed_fields() { let compiled = compile_standard( r#" use sqlite; - let db = sqlite::open({ path: ":memory:", mode: "memory", limits: {} }); + let options: SqliteOpenOptions = { path: Option::Some(":memory:"), mode: Option::Some("memory") }; + let db = sqlite::open(options); sqlite::execute(&db, "CREATE TABLE t (a INTEGER)", []); let inserted = sqlite::execute(&db, "INSERT INTO t VALUES (9)", []); - let queried = sqlite::query(&db, "SELECT a FROM t", [], { max_rows: 10 }); + let limits: SqliteLimits = { max_rows: Option::Some(10) }; + let queried = sqlite::query(&db, "SELECT a FROM t", [], limits); let affected = inserted.rows_affected; let rowid = inserted.last_insert_rowid; let columns = queried.columns; @@ -561,39 +745,54 @@ fn documented_open_modes_compile() { compile( r#" use sqlite; - sqlite::open({ path: ":memory:", mode: "memory" }); - sqlite::open({ path: "state.db", mode: "read_only" }); - sqlite::open({ path: "state.db", mode: "read_write" }); - sqlite::open({ path: "state.db", mode: "read_write_create" }); - sqlite::open({ path: "state.db" }); + let memory: SqliteOpenOptions = { path: Option::Some(":memory:"), mode: Option::Some("memory") }; + sqlite::open(memory); + let read_only: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_only") }; + sqlite::open(read_only); + let read_write: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write") }; + sqlite::open(read_write); + let read_write_create: SqliteOpenOptions = { path: Option::Some("state.db"), mode: Option::Some("read_write_create") }; + sqlite::open(read_write_create); + let default_mode: SqliteOpenOptions = { path: Option::Some("state.db") }; + sqlite::open(default_mode); "#, ) .expect("exact open modes and omitted default should compile"); } #[test] -fn optional_null_fields_compile_as_omitted() { +fn explicit_none_option_configuration_fields_compile() { compile( r#" use sqlite; - let db = sqlite::open({ - path: ":memory:", - mode: "memory", - root: null, - limits: null, - }); + let no_root: Option = Option::None; + let no_open_limits: Option = Option::None; + let options: SqliteOpenOptions = { + path: Option::Some(":memory:"), + mode: Option::Some("memory"), + root: no_root, + limits: no_open_limits, + }; + let db = sqlite::open(options); sqlite::execute(&db, "CREATE TABLE t (a INTEGER)", []); - sqlite::query(&db, "SELECT a FROM t", [], { max_rows: null }); - sqlite::transaction(&db, [{ + let no_max_rows: Option = Option::None; + let query_limits: SqliteLimits = { max_rows: no_max_rows }; + let empty = sqlite::query(&db, "SELECT a FROM t", [], query_limits); + let no_params: Option<[SqliteValue]> = Option::None; + let no_query: Option = Option::None; + let no_statement_limits: Option = Option::None; + let statement: SqliteStatement = { sql: "INSERT INTO t VALUES (1)", - params: null, - query: null, - limits: null, - }]); + params: no_params, + query: no_query, + limits: no_statement_limits, + }; + let statements: [SqliteStatement] = [statement]; + sqlite::transaction(&db, statements); sqlite::close(db); "#, ) - .expect("present Null on optional sqlite fields should compile"); + .expect("explicit None logical Option fields should compile"); } #[test] @@ -601,9 +800,11 @@ fn next_cursor_field_access_compiles() { compile( r#" use sqlite; - let db = sqlite::open({ path: ":memory:", mode: "memory", limits: {} }); + let options: SqliteOpenOptions = { path: Option::Some(":memory:"), mode: Option::Some("memory") }; + let db = sqlite::open(options); sqlite::execute(&db, "CREATE TABLE t (a INTEGER)", []); - let queried = sqlite::query(&db, "SELECT a FROM t", [], {}); + let limits: SqliteLimits = {}; + let queried = sqlite::query(&db, "SELECT a FROM t", [], limits); let cursor = queried.next_cursor; sqlite::close(db); "#, @@ -616,28 +817,282 @@ fn runtime_null_optional_fields_and_next_cursor_field_access() { let compiled = compile_standard( r#" use sqlite; - let db = sqlite::open({ - path: ":memory:", - mode: "memory", - root: null, - limits: null, - }); + let no_root: Option = Option::None; + let no_open_limits: Option = Option::None; + let options: SqliteOpenOptions = { + path: Option::Some(":memory:"), + mode: Option::Some("memory"), + root: no_root, + limits: no_open_limits, + }; + let db = sqlite::open(options); sqlite::execute(&db, "CREATE TABLE t (a INTEGER)", []); - let empty = sqlite::query(&db, "SELECT a FROM t", [], { max_rows: null }); - let empty_cursor = empty.next_cursor; - sqlite::transaction(&db, [{ + let no_max_rows: Option = Option::None; + let empty_limits: SqliteLimits = { max_rows: no_max_rows }; + let empty = sqlite::query(&db, "SELECT a FROM t", [], empty_limits); + let empty_cursor = match empty.next_cursor { + Option::Some(value) => value, + Option::None => -1, + }; + assert(empty_cursor == -1); + let no_params: Option<[SqliteValue]> = Option::None; + let no_query: Option = Option::None; + let no_statement_limits: Option = Option::None; + let statement: SqliteStatement = { sql: "INSERT INTO t VALUES (9)", - params: null, - query: null, - limits: null, - }]); - let queried = sqlite::query(&db, "SELECT a FROM t", [], {}); - let cursor = queried.next_cursor; + params: no_params, + query: no_query, + limits: no_statement_limits, + }; + let statements: [SqliteStatement] = [statement]; + sqlite::transaction(&db, statements); + let queried_limits: SqliteLimits = {}; + let queried = sqlite::query(&db, "SELECT a FROM t", [], queried_limits); + let cursor = match queried.next_cursor { + Option::Some(value) => value, + Option::None => -2, + }; + assert(cursor == 9); sqlite::close(db); - empty_cursor; - cursor; + empty_cursor + cursor; "#, ) .expect("null optional fields and next_cursor access should compile"); run_compiled_sqlite(compiled); } + +// ------------------------------------------- public field Option completion + +fn schema_contains_legacy_optional(schema: &HostTypeSchema) -> bool { + match schema { + HostTypeSchema::Optional(_) => true, + HostTypeSchema::Array(inner) + | HostTypeSchema::Option(inner) + | HostTypeSchema::Map(inner) => schema_contains_legacy_optional(inner), + HostTypeSchema::Named { fields, .. } => fields + .iter() + .any(|field| schema_contains_legacy_optional(&field.ty)), + HostTypeSchema::Callable { params, result } => { + params.iter().any(schema_contains_legacy_optional) + || schema_contains_legacy_optional(result) + } + _ => false, + } +} + +#[test] +fn sqlite_public_optional_fields_are_all_logical_option() { + let catalog = sqlite_host_catalog(); + let limits = catalog.struct_named("SqliteLimits").expect("SqliteLimits"); + for field in &limits.fields { + assert_eq!( + field.ty, + option(HostTypeSchema::Int), + "SqliteLimits.{} must be a logical Option", + field.name + ); + } + + let open = catalog + .struct_named("SqliteOpenOptions") + .expect("SqliteOpenOptions"); + for (index, expected) in [ + option(HostTypeSchema::String), + option(HostTypeSchema::String), + option(HostTypeSchema::String), + option(named("SqliteLimits", limits_fields())), + ] + .into_iter() + .enumerate() + { + assert_eq!( + open.fields[index].ty, expected, + "SqliteOpenOptions field {index}" + ); + } + + let value = catalog.struct_named("SqliteValue").expect("SqliteValue"); + for (index, expected) in [ + option(HostTypeSchema::Int), + option(HostTypeSchema::Float), + option(HostTypeSchema::String), + option(HostTypeSchema::Bytes), + ] + .into_iter() + .enumerate() + { + assert_eq!( + value.fields[index + 1].ty, + expected, + "SqliteValue payload field {index}" + ); + } + + let statement = catalog + .struct_named("SqliteStatement") + .expect("SqliteStatement"); + assert_eq!( + statement.fields[1].ty, + option(array(named("SqliteValue", sqlite_value_fields()))), + "SqliteStatement.params must be a logical Option" + ); + assert_eq!( + statement.fields[2].ty, + option(HostTypeSchema::Bool), + "SqliteStatement.query must be a logical Option" + ); + assert_eq!( + statement.fields[3].ty, + option(named("SqliteLimits", limits_fields())), + "SqliteStatement.limits must be a logical Option" + ); + + for function in catalog.functions() { + assert!( + !schema_contains_legacy_optional(&function.return_type) + && function + .params + .iter() + .all(|param| !schema_contains_legacy_optional(¶m.ty)), + "{} must not keep a public legacy Optional field or result", + function.name + ); + } +} + +#[test] +fn logical_option_configuration_and_value_consumers_run() { + let compiled = compile_standard( + r#" + use sqlite; + let open_limits: SqliteLimits = { max_rows: Option::Some(8) }; + let options: SqliteOpenOptions = { + path: Option::Some(":memory:"), + mode: Option::Some("memory"), + limits: Option::Some(open_limits), + }; + let db = sqlite::open(options); + sqlite::execute(&db, "CREATE TABLE t (i INTEGER, f REAL, s TEXT, z INTEGER)", []); + let insert_params: [SqliteValue] = [ + { kind: "int", int_value: Option::Some(7) }, + { kind: "float", float_value: Option::Some(1.5) }, + { kind: "text", text_value: Option::Some("") }, + { kind: "null" } + ]; + sqlite::execute( + &db, + "INSERT INTO t (i, f, s, z) VALUES (?1, ?2, ?3, ?4)", + insert_params + ); + let query_limits: SqliteLimits = { max_rows: Option::Some(8) }; + let rows = sqlite::query(&db, "SELECT i, f, s, z FROM t", [], query_limits); + let int_cell = rows.rows.copy()[0].cells.copy()[0]; + let int_value = match int_cell.int_value { + Option::Some(value) => value, + Option::None => -1, + }; + let int_float_absent = match int_cell.float_value { + Option::Some(_) => false, + Option::None => true, + }; + let float_cell = rows.rows.copy()[0].cells.copy()[1]; + let float_value = match float_cell.float_value { + Option::Some(value) => value, + Option::None => -1.0, + }; + let text_cell = rows.rows.copy()[0].cells.copy()[2]; + let text_value = match text_cell.text_value { + Option::Some(value) => value, + Option::None => "missing", + }; + let null_cell = rows.rows.copy()[0].cells.copy()[3]; + let null_absent = match null_cell.int_value { + Option::Some(_) => false, + Option::None => true, + }; + assert(int_value == 7 && int_float_absent); + assert(float_value == 1.5); + assert(text_value == ""); + assert(null_cell.kind == "null" && null_absent); + let insert_statement_params: [SqliteValue] = [{ kind: "int", int_value: Option::Some(8) }]; + let empty_params: [SqliteValue] = []; + let statement_limits: SqliteLimits = { max_rows: Option::Some(8) }; + let insert_statement: SqliteStatement = { + sql: "INSERT INTO t (i) VALUES (?1)", + params: Option::Some(insert_statement_params), + query: Option::Some(false), + }; + let query_statement: SqliteStatement = { + sql: "SELECT i FROM t", + params: Option::Some(empty_params), + query: Option::Some(true), + limits: Option::Some(statement_limits), + }; + let default_statement: SqliteStatement = { sql: "INSERT INTO t (i) VALUES (9)" }; + let statements: [SqliteStatement] = [insert_statement, query_statement, default_statement]; + let results = sqlite::transaction(&db, statements); + assert(results.copy()[0].kind == "execute"); + assert(results.copy()[1].kind == "query"); + assert(results.copy()[2].kind == "execute"); + let present = match results.copy()[1].query { + Option::Some(result) => result.rows.length, + Option::None => 0, + }; + assert(present == 2); + sqlite::close(db); + "#, + ) + .expect("logical Option sqlite consumers should compile"); + run_compiled_sqlite(compiled); +} + +#[test] +fn legacy_scalar_and_physical_null_public_fields_are_rejected() { + for source in [ + r#"use sqlite; sqlite::open({ path: ":memory:", mode: Option::Some("memory") });"#, + r#"use sqlite; sqlite::open({ path: Option::Some(":memory:"), mode: ":memory:" });"#, + r#"use sqlite; let db = sqlite::open({ path: Option::Some(":memory:"), mode: Option::Some("memory") }); sqlite::execute(&db, "SELECT ?", [{ kind: "int", int_value: 7, float_value: Option::None, text_value: Option::None, blob_value: Option::None }]);"#, + r#"use sqlite; let db = sqlite::open({ path: Option::Some(":memory:"), mode: Option::Some("memory") }); sqlite::query(&db, "SELECT 1", [], { max_rows: 8 });"#, + ] { + let message = compile_err(source); + assert!( + message.contains("sqlite") && message.contains("expected"), + "legacy field reading must be rejected, got {message}" + ); + } + let null_literal = r#"use sqlite; let db = sqlite::open({ path: Option::Some(":memory:"), mode: Option::Some("memory") }); sqlite::execute(&db, "SELECT ?", [{ kind: "int", int_value: null, float_value: Option::None, text_value: Option::None, blob_value: Option::None }]);"#; + let null_message = compile_err(null_literal); + assert!( + null_message.contains("RSS rejects null literals"), + "a null literal is rejected before a legacy sqlite field, got {null_message}" + ); +} + +#[test] +fn logical_option_limit_zero_is_a_provided_invalid_value() { + let compiled = compile_standard( + r#" + let options: SqliteOpenOptions = { + path: Option::Some(":memory:"), + mode: Option::Some("memory"), + }; + let db = sqlite::open(options); + let limits: SqliteLimits = { max_rows: Option::Some(0) }; + sqlite::query(&db, "SELECT 1", [], limits); + "#, + ) + .expect("the explicit Some(0) limit should compile"); + let message = run_compiled_sqlite_host_error(compiled); + assert!( + message.contains("max_rows must be positive"), + "Some(0) must be a provided invalid limit rather than the default, got {message}" + ); +} + +#[test] +fn bundled_sqlite_named_struct_example_compiles_with_standard_catalog() { + let source = include_str!("../examples/sqlite_named_structs.rss"); + compile_standard(source).unwrap_or_else(|error| { + panic!("examples/sqlite_named_structs.rss should compile with the sqlite catalog: {error}") + }); +} diff --git a/tests/standard_host_descriptor_arch_tests.rs b/tests/standard_host_descriptor_arch_tests.rs index 3a799e3b..395c58dd 100644 --- a/tests/standard_host_descriptor_arch_tests.rs +++ b/tests/standard_host_descriptor_arch_tests.rs @@ -24,17 +24,17 @@ const HTTP_SURFACE_ENABLED: bool = cfg!(all(feature = "http-client", not(target_ // Current descriptor identities, selected for the actual native feature set. const SQLITE_SURFACE_ENABLED: bool = cfg!(all(feature = "sqlite", not(target_family = "wasm"))); -const STANDARD_CATALOG_FINGERPRINT_HTTP_SQLITE: &str = "21d1dc351d613184"; -const STANDARD_CATALOG_FINGERPRINT_SQLITE: &str = "8afd8a69c58f02bd"; -const STANDARD_CATALOG_FINGERPRINT_HTTP: &str = "7a89a8dee2df6de7"; +const STANDARD_CATALOG_FINGERPRINT_HTTP_SQLITE: &str = "b704c1210e3ab4a4"; +const STANDARD_CATALOG_FINGERPRINT_SQLITE: &str = "6cad8e423debdebd"; +const STANDARD_CATALOG_FINGERPRINT_HTTP: &str = "c9e705595cfd2cc7"; const STANDARD_CATALOG_FINGERPRINT_BASE: &str = "8254b00d727494b4"; const IO_CATALOG_FINGERPRINT: &str = "a16730bd11bf5e10"; #[cfg(all(feature = "sqlite", not(target_family = "wasm")))] -const SQLITE_CATALOG_FINGERPRINT: &str = "dce61460a46c421a"; +const SQLITE_CATALOG_FINGERPRINT: &str = "740ab59ccb8fd09a"; const JIT_CATALOG_FINGERPRINT: &str = "ae81318e8a018681"; const TIMER_CATALOG_FINGERPRINT: &str = "7ecc0517cea3570b"; #[cfg(all(feature = "http-client", not(target_family = "wasm")))] -const HTTP_CATALOG_FINGERPRINT: &str = "642cb941ab966eb8"; +const HTTP_CATALOG_FINGERPRINT: &str = "41e6557a7dd03118"; fn standard_catalog_fingerprint() -> &'static str { match (HTTP_SURFACE_ENABLED, SQLITE_SURFACE_ENABLED) { (true, true) => STANDARD_CATALOG_FINGERPRINT_HTTP_SQLITE, diff --git a/tests/support/legacy_nullable_plugin.rs b/tests/support/legacy_nullable_plugin.rs new file mode 100644 index 00000000..afa6f944 --- /dev/null +++ b/tests/support/legacy_nullable_plugin.rs @@ -0,0 +1,103 @@ +//! Shared test plugin for fixtures that still exercise the pre-Option nullable +//! VM contract. This is not a product language mode: RustScript itself +//! continues to reject legacy null. + +use std::sync::Arc; + +use vm::{ + CompileSourceFileOptions, FrontendId, FrontendIr, FrontendTypingPolicy, ParseError, + ParserDialect, SharedParserOptions, SourceFlavor, SourceFrontendContext, + SourceFrontendDescriptor, SourcePlugin, parse_source_with_dialect_and_catalog, +}; + +struct LegacyNullableDialect; + +impl ParserDialect for LegacyNullableDialect { + fn is_import_keyword(&self, ident: &str) -> bool { + ident == "import" + } + + fn allow_let_mut_binding(&self) -> bool { + true + } + + fn allow_macro_calls(&self) -> bool { + true + } + + fn allow_plus_equal_operator(&self) -> bool { + true + } + + fn allow_for_in_loop(&self) -> bool { + true + } + + fn allow_result_syntax(&self) -> bool { + true + } + + fn allow_option_syntax(&self) -> bool { + true + } + + fn allow_result_try(&self) -> bool { + true + } + + fn reject_legacy_null(&self) -> bool { + false + } +} + +static LEGACY_NULLABLE_DIALECT: LegacyNullableDialect = LegacyNullableDialect; +static LEGACY_NULLABLE_PLUGIN: LegacyNullablePlugin = LegacyNullablePlugin; + +struct LegacyNullablePlugin; + +impl SourcePlugin for LegacyNullablePlugin { + fn descriptor(&self) -> SourceFrontendDescriptor { + SourceFrontendDescriptor { + id: FrontendId::new("legacy-nullable-fixture"), + extensions: &["rss"], + typing_policy: FrontendTypingPolicy::Strict, + supports_ownership_checking: true, + } + } + + fn parser_dialect(&self) -> Option<&'static dyn ParserDialect> { + Some(&LEGACY_NULLABLE_DIALECT) + } + + fn parse_source( + &self, + source: &str, + context: &SourceFrontendContext<'_>, + ) -> Result { + parse_source_with_dialect_and_catalog( + source, + &LEGACY_NULLABLE_DIALECT, + SharedParserOptions { + source_id: context.source_id, + allow_implicit_externs: true, + allow_implicit_semicolons: false, + enforce_mutable_bindings: true, + import_scan_mode: false, + }, + context + .host_api_catalog + .map(|catalog| Arc::new(catalog.clone())), + ) + } +} + +pub(crate) fn legacy_nullable_flavor() -> SourceFlavor { + SourceFlavor::Plugin(LEGACY_NULLABLE_PLUGIN.descriptor().id) +} + +pub(crate) fn legacy_nullable_options( + mut options: CompileSourceFileOptions, +) -> CompileSourceFileOptions { + options.add_source_plugin(&LEGACY_NULLABLE_PLUGIN); + options +} diff --git a/tests/timer_host_arch_tests.rs b/tests/timer_host_arch_tests.rs index eca62a3b..1b27ea83 100644 --- a/tests/timer_host_arch_tests.rs +++ b/tests/timer_host_arch_tests.rs @@ -15,6 +15,8 @@ use std::fs; use std::path::{Path, PathBuf}; +use vm::HostTypeSchema; + /// The single file that owns every timer implementation symbol. const TIMER_HOST_MODULE: &str = "src/builtins/runtime/timer.rs"; @@ -531,3 +533,91 @@ fn architecture_scanner_ignores_literals_and_comment_markers_inside_them() { assert!(!code.contains("max_running")); assert!(!code.contains("poll_reporting")); } + +// --------------------------------------------- Option/Unit public contract + +fn assert_timer_no_nullable_or_void(path: &str, schema: &HostTypeSchema) { + match schema { + HostTypeSchema::Optional(_) | HostTypeSchema::Option(_) => { + panic!("timer public schema {path} exposes a nullable route: {schema:?}") + } + HostTypeSchema::Null | HostTypeSchema::Unit => { + panic!("timer public schema {path} exposes a void/null route: {schema:?}") + } + HostTypeSchema::Array(inner) => { + assert_timer_no_nullable_or_void(&format!("{path}[]"), inner) + } + HostTypeSchema::Map(inner) => { + assert_timer_no_nullable_or_void(&format!("{path}{{}}"), inner) + } + HostTypeSchema::Callable { params, result } => { + for (index, param) in params.iter().enumerate() { + assert_timer_no_nullable_or_void(&format!("{path}.callback_param[{index}]"), param); + } + // The standard callback result is the accepted discarded-callable + // exception; it is not a nullable or void route. + if !matches!(result.as_ref(), HostTypeSchema::Unknown) { + assert_timer_no_nullable_or_void(&format!("{path}.callback_result"), result); + } + } + HostTypeSchema::Result { ok, err } => { + assert_timer_no_nullable_or_void(&format!("{path}.ok"), ok); + assert_timer_no_nullable_or_void(&format!("{path}.err"), err); + } + HostTypeSchema::Named { fields, .. } => { + for field in fields { + assert_timer_no_nullable_or_void(&format!("{path}.{}", field.name), &field.ty); + } + } + HostTypeSchema::Unknown + | HostTypeSchema::Int + | HostTypeSchema::Float + | HostTypeSchema::Number + | HostTypeSchema::Bool + | HostTypeSchema::String + | HostTypeSchema::Bytes + | HostTypeSchema::Resource(_) => {} + } +} + +#[test] +fn timer_public_contract_keeps_boolean_and_count_results_without_nullable_routes() { + let catalog = vm::timer_host_catalog(); + for function in catalog.functions() { + for param in &function.params { + assert_timer_no_nullable_or_void( + &format!("{}({})", function.name, param.name), + ¶m.ty, + ); + } + assert_timer_no_nullable_or_void( + &format!("{} return", function.name), + &function.return_type, + ); + } + + let at = catalog.function("timer::at").expect("timer::at"); + assert_eq!(at.return_type, HostTypeSchema::Bool); + assert_eq!( + at.params[1].ty, + HostTypeSchema::Callable { + params: vec![HostTypeSchema::Bool], + result: Box::new(HostTypeSchema::Unknown), + }, + "the discarded callback result stays the documented narrow exception" + ); + assert_eq!( + catalog + .function("timer::pending_count") + .expect("timer::pending_count") + .return_type, + HostTypeSchema::Int + ); + assert_eq!( + catalog + .function("timer::every") + .expect("timer::every") + .return_type, + HostTypeSchema::Bool + ); +} diff --git a/tests/timer_host_tests.rs b/tests/timer_host_tests.rs index 5784705b..5e000532 100644 --- a/tests/timer_host_tests.rs +++ b/tests/timer_host_tests.rs @@ -319,12 +319,6 @@ impl HostAsyncBridge for ControlledBridge { } } -struct NoopWake; - -impl std::task::Wake for NoopWake { - fn wake(self: Arc) {} -} - struct AdmissionBackend { barrier: Barrier, registrations: Mutex>, @@ -513,7 +507,7 @@ impl vm::HostExtension for TimerTestExtension { HostImportSchema { name: "test::wait".to_string(), params: Vec::new(), - return_type: HostTypeSchema::Unknown, + return_type: HostTypeSchema::Bool, fingerprint: self.catalog.fingerprint(), }, |vm, _args| { @@ -528,7 +522,7 @@ impl vm::HostExtension for TimerTestExtension { HostImportSchema { name: "test::panic".to_string(), params: Vec::new(), - return_type: HostTypeSchema::Unknown, + return_type: HostTypeSchema::Bool, fingerprint: self.catalog.fingerprint(), }, panic_host, @@ -543,7 +537,7 @@ impl vm::HostExtension for TimerTestExtension { schema: HostTypeSchema::Int, passing: HostParamPassing::Value, }], - return_type: HostTypeSchema::Unknown, + return_type: HostTypeSchema::Unit, fingerprint: self.catalog.fingerprint(), }, move || -> Box { @@ -560,7 +554,7 @@ impl vm::HostExtension for TimerTestExtension { HostImportSchema { name: "test::probe".to_string(), params: Vec::new(), - return_type: HostTypeSchema::Unknown, + return_type: HostTypeSchema::Unit, fingerprint: self.catalog.fingerprint(), }, move || -> Box { @@ -591,25 +585,27 @@ fn test_wait_catalog() -> Arc { for function in standard.functions() { builder.function(function.clone()); } + // These custom hosts are not the trusted timer callback-result exception. + // wait/panic are consumed as bool; record/probe are void Unit calls. builder.function(HostFunctionSchema::with_return( "test::wait", Vec::new(), - HostTypeSchema::Unknown, + HostTypeSchema::Bool, )); builder.function(HostFunctionSchema::with_return( "test::panic", Vec::new(), - HostTypeSchema::Unknown, + HostTypeSchema::Bool, )); builder.function(HostFunctionSchema::with_return( "test::record", vec![HostParamSchema::value("value", HostTypeSchema::Int)], - HostTypeSchema::Unknown, + HostTypeSchema::Unit, )); builder.function(HostFunctionSchema::with_return( "test::probe", Vec::new(), - HostTypeSchema::Unknown, + HostTypeSchema::Unit, )); Arc::new(builder.build().expect("test host catalog")) } @@ -689,7 +685,7 @@ fn run_with_probe( #[test] fn at_compile_contract_uses_callable_take_owned() { - let compiled = compile_standard("use timer; timer::at(10, |premature| null);") + let compiled = compile_standard("use timer; timer::at(10, |premature| ());") .expect("timer::at must compile"); let index = compiled .program @@ -708,7 +704,7 @@ fn at_compile_contract_uses_callable_take_owned() { } /// The typed callback surface declares its result as `unknown` because the -/// module discards it: an `int`/`string`/`map`/`bool`/`null` callback body all +/// module discards it: an `int`/`string`/`map`/`bool`/`()` callback body all /// compile against the standard catalog and run to completion, and the /// discarded value never reaches the guest. #[test] @@ -718,7 +714,7 @@ fn callbacks_with_any_declared_result_compile_and_discard_their_result() { ("string", "\"text\""), ("map", "{a: 1}"), ("bool", "true"), - ("null", "null"), + ("unit", "()"), ] { let backend = Arc::new(ManualBackend::default()); let mut vm = run_with_backend( @@ -749,7 +745,7 @@ fn callbacks_with_any_declared_result_compile_and_discard_their_result() { fn at_registers_and_returns_then_callback_runs() { let backend = Arc::new(ManualBackend::default()); let mut vm = run_with_backend( - "use timer; timer::at(25, |premature| null);", + "use timer; timer::at(25, |premature| ());", Arc::clone(&backend), TimerConfig::default(), ) @@ -772,7 +768,7 @@ fn at_registers_and_returns_then_callback_runs() { fn at_callback_observes_a_non_premature_round() { let backend = Arc::new(ManualBackend::default()); run_with_backend( - "use timer; timer::at(10, |premature| if true => { assert(!premature); null } else => { null });", + "use timer; timer::at(10, |premature| if true => { assert(!premature); () } else => { () });", Arc::clone(&backend), TimerConfig::default(), ) @@ -803,7 +799,7 @@ fn default_limits_match_the_documented_contract() { let backend = Arc::new(ManualBackend::default()); run_with_backend( - "use timer; timer::at(1, |premature| null);", + "use timer; timer::at(1, |premature| ());", Arc::clone(&backend), config, ) @@ -817,7 +813,7 @@ fn default_limits_match_the_documented_contract() { fn every_marks_interval_and_preserves_callable_between_rounds() { let backend = Arc::new(ManualBackend::default()); run_with_backend( - "use timer; timer::every(7, |premature| null);", + "use timer; timer::every(7, |premature| ());", Arc::clone(&backend), TimerConfig::default(), ) @@ -840,7 +836,7 @@ fn every_marks_interval_and_preserves_callable_between_rounds() { fn every_preserves_captured_callable_graph_across_rounds() { let backend = Arc::new(ManualBackend::default()); let source_vm = run_with_backend( - "use timer; let expected = false; timer::every(1, |premature| if true => { assert(expected == premature); null } else => { null });", + "use timer; let expected = false; timer::every(1, |premature| if true => { assert(expected == premature); () } else => { () });", Arc::clone(&backend), TimerConfig::default(), ) @@ -865,7 +861,7 @@ fn every_preserves_captured_callable_graph_across_rounds() { fn every_rounds_share_one_mutable_capture_cell() { let backend = Arc::new(ManualBackend::default()); let (_vm, recorded) = run_with_test_recorder( - "use test;\nuse timer;\nlet mut rounds = 0;\ntimer::every(5, |premature| if true => { rounds = rounds + 1; test::record(rounds); null } else => { test::record(rounds); null });\n", + "use test;\nuse timer;\nlet mut rounds = 0;\ntimer::every(5, |premature| if true => { rounds = rounds + 1; test::record(rounds); () } else => { test::record(rounds); () });\n", Arc::clone(&backend), TimerConfig::default(), ) @@ -890,7 +886,7 @@ fn counts_are_synchronous_and_reflect_backend_state() { let backend = Arc::new(ManualBackend::default()); *backend.running.lock().expect("running") = 3; let vm = run_with_backend( - "use timer; timer::at(1, |premature| null); timer::pending_count() + timer::running_count();", + "use timer; timer::at(1, |premature| ()); timer::pending_count() + timer::running_count();", Arc::clone(&backend), TimerConfig::default(), ) @@ -901,8 +897,8 @@ fn counts_are_synchronous_and_reflect_backend_state() { #[test] fn invalid_durations_and_pending_limit_are_rejected() { for source in [ - "use timer; let callback = |premature| null; timer::at(-1, callback);", - "use timer; let callback = |premature| null; timer::every(0, callback);", + "use timer; let callback = |premature| (); timer::at(-1, callback);", + "use timer; let callback = |premature| (); timer::every(0, callback);", ] { let backend = Arc::new(ManualBackend::default()); let mut vm = build_vm_with_backend(source, &backend, TimerConfig::default()) @@ -925,7 +921,7 @@ fn invalid_durations_and_pending_limit_are_rejected() { let backend = Arc::new(ManualBackend::default()); run_with_backend( - "use timer; timer::at(1, |premature| null);", + "use timer; timer::at(1, |premature| ());", Arc::clone(&backend), TimerConfig { max_pending: 1, @@ -934,7 +930,7 @@ fn invalid_durations_and_pending_limit_are_rejected() { ) .expect("first timer accepted"); let mut vm = build_vm_with_backend( - "use timer; let callback = |premature| null; timer::at(2, callback);", + "use timer; let callback = |premature| (); timer::at(2, callback);", &backend, TimerConfig { max_pending: 1, @@ -959,7 +955,7 @@ fn invalid_durations_and_pending_limit_are_rejected() { fn backend_rejection_restores_callback_argument_and_allows_shutdown() { let backend = Arc::new(RejectingBackend::default()); let mut vm = build_vm_with_backend( - "use timer; timer::at(1, |premature| null);", + "use timer; timer::at(1, |premature| ());", &backend, TimerConfig::default(), ) @@ -981,7 +977,7 @@ fn backend_rejection_restores_callback_argument_and_allows_shutdown() { fn backend_panic_restores_callback_argument_before_unwinding() { let backend = Arc::new(PanickingBackend::default()); let mut vm = build_vm_with_backend( - "use timer; timer::at(1, |premature| null);", + "use timer; timer::at(1, |premature| ());", &backend, TimerConfig::default(), ) @@ -1009,7 +1005,7 @@ fn scheduler_leaves_due_callback_pending_until_running_slot_is_free() { max_running: 1, }; run_with_test_wait_backend( - "use test;\nuse timer;\nfn wait() -> bool { test::wait() }\ntimer::every(1, |premature| if true => { wait(); null } else => { null });\ntimer::every(1, |premature| if true => { wait(); null } else => { null });\n", + "use test;\nuse timer;\nfn wait() -> bool { test::wait() }\ntimer::every(1, |premature| if true => { wait(); () } else => { () });\ntimer::every(1, |premature| if true => { wait(); () } else => { () });\n", Arc::clone(&backend), config, ) @@ -1048,7 +1044,7 @@ fn scheduler_leaves_due_callback_pending_until_running_slot_is_free() { assert_eq!(backend.running_count(), 1); first_state.lock().expect("first bridge state").complete = true; - let waker = std::task::Waker::from(Arc::new(NoopWake)); + let waker = std::task::Waker::noop().clone(); let mut cx = Context::from_waker(&waker); assert!(matches!( backend.poll_active(0, &mut cx), @@ -1074,7 +1070,7 @@ fn scheduler_leaves_due_callback_pending_until_running_slot_is_free() { fn scheduler_survives_callback_panic_and_repeats_after_recovery() { let backend = Arc::new(LimitedSchedulerBackend::new(1)); run_with_test_wait_backend( - "use test;\nuse timer;\nfn invoke_panic() -> bool { test::panic() }\ntimer::every(1, |premature| if true => { invoke_panic(); null } else => { null });\n", + "use test;\nuse timer;\nfn invoke_panic() -> bool { test::panic() }\ntimer::every(1, |premature| if true => { invoke_panic(); () } else => { () });\n", Arc::clone(&backend), TimerConfig { max_pending: 4, @@ -1104,7 +1100,7 @@ fn scheduler_survives_callback_panic_and_repeats_after_recovery() { fn backend_installs_independent_bridges_for_waiting_timer_callbacks() { let backend = Arc::new(ManualBackend::default()); run_with_test_wait_backend( - "use test;\nuse timer;\nfn wait() -> bool { test::wait() }\ntimer::at(1, |premature| if true => { wait(); null } else => { null });\ntimer::at(1, |premature| if true => { wait(); null } else => { null });\n", + "use test;\nuse timer;\nfn wait() -> bool { test::wait() }\ntimer::at(1, |premature| if true => { wait(); () } else => { () });\ntimer::at(1, |premature| if true => { wait(); () } else => { () });\n", Arc::clone(&backend), TimerConfig::default(), ) @@ -1135,7 +1131,7 @@ fn backend_installs_independent_bridges_for_waiting_timer_callbacks() { vm::TimerCallbackStatus::Waiting(_) )); - let waker = std::task::Waker::from(Arc::new(NoopWake)); + let waker = std::task::Waker::noop().clone(); let mut cx = Context::from_waker(&waker); let first_poll = first.callback.poll(&mut cx); assert!(matches!(first_poll, Poll::Pending), "{first_poll:?}"); @@ -1160,7 +1156,7 @@ fn backend_installs_independent_bridges_for_waiting_timer_callbacks() { fn backend_cancellation_of_waiting_timer_calls_bridge_once() { let backend = Arc::new(ManualBackend::default()); run_with_test_wait_backend( - "use test;\nuse timer;\nfn wait() -> bool { test::wait() }\ntimer::at(1, |premature| if true => { wait(); null } else => { null });\n", + "use test;\nuse timer;\nfn wait() -> bool { test::wait() }\ntimer::at(1, |premature| if true => { wait(); () } else => { () });\n", Arc::clone(&backend), TimerConfig::default(), ) @@ -1189,7 +1185,7 @@ fn backend_cancellation_of_waiting_timer_calls_bridge_once() { fn callback_errors_go_to_backend_sink_without_touching_source_vm() { let backend = Arc::new(ManualBackend::default()); let source_vm = run_with_backend( - "use timer; timer::at(1, |premature| if true => { let unused = 1 / 0; null } else => { null });", + "use timer; timer::at(1, |premature| if true => { let unused = 1 / 0; () } else => { () });", Arc::clone(&backend), TimerConfig::default(), ) @@ -1211,7 +1207,7 @@ fn callback_errors_go_to_backend_sink_without_touching_source_vm() { fn every_callback_can_report_each_round_after_an_error() { let backend = Arc::new(ManualBackend::default()); run_with_backend( - "use timer; timer::every(1, |premature| if true => { let unused = 1 / 0; null } else => { null });", + "use timer; timer::every(1, |premature| if true => { let unused = 1 / 0; () } else => { () });", Arc::clone(&backend), TimerConfig::default(), ) @@ -1237,7 +1233,7 @@ fn every_callback_can_report_each_round_after_an_error() { fn source_vm_drop_leaves_pending_callbacks_until_explicit_shutdown() { let backend = Arc::new(ShutdownBackend::default()); let vm = run_with_backend( - "use timer; timer::at(100, |premature| if true => { assert(premature); null } else => { null });", + "use timer; timer::at(100, |premature| if true => { assert(premature); () } else => { () });", Arc::clone(&backend), TimerConfig::default(), ) @@ -1259,7 +1255,7 @@ fn source_vm_drop_leaves_pending_callbacks_until_explicit_shutdown() { fn every_shutdown_runs_once_premature_and_stops_rescheduling() { let backend = Arc::new(ShutdownBackend::default()); run_with_backend( - "use timer; timer::every(50, |premature| if true => { assert(premature); null } else => { null });", + "use timer; timer::every(50, |premature| if true => { assert(premature); () } else => { () });", Arc::clone(&backend), TimerConfig::default(), ) @@ -1289,8 +1285,8 @@ fn concurrent_backend_admission_allows_only_the_configured_pending_count() { }; let mut handles = Vec::new(); for source in [ - "use timer; timer::at(1, |premature| null);", - "use timer; timer::at(2, |premature| null);", + "use timer; timer::at(1, |premature| ());", + "use timer; timer::at(2, |premature| ());", ] { let backend = Arc::clone(&backend); handles.push(std::thread::spawn(move || { @@ -1312,7 +1308,7 @@ fn concurrent_backend_admission_allows_only_the_configured_pending_count() { fn cancellation_is_idempotent_and_blocks_restart() { let backend = Arc::new(ManualBackend::default()); run_with_backend( - "use timer; timer::at(1, |premature| null);", + "use timer; timer::at(1, |premature| ());", Arc::clone(&backend), TimerConfig::default(), ) @@ -1351,7 +1347,7 @@ fn expect_compile_code( fn timer_move_contract_rejects_reusing_a_moved_callback() { for register in ["timer::at(1, callback);", "timer::every(1, callback);"] { let source = format!( - "use timer;\nlet callback = |premature| null;\n{register}\ntimer::at(2, callback);\n" + "use timer;\nlet callback = |premature| ();\n{register}\ntimer::at(2, callback);\n" ); let error = expect_compile_code(compile_standard(&source), "E_LOCAL_MOVED"); if let SourceError::Parse(parse) = &error { @@ -1368,7 +1364,7 @@ fn timer_move_contract_rejects_reusing_a_moved_callback() { #[test] fn timer_calls_without_installed_runtime_state_report_missing_backend() { for source in [ - "use timer; timer::at(1, |premature| null);", + "use timer; timer::at(1, |premature| ());", "use timer; timer::pending_count();", "use timer; timer::running_count();", ] { @@ -1395,7 +1391,7 @@ fn timer_calls_without_installed_runtime_state_report_missing_backend() { fn clearing_the_timer_runtime_blocks_further_timer_calls() { let backend = Arc::new(ManualBackend::default()); let mut vm = build_vm_with_backend( - "use timer; timer::at(1, |premature| null);", + "use timer; timer::at(1, |premature| ());", &backend, TimerConfig::default(), ) @@ -1418,7 +1414,7 @@ fn clearing_the_timer_runtime_blocks_further_timer_calls() { fn timer_callback_with_a_dropped_backend_reports_shutdown() { let backend = Arc::new(ManualBackend::default()); let vm = run_with_backend( - "use timer;\nfn count() -> int { timer::pending_count() }\ntimer::at(1, |premature| if true => { count(); null } else => { null });\n", + "use timer;\nfn count() -> int { timer::pending_count() }\ntimer::at(1, |premature| if true => { count(); () } else => { () });\n", Arc::clone(&backend), TimerConfig::default(), ) @@ -1442,7 +1438,7 @@ fn timer_callback_with_a_dropped_backend_reports_shutdown() { fn drop_while_waiting_cancels_the_bridge_exactly_once() { let backend = Arc::new(ManualBackend::default()); run_with_test_wait_backend( - "use test;\nuse timer;\nfn wait() -> bool { test::wait() }\ntimer::at(1, |premature| if true => { wait(); null } else => { null });\n", + "use test;\nuse timer;\nfn wait() -> bool { test::wait() }\ntimer::at(1, |premature| if true => { wait(); () } else => { () });\n", Arc::clone(&backend), TimerConfig::default(), ) @@ -1477,7 +1473,7 @@ fn completed_callback_vm_is_released_exactly_once() { let counters = Arc::new(ProbeCounters::default()); let backend = Arc::new(ManualBackend::default()); let source_vm = run_with_probe( - "use test;\nuse timer;\ntimer::at(1, |premature| if true => { test::probe(); null } else => { test::probe(); null });\n", + "use test;\nuse timer;\ntimer::at(1, |premature| if true => { test::probe(); () } else => { test::probe(); () });\n", Arc::clone(&backend), TimerConfig::default(), Arc::clone(&counters), @@ -1531,7 +1527,7 @@ fn rejected_registration_releases_the_callback_vm_exactly_once() { let counters = Arc::new(ProbeCounters::default()); let backend = Arc::new(RejectingBackend::default()); let mut vm = build_vm_with_probe( - "use test;\nuse timer;\ntimer::at(1, |premature| if true => { test::probe(); null } else => { test::probe(); null });\n", + "use test;\nuse timer;\ntimer::at(1, |premature| if true => { test::probe(); () } else => { test::probe(); () });\n", &backend, TimerConfig::default(), Arc::clone(&counters), @@ -1566,7 +1562,7 @@ fn error_rounds_reuse_one_callback_vm_released_exactly_once() { let counters = Arc::new(ProbeCounters::default()); let backend = Arc::new(ManualBackend::default()); let source_vm = run_with_probe( - "use test;\nuse timer;\ntimer::every(1, |premature| if true => { test::probe(); let unused = 1 / 0; null } else => { null });\n", + "use test;\nuse timer;\ntimer::every(1, |premature| if true => { test::probe(); let unused = 1 / 0; () } else => { () });\n", Arc::clone(&backend), TimerConfig::default(), Arc::clone(&counters), @@ -1618,7 +1614,7 @@ fn cancelled_waiting_callback_releases_its_vm_exactly_once() { let counters = Arc::new(ProbeCounters::default()); let backend = Arc::new(ManualBackend::default()); let source_vm = run_with_probe( - "use test;\nuse timer;\nfn wait() -> bool { test::wait() }\ntimer::at(1, |premature| if true => { test::probe(); wait(); null } else => { null });\n", + "use test;\nuse timer;\nfn wait() -> bool { test::wait() }\ntimer::at(1, |premature| if true => { test::probe(); wait(); () } else => { () });\n", Arc::clone(&backend), TimerConfig::default(), Arc::clone(&counters), @@ -1888,7 +1884,7 @@ fn downstream_adapter_registers_through_the_shared_backend_path() { max_running: 3, }; let vm = run_with_vendor( - "use vendor;\nvendor::at(3, |premature| null);\n", + "use vendor;\nvendor::at(3, |premature| ());\n", Arc::clone(&backend), config, ) @@ -1914,7 +1910,7 @@ fn downstream_adapter_registers_through_the_shared_backend_path() { fn downstream_adapter_seconds_interval_repeats_on_the_same_owned_callback() { let backend = Arc::new(ManualBackend::default()); run_with_vendor( - "use vendor;\nlet mut rounds = 0;\nvendor::every(2, |premature| if true => { rounds = rounds + 1; null } else => { null });\n", + "use vendor;\nlet mut rounds = 0;\nvendor::every(2, |premature| if true => { rounds = rounds + 1; () } else => { () });\n", Arc::clone(&backend), TimerConfig::default(), ) @@ -1935,7 +1931,7 @@ fn downstream_adapter_seconds_interval_repeats_on_the_same_owned_callback() { fn downstream_adapter_rejects_a_negative_duration_before_the_handoff() { let backend = Arc::new(ManualBackend::default()); let mut vm = build_vm_with_vendor( - "use vendor;\nlet callback = |premature| null;\nvendor::at(-1, callback);\n", + "use vendor;\nlet callback = |premature| ();\nvendor::at(-1, callback);\n", &backend, TimerConfig::default(), ) @@ -1958,7 +1954,7 @@ fn downstream_adapter_rejects_a_negative_duration_before_the_handoff() { fn downstream_adapter_rejects_a_zero_repeating_interval() { let backend = Arc::new(ManualBackend::default()); let mut vm = build_vm_with_vendor( - "use vendor;\nlet callback = |premature| null;\nvendor::every(0, callback);\n", + "use vendor;\nlet callback = |premature| ();\nvendor::every(0, callback);\n", &backend, TimerConfig::default(), ) @@ -1975,7 +1971,7 @@ fn downstream_adapter_rejects_a_zero_repeating_interval() { fn downstream_adapter_backend_rejection_and_panic_restore_the_callback() { let backend = Arc::new(RejectingBackend::default()); let mut vm = build_vm_with_vendor( - "use vendor;\nvendor::at(1, |premature| null);\n", + "use vendor;\nvendor::at(1, |premature| ());\n", &backend, TimerConfig::default(), ) @@ -1992,7 +1988,7 @@ fn downstream_adapter_backend_rejection_and_panic_restore_the_callback() { let backend = Arc::new(PanickingBackend::default()); let mut vm = build_vm_with_vendor( - "use vendor;\nvendor::at(1, |premature| null);\n", + "use vendor;\nvendor::at(1, |premature| ());\n", &backend, TimerConfig::default(), ) @@ -2012,7 +2008,7 @@ fn downstream_adapter_backend_rejection_and_panic_restore_the_callback() { fn downstream_adapter_without_installed_runtime_reports_missing_backend() { let catalog = vendor_catalog(); let compiled = compile_source_with_flavor_and_options( - "use vendor;\nvendor::at(1, |premature| null);\n", + "use vendor;\nvendor::at(1, |premature| ());\n", SourceFlavor::RustScript, CompileSourceFileOptions::default().with_host_api_catalog(Arc::clone(&catalog)), ) @@ -2066,7 +2062,7 @@ fn downstream_count_functions_report_the_installed_backend_synchronously() { let backend = Arc::new(ManualBackend::default()); *backend.running.lock().expect("running") = 4; let vm = run_with_vendor( - "use vendor;\nvendor::at(1, |premature| null);\nvendor::pending() + vendor::running();\n", + "use vendor;\nvendor::at(1, |premature| ());\nvendor::pending() + vendor::running();\n", Arc::clone(&backend), TimerConfig::default(), ) diff --git a/tests/typed_host_no_dynamic_contract_tests.rs b/tests/typed_host_no_dynamic_contract_tests.rs index 1c42e322..0c64f268 100644 --- a/tests/typed_host_no_dynamic_contract_tests.rs +++ b/tests/typed_host_no_dynamic_contract_tests.rs @@ -43,6 +43,7 @@ fn visit(path: &str, schema: &HostTypeSchema, seen: &mut BTreeSet, polic } HostTypeSchema::Array(inner) => visit(&format!("{path}[]"), inner, seen, policy), HostTypeSchema::Optional(inner) => visit(&format!("{path}?"), inner, seen, policy), + HostTypeSchema::Option(inner) => visit(&format!("{path}.option"), inner, seen, policy), HostTypeSchema::Result { ok, err } => { visit(&format!("{path}.ok"), ok, seen, policy); visit(&format!("{path}.err"), err, seen, policy); @@ -79,6 +80,7 @@ fn visit(path: &str, schema: &HostTypeSchema, seen: &mut BTreeSet, polic visit(&format!("{path}.callback_result"), result, seen, policy); } HostTypeSchema::Null + | HostTypeSchema::Unit | HostTypeSchema::Bool | HostTypeSchema::Int | HostTypeSchema::Float diff --git a/tests/typed_json_source_boundary_tests.rs b/tests/typed_json_source_boundary_tests.rs new file mode 100644 index 00000000..767d7f58 --- /dev/null +++ b/tests/typed_json_source_boundary_tests.rs @@ -0,0 +1,146 @@ +#![cfg(feature = "runtime")] + +use vm::compile_source; + +fn untyped_json_rejection(source: &str) -> (String, Option) { + match compile_source(source) { + Ok(_) => panic!("untyped public JSON must not remain a RustScript source route: {source}"), + Err(vm::SourceError::Compile(vm::CompileError::StrictTypingRequired { + detail, + span, + .. + })) => (detail, span), + Err(error) => panic!("unexpected request rejection shape: {error}"), + } +} + +#[test] +fn rss_rejects_untyped_public_json_decode_with_call_span() { + let source = "use json; json::decode(\"null\");"; + let (detail, span) = untyped_json_rejection(source); + assert!( + detail.contains("typed JSON target"), + "untyped decode must fail at the typed boundary: {detail}" + ); + let span = span.expect("untyped decode must keep its parser-origin call span"); + assert_eq!(&source[span.lo..span.hi], "json::decode"); +} + +#[test] +fn rss_untyped_decode_null_cannot_reach_language_print() { + let source = "use json; print(json::decode(\"null\"));"; + let (detail, span) = untyped_json_rejection(source); + assert!( + detail.contains("typed JSON target"), + "raw decode must not flow into print: {detail}" + ); + let span = span.expect("untyped decode must keep its parser-origin call span"); + assert_eq!(&source[span.lo..span.hi], "json::decode"); +} + +#[test] +fn rss_untyped_public_json_encode_without_concrete_target_fails_explicitly() { + let source = "use json; let identity = |value| value; json::encode(identity);"; + let error = match compile_source(source) { + Ok(_) => panic!("untyped encode without a concrete target must fail explicitly: {source}"), + Err(error) => error.to_string(), + }; + assert!( + error.contains("cannot encode this value"), + "untyped encode without a concrete target must fail explicitly: {error}" + ); +} + +#[test] +fn rss_untyped_encode_of_dynamic_runtime_map_is_rejected_at_the_typed_boundary() { + let source = "use json; let payload: map = {}; json::encode(payload);"; + let (detail, span) = untyped_json_rejection(source); + assert!( + detail.contains("typed JSON target"), + "untyped runtime-map encode must fail at the typed boundary: {detail}" + ); + let span = span.expect("untyped encode must keep its parser-origin call span"); + assert_eq!(&source[span.lo..span.hi], "json::encode"); +} + +#[test] +fn typed_rss_decode_and_print_keep_logical_unit() { + let source = "use json;\nprint(json::decode::<()>(\"null\"));\n"; + let compiled = compile_source(source) + .unwrap_or_else(|error| panic!("typed decode should compile: {source}\n{error}")); + let mut machine = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + let lines = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let recorded = std::sync::Arc::clone(&lines); + machine.set_runtime_print_sink(move |rendered| { + recorded.lock().expect("sink").push(rendered); + }); + assert_eq!( + machine.run().expect("typed decode run"), + vm::VmStatus::Halted + ); + assert_eq!(lines.lock().expect("sink").as_slice(), ["()".to_string()]); +} + +#[test] +fn rss_public_json_function_value_syntax_is_not_a_source_route() { + // Current RustScript source accepts `json::decode` / `json::encode` only + // as namespaced calls. A first-class public JSON function value is not + // legal syntax, so no reference form can bypass the direct-call typed + // policy. + for member in ["decode", "encode"] { + let source = format!("use json;\nlet function = json::{member};\nfunction;\n"); + let error = match compile_source(&source) { + Ok(_) => panic!("public json function reference became a source route: {source}"), + Err(error) => error.to_string(), + }; + assert!( + error.contains("expected '(' after namespaced function name"), + "public json function reference must stay outside source syntax: {error}" + ); + } +} + +#[test] +fn rss_source_cannot_name_compiler_internal_json_helpers() { + for name in [ + "__json_decode_typed", + "__json_encode_typed", + "__logical_render", + ] { + let source = format!("let hidden = {name};\nhidden;\n"); + let error = match compile_source(&source) { + Ok(_) => panic!("compiler-internal helper '{name}' resolved as a source name"), + Err(error) => error.to_string(), + }; + assert!( + error.contains("unknown local"), + "compiler-internal helper '{name}' must not be a source name: {error}" + ); + } +} + +#[test] +fn rss_ordinary_builtin_and_user_underscore_function_values_remain_source_visible() { + let builtin = "let function = len;\nfunction([1, 2]);\n"; + let compiled = + compile_source(builtin).unwrap_or_else(|error| panic!("ordinary builtin ref: {error}")); + let mut machine = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + assert_eq!( + machine.run().expect("ordinary builtin ref run"), + vm::VmStatus::Halted + ); + assert_eq!(machine.stack().last(), Some(&vm::Value::Int(2))); + + let user = "fn _helper() -> int { 7 }\nlet function = _helper;\nfunction();\n"; + let compiled = + compile_source(user).unwrap_or_else(|error| panic!("user underscore ref: {error}")); + let mut machine = vm::Vm::new(compiled.program.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + assert_eq!( + machine.run().expect("user underscore ref run"), + vm::VmStatus::Halted + ); + assert_eq!(machine.stack().last(), Some(&vm::Value::Int(7))); +} diff --git a/tests/vm/drop_contract_tests.rs b/tests/vm/drop_contract_tests.rs index 3de88123..b370e289 100644 --- a/tests/vm/drop_contract_tests.rs +++ b/tests/vm/drop_contract_tests.rs @@ -673,7 +673,7 @@ fn reset_for_reuse_clears_all_locals_to_null() { let a = { name: "first" }; let b = [1, 2, 3]; let c = "hello"; - null; + (); "#; let compiled = compile_source(source).expect("compile should succeed"); let mut vm = new_drop_contract_vm(compiled.program); diff --git a/tests/vm/io_http_coexistence_tests.rs b/tests/vm/io_http_coexistence_tests.rs index f39f343a..2ba0f183 100644 --- a/tests/vm/io_http_coexistence_tests.rs +++ b/tests/vm/io_http_coexistence_tests.rs @@ -11,6 +11,8 @@ use vm::{ HostFunctionRegistry, HttpConfig, HttpHostExt, IoHostExt, IoPolicy, ResourceTypeKey, Value, Vm, VmResult, VmStatus, compile_source, register_http_builtin_module, standard_host_catalog, }; +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +use vm::{SqliteHostExt, SqlitePolicy, VmError}; #[path = "../support/http_test_bridge.rs"] mod http_test_bridge; @@ -433,3 +435,596 @@ fn standard_catalog_contains_io_and_http_surfaces() { ); } } + +// --------------------------------------------------------------------------- +// G3: IO + HTTP + SQLite in one VM, under the shared standard catalog. +// +// Everything below is compiled only when the SQLite domain is part of the +// feature union, so the HTTP-only target stays independently usable. The +// HTTP-only path above keeps proving IO/HTTP without SQLite; these cases add +// the genuine third domain rather than assuming an import or a registration +// equals coexistence. +// --------------------------------------------------------------------------- + +/// One real payload read through IO, echoed through loopback HTTP, stored and +/// read back through SQLite. The text is stable so the final guest value and +/// the server-side request body can both be asserted byte-for-byte. +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +const TRIPLE_DOMAIN_PAYLOAD: &str = "g3-triple-domain-payload-4271"; + +/// A TEMP-owned fixture root for the triple-domain cases: one IO root and one +/// SQLite database root beneath it. Cleanup is unconditional so a failed case +/// does not leave files behind. +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +struct TripleDomainFixture { + root: std::path::PathBuf, + sqlite_root: std::path::PathBuf, +} + +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +impl TripleDomainFixture { + fn new(label: &str) -> Self { + use std::time::{SystemTime, UNIX_EPOCH}; + let nonce = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("system clock should be after the Unix epoch") + .as_nanos(); + let root = std::env::temp_dir().join(format!( + "rustscript-g3-{label}-{}-{nonce}", + std::process::id() + )); + let sqlite_root = root.join("sqlite"); + std::fs::create_dir_all(&sqlite_root) + .expect("triple-domain fixture root should be created"); + Self { root, sqlite_root } + } + + fn io_path(&self) -> String { + self.root.join("input.txt").display().to_string() + } + + fn sqlite_path(&self) -> String { + self.sqlite_root.display().to_string() + } + + fn write_input(&self, text: &str) { + std::fs::write(self.root.join("input.txt"), text).expect("IO fixture should be writable"); + } +} + +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +impl Drop for TripleDomainFixture { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.root); + } +} + +/// A loopback server that reads each POST body and answers with `echo:` +/// plus one empty-valued header. The thread returns every request body it +/// observed, so the caller can prove the bytes that travelled through IO +/// actually reached the wire. +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +fn spawn_echo_server(requests: usize) -> (u16, thread::JoinHandle>>) { + let listener = TcpListener::bind(("127.0.0.1", 0)).expect("echo listener should bind"); + let port = listener + .local_addr() + .expect("echo listener should have an address") + .port(); + let server = thread::spawn(move || { + let mut observed = Vec::new(); + for _ in 0..requests { + let (mut stream, _) = listener.accept().expect("echo request should arrive"); + let mut request = Vec::new(); + let mut buffer = [0_u8; 1024]; + let head_end = loop { + if let Some(position) = request.windows(4).position(|window| window == b"\r\n\r\n") + { + break position + 4; + } + let read = stream + .read(&mut buffer) + .expect("echo request headers should be readable"); + assert_ne!(read, 0, "client closed before sending HTTP headers"); + request.extend_from_slice(&buffer[..read]); + }; + let head = String::from_utf8_lossy(&request[..head_end]).into_owned(); + assert!( + head.starts_with("POST /echo HTTP/1.1"), + "unexpected echo request line: {head}" + ); + let content_length = head + .lines() + .filter_map(|line| line.split_once(':')) + .find_map(|(name, value)| { + name.eq_ignore_ascii_case("content-length") + .then(|| value.trim().parse::().expect("content length")) + }) + .expect("POST request should carry a body length"); + let mut body = request[head_end..].to_vec(); + while body.len() < content_length { + let read = stream + .read(&mut buffer) + .expect("echo request body should be readable"); + assert_ne!(read, 0, "client closed before sending the HTTP body"); + body.extend_from_slice(&buffer[..read]); + } + assert_eq!(body.len(), content_length, "request body must be complete"); + observed.push(body.clone()); + let mut payload = b"echo:".to_vec(); + payload.extend_from_slice(&body); + let response = format!( + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nX-Empty: \r\n\r\n", + payload.len() + ); + stream + .write_all(response.as_bytes()) + .expect("echo response head should be writable"); + stream + .write_all(&payload) + .expect("echo response body should be writable"); + } + observed + }); + (port, server) +} + +/// Compiles one guest source against the shared standard catalog and proves +/// every emitted host import carries that catalog's exact schema. The same +/// catalog then installs the three module surfaces into one registry, so +/// compilation and binding share one contract. +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +fn compile_with_shared_catalog( + source: &str, + expected_imports: &[&str], +) -> (Arc, vm::Program) { + let catalog = standard_host_catalog(); + let compiled = vm::compile_source_with_flavor_and_options( + source, + vm::SourceFlavor::RustScript, + vm::CompileSourceFileOptions::default().with_host_api_catalog(Arc::clone(&catalog)), + ) + .expect("standard-catalog triple-domain source should compile"); + assert!( + compiled + .program + .host_import_schemas() + .iter() + .all(Option::is_some), + "every triple-domain host import must carry an exact catalog schema" + ); + assert!( + compiled + .program + .host_import_schemas() + .iter() + .flatten() + .all(|schema| schema.fingerprint == catalog.fingerprint()), + "every import schema must come from the shared standard catalog" + ); + let imports: Vec<&str> = compiled + .program + .imports + .iter() + .map(|import| import.name.as_str()) + .collect(); + for expected in expected_imports { + assert!( + imports.contains(expected), + "the program must import {expected}, got {imports:?}" + ); + } + (catalog, compiled.program) +} + +/// Binds the same shared catalog into one registry through the installed +/// standard module descriptors: IO, HTTP and SQLite coexist in one dispatch +/// table and bind by exact schema, not by name/arity alone. +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +fn bind_shared_catalog(vm: &mut Vm, catalog: &Arc) { + let mut registry = HostFunctionRegistry::empty(); + for module_name in ["io", "http", "sqlite"] { + let module = vm::standard_host_modules() + .iter() + .find(|module| module.name == module_name) + .expect("the standard module must be composed"); + let descriptor = module + .catalog_module() + .expect("the standard module must publish a catalog surface"); + descriptor + .install_from_catalog(&mut registry, catalog) + .expect("the standard module must install from the shared catalog"); + } + registry + .bind_vm_cached(vm) + .expect("the three exact catalog surfaces must bind in one registry"); +} + +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +#[tokio::test(flavor = "current_thread")] +async fn triple_domain_single_vm_typed_dataflow_uses_shared_catalog() { + let fixture = TripleDomainFixture::new("triple-dataflow"); + fixture.write_input(TRIPLE_DOMAIN_PAYLOAD); + let (port, server) = spawn_echo_server(1); + let source = format!( + r#" + use io; + use http; + use sqlite; + use bytes; + + let source = io::open("{fixture_path}", "r"); + let payload = io::read_all(&source); + let closed = io::close(source); + + let mut request = http::request::new("POST", "http://127.0.0.1:{port}/echo"); + let unit: () = http::request::set_body_text(&mut request, payload); + let response = http::client::request(request); + let status = http::response::status(&response); + let echoed_bytes = http::response::body(&response); + let echoed = bytes::to_utf8(echoed_bytes); + let empty_values = http::response::header_values(&response, "x-empty"); + let absent_values = http::response::header_values(&response, "x-absent"); + + let limits: SqliteLimits = {{}}; + let options: SqliteOpenOptions = {{ path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(limits) }}; + let db = sqlite::open(options); + let no_params: [SqliteValue] = []; + sqlite::execute(&db, "CREATE TABLE payloads (id INTEGER PRIMARY KEY, value TEXT)", no_params); + + let text_value: SqliteValue = {{ kind: "text", text_value: Option::Some(echoed) }}; + let text_params: [SqliteValue] = [text_value]; + let inserted = sqlite::execute(&db, "INSERT INTO payloads (id, value) VALUES (1, ?1)", text_params); + + let null_value: SqliteValue = {{ kind: "null" }}; + let null_params: [SqliteValue] = [null_value]; + let null_inserted = sqlite::execute(&db, "INSERT INTO payloads (id, value) VALUES (2, ?1)", null_params); + + let empty_value: SqliteValue = {{ kind: "text", text_value: Option::Some("") }}; + let empty_params: [SqliteValue] = [empty_value]; + sqlite::execute(&db, "INSERT INTO payloads (id, value) VALUES (3, ?1)", empty_params); + + let query_limits: SqliteLimits = {{}}; + let queried = sqlite::query(&db, "SELECT id, value FROM payloads ORDER BY id", [], query_limits); + let rows = queried.rows; + assert(rows.length == 3); + let text_cell = rows[0].cells[1]; + let stored = match text_cell.text_value {{ Option::Some(value) => value, Option::None => "" }}; + let null_cell = rows[1].cells[1]; + assert(null_cell.kind == "null"); + let null_int_absent = match null_cell.int_value {{ Option::Some(value) => false, Option::None => true }}; + let null_float_absent = match null_cell.float_value {{ Option::Some(value) => false, Option::None => true }}; + let null_text_absent = match null_cell.text_value {{ Option::Some(value) => false, Option::None => true }}; + let null_blob_absent = match null_cell.blob_value {{ Option::Some(value) => false, Option::None => true }}; + assert(null_int_absent); + assert(null_float_absent); + assert(null_text_absent); + assert(null_blob_absent); + let empty_cell = rows[2].cells[1]; + assert(empty_cell.kind == "text"); + let empty_present = match empty_cell.text_value {{ Option::Some(value) => value == "", Option::None => false }}; + assert(empty_present); + let cursor = match queried.next_cursor {{ Option::Some(value) => value, Option::None => -1 }}; + assert(cursor == 3); + + let missing_limits: SqliteLimits = {{}}; + let missing = sqlite::query(&db, "SELECT id, value FROM payloads WHERE id = 99", [], missing_limits); + assert(missing.rows.length == 0); + let missing_absent = match missing.next_cursor {{ Option::Some(value) => false, Option::None => true }}; + assert(missing_absent); + + assert(status == 200); + assert(empty_values.length == 1); + assert(empty_values[0] == ""); + assert(absent_values.length == 0); + assert(inserted.rows_affected == 1); + assert(null_inserted.rows_affected == 1); + assert(closed == true); + let close_unit: () = sqlite::close(db); + stored; + "#, + fixture_path = fixture.io_path(), + port = port + ); + let (catalog, program) = compile_with_shared_catalog( + &source, + &[ + "io::open", + "io::read_all", + "io::close", + "http::request::new", + "http::request::set_body_text", + "http::client::request", + "http::response::status", + "http::response::body", + "http::response::header_values", + "sqlite::open", + "sqlite::execute", + "sqlite::query", + "sqlite::close", + ], + ); + let mut vm = Vm::new(program); + http_test_bridge::install(&mut vm); + vm.configure_io(IoPolicy { + allowed_roots: vec![fixture.root.display().to_string()], + ..IoPolicy::default() + }); + vm.configure_http(local_http_config(port)) + .expect("HTTP configuration should be valid"); + vm.configure_sqlite(SqlitePolicy { + database_root: Some(fixture.sqlite_path()), + ..SqlitePolicy::default() + }); + bind_shared_catalog(&mut vm, &catalog); + + drive_vm_to_halt(&mut vm) + .await + .expect("the three-domain program should complete"); + let observed = server.join().expect("echo server should finish"); + assert_eq!( + observed, + vec![TRIPLE_DOMAIN_PAYLOAD.as_bytes().to_vec()], + "the HTTP request body must be the exact bytes read through IO" + ); + let expected = Value::String(Arc::new(format!("echo:{TRIPLE_DOMAIN_PAYLOAD}"))); + assert_eq!( + vm.stack().last(), + Some(&expected), + "the final value must be the SQLite text cell echoed through HTTP" + ); + assert_eq!( + vm.execution_scope().operations().len(), + 0, + "no composed host operation may remain pending" + ); + assert_eq!( + vm.execution_scope().resources().len(), + 1, + "the immutable HTTP response is the only live resource after IO and SQLite close" + ); +} + +/// The composed error/cleanup path: all three domains are configured and +/// active, the SQLite connection exists, and a policy violation in IO fails the +/// invocation. The domain resources then survive until the established reset +/// retires them, the same VM runs again with every policy intact, and a token +/// minted before reset cannot be revived afterwards. +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +struct RestResourceProbe; + +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +impl vm::resource::HostResource for RestResourceProbe { + fn begin_close( + &mut self, + _reason: vm::resource::ResourceCloseReason, + ) -> vm::resource::ResourceResult { + Ok(vm::resource::CloseProgress::Ready) + } +} + +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +#[tokio::test(flavor = "current_thread")] +async fn triple_domain_error_cleanup_reset_and_reuse_keep_all_policies() { + let fixture = TripleDomainFixture::new("triple-lifecycle"); + fixture.write_input(TRIPLE_DOMAIN_PAYLOAD); + let (port, server) = spawn_echo_server(2); + let source = format!( + r#" + use io; + use http; + use sqlite; + + let source = io::open("{fixture_path}", "r"); + let payload = io::read_all(&source); + + let mut request = http::request::new("POST", "http://127.0.0.1:{port}/echo"); + let unit: () = http::request::set_body_text(&mut request, payload); + let response = http::client::request(request); + let status = http::response::status(&response); + + let limits: SqliteLimits = {{}}; + let options: SqliteOpenOptions = {{ path: Option::Some("state.db"), mode: Option::Some("read_write_create"), limits: Option::Some(limits) }}; + let db = sqlite::open(options); + let no_params: [SqliteValue] = []; + sqlite::execute(&db, "CREATE TABLE IF NOT EXISTS items (value TEXT)", no_params); + + let write_probe = io::open("{fixture_path}", "w"); + status; + "#, + fixture_path = fixture.io_path(), + port = port + ); + let (catalog, program) = compile_with_shared_catalog( + &source, + &[ + "io::open", + "io::read_all", + "http::request::new", + "http::request::set_body_text", + "http::client::request", + "http::response::status", + "sqlite::open", + "sqlite::execute", + ], + ); + let mut vm = Vm::new(program); + http_test_bridge::install(&mut vm); + vm.configure_io(IoPolicy { + allowed_roots: vec![fixture.root.display().to_string()], + ..IoPolicy::default() + }); + vm.configure_http(local_http_config(port)) + .expect("HTTP configuration should be valid"); + vm.configure_sqlite(SqlitePolicy { + database_root: Some(fixture.sqlite_path()), + ..SqlitePolicy::default() + }); + bind_shared_catalog(&mut vm, &catalog); + + let first_error = drive_vm_to_halt(&mut vm) + .await + .expect_err("a write without the IO write capability must fail"); + let first_message = match first_error { + VmError::HostError(message) => message, + other => panic!("the composed policy error must stay on the fatal host channel: {other:?}"), + }; + assert!( + first_message.contains("write capability"), + "the IO policy error must name the write capability, got: {first_message}" + ); + assert_eq!( + vm.execution_scope().operations().len(), + 0, + "the failed composed invocation must not leave a pending operation" + ); + assert_eq!( + vm.execution_scope().resources().len(), + 3, + "the live IO file, HTTP response and SQLite connection must remain until reset" + ); + + // Mint a scope capability before reset; it must not be revivable afterwards. + let probe = vm + .host_context() + .push_resource(RestResourceProbe) + .expect("the pre-reset probe resource should register"); + finish_reset(&mut vm) + .await + .expect("reset must retire the failed composed invocation"); + assert_eq!( + vm.execution_scope().resources().len(), + 0, + "reset must close every domain and probe resource" + ); + assert_eq!( + vm.execution_scope().operations().len(), + 0, + "reset must leave no pending operation" + ); + assert!( + vm.is_reusable(), + "the VM must be reusable after the composed error reset" + ); + assert!( + vm.http_is_configured(), + "the HTTP module state must survive reset" + ); + assert_eq!( + vm.sqlite_policy().database_root.as_deref(), + Some(fixture.sqlite_path().as_str()), + "the SQLite database root must survive reset" + ); + let stale = vm + .host_context() + .take_resource::(probe.handle()); + assert!( + stale.is_err(), + "a capability minted before reset must not be revived by VM reuse" + ); + + let second_error = drive_vm_to_halt(&mut vm) + .await + .expect_err("the same policy violation must remain in force after reuse"); + let second_message = match second_error { + VmError::HostError(message) => message, + other => panic!("the reused failure must stay on the fatal host channel: {other:?}"), + }; + assert!( + second_message.contains("write capability"), + "the IO policy must persist across reset, got: {second_message}" + ); + let observed = server.join().expect("echo server should finish"); + assert_eq!( + observed.len(), + 2, + "both invocations must have performed their real HTTP request" + ); + for body in observed { + assert_eq!(body, TRIPLE_DOMAIN_PAYLOAD.as_bytes()); + } + finish_reset(&mut vm) + .await + .expect("the second composed error must also reset cleanly"); + assert_eq!( + vm.execution_scope().resources().len(), + 0, + "the reused invocation must leave no live resource" + ); +} + +/// The composed catalog keeps each module's resource keys and named-struct +/// table; merging the three surfaces must not overwrite a key or a name. +#[cfg(all(feature = "sqlite", not(target_family = "wasm")))] +#[test] +fn triple_domain_catalog_keys_and_named_structs_coexist() { + let catalog = standard_host_catalog(); + for key in [ + "io.file", + "http.request", + "http.response", + "http.headers", + "http.sse_summary", + "sqlite.connection", + ] { + assert!( + catalog.resource(key).is_some(), + "the composed catalog must publish the {key} resource" + ); + } + let keys: Vec<&str> = catalog + .resources() + .iter() + .map(|resource| resource.key.as_str()) + .collect(); + let unique: std::collections::BTreeSet<&str> = keys.iter().copied().collect(); + assert_eq!( + keys.len(), + unique.len(), + "composed resource keys must be unique: {keys:?}" + ); + for name in [ + "SqliteLimits", + "SqliteOpenOptions", + "SqliteExecuteResult", + "SqliteValue", + "SqliteRow", + "SqliteQueryResult", + "SqliteStatement", + "SqliteTransactionResult", + "JitConfig", + ] { + assert!( + catalog.struct_named(name).is_some(), + "the composed catalog must keep the {name} named struct" + ); + assert!( + catalog.resource(name).is_none(), + "the {name} named struct must not be overwritten by a resource key" + ); + } + for name in [ + "io::open", + "io::read_all", + "io::close", + "http::client::request", + "http::response::body", + "sqlite::open", + "sqlite::execute", + "sqlite::query", + "sqlite::close", + ] { + assert!( + !catalog.functions_named(name).is_empty(), + "the composed catalog must keep {name}" + ); + } + let modules = vm::standard_catalog_modules(); + assert_eq!( + catalog.functions().len(), + modules + .iter() + .map(|module| module.functions.len()) + .sum::(), + "the composed catalog must be the exact merge of every catalog surface" + ); +} diff --git a/tests/vm/runtime_state_edge_tests.rs b/tests/vm/runtime_state_edge_tests.rs index 9c9bba96..1f45cd66 100644 --- a/tests/vm/runtime_state_edge_tests.rs +++ b/tests/vm/runtime_state_edge_tests.rs @@ -6,7 +6,7 @@ use std::sync::{ Arc, atomic::{AtomicUsize, Ordering}, }; -use std::task::{Context, Poll, Wake, Waker}; +use std::task::{Context, Poll, Waker}; struct PendingOnce { call_count: Arc, @@ -20,16 +20,6 @@ impl HostFunction for PendingOnce { } } -struct NoopWake; - -impl Wake for NoopWake { - fn wake(self: Arc) {} -} - -fn noop_waker() -> Waker { - Waker::from(Arc::new(NoopWake)) -} - fn new_runtime_state_vm(program: Program) -> Vm { let mut vm = Vm::new(program); vm.set_drop_contract_events_enabled(true); @@ -131,7 +121,7 @@ fn poll_waiting_host_op_reports_missing_async_bridge() { let status = vm.run().expect("run should wait"); assert_eq!(status, VmStatus::Waiting(321)); - let waker = noop_waker(); + let waker = Waker::noop().clone(); let mut cx = Context::from_waker(&waker); match vm.poll_waiting_host_op(&mut cx) { Poll::Ready(Err(err)) => { @@ -275,7 +265,7 @@ fn drop_contract_counts_overwrites_and_reset_clears_counter() { let source = r#" let mut value = { payload: [1, 2, 3], name: "a" }; value = { payload: [4], name: "b" }; - null; + (); "#; let compiled = compile_source(source).expect("compile should succeed"); diff --git a/tests/vm/vm_runtime_tests.rs b/tests/vm/vm_runtime_tests.rs index 8659f81f..af432fef 100644 --- a/tests/vm/vm_runtime_tests.rs +++ b/tests/vm/vm_runtime_tests.rs @@ -2,6 +2,7 @@ mod common; use common::*; use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; use vm::OpCode; use vm::{HostImport, RegexCacheVmExt, StandardSurfaceComposition}; @@ -362,6 +363,708 @@ fn namespaced_builtin_json_encode_call_can_be_overridden_by_host_binding() { assert_eq!(vm.stack(), &[Value::string("\"override\"")]); } +struct RecordingJsonEncode { + seen: Arc>>>, + label: &'static str, +} + +impl HostFunction for RecordingJsonEncode { + fn call(&mut self, _vm: &mut Vm, args: &[Value]) -> Result { + self.seen.lock().expect("override args").push(args.to_vec()); + Ok(CallOutcome::Return(vec![Value::string(self.label)].into())) + } +} + +struct FailingJsonEncode { + calls: Arc, +} + +impl HostFunction for FailingJsonEncode { + fn call(&mut self, _vm: &mut Vm, args: &[Value]) -> Result { + assert_eq!(args, &[Value::string("request_body")]); + self.calls.fetch_add(1, Ordering::SeqCst); + Err(vm::VmError::HostError( + "encode override rejected".to_string(), + )) + } +} + +fn json_encode_request_source() -> &'static str { + "use json;\njson::encode(\"request_body\");\n" +} + +fn bytecode_calls(code: &[u8]) -> Vec<(u16, u8)> { + let mut calls = Vec::new(); + let mut ip = 0; + while ip < code.len() { + let opcode = OpCode::try_from(code[ip]).expect("opcode"); + if opcode == OpCode::Call { + let index = u16::from_le_bytes([code[ip + 1], code[ip + 2]]); + calls.push((index, code[ip + 3])); + } + ip += 1 + opcode.operand_len(); + } + calls +} + +fn public_json_encode_program(value: Value) -> Program { + let mut bc = BytecodeBuilder::new(); + bc.ldc(0); + bc.call(vm::BuiltinFunction::JsonEncode.call_index(), 1); + bc.ret(); + Program::new(vec![value], bc.finish()) +} + +fn typed_json_encode_program(value: Value, descriptor: Vec) -> Program { + let mut bc = BytecodeBuilder::new(); + bc.ldc(0); + bc.ldc(1); + bc.call(vm::BuiltinFunction::JsonEncodeTyped.call_index(), 2); + bc.ret(); + Program::new(vec![value, Value::bytes(descriptor)], bc.finish()) +} + +#[test] +fn public_json_encode_override_is_cold_warm_reset_and_rebind_stable() { + let seen = Arc::new(std::sync::Mutex::new(Vec::new())); + let compiled = compile_source(json_encode_request_source()).expect("source should compile"); + let mut vm = Vm::new(compiled.program); + vm.bind_function( + "json::encode", + Box::new(RecordingJsonEncode { + seen: Arc::clone(&seen), + label: "cold", + }), + ); + + let status = vm.run().expect("cold override should run"); + assert_eq!(status, VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("cold")]); + assert_eq!( + seen.lock().expect("cold args").as_slice(), + &[vec![Value::string("request_body")]] + ); + + vm.reset_for_reuse().expect("warm reset should succeed"); + let status = vm.run().expect("warm override should run"); + assert_eq!(status, VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("cold")]); + assert_eq!(seen.lock().expect("warm args").len(), 2); + + vm.bind_function( + "json::encode", + Box::new(RecordingJsonEncode { + seen: Arc::clone(&seen), + label: "rebind", + }), + ); + vm.reset_for_reuse().expect("rebind reset should succeed"); + let status = vm.run().expect("rebound override should run"); + assert_eq!(status, VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("rebind")]); + let seen = seen.lock().expect("rebind args"); + assert_eq!(seen.len(), 3); + assert_eq!(seen[2], vec![Value::string("request_body")]); +} + +#[test] +fn public_json_encode_override_reports_structured_host_failure_once() { + let calls = Arc::new(AtomicUsize::new(0)); + let compiled = compile_source(json_encode_request_source()).expect("source should compile"); + let mut vm = Vm::new(compiled.program); + vm.bind_function( + "json::encode", + Box::new(FailingJsonEncode { + calls: Arc::clone(&calls), + }), + ); + + let err = vm.run().expect_err("override host failure should surface"); + match err { + vm::VmError::HostError(message) => { + assert_eq!(message, "encode override rejected"); + } + other => panic!("unexpected vm error: {other}"), + } + assert_eq!(calls.load(Ordering::SeqCst), 1); +} + +#[test] +fn typed_json_encode_without_override_keeps_descriptor_validation() { + let program = typed_json_encode_program(Value::string("request_body"), vec![99]); + let mut vm = Vm::new(program); + let err = vm + .run() + .expect_err("invalid descriptor must fail when no override is installed"); + match err { + vm::VmError::HostError(message) => { + assert!(message.contains("typed json descriptor"), "{message}"); + } + other => panic!("unexpected vm error: {other}"), + } +} + +#[test] +fn typed_json_encode_override_drops_private_descriptor() { + let seen = Arc::new(std::sync::Mutex::new(Vec::new())); + let program = typed_json_encode_program(Value::string("request_body"), vec![99]); + let mut vm = Vm::new(program); + vm.bind_function( + "json::encode", + Box::new(RecordingJsonEncode { + seen: Arc::clone(&seen), + label: "descriptor-hidden", + }), + ); + + let status = vm.run().expect("override should ignore the descriptor"); + assert_eq!(status, VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("descriptor-hidden")]); + assert_eq!( + seen.lock().expect("descriptor args").as_slice(), + &[vec![Value::string("request_body")]] + ); +} + +#[test] +fn typed_public_json_encode_override_yield_restores_hidden_descriptor() { + struct YieldThenReturn { + calls: Arc, + } + + impl HostFunction for YieldThenReturn { + fn call(&mut self, _vm: &mut Vm, args: &[Value]) -> Result { + assert_eq!(args, &[Value::string("request_body")]); + let count = self.calls.fetch_add(1, Ordering::SeqCst); + if count == 0 { + Ok(CallOutcome::Yield) + } else { + Ok(CallOutcome::Return( + vec![Value::string("\"override\"")].into(), + )) + } + } + } + + let calls = Arc::new(AtomicUsize::new(0)); + let compiled = compile_source(json_encode_request_source()).expect("source should compile"); + let mut vm = Vm::new(compiled.program); + vm.bind_function( + "json::encode", + Box::new(YieldThenReturn { + calls: Arc::clone(&calls), + }), + ); + + let status = vm.run().expect("first typed override call should yield"); + assert_eq!(status, VmStatus::Yielded); + assert_eq!(calls.load(Ordering::SeqCst), 1); + assert!( + vm.stack().len() >= 2 && matches!(vm.stack().last(), Some(Value::Bytes(_))), + "yield retry must keep the hidden descriptor, stack={:?}", + vm.stack() + ); + assert_eq!( + vm.stack()[vm.stack().len() - 2], + Value::string("request_body") + ); + + let status = vm + .resume() + .expect("resume should replay the same typed call"); + assert_eq!(status, VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("\"override\"")]); + assert_eq!(calls.load(Ordering::SeqCst), 2); + assert_eq!( + vm.jit_native_exec_count(), + 0, + "typed override yield stays off the native specialization path" + ); +} + +#[test] +fn typed_public_json_encode_override_repeated_yield_keeps_lower_sentinel() { + struct YieldTwice { + calls: Arc, + } + + impl HostFunction for YieldTwice { + fn call(&mut self, _vm: &mut Vm, args: &[Value]) -> Result { + assert_eq!(args, &[Value::string("request_body")]); + let count = self.calls.fetch_add(1, Ordering::SeqCst); + if count < 2 { + Ok(CallOutcome::Yield) + } else { + Ok(CallOutcome::Return(vec![Value::string("done")].into())) + } + } + } + + let mut bc = BytecodeBuilder::new(); + bc.ldc(0); + bc.ldc(1); + bc.ldc(2); + bc.call(vm::BuiltinFunction::JsonEncodeTyped.call_index(), 2); + bc.ret(); + let program = Program::new( + vec![ + Value::Int(7), + Value::string("request_body"), + Value::bytes(vec![9, 9]), + ], + bc.finish(), + ); + let calls = Arc::new(AtomicUsize::new(0)); + let mut vm = Vm::new(program); + vm.bind_function( + "json::encode", + Box::new(YieldTwice { + calls: Arc::clone(&calls), + }), + ); + + for expected_calls in 1..=2 { + let status = if expected_calls == 1 { + vm.run().expect("first yield") + } else { + vm.resume().expect("second yield") + }; + assert_eq!(status, VmStatus::Yielded); + assert_eq!(calls.load(Ordering::SeqCst), expected_calls); + assert_eq!( + vm.stack(), + &[ + Value::Int(7), + Value::string("request_body"), + Value::bytes(vec![9, 9]) + ] + ); + } + + let status = vm.resume().expect("third entry should return"); + assert_eq!(status, VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::Int(7), Value::string("done")]); + assert_eq!(calls.load(Ordering::SeqCst), 3); +} + +#[test] +fn typed_public_json_encode_override_yield_evaluates_source_argument_once() { + struct YieldThenReturn { + calls: Arc, + } + + impl HostFunction for YieldThenReturn { + fn call(&mut self, _vm: &mut Vm, args: &[Value]) -> Result { + assert_eq!(args, &[Value::string("request_body")]); + let count = self.calls.fetch_add(1, Ordering::SeqCst); + if count == 0 { + Ok(CallOutcome::Yield) + } else { + Ok(CallOutcome::Return(vec![Value::string("once")].into())) + } + } + } + + let calls = Arc::new(AtomicUsize::new(0)); + let compiled = compile_source( + "use json;\nlet mut n = 0;\nfn bump() -> string { n = n + 1; \"request_body\"; }\nlet encoded = json::encode(bump());\nencoded;\nn;\n", + ) + .expect("argument expression should compile"); + let mut vm = Vm::new(compiled.program); + vm.bind_function( + "json::encode", + Box::new(YieldThenReturn { + calls: Arc::clone(&calls), + }), + ); + + assert_eq!(vm.run().expect("yield"), VmStatus::Yielded); + assert_eq!(vm.resume().expect("resume"), VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("once"), Value::Int(1)]); + assert_eq!(calls.load(Ordering::SeqCst), 2); +} + +#[test] +fn typed_public_json_encode_args_override_yield_restores_hidden_descriptor() { + struct YieldThenReturn { + calls: Arc, + } + + impl HostArgsFunction for YieldThenReturn { + fn call(&mut self, args: &[Value]) -> Result { + assert_eq!(args, &[Value::string("request_body")]); + let count = self.calls.fetch_add(1, Ordering::SeqCst); + if count == 0 { + Ok(CallOutcome::Yield) + } else { + Ok(CallOutcome::Return(vec![Value::string("args")].into())) + } + } + } + + let calls = Arc::new(AtomicUsize::new(0)); + let compiled = compile_source(json_encode_request_source()).expect("source should compile"); + let mut vm = Vm::new(compiled.program); + vm.bind_args_function( + "json::encode", + Box::new(YieldThenReturn { + calls: Arc::clone(&calls), + }), + ); + + assert_eq!(vm.run().expect("args yield"), VmStatus::Yielded); + assert!(matches!(vm.stack().last(), Some(Value::Bytes(_)))); + assert_eq!(vm.resume().expect("args resume"), VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("args")]); + assert_eq!(calls.load(Ordering::SeqCst), 2); +} + +#[test] +fn typed_public_json_encode_override_yield_survives_vmbc_roundtrip() { + struct YieldThenReturn { + calls: Arc, + } + + impl HostFunction for YieldThenReturn { + fn call(&mut self, _vm: &mut Vm, args: &[Value]) -> Result { + assert_eq!(args, &[Value::string("request_body")]); + let count = self.calls.fetch_add(1, Ordering::SeqCst); + if count == 0 { + Ok(CallOutcome::Yield) + } else { + Ok(CallOutcome::Return(vec![Value::string("vmbc")].into())) + } + } + } + + let compiled = compile_source(json_encode_request_source()).expect("source should compile"); + let bytes = vm::encode_program(&compiled.program).expect("vmbc encode"); + let program = vm::decode_program(&bytes).expect("vmbc decode"); + assert_eq!( + bytecode_calls(&program.code), + vec![(vm::BuiltinFunction::JsonEncodeTyped.call_index(), 2)] + ); + let calls = Arc::new(AtomicUsize::new(0)); + let mut vm = Vm::new(program); + vm.bind_function( + "json::encode", + Box::new(YieldThenReturn { + calls: Arc::clone(&calls), + }), + ); + + assert_eq!(vm.run().expect("vmbc yield"), VmStatus::Yielded); + assert!(matches!(vm.stack().last(), Some(Value::Bytes(_)))); + assert_eq!(vm.resume().expect("vmbc resume"), VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("vmbc")]); + assert_eq!(calls.load(Ordering::SeqCst), 2); +} + +#[test] +fn typed_public_json_encode_override_pending_does_not_replay() { + struct PendingOnce { + calls: Arc, + } + + impl HostFunction for PendingOnce { + fn call(&mut self, _vm: &mut Vm, args: &[Value]) -> Result { + assert_eq!(args, &[Value::string("request_body")]); + let count = self.calls.fetch_add(1, Ordering::SeqCst); + if count == 0 { + Ok(CallOutcome::Pending(99)) + } else { + Err(vm::VmError::HostError( + "pending override must not replay".to_string(), + )) + } + } + } + + let calls = Arc::new(AtomicUsize::new(0)); + let compiled = compile_source(json_encode_request_source()).expect("source should compile"); + let mut vm = Vm::new(compiled.program); + vm.bind_function( + "json::encode", + Box::new(PendingOnce { + calls: Arc::clone(&calls), + }), + ); + + assert_eq!(vm.run().expect("pending"), VmStatus::Waiting(99)); + assert!( + vm.stack() + .iter() + .all(|value| !matches!(value, Value::Bytes(_))), + "pending consumes the hidden descriptor, stack={:?}", + vm.stack() + ); + vm.complete_host_op(99, vec![Value::string("pending")]) + .expect("completion"); + assert_eq!(vm.resume().expect("resume after pending"), VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("pending")]); + assert_eq!(calls.load(Ordering::SeqCst), 1); +} + +#[test] +fn typed_public_json_encode_override_halt_consumes_hidden_descriptor() { + struct HaltEncode { + calls: Arc, + } + + impl HostFunction for HaltEncode { + fn call(&mut self, _vm: &mut Vm, args: &[Value]) -> Result { + assert_eq!(args, &[Value::string("request_body")]); + self.calls.fetch_add(1, Ordering::SeqCst); + Ok(CallOutcome::Halt) + } + } + + let calls = Arc::new(AtomicUsize::new(0)); + let compiled = compile_source(json_encode_request_source()).expect("source should compile"); + let mut vm = Vm::new(compiled.program); + vm.bind_function( + "json::encode", + Box::new(HaltEncode { + calls: Arc::clone(&calls), + }), + ); + + assert_eq!(vm.run().expect("halt"), VmStatus::Halted); + assert!(vm.stack().is_empty(), "halt leaves no call operands"); + assert_eq!(calls.load(Ordering::SeqCst), 1); +} + +#[test] +fn typed_public_json_encode_override_release_skips_guest_drop_contract() { + struct ReturnEncode; + + impl HostFunction for ReturnEncode { + fn call(&mut self, _vm: &mut Vm, args: &[Value]) -> Result { + assert_eq!(args, &[Value::string("request_body")]); + Ok(CallOutcome::Return(vec![Value::string("kept")].into())) + } + } + + let compiled = compile_source(json_encode_request_source()).expect("source should compile"); + let mut vm = Vm::new(compiled.program); + vm.set_drop_contract_events_enabled(true); + vm.bind_function("json::encode", Box::new(ReturnEncode)); + + assert_eq!(vm.run().expect("return"), VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("kept")]); + assert_eq!( + vm.drop_contract_event_count(), + 0, + "compiler-owned descriptor release must not emit a guest drop event" + ); +} + +#[test] +fn typed_public_json_encode_override_yield_keeps_aot_without_native_specialization() { + struct YieldThenReturn { + calls: Arc, + } + + impl HostFunction for YieldThenReturn { + fn call(&mut self, _vm: &mut Vm, args: &[Value]) -> Result { + assert_eq!(args, &[Value::string("request_body")]); + let count = self.calls.fetch_add(1, Ordering::SeqCst); + if count == 0 { + Ok(CallOutcome::Yield) + } else { + Ok(CallOutcome::Return(vec![Value::string("aot")].into())) + } + } + } + + let calls = Arc::new(AtomicUsize::new(0)); + let compiled = compile_source(json_encode_request_source()).expect("source should compile"); + let mut vm = Vm::new(compiled.program); + vm.set_jit_config(vm::JitConfig { + enabled: false, + hot_loop_threshold: 1, + max_trace_len: 64, + }); + vm.compile_aot().expect("aot compile should succeed"); + vm.bind_function( + "json::encode", + Box::new(YieldThenReturn { + calls: Arc::clone(&calls), + }), + ); + + assert_eq!(vm.run().expect("aot yield"), VmStatus::Yielded); + assert!(matches!(vm.stack().last(), Some(Value::Bytes(_)))); + assert_eq!(vm.resume().expect("aot resume"), VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("aot")]); + assert_eq!(calls.load(Ordering::SeqCst), 2); + assert_eq!(vm.jit_native_exec_count(), 0); + assert!( + vm.aot_exec_count() > 0, + "compiled aot still runs; the override yield stays on the host redirect" + ); +} + +#[test] +fn low_level_public_json_encode_override_receives_original_value() { + struct PublicOverride; + + impl HostFunction for PublicOverride { + fn call(&mut self, _vm: &mut Vm, args: &[Value]) -> Result { + assert_eq!(args, &[Value::string("request_body")]); + Ok(CallOutcome::Return( + vec![Value::string("\"override\"")].into(), + )) + } + } + + let mut vm = Vm::new(public_json_encode_program(Value::string("request_body"))); + vm.bind_function("json::encode", Box::new(PublicOverride)); + + let status = vm.run().expect("public call override should run"); + assert_eq!(status, VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("\"override\"")]); +} + +#[test] +fn private_json_encode_helper_is_not_source_bindable() { + struct Unused; + + impl HostFunction for Unused { + fn call(&mut self, _vm: &mut Vm, _args: &[Value]) -> Result { + Err(vm::VmError::HostError( + "private helper must not be called".to_string(), + )) + } + } + + let mut vm = Vm::new(Program::new(Vec::new(), vec![OpCode::Ret as u8])); + let err = vm + .bind_builtin_override("__json_encode_typed", Box::new(Unused)) + .expect_err("private helper name must not resolve"); + assert!(matches!(err, vm::VmError::HostError(_))); + + let compiled = compile_source(json_encode_request_source()).expect("source should compile"); + let mut vm = Vm::new(compiled.program); + vm.bind_function("__json_encode_typed", Box::new(Unused)); + let status = vm.run().expect("private name must not intercept encode"); + assert_eq!(status, VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("\"request_body\"")]); +} + +#[test] +fn public_json_encode_override_runs_on_aot_and_survives_rebind() { + let seen = Arc::new(std::sync::Mutex::new(Vec::new())); + let compiled = compile_source(json_encode_request_source()).expect("source should compile"); + let mut vm = Vm::new(compiled.program); + vm.set_jit_config(vm::JitConfig { + enabled: false, + hot_loop_threshold: 1, + max_trace_len: 64, + }); + vm.compile_aot().expect("aot compile should succeed"); + vm.bind_function( + "json::encode", + Box::new(RecordingJsonEncode { + seen: Arc::clone(&seen), + label: "aot", + }), + ); + + let status = vm.run().expect("aot override should run"); + assert_eq!(status, VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("aot")]); + assert!( + vm.aot_exec_count() > 0, + "aot should execute the encode call" + ); + assert_eq!(vm.jit_native_exec_count(), 0); + + vm.bind_function( + "json::encode", + Box::new(RecordingJsonEncode { + seen: Arc::clone(&seen), + label: "aot-rebind", + }), + ); + vm.reset_for_reuse().expect("aot reset should succeed"); + let status = vm.run().expect("rebound aot override should run"); + assert_eq!(status, VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("aot-rebind")]); + assert!( + vm.has_aot_program(), + "reset must retain the compiled aot program" + ); + assert!( + vm.aot_exec_count() > 1, + "rebind must not require a fresh aot compile" + ); +} + +#[test] +fn public_json_encode_override_disables_jit_and_still_dispatches() { + let seen = Arc::new(std::sync::Mutex::new(Vec::new())); + let compiled = compile_source( + r#" + use json; + let mut i = 0; + let mut text = ""; + while i < 8 { + text = json::encode("request_body"); + i = i + 1; + } + text; + "#, + ) + .expect("looping encode should compile"); + let mut vm = Vm::new(compiled.program); + vm.bind_function( + "json::encode", + Box::new(RecordingJsonEncode { + seen: Arc::clone(&seen), + label: "jit-disabled", + }), + ); + + let status = vm.run().expect("override loop should run"); + assert_eq!(status, VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("jit-disabled")]); + assert_eq!(seen.lock().expect("jit args").len(), 8); + assert!( + vm.jit_snapshot().traces.is_empty(), + "builtin override disables trace compilation" + ); + assert_eq!(vm.jit_native_exec_count(), 0); +} + +#[test] +fn public_json_encode_override_survives_vmbc_roundtrip() { + let compiled = compile_source(json_encode_request_source()).expect("source should compile"); + let bytes = vm::encode_program(&compiled.program).expect("vmbc encode"); + let program = vm::decode_program(&bytes).expect("vmbc decode"); + assert_eq!( + bytecode_calls(&program.code), + vec![(vm::BuiltinFunction::JsonEncodeTyped.call_index(), 2)] + ); + + let seen = Arc::new(std::sync::Mutex::new(Vec::new())); + let mut vm = Vm::new(program); + vm.bind_function( + "json::encode", + Box::new(RecordingJsonEncode { + seen: Arc::clone(&seen), + label: "vmbc", + }), + ); + let status = vm.run().expect("decoded override should run"); + assert_eq!(status, VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::string("vmbc")]); + assert_eq!( + seen.lock().expect("vmbc args").as_slice(), + &[vec![Value::string("request_body")]] + ); +} + #[test] fn namespaced_builtin_math_call_can_be_overridden_by_host_binding() { struct MathSqrtOverride; @@ -493,18 +1196,13 @@ fn host_function_registry_includes_default_runtime_exit() { #[test] fn json_encode_rejects_non_string_map_keys() { - // Non-string keys are not representable in `TypeSchema::Map`, so the - // compiler admits the map and the runtime encoder rejects the key. - let compiled = compile_source( - r#" - use json; - let payload = { 1: "one" }; - json::encode(payload); - "#, - ) - .expect("non-string-key maps must compile; runtime must reject them"); - - let mut vm = Vm::new(compiled.program); + let program = + public_json_encode_program(Value::map(vec![(Value::Int(1), Value::string("one"))])); + assert_eq!( + bytecode_calls(&program.code), + vec![(vm::BuiltinFunction::JsonEncode.call_index(), 1)] + ); + let mut vm = Vm::new(program); let err = vm .run() .expect_err("json::encode must reject non-string map keys"); @@ -521,19 +1219,12 @@ fn json_encode_rejects_non_string_map_keys() { #[test] fn json_encode_rejects_nan_at_runtime() { - // NaN has no JSON representation. The compiler cannot prove the value - // non-finite, so the runtime encoder must reject it. - let compiled = compile_source( - r#" - use json; - use math; - let payload: float = math::nan(); - json::encode(payload); - "#, - ) - .expect("nan float must compile; runtime must reject it"); - - let mut vm = Vm::new(compiled.program); + let program = public_json_encode_program(Value::Float(f64::NAN)); + assert_eq!( + bytecode_calls(&program.code), + vec![(vm::BuiltinFunction::JsonEncode.call_index(), 1)] + ); + let mut vm = Vm::new(program); let err = vm.run().expect_err("json::encode must reject NaN"); match err { vm::VmError::HostError(message) => { @@ -548,25 +1239,13 @@ fn json_encode_rejects_nan_at_runtime() { #[test] fn json_encode_rejects_infinite_floats_at_runtime() { - // Positive and negative infinity have no JSON representation; the - // runtime encoder must reject both. - for source in [ - r#" - use json; - use math; - let payload: float = math::inf(); - json::encode(payload); - "#, - r#" - use json; - use math; - let payload: float = math::neg_inf(); - json::encode(payload); - "#, - ] { - let compiled = - compile_source(source).expect("infinite float must compile; runtime must reject it"); - let mut vm = Vm::new(compiled.program); + for value in [f64::INFINITY, f64::NEG_INFINITY] { + let program = public_json_encode_program(Value::Float(value)); + assert_eq!( + bytecode_calls(&program.code), + vec![(vm::BuiltinFunction::JsonEncode.call_index(), 1)] + ); + let mut vm = Vm::new(program); let err = vm.run().expect_err("json::encode must reject infinity"); match err { vm::VmError::HostError(message) => { @@ -586,16 +1265,12 @@ fn json_encode_uses_last_duplicate_map_entry_from_constructor() { (Value::string("k"), Value::Int(1)), (Value::string("k"), Value::Int(2)), ]); - let mut compiled = compile_source( - r#" - use json; - json::encode("placeholder"); - "#, - ) - .expect("source should compile"); - compiled.program.constants = vec![duplicate_map]; - - let mut vm = Vm::new(compiled.program); + let program = public_json_encode_program(duplicate_map); + assert_eq!( + bytecode_calls(&program.code), + vec![(vm::BuiltinFunction::JsonEncode.call_index(), 1)] + ); + let mut vm = Vm::new(program); let status = vm.run().expect("json::encode should succeed"); assert_eq!(status, VmStatus::Halted); assert_eq!(vm.stack(), &[Value::string("{\"k\":2}")]); @@ -623,10 +1298,11 @@ fn member_has_lowering_checks_container_membership() { #[test] fn json_decode_rejects_duplicate_object_keys() { + // Typed decode keeps the same strict unique-key oracle on the RSS route. let compiled = compile_source( r#" use json; - json::decode("{\"k\":1,\"k\":2}"); + json::decode::>("{\"k\":1,\"k\":2}"); "#, ) .expect("source should compile"); @@ -1343,6 +2019,57 @@ fn get_and_set_use_hash_map_overwrite_semantics() { assert_eq!(entries.get(&Value::string("z")), Some(&Value::Int(3))); } +#[test] +fn low_level_null_key_map_round_trip_keeps_vm_semantics() { + let builtin_get = builtin_call_index_with_arity( + r#" + let text = "abc"; + text[0]; + "#, + 2, + ); + let builtin_set = builtin_call_index_with_arity( + r#" + let mut values = [1]; + values[0] = 2; + "#, + 3, + ); + let constants = vec![Value::map(vec![]), Value::Null, Value::Int(42)]; + let mut bc = BytecodeBuilder::new(); + bc.ldc(0); + bc.ldc(1); + bc.ldc(2); + bc.call(builtin_set, 3); + bc.ldc(1); + bc.call(builtin_get, 2); + bc.ret(); + let mut vm = Vm::new(Program::new(constants, bc.finish())); + assert_eq!(vm.run().expect("low-level null key"), VmStatus::Halted); + assert_eq!(vm.stack(), &[Value::Int(42)]); + + let deletion = vec![ + Value::map(vec![(Value::string("k"), Value::Int(1))]), + Value::string("k"), + Value::Null, + ]; + let mut delete_bc = BytecodeBuilder::new(); + delete_bc.ldc(0); + delete_bc.ldc(1); + delete_bc.ldc(2); + delete_bc.call(builtin_set, 3); + delete_bc.ret(); + let mut delete_vm = Vm::new(Program::new(deletion, delete_bc.finish())); + assert_eq!( + delete_vm.run().expect("null value deletes"), + VmStatus::Halted + ); + let [Value::Map(entries)] = delete_vm.stack() else { + panic!("expected map, got {:?}", delete_vm.stack()); + }; + assert!(entries.get(&Value::string("k")).is_none()); +} + #[test] fn set_rejects_sparse_array_indexes() { let builtin_set = builtin_call_index_with_arity( diff --git a/tests/wire/catalog_contract_tests.rs b/tests/wire/catalog_contract_tests.rs index 0faee5f1..05c934ff 100644 --- a/tests/wire/catalog_contract_tests.rs +++ b/tests/wire/catalog_contract_tests.rs @@ -21,10 +21,10 @@ use std::collections::HashMap; use vm::{BUILTIN_CATALOG, builtin_call_index}; /// Documented call-index blocks; must match `src/builtins/catalog.rs`. -/// Extension block: 0x0000..=0xFF8F (reserved for future builtins and host +/// Extension block: 0x0000..=0xFF8B (reserved for future builtins and host /// imports). -const EXTENSION_BLOCK_END: u16 = 0xFF8F; -const SPECIAL_CALL_BLOCK_START: u16 = 0xFF90; +const EXTENSION_BLOCK_END: u16 = 0xFF8B; +const SPECIAL_CALL_BLOCK_START: u16 = 0xFF8C; const SPECIAL_CALL_BLOCK_END: u16 = 0xFFA1; const ORDINARY_BLOCK_START: u16 = 0xFFA2; #[cfg(all(feature = "sqlite", not(target_family = "wasm")))] @@ -239,15 +239,26 @@ fn static_builtin_ids_are_frozen() { assert_eq!(builtin.name(), expected_name); } - // The public reverse lookup resolves every catalog source name to its - // explicit static ID. + // The public source-name reverse lookup resolves every source-visible + // catalog name to its explicit static ID. Compiler-internal helpers + // (`CatalogClass::Internal`) are addressable only through the generated + // call index, never through a source name. for entry in &entries { - assert_eq!( - builtin_call_index(&entry.source_name), - Some(entry.id), - "reverse lookup for '{}'", - entry.source_name - ); + let resolved = builtin_call_index(&entry.source_name); + if entry.class == "Internal" { + assert_eq!( + resolved, None, + "internal source name '{}' must not resolve for source", + entry.source_name + ); + } else { + assert_eq!( + resolved, + Some(entry.id), + "reverse lookup for '{}'", + entry.source_name + ); + } } } diff --git a/tests/wire/wire_tests.rs b/tests/wire/wire_tests.rs index 330c1a92..71006785 100644 --- a/tests/wire/wire_tests.rs +++ b/tests/wire/wire_tests.rs @@ -59,7 +59,7 @@ fn wire_roundtrip_preserves_constants_and_code() { }); let encoded = encode_program(&program).expect("encode should succeed"); - assert_eq!(u16::from_le_bytes([encoded[4], encoded[5]]), 13); + assert_eq!(u16::from_le_bytes([encoded[4], encoded[5]]), 14); let decoded = decode_program(&encoded).expect("decode should succeed"); assert_eq!(decoded.constants, program.constants); @@ -82,7 +82,7 @@ fn wire_v11_legacy_imports_decode_without_schema_metadata() { vec![import.clone()], None, ); - let encoded = encode_program(&program).expect("v13 encoding should succeed"); + let encoded = encode_program(&program).expect("v14 encoding should succeed"); let marker_offset = 8 + 4 + 4 + program.code.len() + 4 + 4 + import.name.len() + 2; assert_eq!(encoded[marker_offset], 0); let mut legacy = encoded; @@ -102,7 +102,7 @@ fn wire_v11_legacy_imports_decode_without_schema_metadata() { #[test] fn wire_v11_zero_import_program_decodes_by_version() { let program = Program::new(Vec::new(), vec![vm::OpCode::Ret as u8]); - let mut encoded = encode_program(&program).expect("v13 encoding should succeed"); + let mut encoded = encode_program(&program).expect("v14 encoding should succeed"); strip_empty_named_struct_section(&mut encoded); encoded[4..6].copy_from_slice(&11u16.to_le_bytes()); @@ -128,7 +128,7 @@ fn strip_empty_named_struct_section(encoded: &mut Vec) { fn minimal_vmbc_prefix(constant_count: u32, code: &[u8], import_count: u32) -> Vec { let mut bytes = Vec::new(); bytes.extend_from_slice(b"VMBC"); - bytes.extend_from_slice(&13u16.to_le_bytes()); + bytes.extend_from_slice(&14u16.to_le_bytes()); bytes.extend_from_slice(&0u16.to_le_bytes()); bytes.extend_from_slice(&constant_count.to_le_bytes()); bytes.extend_from_slice(&(code.len() as u32).to_le_bytes()); @@ -1060,10 +1060,10 @@ fn validate_rejects_call_script_targeting_host_import_prototype() { } #[test] -fn call_script_wire_version_is_v13() { +fn call_script_wire_version_is_v14() { let program = Program::new(vec![], vec![vm::OpCode::Ret as u8]); let encoded = encode_program(&program).expect("encode should succeed"); - assert_eq!(u16::from_le_bytes([encoded[4], encoded[5]]), 13); + assert_eq!(u16::from_le_bytes([encoded[4], encoded[5]]), 14); let mut old = encoded; strip_empty_named_struct_section(&mut old); @@ -1083,7 +1083,7 @@ fn current_vmbc_preserves_code_bytes_without_script_calls() { bc.ret(); let program = Program::new(vec![Value::Int(1), Value::Int(2)], bc.finish()); let encoded = encode_program(&program).expect("encode should succeed"); - assert_eq!(u16::from_le_bytes([encoded[4], encoded[5]]), 13); + assert_eq!(u16::from_le_bytes([encoded[4], encoded[5]]), 14); let decoded = decode_program(&encoded).expect("decode should succeed"); assert_eq!(decoded.code, program.code); assert_eq!(decoded.constants, program.constants); @@ -1092,7 +1092,7 @@ fn current_vmbc_preserves_code_bytes_without_script_calls() { #[test] fn v12_trailing_zero_count_is_not_a_named_struct_table() { let program = Program::new(Vec::new(), vec![vm::OpCode::Ret as u8]); - let mut encoded = encode_program(&program).expect("v13 encoding should succeed"); + let mut encoded = encode_program(&program).expect("v14 encoding should succeed"); strip_empty_named_struct_section(&mut encoded); encoded[4..6].copy_from_slice(&12u16.to_le_bytes()); assert!(matches!( @@ -1112,7 +1112,7 @@ fn v12_trailing_zero_count_is_not_a_named_struct_table() { } #[test] -fn v13_roundtrip_preserves_guest_named_struct_payload() { +fn v14_roundtrip_preserves_guest_named_struct_payload() { let compiled = compile_source( r#" struct Point { x: int, y: int } @@ -1126,25 +1126,25 @@ fn v13_roundtrip_preserves_guest_named_struct_payload() { "codegen should attach guest struct decls" ); let encoded = encode_program(&compiled.program).expect("struct-bearing program should encode"); - assert_eq!(u16::from_le_bytes([encoded[4], encoded[5]]), 13); - assert_eq!(vm::bytecode::BYTECODE_ABI_VERSION, 13); - let decoded = decode_program(&encoded).expect("v13 named-struct section should decode"); + assert_eq!(u16::from_le_bytes([encoded[4], encoded[5]]), 14); + assert_eq!(vm::bytecode::BYTECODE_ABI_VERSION, 14); + let decoded = decode_program(&encoded).expect("v14 named-struct section should decode"); assert!( decoded.named_struct_decls().contains_key("Point"), - "VMBC v13 should preserve guest struct decls" + "VMBC v14 should preserve guest struct decls" ); } #[test] -fn result_schema_roundtrip_rejects_non_v13_unknown_tags_and_excess_depth() { +fn result_schema_roundtrip_rejects_non_current_unknown_tags_and_excess_depth() { let compiled = compile_source( "let value: Result = Result::Ok(7); match value { Result::Ok(v) => v, Result::Err(e) => -1 };", ) .expect("Result source should compile"); let encoded = encode_program(&compiled.program.with_local_count(compiled.locals)) .expect("Result program should encode"); - assert_eq!(u16::from_le_bytes([encoded[4], encoded[5]]), 13); - let decoded = decode_program(&encoded).expect("v13 Result program should decode"); + assert_eq!(u16::from_le_bytes([encoded[4], encoded[5]]), 14); + let decoded = decode_program(&encoded).expect("v14 Result program should decode"); let has_result = decoded.type_map.as_ref().is_some_and(|map| { map.local_schemas .iter() @@ -1193,3 +1193,105 @@ fn result_schema_roundtrip_rejects_non_v13_unknown_tags_and_excess_depth() { Err(WireError::LengthTooLarge("schema nesting depth", _)) )); } + +#[test] +fn option_and_unit_compiler_schemas_roundtrip_on_the_current_wire() { + let compiled = compile_source( + "let nested: Option> = Option::Some(Option::None);\n\ + let unit: () = ();\n\ + let value = match nested { Option::Some(inner) => match inner { Option::Some(v) => v, Option::None => 0 }, Option::None => -1 };\n\ + value;", + ) + .expect("Option source should compile"); + let encoded = encode_program(&compiled.program.with_local_count(compiled.locals)) + .expect("Option program should encode"); + assert_eq!(u16::from_le_bytes([encoded[4], encoded[5]]), 14); + let decoded = decode_program(&encoded).expect("current Option program should decode"); + let maps = decoded + .type_map + .as_ref() + .map(|map| &map.local_schemas) + .expect("Option program should keep local schemas"); + assert!( + maps.iter() + .any(|schema| matches!(schema, Some(TypeSchema::Option(_)))) + ); + assert!( + maps.iter() + .any(|schema| matches!(schema, Some(TypeSchema::Unit))) + ); + let mut machine = vm::Vm::new(decoded.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + assert_eq!(machine.run().unwrap(), vm::VmStatus::Halted); + assert_eq!(machine.stack().last(), Some(&Value::Int(0))); +} + +#[test] +fn nested_option_result_schemas_roundtrip_on_the_current_wire() { + let compiled = compile_source( + "let option_result: Option> = Option::Some(Result::Ok(1));\n\ + let result_option: Result, string> = Result::Ok(Option::Some(2));\n\ + let first = match option_result { Option::Some(result) => match result { Result::Ok(value) => value, Result::Err(error) => -1 }, Option::None => -2 };\n\ + let second = match result_option { Result::Ok(option) => match option { Option::Some(value) => value, Option::None => -4 }, Result::Err(error) => -8 };\n\ + first + second;", + ) + .expect("nested Option/Result source should compile"); + let encoded = encode_program(&compiled.program.with_local_count(compiled.locals)) + .expect("nested Option/Result program should encode"); + assert_eq!(u16::from_le_bytes([encoded[4], encoded[5]]), 14); + let decoded = decode_program(&encoded).expect("nested Option/Result program should decode"); + let schemas = decoded + .type_map + .as_ref() + .map(|map| &map.local_schemas) + .expect("nested program should keep local schemas"); + assert!(schemas.iter().any(|schema| { + matches!( + schema, + Some(TypeSchema::Option(inner)) + if matches!(inner.as_ref(), TypeSchema::Result { .. }) + ) + })); + assert!(schemas.iter().any(|schema| { + matches!( + schema, + Some(TypeSchema::Result { ok, .. }) + if matches!(ok.as_ref(), TypeSchema::Option(_)) + ) + })); + let mut machine = vm::Vm::new(decoded.with_local_count(compiled.locals)); + machine.set_fuel(100_000); + assert_eq!(machine.run().unwrap(), vm::VmStatus::Halted); + assert_eq!(machine.stack().last(), Some(&Value::Int(3))); +} + +#[test] +fn host_option_and_unit_schemas_roundtrip_on_the_current_wire() { + let function = HostFunctionSchema::with_return( + "wire::option_unit", + vec![HostParamSchema::value( + "value", + HostTypeSchema::Option(Box::new(HostTypeSchema::Array(Box::new( + HostTypeSchema::Int, + )))), + )], + HostTypeSchema::Unit, + ); + let mut builder = HostApiBuilder::new(); + builder.function(function.clone()); + let catalog = builder.build().expect("catalog"); + let schema = HostImportSchema::from_function(&catalog, &function); + let mut program = Program::new(Vec::new(), vec![vm::OpCode::Ret as u8]); + program.imports.push(HostImport { + name: "wire::option_unit".to_string(), + arity: 1, + return_type: ValueType::Unknown, + }); + let program = program + .with_host_import_schemas(vec![schema.clone()]) + .expect("schema alignment"); + let encoded = encode_program(&program).expect("Option/Unit host schema should encode"); + assert_eq!(u16::from_le_bytes([encoded[4], encoded[5]]), 14); + let decoded = decode_program(&encoded).expect("Option/Unit host schema should decode"); + assert_eq!(decoded.host_import_schemas()[0], Some(schema)); +}