aliased-mut: a primitive part of a lent object is a copy, also on a path from this #164

Merged
sionzee merged 2 commits from aliased-mut-primitives into main 2026-11-24 14:22:19 +01:00
Owner

What was wrong

aliased-mut counted a primitive member of a lent object as "a part of it":

class Renderer {
  readonly ctx: { gl: WebGL2RenderingContext; state: GlState };
  draw(): void {
    const { gl, state } = this.ctx;
    state.setDepth(mut(gl), true, true, gl.LEQUAL);
    // aliased-mut: lends `this.ctx.gl` mutably and also gets `this.ctx.gl.LEQUAL`, a part of it
  }
}

LEQUAL is a number read before the call. The callee gets its value, not a reference, so nothing it changes through the loan can change what it was given. An adopter had 15 such findings with WebGL constants.

What I reproduced on main:

  • A path from this (this.ctx.gl.LEQUAL, deeper this.ctx.nested.gl.GREATER, this.settings.depth, this.counter.n through a getter, an enum member) was reported every time. The declared type was never followed for this, only for a parameter or a local.
  • A path from a parameter or a destructured local (gl.GREATER, const { LEQUAL } = gl) was not reported. The type wasn't settled there, and an unsettled type from a binding is no finding.
  • const ctx = this.ctx: names are bound before the walk, so this got the module's owner. The name stood for no path of the method. That is why the adopter's const { gl } = ctx form reported this.ctx.gl.LEQUAL with no way to settle it.

What changed

  • A path from this (an instance of a module-level class) goes to the whole-program pass, as a path from a binding already did. An unsettled type from this is still reported, as before. From a binding it still isn't.
  • member_primitive settles more types:
    • Lib constants. A member written in two or more capitals of a global type the program doesn't declare (gl.LEQUAL, gl.ARRAY_BUFFER, node.ELEMENT_NODE) is taken to be a constant. TypeScript's lib declares each such property as a number or a string, except the method Date.UTC and the single letter BiquadFilterNode.Q (an AudioParam). Both are left out. The new tests/platform_constants.rs parses every lib.*.d.ts and fails on any capitalised member that isn't a primitive, which is how it found Q. A type the program declares or augments (declare global { interface Window { … } }) is decided by its declaration: tested, still reported.
    • Enums, Mode or Mode.Fill, are primitives. A class of the program is not.
    • Class members: a field, accessor or parameter property declared number, string, boolean, bigint, symbol, a literal or a union of them, or set to a literal without a type, and a getter with such a return type. Inherited ones are found too.
    • Object literals: a member set to a literal, and the declared type of a readonly object field (readonly settings: Settings = { … }), which wins over the literal.
  • Getters: a primitive read through a getter is a copy taken when the argument is evaluated, before the call. It isn't reported. This is documented.
  • const x = this.y is now bound with the function around it as owner.

Still reported (fixture loans_primitives)

  • An array or object member: settings.tags, settings.inner, counter.items, this.limits.inner.
  • A lib member that isn't a constant: this.ctx.gl.canvas.
  • Two overlapping loans: both(mut(this.settings), mut(this.settings.inner)).
  • A second loan while a pending call holds one: const saving = save(mut(this.settings)); adjust(mut(this.settings), this.settings.depth).
  • The receiver form through a const name: const ctx = this.ctx; this.redraw(mut(ctx)). It was missed before because of the owner bug.

Adopter tree (apps packages scripts, main binary and this branch run back to back)

total aliased-mut
main 4358 136
this branch 4345 123
  • Gone, 15: all are state.setDepth(mut(gl), …, gl.LEQUAL) (14) or gl.GREATER (1), with gl from const { gl, state } = ctx and const ctx = this.ctx. All are false positives.
  • New, 2: this.drawStaticChunkBatches(mut(ctx), …) with const ctx = this.ctx. The method gets this, which holds this.ctx. This is the documented receiver form, and main reports it when written mut(this.ctx). These are true positives that the owner bug hid.
  • No other rule moved.

