module scheme import arena import reader enum CompilerScratchAttempt { quasiquote_constructor quasiquote_jobs quasiquote_marks datum_constructor datum_jobs template_analysis template_indices template_emission value_reference value_symbol value_conversion capture_spelling input_origin_keep hydration_jobs marked_lookup } struct CompilerScratchFixture { storage arena.Arena wrap &SyntaxWrap mut: quasiquote QQGraphScratch datum DatumGraphJobs render SyntaxCaseRenderState source SyntaxValueSource analysis []i64 origins SyntaxInputOrigins hydrator SyntaxHydrator context &SyntaxLookupContext } fn attempt_compiler_scratch(which CompilerScratchAttempt, mut fixture CompilerScratchFixture, mut result BoundedResult) { defer { if message := recover() { result.exhausted = arena.allocation_failure() or { panic(message) } } } match which { .quasiquote_constructor { mut scratch := new_qq_graph_scratch() scratch.release() } .quasiquote_jobs { fixture.quasiquote.add(reader.NodeId(0), 0, false) } .quasiquote_marks { fixture.quasiquote.mark(reader.NodeId(0), 0, false) } .datum_constructor { mut jobs := new_datum_graph_jobs() jobs.release() } .datum_jobs { fixture.datum.add(reader.NodeId(0), true) } .template_analysis { template_analysis_push(mut fixture.analysis, fixture.storage, 0) } .template_indices { fixture.render.push(0) } .template_emission { fixture.render.acquire_emission(unsafe { nil }) } .value_reference { fixture.source.reference(integer(42)) or { panic(err) } } .value_symbol { fixture.source.symbol('answer') or { panic(err) } } .value_conversion { map_syntax_value_graph(integer(42), false, unsafe { nil }, unsafe { nil }, unsafe { nil }) or { panic(err) } } .capture_spelling { syntax_capture_spelling(fixture.wrap, fixture.storage, true) } .input_origin_keep { fixture.origins.keep(0, reader.NodeId(0)) } .hydration_jobs { fixture.hydrator.push(SyntaxHydrationJob{reader.NodeId(0), reader.NodeId(0)}) } .marked_lookup { fixture.context.binding_with_marks('answer', fixture.wrap) } } } fn check_compiler_scratch_failure(which CompilerScratchAttempt) { limits := if which in [.quasiquote_constructor, .datum_constructor, .value_conversion, .marked_lookup] { arena.ChunkLimits{ max_live_bytes: 0 } } else { arena.ChunkLimits{} } mut owner := arena.new(limits: limits) defer { owner.free() } mut storage := arena.new(limits: arena.ChunkLimits{ max_live_bytes: 0 }) defer { storage.free() } mut fixture := CompilerScratchFixture{ storage: storage wrap: &SyntaxWrap{ domain: unsafe { nil }, mark: 1 } quasiquote: QQGraphScratch{ storage: storage } datum: DatumGraphJobs{ storage: storage } render: SyntaxCaseRenderState{ storage: storage } source: SyntaxValueSource{ root: nil_value(), storage: storage } origins: SyntaxInputOrigins{ storage: storage } hydrator: SyntaxHydrator{ source: unsafe { nil } input: unsafe { nil } origins: unsafe { nil } storage: storage marks: SyntaxExpansion{ domain: unsafe { nil } } } context: &SyntaxLookupContext{} } mut result := BoundedResult{} owner.push() attempt_compiler_scratch(which, mut fixture, mut result) restored := owner.is_current() // Clean up the old implementation's selected scratch arena before reporting // a failed assertion. Caller-owned storage still has its ordinary defer. if !restored { borrowed := storage.is_current() child := v_arena_current() unsafe { v_arena_pop(child) if !borrowed { v_arena_free(child) } } } owner.pop() assert restored, '${which}' assert (result.exhausted or { panic('expected compiler scratch denial') }) == .chunk_limit } fn test_denied_quasiquote_scratch_construction_restores_allocator() { check_compiler_scratch_failure(.quasiquote_constructor) } fn test_denied_quasiquote_worklist_growth_restores_allocator() { check_compiler_scratch_failure(.quasiquote_jobs) } fn test_denied_quasiquote_mark_insertion_restores_allocator() { check_compiler_scratch_failure(.quasiquote_marks) } fn test_denied_datum_worklist_construction_restores_allocator() { check_compiler_scratch_failure(.datum_constructor) } fn test_denied_datum_worklist_growth_restores_allocator() { check_compiler_scratch_failure(.datum_jobs) } fn test_denied_template_analysis_growth_restores_allocator() { check_compiler_scratch_failure(.template_analysis) } fn test_denied_template_render_index_growth_restores_allocator() { check_compiler_scratch_failure(.template_indices) } fn test_denied_template_emission_creation_restores_allocator() { check_compiler_scratch_failure(.template_emission) } fn test_denied_syntax_value_reference_growth_restores_allocator() { check_compiler_scratch_failure(.value_reference) } fn test_denied_syntax_symbol_memo_insertion_restores_allocator() { check_compiler_scratch_failure(.value_symbol) } fn test_denied_syntax_value_facade_construction_restores_allocator() { check_compiler_scratch_failure(.value_conversion) } fn test_denied_syntax_mark_spelling_restores_allocator() { check_compiler_scratch_failure(.capture_spelling) } fn test_denied_procedural_input_origin_insertion_restores_allocator() { check_compiler_scratch_failure(.input_origin_keep) } fn test_denied_procedural_hydration_worklist_growth_restores_allocator() { check_compiler_scratch_failure(.hydration_jobs) } fn test_denied_marked_identifier_lookup_restores_allocator() { check_compiler_scratch_failure(.marked_lookup) }