feat(pbs): add parametric arrays and concrete generic contracts
Support explicit contract type parameters, canonical nominal applications, recursive substitution, concrete implementations and instantiated editor views. Complete parametric Array syntax and contextual construction integration. Preserve type origins across imports and reexports, fix trait argument stack order, and relocate global slots before lifecycle assembly. Publish specs, installed examples and consolidated discussion lessons. Validate frontend, common pipeline, architecture, LSP and installed knowledge gates, plus the full production VM conformance corpus with 14 generic fixtures.
This commit is contained in:
@@ -36,6 +36,7 @@ impl NativeInterface for NoNative {
|
||||
|
||||
fn main() {
|
||||
let root = std::path::PathBuf::from(std::env::args().nth(1).expect("fixture directory"));
|
||||
if std::env::args().any(|arg| arg == "--generic-contracts-only") { verify_pbs_generic_contract_fixtures(&root); return; }
|
||||
if std::env::args().any(|arg| arg == "--random-only") { verify_pbs_random_fixtures(&root); return; }
|
||||
verify_pbs_math_fixtures(&root);
|
||||
if std::env::args().any(|arg| arg == "--math-only") { return; }
|
||||
@@ -43,6 +44,7 @@ fn main() {
|
||||
verify_catalog_fixtures(&root);
|
||||
verify_pbs_fixtures(&root);
|
||||
verify_pbs_structured_fixtures(&root);
|
||||
verify_pbs_generic_contract_fixtures(&root);
|
||||
verify_pbs_resource_refusals(&root);
|
||||
verify_pbs_closure_frames(&root);
|
||||
for name in [
|
||||
@@ -479,7 +481,7 @@ fn verify_pbs_fixtures(root: &std::path::Path) {
|
||||
for entry in std::fs::read_dir(root).unwrap() {
|
||||
let path = entry.unwrap().path();
|
||||
let name = path.file_stem().unwrap().to_str().unwrap();
|
||||
if name.starts_with("pbs-random-") || name.starts_with("pbs-math-") || name.starts_with("pbs-structured-") || !name.starts_with("pbs-") || path.extension().and_then(|v| v.to_str()) != Some("pbx") { continue; }
|
||||
if name.starts_with("pbs-random-") || name.starts_with("pbs-math-") || name.starts_with("pbs-structured-") || name.starts_with("pbs-generic-contract-") || !name.starts_with("pbs-") || path.extension().and_then(|v| v.to_str()) != Some("pbx") { continue; }
|
||||
count += 1;
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE)
|
||||
@@ -547,7 +549,7 @@ fn verify_pbs_fixtures(root: &std::path::Path) {
|
||||
|
||||
fn verify_pbs_structured_fixtures(root: &std::path::Path) {
|
||||
use prometeu_vm::LogicalFrameEndingReason as End;
|
||||
let success = ["closure-import", "closure-installed", "closure-groups", "closure-isolation", "closure-fields", "closure-functions", "closure-snapshot", "closure-objects", "closure-nested", "array-cycle", "array-nested", "optional-flow", "struct-contract", "service-contract",
|
||||
let success = ["closure-import", "closure-installed", "closure-groups", "closure-isolation", "closure-fields", "closure-functions", "closure-snapshot", "closure-objects", "closure-nested", "array-cycle", "array-nested", "array-parametric", "optional-flow", "struct-contract", "service-contract",
|
||||
"optional-tuple", "tuple-storage", "optional-intrinsics", "optional-lazy", "array-context",
|
||||
"binding-scope", "overloads", "single-evaluation", "long-boundaries", "gc-roots", "native-groups", "contract-elements", "imports", "global-array", "installed-example", "return-overloads", "inferred-optional-tuple", "inline-integers"];
|
||||
for name in success.into_iter().chain(["negative-index", "past-end", "negative-length", "large-length"]) {
|
||||
@@ -590,6 +592,111 @@ fn verify_pbs_structured_fixtures(root: &std::path::Path) {
|
||||
}
|
||||
}
|
||||
|
||||
fn verify_pbs_generic_contract_fixtures(root: &std::path::Path) {
|
||||
use prometeu_vm::{LogicalFrameEndingReason as End, resource_budget::{RuntimeLimits, RuntimeResource}};
|
||||
use prometeu_bytecode::{registry::{TypeId, TypeKind}, isa::core::CoreOpCode};
|
||||
let mut reference_heap = None;
|
||||
for name in ["identity", "manual", "multiple", "tuple", "optional", "function", "contract", "native", "recursive", "phantom", "receiver", "gc", "installed", "imports"] {
|
||||
let bytes = std::fs::read(root.join(format!("pbs-generic-contract-{name}.pbx")))
|
||||
.unwrap_or_else(|error| panic!("missing mandatory generic-contract fixture {name}: {error}"));
|
||||
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE)
|
||||
.unwrap_or_else(|error| panic!("generic contract {name}: {error:?}"));
|
||||
let declarations = image.registry().declarations();
|
||||
let traits: Vec<_> = declarations.types.iter().enumerate().filter(|(_, t)| matches!(t.kind, TypeKind::Trait(_))).collect();
|
||||
assert!(!traits.is_empty(), "{name}: source must emit concrete traits");
|
||||
for (index, _) in &traits {
|
||||
assert_eq!(image.registry().type_layout(TypeId(*index as u32)).unwrap().storage.default_values(), &[Value::Null, Value::Null], "{name}: absent concrete trait");
|
||||
}
|
||||
assert!(!declarations.implementations.is_empty());
|
||||
// The first dense implementation is valid; zero must never act as an absence sentinel.
|
||||
assert_eq!(image.registry().implementation(declarations.implementations[0].owner, declarations.implementations[0].trait_id).unwrap().0, 0);
|
||||
if name == "multiple" || name == "phantom" {
|
||||
assert_eq!(traits.len(), 2);
|
||||
assert_eq!(declarations.implementations.len(), 2);
|
||||
assert_eq!(declarations.implementations[0].owner, declarations.implementations[1].owner);
|
||||
assert_ne!(declarations.implementations[0].trait_id, declarations.implementations[1].trait_id);
|
||||
}
|
||||
if name == "imports" { assert_eq!(traits.len(), 3); assert_eq!(declarations.implementations.len(), 3); }
|
||||
if name == "phantom" {
|
||||
let mut code = image.code().to_vec();
|
||||
let mut pc = 0;
|
||||
let mut changed = false;
|
||||
while pc < code.len() {
|
||||
let instruction = prometeu_bytecode::decode_next(pc, &code).unwrap();
|
||||
if instruction.opcode == CoreOpCode::CallTrait {
|
||||
let (original, _) = instruction.imm_u32x2().unwrap();
|
||||
let other = traits.iter().map(|(index, _)| *index as u32).find(|id| *id != original).unwrap();
|
||||
code[pc+2..pc+6].copy_from_slice(&other.to_le_bytes());
|
||||
changed = true; break;
|
||||
}
|
||||
pc = instruction.next_pc;
|
||||
}
|
||||
assert!(changed);
|
||||
let corrupt = replace(&bytes, 2, &code);
|
||||
match ValidatedProgramImage::load(&corrupt, &mut LoadBudget::new(LoadLimits::default()), caps::NONE) {
|
||||
Err(error) => record_fault(root, "generic-contract-wrong-view", error),
|
||||
Ok(image) => {
|
||||
// Unknown receiver facts may defer rejection to the mandatory runtime check.
|
||||
let mut invalid = VirtualMachine::from_image(image, RuntimeLimits::default());
|
||||
invalid.prepare_boot_call().unwrap();
|
||||
let report = invalid.run_budget(100_000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
||||
assert!(matches!(report.reason, End::Trap(ref fault) if fault.code == prometeu_bytecode::TRAP_TYPE), "mismatched concrete view: {report:?}");
|
||||
}
|
||||
}
|
||||
println!("PBS GENERIC WRONG-VIEW REJECTION PASS");
|
||||
}
|
||||
println!("PBS GENERIC METADATA {name}: types={} implementations={} bytes={}", declarations.types.len(), declarations.implementations.len(), bytes.len());
|
||||
let limits = if name == "identity" || name == "manual" {
|
||||
RuntimeLimits::default().lowered(RuntimeResource::HandleSlots, 1).unwrap()
|
||||
} else { RuntimeLimits::default() };
|
||||
let mut vm = VirtualMachine::from_image(image, limits);
|
||||
let observer = vm.resource_observer();
|
||||
vm.prepare_boot_call().unwrap();
|
||||
let mut report = vm.run_budget(100_000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
||||
if name == "gc" {
|
||||
assert!(matches!(report.reason, End::FrameSync), "{name}: {report:?}");
|
||||
let gc = vm.resource_snapshot().gc;
|
||||
assert!(gc.collections >= 1 && gc.reclaimed >= 1100, "{gc:?}");
|
||||
report = vm.run_budget(100_000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
||||
}
|
||||
if name == "imports" {
|
||||
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "{name}: {report:?}");
|
||||
} else {
|
||||
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "{name}: {report:?}");
|
||||
assert_eq!(vm.operand_stack_top(1), vec![Value::Int32(42)], "{name}");
|
||||
}
|
||||
if name == "identity" || name == "manual" {
|
||||
assert_eq!(vm.heap().len(), 1, "no generic argument box or view wrapper");
|
||||
let heap = observer.used(RuntimeResource::GuestBytes);
|
||||
if let Some(reference) = reference_heap { assert_eq!(heap, reference, "generic and manual contract heap footprints"); }
|
||||
reference_heap = Some(heap);
|
||||
}
|
||||
drop(vm);
|
||||
for resource in [RuntimeResource::GuestBytes, RuntimeResource::HandleSlots, RuntimeResource::AuxiliaryBytes,
|
||||
RuntimeResource::TemporaryBytes, RuntimeResource::StringBytes, RuntimeResource::StringAllocations] {
|
||||
assert_eq!(observer.used(resource), 0, "{name}: release {resource:?}");
|
||||
}
|
||||
if name == "identity" || name == "receiver" {
|
||||
for (resource, limit) in [(RuntimeResource::GuestBytes, 0), (RuntimeResource::GuestBytes, 1), (RuntimeResource::GuestBytes, 2), (RuntimeResource::HandleSlots, 0)] {
|
||||
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
||||
let mut refused = VirtualMachine::from_image(image, RuntimeLimits::default().lowered(resource, limit).unwrap());
|
||||
let observed = refused.resource_observer();
|
||||
refused.prepare_boot_call().unwrap();
|
||||
let report = refused.run_budget(100_000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
||||
assert!(matches!(report.reason, End::Trap(ref fault) if fault.code == prometeu_bytecode::TRAP_RESOURCE_LIMIT), "{name}/{resource:?}: {report:?}");
|
||||
assert_eq!(refused.heap().len(), 0, "refusal cannot publish partial objects");
|
||||
assert!(observed.used(resource) <= limit);
|
||||
drop(refused);
|
||||
for domain in [RuntimeResource::GuestBytes, RuntimeResource::HandleSlots, RuntimeResource::AuxiliaryBytes, RuntimeResource::TemporaryBytes] {
|
||||
assert_eq!(observed.used(domain), 0);
|
||||
}
|
||||
}
|
||||
reject(root, &format!("generic-contract-{name}-load-quota"), &bytes, LoadLimits { implementations: 0, ..LoadLimits::default() });
|
||||
}
|
||||
println!("PBS GENERIC SOURCE/LOAD/EXEC PASS {name}");
|
||||
}
|
||||
}
|
||||
|
||||
fn verify_pbs_resource_refusals(root: &std::path::Path) {
|
||||
use prometeu_vm::{LogicalFrameEndingReason as End, resource_budget::{RuntimeLimits, RuntimeResource}};
|
||||
for name in ["closure-objects", "array-cycle", "tuple-storage", "struct-contract"] {
|
||||
|
||||
Reference in New Issue
Block a user