| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | // Vile-specific protection controls for context-transfer changes. These use |
| 6 | // detached syntax objects to isolate declaration age from macro alias spelling. |
| 7 | // Authored only: this reviewer did not compile or execute these tests on Vile. |
| 8 | fn anchor_fence_review_builder(mut vm VM) &Builder { |
| 9 | code := vm.new_code(reader.read_all('') or { panic(err.msg()) }, vm.next_context(), unsafe { nil }, '') |
| 10 | return &Builder{ code: code, function: &Function{ code: code } } |
| 11 | } |
| 12 | |
| 13 | fn anchor_fence_review_identifier(vm &VM, binding SyntaxBinding, context &SyntaxLookupContext, wrap &SyntaxWrap) Value { |
| 14 | data := &SyntaxObjectData{ |
| 15 | owner: vm.owner |
| 16 | context: context |
| 17 | tree: &SyntaxTree{ |
| 18 | context: context |
| 19 | nodes: [SyntaxTreeNode{ kind: .symbol, car: 0 }] |
| 20 | identifiers: [SyntaxIdentifier{ |
| 21 | name: 'x' |
| 22 | raw_name: 'x' |
| 23 | binding: binding |
| 24 | wrap: wrap |
| 25 | context: context |
| 26 | }] |
| 27 | } |
| 28 | } |
| 29 | return wrapped_syntax(data, 0) or { panic(err.msg()) } |
| 30 | } |
| 31 | |
| 32 | fn test_anchor_fence_review_same_spelling_preserves_exact_and_canonical_declaration_fences() { |
| 33 | mut vm := new() |
| 34 | public_slot := vm.bind_global('x', integer(100)) |
| 35 | canonical := '\x00~anchor-context-review/canonical/x' |
| 36 | exact := '\x00~anchor-context-review/exact/x' |
| 37 | old_slot := vm.bind_global(canonical, integer(99)) |
| 38 | mut builder := anchor_fence_review_builder(mut vm) |
| 39 | mut expansion := SyntaxExpansion{ domain: builder.syntax_mark_domain() } |
| 40 | wrap := expansion.transcription_wrap(unsafe { nil }) |
| 41 | floor := builder.code.syntax_global_generation_floor() or { panic(err.msg()) } |
| 42 | pending := SyntaxBinding{ |
| 43 | kind: .global |
| 44 | name: 'x' |
| 45 | global_slot: public_slot |
| 46 | assignable: true |
| 47 | introduced: exact |
| 48 | introduced_key: canonical |
| 49 | introduced_namespace: builder.code.namespace |
| 50 | introduced_globals: vm.globals.len |
| 51 | introduced_generation: floor |
| 52 | } |
| 53 | context := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 54 | anchor := anchor_fence_review_identifier(vm, pending, context, wrap) |
| 55 | copy := datum_to_syntax(anchor, text_value(.symbol, 'x')) or { panic(err.msg()) } |
| 56 | copied := (syntax_identifier_info(copy) or { panic(err.msg()) }).binding |
| 57 | assert copied.introduced == exact |
| 58 | assert copied.introduced_key == canonical |
| 59 | assert copied.introduced_globals == pending.introduced_globals |
| 60 | assert copied.introduced_generation == floor |
| 61 | assert free_syntax_identifier_equal(anchor, copy) or { panic(err.msg()) } |
| 62 | assert bound_syntax_identifier_equal(anchor, copy) or { panic(err.msg()) } |
| 63 | // Resolution after compiler detachment must still reject the older private x. |
| 64 | builder.code.release_compile_borrows() |
| 65 | assert runtime_syntax_binding(vm.owner, copied).binding.global_slot == public_slot |
| 66 | generation := vm.owner.syntax_generation |
| 67 | container_store(mut vm.globals, old_slot, integer(123)) |
| 68 | assert vm.owner.syntax_generation == generation |
| 69 | assert runtime_syntax_binding(vm.owner, copied).binding.global_slot == public_slot |
| 70 | // A real declaration can reuse the old slot and still cross the birth fence. |
| 71 | assert vm.bind_global(canonical, integer(42)) == old_slot |
| 72 | assert runtime_syntax_binding(vm.owner, copied).binding.global_slot == old_slot |
| 73 | assert vm.globals[public_slot].whole() == 100 |
| 74 | } |
| 75 | |
| 76 | fn test_anchor_fence_review_old_import_rejected_but_fresh_private_alias_can_use_old_provider() { |
| 77 | mut vm := new() |
| 78 | provider := vm.new_environment() or { panic(err.msg()) } |
| 79 | vm.define_in(provider, 'source-x', integer(42)) or { panic(err.msg()) } |
| 80 | provider_space := vm.environment_space(provider) or { panic(err.msg()) } |
| 81 | original := vm.namespace_binding(provider_space, 'source-x') |
| 82 | public_slot := vm.bind_global('x', integer(100)) |
| 83 | canonical := '\x00~anchor-context-review/old-import/x' |
| 84 | exact := '\x00~anchor-context-review/new-import/x' |
| 85 | vm.install_import(unsafe { nil }, NamespaceImport{ name: canonical, binding: original }) |
| 86 | mut builder := anchor_fence_review_builder(mut vm) |
| 87 | mut expansion := SyntaxExpansion{ domain: builder.syntax_mark_domain() } |
| 88 | wrap := expansion.transcription_wrap(unsafe { nil }) |
| 89 | floor := builder.code.syntax_global_generation_floor() or { panic(err.msg()) } |
| 90 | pending := SyntaxBinding{ |
| 91 | kind: .global |
| 92 | name: 'x' |
| 93 | global_slot: public_slot |
| 94 | assignable: true |
| 95 | introduced: exact |
| 96 | introduced_key: canonical |
| 97 | introduced_namespace: builder.code.namespace |
| 98 | introduced_globals: vm.globals.len |
| 99 | introduced_generation: floor |
| 100 | } |
| 101 | context := builder.capture_syntax_lookup_context() or { panic(err.msg()) } |
| 102 | anchor := anchor_fence_review_identifier(vm, pending, context, wrap) |
| 103 | copy := datum_to_syntax(anchor, text_value(.symbol, 'x')) or { panic(err.msg()) } |
| 104 | copied := (syntax_identifier_info(copy) or { panic(err.msg()) }).binding |
| 105 | assert copied.introduced_globals == pending.introduced_globals |
| 106 | assert copied.introduced_generation == floor |
| 107 | builder.code.release_compile_borrows() |
| 108 | assert runtime_syntax_binding(vm.owner, copied).binding.global_slot == public_slot |
| 109 | assert !(free_syntax_identifier_equal(copy, vm.eval_in('(syntax source-x)', provider) or { panic(err.msg()) }) or { panic(err.msg()) }) |
| 110 | // The fresh destination declaration, not the old provider slot, crosses the |
| 111 | // fence. The pre-existing canonical import must remain insufficient by itself. |
| 112 | assert original.global_slot < copied.introduced_globals |
| 113 | vm.install_import(unsafe { nil }, NamespaceImport{ name: exact, binding: original }) |
| 114 | alias_location := syntax_global_location(vm.owner, unsafe { nil }, exact) or { panic('missing imported alias location') } |
| 115 | assert alias_location.generation >= floor |
| 116 | assert runtime_syntax_binding(vm.owner, copied).binding.global_slot == original.global_slot |
| 117 | assert !runtime_syntax_binding(vm.owner, copied).binding.assignable |
| 118 | assert free_syntax_identifier_equal(anchor, copy) or { panic(err.msg()) } |
| 119 | assert vm.globals[public_slot].whole() == 100 |
| 120 | } |
| 121 | |