crates.sage_stash.storage.immutable_entries

Allocated entries are immutable. Stash is append-only: alloc and alloc_slice may add entries, but safe code cannot mutate a Ptr<T> or Slice<T> allocation after it has been interned. Transient inference state is finalized by resolving and copying the completed value into a fresh output stash.

Read in specification context · design/crates/sage_stash/storage.sage

Implementation

Implemented crates/sage-ir/src/check/finalize.rs:24–29

sage_ir::check::finalize · lib:sage_ir

#![allow(unused)]
fn main() {
// {anchor}`crates.sage_stash.storage.immutable_entries`
pub(super) fn finalize_typed_body<'db>(
    source: &Stash,
    egraph: &VersionedEGraph<'db>,
    root: Ptr<TyExpr<'db>>,
    expr_slots: &[Ptr<TyExpr<'db>>],

}

See more source ↗

Implemented crates/sage-ir/src/check/infer/egraph.rs:306–311

sage_ir::check::infer::egraph · lib:sage_ir

#![allow(unused)]
fn main() {
    // {anchor}`crates.sage_stash.storage.immutable_entries`
    pub fn find(&self, version: Version, mut ty: Ptr<Ty<'db>>) -> Ptr<Ty<'db>> {
        self.versions.assert_live(version);
        loop {
            let parent = self.get_parent(version, ty);
            if parent == ty {

}

See more source ↗

Implemented crates/sage-stash/src/lib.rs:872–877

sage_stash · lib:sage_stash

#![allow(unused)]
fn main() {
    // {anchor}`crates.sage_stash.storage.immutable_entries`
    pub fn alloc<'db, T: AllocStashData<'db>>(&mut self, value: T) -> Ptr<T> {
        let type_id = TypeId::of::<T::StaticSelf>();
        let mut hasher = InternHasher::new();
        value.stash_hash(self, &mut hasher);
        let content_hash = hasher.finish();

}

See more source ↗

Tests and review

Indexed tests and the implementation-completeness and test-adequacy review; no test run is implied.

Implemented sage_ir::check::finalize::tests::finalized_nested_body_owns_resolved_and_imported_types_after_sources_drop

crates/sage-ir/src/check/finalize.rs · lib:sage_ir

#![allow(unused)]
fn main() {
#[test]
    #[sage::test(crates.sage_stash.storage.handle_ownership)]
    #[sage::test(crates.sage_stash.storage.immutable_entries)]
    #[sage::test(crates.sage_ir.types.body.no_inference)]
    fn finalized_nested_body_owns_resolved_and_imported_types_after_sources_drop() {
        use crate::cst::expr::Literal;

        let mut imported_stash = Stash::new();
        let imported_bool = imported_stash.alloc(Ty::Bool);
        let imported = Stashed::new(imported_stash, imported_bool);
        let mut source = Stash::new();
        source.alloc(123_u64);
        let boolean = imported.copy_into(&mut source);
        let mut egraph = VersionedEGraph::new();
        let variable = egraph.alloc_var(Version::ROOT, Universe::ROOT);
        let inference = source.alloc(Ty::InferVar(variable));
        try_unify(&mut egraph, &mut source, Version::ROOT, inference, boolean).unwrap();
        let span = RelativeSpan { start: 0, end: 4 };
        let child = source.alloc(TyExpr {
            data: TyExprData::Literal(Literal::Bool(true)),
            ty: inference,
            span,
        });
        let pending = source.alloc(TyExpr {
            data: TyExprData::Unresolved(ExprSlot(0)),
            ty: inference,
            span,
        });
        let elements = source.alloc_slice(&[pending, child]);
        let types = source.alloc_slice(&[inference, boolean]);
        let tuple = source.alloc(Ty::Tuple(types));
        let root = source.alloc(TyExpr {
            data: TyExprData::Tuple(elements),
            ty: tuple,
            span,
        });
        let completed = finalize_typed_body(&source, &egraph, root, &[child], &[], span);
        assert_eq!(source[inference], Ty::InferVar(variable));
        assert_eq!(source[types], [inference, boolean]);
        assert_eq!(source[elements], [pending, child]);
        assert_eq!(source[pending].data, TyExprData::Unresolved(ExprSlot(0)));
        assert_eq!(imported.root_deref(), Ty::Bool);
        drop(egraph);
        drop(source);
        drop(imported);

        let (
            target,
            TyBodyData {
                root,
                locals,
                span: completed_span,
            },
        ) = completed.open_deref();
        assert_eq!(completed_span, span);
        assert!(target[locals].is_empty());
        let TyExpr {
            data,
            ty,
            span: root_span,
        } = target[root];
        assert_eq!(root_span, span);
        let Ty::Tuple(types) = target[ty] else {
            panic!("expected tuple type")
        };
        assert_eq!(target[types].len(), 2);
        for ty in &target[types] {
            assert_eq!(target[*ty], Ty::Bool);
        }
        let TyExprData::Tuple(elements) = data else {
            panic!("expected tuple expression")
        };
        assert_eq!(target[elements].len(), 2);
        assert_eq!(target[elements][0], target[elements][1]);
        for child in &target[elements] {
            let TyExpr {
                data,
                ty,
                span: child_span,
            } = target[*child];
            assert_eq!(data, TyExprData::Literal(Literal::Bool(true)));
            assert_eq!(target[ty], Ty::Bool);
            assert_eq!(child_span, span);
        }
    }
}

Conditional source: #[cfg(test)]

See more source ↗

Implemented sage_stash::appending_and_deduplicating_preserves_existing_entries

crates/sage-stash/tests/storage_contracts.rs · test:storage_contracts

#![allow(unused)]
fn main() {
#[test]
#[sage::test(crates.sage_stash.storage.immutable_entries)]
fn appending_and_deduplicating_preserves_existing_entries() {
    let mut stash = Stash::new();
    let value = stash.alloc(42_u32);
    let slice = stash.alloc_slice(&[1_u32, 2, 3]);
    for n in 0..4096_u64 {
        stash.alloc(n);
    }
    assert_eq!(stash[value], 42);
    assert_eq!(stash[slice], [1, 2, 3]);
    assert_eq!(value, stash.alloc(42_u32));
    assert_eq!(slice, stash.alloc_slice(&[1_u32, 2, 3]));
}
}

See more source ↗

Implemented sage_stash::safe_api_rejects_mutating_interned_scalars_and_slices

crates/sage-stash/tests/storage_contracts.rs · test:storage_contracts

#[test]
#[sage::test(crates.sage_stash.storage.immutable_entries)]
fn safe_api_rejects_mutating_interned_scalars_and_slices() {
    // Keep the rejected operations in the reviewed snapshot as well as the fixtures.
    assert_eq!(
        include_str!("ui/mutate_scalar.rs"),
        r#"use sage_stash::Stash;

fn main() {
    let mut stash = Stash::new();
    let value = stash.alloc(42_u32);
    stash[value] = 99;
}
"#
    );
    assert_eq!(
        include_str!("ui/mutate_slice.rs"),
        r#"use sage_stash::Stash;

fn main() {
    let mut stash = Stash::new();
    let values = stash.alloc_slice(&[1_u32, 2]);
    stash[values][0] = 99;
}
"#
    );
    let tests = trybuild::TestCases::new();
    tests.compile_fail("tests/ui/mutate_scalar.rs");
    tests.compile_fail("tests/ui/mutate_slice.rs");
}

See more source ↗

Review: Accepted · final_quality.

Reviewed all three implementation entries and all three complete associated tests for immutable_entries. Requirements are append-only allocation, no safe mutable access to interned scalar/slice entries, and finalization of transient inference by resolution/copy into fresh storage. Captured storage-guide and architectural rationale specify that mutable inference relationships live outside the stash.

sage-stash::alloc and alloc_slice compute typed/content/count keys, return the existing entry for equal content, or append via push_raw. push_raw pads and extends the byte buffer and appends entry metadata; it does not overwrite earlier entries. Buffer growth copies the existing byte prefix, and resize_zeroed explicitly requires nondecreasing length. append_one constructs a new value vector and interns it rather than modifying the original slice. The public indexing implementations return shared references only; no IndexMut or mutable entry accessor is exposed. Stashed also exposes shared storage views, so retained results cannot be edited through those APIs.

The associated egraph::find follows version-local parent maps under shared access. Mutating inference operations update separate egraph maps; they do not replace Ty::InferVar allocations in the source stash. finalize_typed_body takes shared source/egraph references, constructs a new target stash, resolves each source type via find and expression slots via the completion table, and recursively allocates rebuilt body nodes. Its source memoization tables and target allocations are separate; unresolved infer variables panic instead of mutating source nodes into a result. The inspected copy paths include type/alias/call arguments, expression/statement/pattern slices and fields. This is an ownership/immutability assessment, not a review of authored expression semantics.

appending_and_deduplicating_preserves_existing_entries checks an existing scalar and slice after 4096 additions, then verifies reinterning equal values returns the original handles. safe_api_rejects_mutating_interned_scalars_and_slices visibly pins both attempted assignments in its body, runs trybuild compile_fail on both fixtures, and captured .stderr files require E0594 because IndexMut is absent. Thus compiler rejection is the intended API evidence, not an accidental runtime failure. finalized_nested_body_owns_resolved_and_imported_types_after_sources_drop checks source inference node, type/child slices, pending expression slot and imported bool remain unchanged after finalization; after dropping all source/proof storage it walks the destination tuple and literal children and asserts resolved bool types, stable spans and shared completed children. Together the tests exercise append/dedup stability, inaccessible safe mutation, and fresh finalization without in-place rewriting. They do not constitute an exhaustive proof for arbitrary unsafe client implementations; the claim is the safe allocated-entry API contract.

No TODO/N/A entries are associated. No source edits or tests executed in this review; commands were fresh review start/read and source inspection before record. The captured UI fixtures and expected diagnostics were inspected, not rerun. No requested edits.

Browse crates anchors

139 anchors

Specification anchors and declared coverage
AnchorImplementationTests
crates (139)114 Implemented37 TODO2 N/A74 Implemented107 TODO
crates.sage_inspector (15)13 Implemented1 TODO2 N/A3 Implemented15 TODO
crates.sage_inspector.inspection (15)13 Implemented1 TODO2 N/A3 Implemented15 TODO
balanced_tracesImplementedTODO
canonical_ownershipImplementedImplementedTODO
derive_reflectionImplementedImplementedTODO
detail_free_indexImplementedTODO
deterministic_traceImplementedTODO
durable_navigationN/ATODO
generic_browserN/ATODO
owned_reflectionImplementedImplementedTODO
product_availabilityImplementedTODO
revision_and_demandImplementedTODO
semantic_phase_boundaryImplementedTODO
source_evidenceImplementedTODO
typed_serviceImplementedTODO
unknown_workImplementedTODOTODO
workspace_actorImplementedTODO
crates.sage_ir (90)67 Implemented34 TODO40 Implemented88 TODO
crates.sage_ir.checking (12)12 Implemented3 TODO5 Implemented12 TODO
crates.sage_ir.checking.body (5)5 Implemented2 TODO3 Implemented5 TODO
elaborationImplementedImplementedTODO
expected_typesImplementedTODOTODO
interface_dependenciesImplementedImplementedTODO
isolated_completionImplementedImplementedTODO
operator_callsImplementedTODOTODO
identity_mintingImplementedTODO
owned_productsImplementedImplementedTODO
crates.sage_ir.checking.signature (4)4 Implemented1 TODO1 Implemented4 TODO
body_independenceImplementedTODO
narrow_detailImplementedTODOTODO
owner_binderImplementedImplementedTODO
storageImplementedTODO
symbol_keyed_interfacesImplementedTODO
crates.sage_ir.crate (9)9 TODO9 TODO
explicit_restrictionsTODOTODO
identity_before_detailTODOTODO
incremental_evidenceTODOTODO
narrow_demandsTODOTODO
reexecution_and_invalidationTODOTODO
represented_completenessTODOTODO
restrictions_and_gapsTODOTODO
semantic_query_boundariesTODOTODO
terminal_incompletenessTODOTODO
crates.sage_ir.inference (8)8 Implemented6 Implemented8 TODO
atomic_checkingImplementedImplementedTODO
body_local_quiescenceImplementedImplementedTODO
canonical_boundaryImplementedImplementedTODO
completed_body_boundaryImplementedTODO
numeric_domainsImplementedImplementedTODO
subtyping_and_coercionImplementedImplementedTODO
terminal_obligationsImplementedTODO
transaction_isolationImplementedImplementedTODO
crates.sage_ir.metadata (4)4 Implemented1 TODO4 TODO
explicit_incompletenessImplementedTODO
keyed_reuseImplementedTODOTODO
product_boundariesImplementedTODO
provider_authorityImplementedTODO
crates.sage_ir.modules (8)7 Implemented1 TODO4 Implemented8 TODO
crates.sage_ir.modules.expand (4)4 Implemented2 Implemented4 TODO
completenessImplementedImplementedTODO
dependenciesImplementedTODO
direct_membersImplementedTODO
recursion_and_fixpointImplementedImplementedTODO
crates.sage_ir.modules.parse (4)3 Implemented1 TODO2 Implemented4 TODO
dependenciesImplementedTODO
detail_firewallImplementedImplementedTODO
recovery_completenessTODOTODO
syntax_identitiesImplementedImplementedTODO
crates.sage_ir.resolution (5)5 Implemented1 TODO3 Implemented4 TODO
crates.sage_ir.resolution.lookup (5)5 Implemented1 TODO3 Implemented4 TODO
completenessImplementedTODOImplementedTODO
dependenciesImplementedTODO
expansion_import_ambiguityImplementedImplemented
identity_onlyImplementedTODO
precedenceImplementedImplementedTODO
crates.sage_ir.solver (9)7 Implemented7 TODO5 Implemented9 TODO
alternative_isolationImplementedTODOImplementedTODO
bounded_completenessImplementedTODOTODO
candidate_completenessImplementedTODO
canonical_cyclesTODOTODO
canonical_outputsImplementedImplementedTODO
local_index_firewallTODOTODO
normalization_demandImplementedTODOImplementedTODO
schedule_independenceImplementedTODOImplementedTODO
shared_final_resultImplementedTODOImplementedTODO
crates.sage_ir.symbols (8)8 Implemented4 Implemented7 TODO
associated_identityImplementedImplemented
associated_interfacesImplementedTODO
body_interfacesImplementedImplementedTODO
definition_identityImplementedTODO
external_fact_queriesImplementedTODO
external_identityImplementedTODO
kind_wrappersImplementedImplementedTODO
local_identityImplementedImplementedTODO
crates.sage_ir.types (27)16 Implemented12 TODO13 Implemented27 TODO
crates.sage_ir.types.aliases (2)1 Implemented1 TODO1 Implemented2 TODO
identityImplementedImplementedTODO
revelationTODOTODO
crates.sage_ir.types.binder (1)1 Implemented1 TODO1 Implemented1 TODO
owner_parametersImplementedTODOImplementedTODO
crates.sage_ir.types.body (13)4 Implemented9 TODO3 Implemented13 TODO
awaitTODOTODO
closuresTODOTODO
conditional_patternsTODOTODO
coroutinesTODOTODO
diagnosed_recoveryImplementedImplementedTODO
elaboratedTODOImplementedTODO
for_iterationTODOTODO
indexingTODOTODO
macro_expansionTODOTODO
no_inferenceImplementedImplementedTODO
resolved_referencesImplementedTODO
try_residualTODOTODO
typed_provenanceImplementedTODO
crates.sage_ir.types.calls (2)1 Implemented1 TODO1 Implemented2 TODO
dispatch_familiesTODOTODO
selected_semanticsImplementedImplementedTODO
crates.sage_ir.types.lifetimes (2)2 Implemented2 Implemented2 TODO
dummyImplementedImplementedTODO
unchecked_borrowsImplementedImplementedTODO
crates.sage_ir.types.operators (2)2 Implemented1 Implemented2 TODO
lazy_control_flowImplementedTODO
trait_callsImplementedImplementedTODO
scoped_identitiesImplementedTODO
crates.sage_ir.types.spans (4)4 Implemented4 Implemented4 TODO
generated_identityImplementedImplementedTODO
item_originImplementedImplementedTODO
offset_reuseImplementedImplementedTODO
relative_compositionImplementedImplementedTODO
crates.sage_oracle_harness (4)4 Implemented2 TODO1 Implemented4 TODO
crates.sage_oracle_harness.crate (4)4 Implemented2 TODO1 Implemented4 TODO
deterministic_emissionImplementedTODO
exact_comparisonImplementedTODOImplementedTODO
fixed_projectionImplementedTODO
independent_coverageImplementedTODOTODO
crates.sage_stash (5)5 Implemented5 Implemented
crates.sage_stash.storage (5)5 Implemented5 Implemented
handle_ownershipImplementedImplemented
immutable_entriesImplementedImplemented
query_valueImplementedImplemented
rooted_equalityImplementedImplemented
typed_interningImplementedImplemented
crates.sage_test_harness (25)25 Implemented25 Implemented
crates.sage_test_harness.compile_tests (25)25 Implemented25 Implemented
annotation_validationImplementedImplemented
blessing_gateImplementedImplemented
callsite_updatesImplementedImplemented
checking_scopeImplementedImplemented
compiler_conditionsImplementedImplemented
completed_typesImplementedImplemented
coordinatesImplementedImplemented
dependenciesImplementedImplemented
diagnostic_countsImplementedImplemented
diagnostic_locationsImplementedImplemented
editionImplementedImplemented
fixturesImplementedImplemented
grouped_observationsImplementedImplemented
invocationImplementedImplemented
production_executionImplementedImplemented
project_isolationImplementedImplemented
query_captureImplementedImplemented
query_projectionImplementedImplemented
renderingImplementedImplemented
resolved_symbolsImplementedImplemented
review_visibilityImplementedImplemented
rustc_executionImplementedImplemented
rustc_lintsImplementedImplemented
starting_expressionsImplementedImplemented
target_lineImplementedImplemented