Tests

  • cargo nextest run --workspace (fast profile): 416/416 pass.
  • npm run test:rewrites (full): passes after count updates in misses.txt. Every moved count comes from the new fixture's sites, and no other fixture moved:
    • miss aliased-mut hoist-const 6 → 14 (fast and full)
    • new missing-mut hoist-const 265 → 294
    • new prefer-option optional-guard 338 → 351
    • new unneeded-mut type-alias 8 → 9
    • text aliased-mut call 16 → 24
  • npm run test:soundness: 16 of 110 classes escape, exactly the listed ones.
  • npm run test:docs: passes.

Docs: docs/rules/aliased-mut.md (What it reports, What the rule can't see, one ✅ example line), the README rules row, and CHANGELOG.

Open

  • From a parameter or local, a member whose type isn't settled is still not reported. For example, useCanvas(mut(gl), gl.canvas) with gl a parameter is missed, while the same path from this is reported. That asymmetry predates this PR and is kept. Changing it is a policy decision.
  • Lowercase lib members aren't settled (this.ctx.gl.drawingBufferWidth, a number, is still reported from this).
  • A global interface declared by a package (@types/…) isn't told apart from lib's, so its capitalised members are taken to be constants. This is documented.

🤖 Generated with Claude Code

## What was wrong `aliased-mut` counted a primitive member of a lent object as "a part of it": ```ts class Renderer { readonly ctx: { gl: WebGL2RenderingContext; state: GlState }; draw(): void { const { gl, state } = this.ctx; state.setDepth(mut(gl), true, true, gl.LEQUAL); // aliased-mut: lends `this.ctx.gl` mutably and also gets `this.ctx.gl.LEQUAL`, a part of it } } ``` `LEQUAL` is a `number` read before the call. The callee gets its value, not a reference, so nothing it changes through the loan can change what it was given. An adopter had 15 such findings with WebGL constants. What I reproduced on main: - **A path from `this`** (`this.ctx.gl.LEQUAL`, deeper `this.ctx.nested.gl.GREATER`, `this.settings.depth`, `this.counter.n` through a getter, an enum member) was reported every time. The declared type was never followed for `this`, only for a parameter or a local. - **A path from a parameter or a destructured local** (`gl.GREATER`, `const { LEQUAL } = gl`) was not reported. The type wasn't settled there, and an unsettled type from a binding is no finding. - **`const ctx = this.ctx`**: names are bound before the walk, so `this` got the module's owner. The name stood for no path of the method. That is why the adopter's `const { gl } = ctx` form reported `this.ctx.gl.LEQUAL` with no way to settle it. ## What changed - A path from `this` (an instance of a module-level class) goes to the whole-program pass, as a path from a binding already did. **An unsettled type from `this` is still reported**, as before. From a binding it still isn't. - `member_primitive` settles more types: - **Lib constants.** A member written in two or more capitals of a global type the program doesn't declare (`gl.LEQUAL`, `gl.ARRAY_BUFFER`, `node.ELEMENT_NODE`) is taken to be a constant. TypeScript's lib declares each such property as a number or a string, except the method `Date.UTC` and the single letter `BiquadFilterNode.Q` (an `AudioParam`). Both are left out. The new `tests/platform_constants.rs` parses every `lib.*.d.ts` and fails on any capitalised member that isn't a primitive, which is how it found `Q`. A type the program declares or augments (`declare global { interface Window { … } }`) is decided by its declaration: tested, still reported. - **Enums**, `Mode` or `Mode.Fill`, are primitives. **A class of the program** is not. - **Class members**: a field, accessor or parameter property declared `number`, `string`, `boolean`, `bigint`, `symbol`, a literal or a union of them, or set to a literal without a type, and a getter with such a return type. Inherited ones are found too. - **Object literals**: a member set to a literal, and the declared type of a `readonly` object field (`readonly settings: Settings = { … }`), which wins over the literal. - **Getters**: a primitive read through a getter is a copy taken when the argument is evaluated, before the call. It isn't reported. This is documented. - `const x = this.y` is now bound with the function around it as owner. ## Still reported (fixture `loans_primitives`) - An array or object member: `settings.tags`, `settings.inner`, `counter.items`, `this.limits.inner`. - A lib member that isn't a constant: `this.ctx.gl.canvas`. - Two overlapping loans: `both(mut(this.settings), mut(this.settings.inner))`. - A second loan while a pending call holds one: `const saving = save(mut(this.settings)); adjust(mut(this.settings), this.settings.depth)`. - The receiver form through a `const` name: `const ctx = this.ctx; this.redraw(mut(ctx))`. It was missed before because of the owner bug. ## Adopter tree (`apps packages scripts`, main binary and this branch run back to back) | | total | aliased-mut | |---|---|---| | main | 4358 | 136 | | this branch | 4345 | 123 | - **Gone, 15**: all are `state.setDepth(mut(gl), …, gl.LEQUAL)` (14) or `gl.GREATER` (1), with `gl` from `const { gl, state } = ctx` and `const ctx = this.ctx`. All are false positives. - **New, 2**: `this.drawStaticChunkBatches(mut(ctx), …)` with `const ctx = this.ctx`. The method gets `this`, which holds `this.ctx`. This is the documented receiver form, and main reports it when written `mut(this.ctx)`. These are true positives that the owner bug hid. - No other rule moved. ## Tests - `cargo nextest run --workspace` (fast profile): 416/416 pass. - `npm run test:rewrites` (full): passes after count updates in `misses.txt`. Every moved count comes from the new fixture's sites, and no other fixture moved: - `miss aliased-mut hoist-const` 6 → 14 (fast and full) - `new missing-mut hoist-const` 265 → 294 - `new prefer-option optional-guard` 338 → 351 - `new unneeded-mut type-alias` 8 → 9 - `text aliased-mut call` 16 → 24 - `npm run test:soundness`: 16 of 110 classes escape, exactly the listed ones. - `npm run test:docs`: passes. Docs: `docs/rules/aliased-mut.md` (What it reports, What the rule can't see, one ✅ example line), the README rules row, and CHANGELOG. ## Open - From a parameter or local, a member whose type isn't settled is still not reported. For example, `useCanvas(mut(gl), gl.canvas)` with `gl` a parameter is missed, while the same path from `this` is reported. That asymmetry predates this PR and is kept. Changing it is a policy decision. - Lowercase lib members aren't settled (`this.ctx.gl.drawingBufferWidth`, a number, is still reported from `this`). - A global interface declared by a package (`@types/…`) isn't told apart from lib's, so its capitalised members are taken to be constants. This is documented. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
aliased-mut: a primitive part of a lent object is a copy, also on a path from this
Some checks failed
Benchmarks / bench (pull_request) Successful in 4m38s
Tests / test (pull_request) Has been cancelled
61ccef94e8
`state.setDepth(mut(this.ctx.gl), true, true, this.ctx.gl.LEQUAL)` was reported as getting
"a part of" `this.ctx.gl`; the callee gets a copy of the number, read before the call.

- A path from `this` (an instance of a module-level class) now goes to the whole-program pass
  like one from a binding; a type it can't settle is still reported, as before.
- Settled now: a member of a type of TypeScript's lib written in two or more capitals (lib
  declares each as a number or a string; tests/platform_constants.rs reads every lib file to
  hold that), an enum, a getter's declared return type, a class field / accessor / parameter
  property declared a primitive keyword or set to a literal without a type, an object-literal
  member set to a literal, and a `readonly` object field's declared type.
- `const ctx = this.ctx` bound before the walk took `this` of the module (owner 0), so the
  name stood for no path of the method: now the function around it.

Objects, arrays, two overlapping loans and a loan while a pending call holds one are still
reported (fixture loans_primitives).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee force-pushed aliased-mut-primitives from 61ccef94e8
Some checks failed
Benchmarks / bench (pull_request) Successful in 4m38s
Tests / test (pull_request) Has been cancelled
to 5e52863eef
All checks were successful
Benchmarks / bench (pull_request) Successful in 4m0s
Tests / test (pull_request) Successful in 32m16s
2026-11-24 13:21:58 +01:00
Compare
Merge main into aliased-mut-primitives
Some checks failed
Tests / test (pull_request) Has been cancelled
Benchmarks / bench (pull_request) Has been cancelled
d91009797e
# Conflicts:
#	CHANGELOG.md
#	crates/safe_ts_core/tests/rewrites/misses.txt
sionzee deleted branch aliased-mut-primitives 2026-11-24 14:22:19 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!164
No description provided.