Skip to content

Comments

[ty] use interior mutability in type visitors#19871

Merged
carljm merged 1 commit intomainfrom
cjm/visitor-interior-mutability
Aug 11, 2025
Merged

[ty] use interior mutability in type visitors#19871
carljm merged 1 commit intomainfrom
cjm/visitor-interior-mutability

Conversation

@carljm
Copy link
Contributor

@carljm carljm commented Aug 11, 2025

Summary

Type visitors are conceptually immutable, they just internally track the types they've seen (and some maintain a cache of results.) Passing around mutable visitors everywhere can get us into borrow-checker trouble in some cases, where we need to recursively pass along the visitor inside more than one closure with non-disjoint lifetime.

Use interior mutability (via RefCell and Cell) inside the visitors instead, to allow us to pass around shared references.

Test Plan

Existing tests.

@carljm carljm added internal An internal refactor or improvement ty Multi-file analysis & type inference labels Aug 11, 2025
@carljm carljm force-pushed the cjm/visitor-interior-mutability branch from 2793221 to 7e612d0 Compare August 11, 2025 21:38
@github-actions
Copy link
Contributor

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@github-actions
Copy link
Contributor

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

@carljm carljm marked this pull request as ready for review August 11, 2025 22:10
@carljm carljm merged commit ea1aa9e into main Aug 11, 2025
38 checks passed
@carljm carljm deleted the cjm/visitor-interior-mutability branch August 11, 2025 22:42
carljm added a commit that referenced this pull request Aug 12, 2025
## Summary

After #19871, I realized that now
that we are passing around shared references to `CycleDetector`
visitors, we can now also simplify the `visit` callback signature; we
don't need to smuggle a single visitor reference through it anymore.
This is a pretty minor simplification, and it doesn't really make
anything shorter since I typically used a very short name (`v`) for the
smuggled reference, but I think it reduces cognitive overhead in reading
these `visit` usages; the extra variable would likely be confusing
otherwise for a reader.

## Test Plan

Existing CI.
dcreager added a commit that referenced this pull request Aug 12, 2025
* main:
  Don't cache files with diagnostics (#19869)
  [ty] support recursive type aliases (#19805)
  [ty] Remove unsafe `salsa::Update` implementations in `tuple.rs` (#19880)
  [ty] Function argument inlay hints (#19269)
  [ty] Remove Salsa interning for `TypedDictType` (#19879)
  Fix `lint.future-annotations` link (#19876)
  [ty] Reduce memory usage of `TupleSpec` and `TupleType` (#19872)
  [ty] Track heap usage of salsa structs (#19790)
  Update salsa to pull in tracked struct changes (#19843)
  [ty] simplify CycleDetector::visit signature (#19873)
  [ty] use interior mutability in type visitors (#19871)
  [ty] Fix tool name is None when no ty path is given in ty_benchmark (#19870)
  [ty] Remove `Type::Tuple` (#19669)
  [ty] Short circuit `ReachabilityConstraints::analyze_single` for dynamic types (#19867)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants