Refactor reference types: Replace Rc<RefCell<T>> with dynamic reference system#55
Draft
dhedey wants to merge 11 commits intofeat/references-reworkfrom
Draft
Refactor reference types: Replace Rc<RefCell<T>> with dynamic reference system#55dhedey wants to merge 11 commits intofeat/references-reworkfrom
dhedey wants to merge 11 commits intofeat/references-reworkfrom
Conversation
Replace the old Rc<RefCell<T>>-based reference system with the new custom dynamic reference types: - Shared<T> becomes a type alias for SharedReference<T> - Mutable<T> becomes a type alias for MutableReference<T> - DisabledShared<T> becomes InactiveSharedReference<T> - DisabledMutable<T> becomes InactiveMutableReference<T> - VariableContent::Referenceable uses new Referenceable struct Key changes: - Added bridge methods (_legacy suffix) to SharedReference and MutableReference for backward compatibility with old map/try_map patterns that didn't require PathExtension - QqqShared<T> and QqqMutable<T> now alias directly to the reference types, collapsing the old form/wrapper type distinction - Added explicit IsArgument impls for SharedReference<AnyValue> and MutableReference<AnyValue> since the blanket impl only covers leaf types - Manual Clone impls for InactiveSharedReference/InactiveMutableReference to avoid unnecessary T: Clone bound from derive - CopyOnWrite conversions use replace_legacy pattern instead of into_content() for non-leaf types - Updated trybuild test for improved aliasing error message from new reference tracking system https://claude.ai/code/session_01XaCsWwXFYntkKqZNf8kizU
…d SpanRange Replace all _legacy bridge methods (map_legacy, try_map_legacy, map_optional_legacy, replace_legacy, emplace_unchecked_legacy) with calls using proper PathExtension values and real SpanRange values. Key changes: - Add current_span() to EmplacerCore, SharedEmplacerV2, MutableEmplacerV2, SharedReference, MutableReference, AnyRefEmplacer, and AnyMutEmplacer - Use PathExtension::Tightened(T::type_kind()) for type-narrowing operations (leaf_to_dyn, into_shared, into_mutable, into_assignee, etc.) - Use PathExtension::Child(ObjectChild(name), AnyType) for property access - Use real spans from emplacer.current_span() instead of Span::call_site() - Make AnyRef/AnyMut map methods and emplacers take PathExtension + SpanRange - Remove all _legacy bridge methods from SharedReference and MutableReference https://claude.ai/code/session_01XaCsWwXFYntkKqZNf8kizU
- Introduce MappedRef and MappedMut types with unsafe constructors to confine unsafety to construction rather than use sites - Update SharedReference/MutableReference map/try_map to accept closures returning MappedRef/MappedMut, making callers safe - Fix &mut aliasing UB in AnyMut::emplace_map by storing *mut T in emplacer state instead of duplicate &mut T - Have PropertyAccessInterface/IndexAccessInterface return MappedRef/MappedMut with proper ChildSpecifier (ObjectChild/ArrayChild) - Add output_span_range to PropertyAccessCallContext/IndexAccessCallContext - Thread SpanRange through DynResolveFrom, leaf_to_dyn, and from_argument_value to eliminate None span arguments - Remove all TODO[references] comments https://claude.ai/code/session_01XaCsWwXFYntkKqZNf8kizU
…ields private - Make MappedRef/MappedMut fields private, add into_parts() for decomposition. This preserves the invariant that construction must go through the unsafe new() constructor. - Add emplace() method back to SharedEmplacer/MutableEmplacer with lifetime-checked &'e V parameter, keeping emplace_unchecked for cases where the compiler can't prove the lifetime (e.g. __InlineMapper). - Switch callers from emplace_unchecked to emplace where the lifetime is available: SharedReference::map/try_map, MutableReference::map/ try_map, AnyRef::map_optional, AnyMut::map_optional, and leaf_to_dyn implementations in shared.rs, mutable.rs, and assignee.rs. https://claude.ai/code/session_01XaCsWwXFYntkKqZNf8kizU
…ectly - Move MappedRef to shared_reference.rs and MappedMut to mutable_reference.rs - Add new_unchecked constructors for lifetime transmute cases (__InlineMapper) - Change all emplacer emplace methods to take MappedRef/MappedMut, making them safe - Drop emplace_unchecked from all emplacers (SharedEmplacer, MutableEmplacer, AnyRefEmplacer, AnyMutEmplacer) - All unsafe is now confined to MappedRef/MappedMut constructors https://claude.ai/code/session_01XaCsWwXFYntkKqZNf8kizU
The swap_itself.stderr error was pointing to `let a = "a"` instead of the second `a` in `a.swap(a)` because enable() ignored its span parameter and creation_span was never updated from the root span. - Add set_tracked_span to SharedReference, MutableReference, and their inactive variants - Update enable() to call set_tracked_span before activate(), so borrow-conflict errors point to the usage site - Update VariableBinding::into_mut/into_shared/into_late_bound to set the tracked span before initial activation too https://claude.ai/code/session_01XaCsWwXFYntkKqZNf8kizU
Move span-setting into activate() so callers don't need a separate set_tracked_span step. Remove enable() and set_tracked_span() from Inactive*Reference types. Add new_active_shared/new_active_mutable convenience methods to Referenceable. https://claude.ai/code/session_01XaCsWwXFYntkKqZNf8kizU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR replaces the old
Rc<RefCell<T>>based reference system with a new dynamic reference tracking system. The changes introduceSharedReference<T>andMutableReference<T>as the primary reference types, backed by aReferenceCorethat tracks active/inactive states and reference metadata (creation spans, paths).Key Changes
SharedReference<T>andMutableReference<T>to replaceSharedSubRcRefCellandMutableSubRcRefCellShared<T>,Mutable<T>,DisabledShared<T>,DisabledMutable<T>) mapping old names to new reference types for gradual migrationReferenceablefrom a generic type alias to a concrete struct that wrapsAnyValue, with methods to create inactive references and attempt unwrappingcurrent_span()methods to reference types and emplacers to preserve span information during type narrowing operationsemplace()andemplace_unchecked()methods to acceptPathExtensionandSpanRangeparameters for accurate error reportingunsafewith requirements for correctPathExtensionvaluesQqqShared<T>andQqqMutable<T>type aliases to use new reference types; updatedIsDynCompatibleFormimplementations to pass path and span informationMutable<T>andShared<T>struct definitions and their trait implementations, now delegating to the new reference typesNotable Implementation Details
PathExtensionis used to describe how a reference navigates through the value hierarchy (e.g.,Childfor property access,Tightenedfor type narrowing)InactiveSharedReferenceandInactiveMutableReferencecan be cloned and later activated, enabling flexible reference managementdisable()andenable()maintain API compatibility with the old systemReferenceabletype is now non-generic and always wrapsAnyValue, simplifying the reference creation APIhttps://claude.ai/code/session_01XaCsWwXFYntkKqZNf8kizU