From 8972349f8ec6b34c5e9a12123ff23a4af73de1a9 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 19:00:56 -0700 Subject: [PATCH 1/2] fix(ts): write through DerefMut of a pinned or plain &mut to a value JS copies Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/objects.ts | 184 ++++++++++++++++++--------- ts/fixtures/lang/src/objects.rs | 26 ++++ ts/src/lower/mir/call.rs | 9 ++ ts/src/lower/task.rs | 17 +-- 4 files changed, 160 insertions(+), 76 deletions(-) diff --git a/ts/fixtures/lang/expected/objects.ts b/ts/fixtures/lang/expected/objects.ts index 2e8971509..8266394a3 100644 --- a/ts/fixtures/lang/expected/objects.ts +++ b/ts/fixtures/lang/expected/objects.ts @@ -59,6 +59,40 @@ export function throughMutex$Mutex_u8$2(p: rt.Mutex): rt.u8 { return _0; } +/** Writes a byte through a pointer's `DerefMut`. */ +export function writeThrough$Pin_u8(p: rt.Place, by: rt.u8): void { + p.v = p.v ^ by; +} + +/** Writes a byte through a pointer's `DerefMut`. */ +export function writeThrough$u8(p: rt.Place, by: rt.u8): void { + p.v = p.v ^ by; +} + +/** Replaces an `Arc` through a pointer's `DerefMut`. */ +export function replaceThrough$Arc_u8(p: rt.Place>, by: rt.u8): void { + const _3: rt.Arc = new rt.Arc(by); + const _4: rt.Place> = p; + _4.v[Symbol.dispose](); + _4.v = _3; +} + +/** Replaces an `Arc` through a pointer's `DerefMut`. */ +export function replaceThrough$MutexGuard_Arc_u8(p: rt.Mutex>, by: rt.u8): void { + const _3: rt.Arc = new rt.Arc(by); + p.value[Symbol.dispose](); + p.value = _3; + rt.mutexUnlock(p); +} + +/** Replaces an `Arc` through a pointer's `DerefMut`. */ +export function replaceThrough$Pin_Arc_u8(p: rt.Place>, by: rt.u8): void { + const _3: rt.Arc = new rt.Arc(by); + const _4: rt.Place> = p; + _4.v[Symbol.dispose](); + _4.v = _3; +} + export function lockedBy(n: rt.u8): rt.Mutex { return new rt.Mutex(rt.u8WrappingAdd(n, 3)); } @@ -179,7 +213,7 @@ export function counts(out: rt.ByteVec, arcs: rt.Arc[], items: rt.Arc = new rt.Arc(by); const two: rt.Arc = new rt.Arc(rt.u8WrappingAdd(by, 1)); @@ -273,87 +307,109 @@ export function objects(data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(throughArc$Arc_u8(rt.place(() => _199, (value5) => _199 = value5)), through$Pin_Arc_u8(rt.place(() => pinned, (value6) => pinned = value6))))); _199[Symbol.dispose](); rt.byteVecPush(out, throughMutex$Mutex_u8(lockedBy(by))); + let byte: rt.u8 = by; + const _210: rt.Place = rt.place(() => byte, (value7) => byte = value7); + _210.v = _210.v ^ 90; + writeThrough$Pin_u8(rt.place(() => byte, (value8) => byte = value8), 33); + writeThrough$u8(rt.place(() => byte, (value9) => byte = value9), 12); + rt.byteVecPush(out, byte); + let replaced: rt.Arc = new rt.Arc(by); + const _220: rt.Arc = new rt.Arc(rt.u8WrappingAdd(by, 1)); + const _222: rt.Place> = rt.place(() => replaced, (value10) => replaced = value10); + _222.v[Symbol.dispose](); + _222.v = _220; + rt.byteVecPush(out, replaced.value); + replaceThrough$Pin_Arc_u8(rt.place(() => replaced, (value11) => replaced = value11), rt.u8WrappingAdd(by, 2)); + rt.byteVecPush(out, replaced.value); + const _240: rt.u8 = rt.u8WrappingAdd(by, 3); + replaceThrough$Arc_u8(rt.place(() => replaced, (value12) => replaced = value12), _240); + rt.byteVecPush(out, replaced.value); + const guarded: rt.Mutex> = new rt.Mutex(new rt.Arc(by)); + replaceThrough$MutexGuard_Arc_u8(rt.mutexLock(guarded), rt.u8WrappingAdd(by, 4)); + const _257: rt.Mutex> = rt.mutexLock(guarded); + rt.byteVecPush(out, _257.value.value); + rt.mutexUnlock(_257); let owned: rt.Arc = rt.arcClone(held); - const lent2: rt.Place> = rt.place(() => owned, (value7) => owned = value7); + const lent2: rt.Place> = rt.place(() => owned, (value13) => owned = value13); rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(lent2.v.strong & 0xff, lent2.v.value))); const locked: rt.Mutex[]> = new rt.Mutex(v); - const _228: rt.Mutex[]> = rt.mutexLock(locked); - const taken3 = _228.value; - _228.value = []; + const _281: rt.Mutex[]> = rt.mutexLock(locked); + const taken3 = _281.value; + _281.value = []; const taken: rt.Arc[] = taken3; - rt.mutexUnlock(_228); + rt.mutexUnlock(_281); counts(out, [one, two], taken); - const _239: rt.Arc[] = [rt.arcClone(two)]; - const _245: rt.Mutex[]> = rt.mutexLock(locked); - rt.dropAll(_245.value); - _245.value = _239; - rt.mutexUnlock(_245); + const _292: rt.Arc[] = [rt.arcClone(two)]; + const _298: rt.Mutex[]> = rt.mutexLock(locked); + rt.dropAll(_298.value); + _298.value = _292; + rt.mutexUnlock(_298); rt.dropAll(taken); - const _250: rt.Arc[] = [one, two]; - const _258: rt.Mutex[]> = rt.mutexLock(locked); - counts(out, _250, _258.value); - rt.mutexUnlock(_258); + const _303: rt.Arc[] = [one, two]; + const _311: rt.Mutex[]> = rt.mutexLock(locked); + counts(out, _303, _311.value); + rt.mutexUnlock(_311); const cell: rt.RefCell[]> = new rt.RefCell([rt.arcClone(one)]); - const _269: rt.RefCell[]> = rt.refCellBorrowMut(cell); - const taken4 = _269.value; - _269.value = []; + const _322: rt.RefCell[]> = rt.refCellBorrowMut(cell); + const taken4 = _322.value; + _322.value = []; const taken2: rt.Arc[] = taken4; - rt.refMutRelease(_269); - const _271: rt.Arc[] = [rt.arcClone(two), rt.arcClone(two)]; - const _279: rt.RefCell[]> = rt.refCellBorrowMut(cell); - rt.dropAll(_279.value); - _279.value = _271; - rt.refMutRelease(_279); + rt.refMutRelease(_322); + const _324: rt.Arc[] = [rt.arcClone(two), rt.arcClone(two)]; + const _332: rt.RefCell[]> = rt.refCellBorrowMut(cell); + rt.dropAll(_332.value); + _332.value = _324; + rt.refMutRelease(_332); counts(out, [one, two], taken2); - const _290: rt.Arc[] = [one, two]; - const _298: rt.RefCell[]> = rt.refCellBorrow(cell); - counts(out, _290, _298.value); - rt.refRelease(_298); + const _343: rt.Arc[] = [one, two]; + const _351: rt.RefCell[]> = rt.refCellBorrow(cell); + counts(out, _343, _351.value); + rt.refRelease(_351); const sets: rt.HashMap> = rt.hashNew() as rt.HashMap>; - const _304: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); - const _303: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _304, true); - rt.hashInsert(sets, by, _303); - const _306: rt.BTreeSet | undefined = rt.hashGet(sets, by); - if (_306 !== undefined) { - const _313: Uint8Array = Uint8Array.of(rt.u8WrappingAdd(by, 1)); - rt.byteVecPush(out, rt.btreeLen(rt.replaceIn(_306, rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterSlice(_313), true))) & 0xff); + const _357: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); + const _356: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _357, true); + rt.hashInsert(sets, by, _356); + const _359: rt.BTreeSet | undefined = rt.hashGet(sets, by); + if (_359 !== undefined) { + const _366: Uint8Array = Uint8Array.of(rt.u8WrappingAdd(by, 1)); + rt.byteVecPush(out, rt.btreeLen(rt.replaceIn(_359, rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterSlice(_366), true))) & 0xff); } - const _320: Uint8Array = Uint8Array.of(by); - const _319: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterSlice(_320), true); - const _324: rt.u8 = rt.u8WrappingAdd(by, 1); - Object.assign(rt.entryOrInsertWith(rt.entry(sets, _324), (): rt.BTreeSet => { + const _373: Uint8Array = Uint8Array.of(by); + const _372: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterSlice(_373), true); + const _377: rt.u8 = rt.u8WrappingAdd(by, 1); + Object.assign(rt.entryOrInsertWith(rt.entry(sets, _377), (): rt.BTreeSet => { return rt.btreeNew(rt.cmp) as rt.BTreeSet; - }), _319); + }), _372); const keys: [rt.u8, rt.u8][] = rt.iterToArray(rt.iterMap(rt.hashIter(sets), (_22: [rt.u8, rt.BTreeSet]): [rt.u8, rt.u8] => { return [_22[0], rt.btreeLen(_22[1]) & 0xff] as [rt.u8, rt.u8]; })); rt.sliceSort(keys, rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp]))); - for (const _335 of keys) { - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(_335[0], _335[1]))); + for (const _388 of keys) { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(_388[0], _388[1]))); } - const _345: rt.Arc[] = [rt.arcClone(one)]; - const w: Window.Window = new Window.Window(by, _345); + const _398: rt.Arc[] = [rt.arcClone(one)]; + const w: Window.Window = new Window.Window(by, _398); const other: rt.Arc[] = [rt.arcClone(two), rt.arcClone(two)]; const bytes: rt.ByteVec = new rt.ByteVec(); for (const b4 of rt.byteSliceView(data)) { - const _363: rt.u8 = rt.u8Rem(b4, 5); - if (_363 === 0) { + const _416: rt.u8 = rt.u8Rem(b4, 5); + if (_416 === 0) { apply(w, b4); continue; } - if (_363 === 1) { + if (_416 === 1) { rt.byteVecPush(out, park(w, b4)); continue; } - if (_363 === 2) { + if (_416 === 2) { swapAll(w.held, other); continue; } - if (_363 === 3) { + if (_416 === 3) { refill(other, bytes, one, b4); continue; } - if (_363 === 4) { + if (_416 === 4) { if (rt.u8Rem(b4, 2) === 0) { const mine: rt.Arc[] = swapIn(other, two); counts(out, [one, two], mine); @@ -364,8 +420,8 @@ export function objects(data: rt.ByteSlice, out: rt.ByteVec): void { rt.dropAll(takeAll(w.held)); } const pinnedAll: rt.Arc[] = [rt.arcClone(two)]; - for (const _396 of rt.byteSliceView(data)) { - rt.byteVecPush(out, repinAll(pinnedAll, one, _396) & 0xff); + for (const _449 of rt.byteSliceView(data)) { + rt.byteVecPush(out, repinAll(pinnedAll, one, _449) & 0xff); } counts(out, [one, two], pinnedAll); rt.dropAll(pinnedAll); @@ -377,11 +433,11 @@ export function objects(data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecExtendFromSlice(out, rt.byteVecSlice(bytes)); counts(out, [one, two], w.held); counts(out, [one, two], other); - const _456: Uint8Array = Uint8Array.of(by); - const q: rt.VecDeque = rt.dequeExtend(rt.dequeNew() as rt.VecDeque, rt.iterSlice(_456)); + const _509: Uint8Array = Uint8Array.of(by); + const q: rt.VecDeque = rt.dequeExtend(rt.dequeNew() as rt.VecDeque, rt.iterSlice(_509)); const map: rt.HashMap = rt.hashNew() as rt.HashMap; - for (const _460 of rt.byteSliceView(data)) { - rt.byteVecPush(out, requeue(q, map, _460) & 0xff); + for (const _513 of rt.byteSliceView(data)) { + rt.byteVecPush(out, requeue(q, map, _513) & 0xff); } rt.iterExtendBytes(out, rt.dequeIter(q)); rt.iterExtendBytes(out, rt.hashValues(map)); @@ -395,9 +451,9 @@ export function objects(data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, two.weak & 0xff); const a: Window.Window = Window.withStart(Window.default$Default(), by); const b7: Window.Window = new Window.Window(rt.u8WrappingAdd(by, 2), [rt.arcClone(one)]); - const _496: rt.u16 = by as rt.u16; + const _549: rt.u16 = by as rt.u16; const array: rt.u16[] = Array.from({ length: 2 }, (): rt.u16 => { - return _496; + return _549; }); for (const x of rt.byteSliceView(data)) { if (rt.u8Rem(x, 2) === 0) { @@ -412,13 +468,13 @@ export function objects(data: rt.ByteSlice, out: rt.ByteVec): void { const counting: rt.Waker = new rt.ArcWaker(rt.arcClone(wakes), Wakes.wake$Wake, (arc: rt.Arc) => arc[Symbol.dispose]()); const noop: rt.Waker = rt.NOOP_WAKER.clone(); let w2: rt.Waker = noop.clone(); - for (const _560 of rt.byteSliceView(data)) { - if (!(rt.u8Rem(_560, 2) === 0)) { - _566 = noop; + for (const _613 of rt.byteSliceView(data)) { + if (!(rt.u8Rem(_613, 2) === 0)) { + _619 = noop; } else { - _566 = counting; + _619 = counting; } - const old3: rt.Waker = rewake(rt.place(() => w2, (value8) => w2 = value8), _566); + const old3: rt.Waker = rewake(rt.place(() => w2, (value14) => w2 = value14), _619); old3.wakeByRef(); w2.wakeByRef(); old3[Symbol.dispose](); @@ -438,6 +494,8 @@ export function objects(data: rt.ByteSlice, out: rt.ByteVec): void { rt.dropAll(cell.value); rt.dropAll(locked.value); owned[Symbol.dispose](); + guarded[Symbol.dispose](); + replaced[Symbol.dispose](); boxed[Symbol.dispose](); held[Symbol.dispose](); pinned[Symbol.dispose](); diff --git a/ts/fixtures/lang/src/objects.rs b/ts/fixtures/lang/src/objects.rs index 7b274d8bb..f9f97d5f4 100644 --- a/ts/fixtures/lang/src/objects.rs +++ b/ts/fixtures/lang/src/objects.rs @@ -39,6 +39,16 @@ fn through_mutex>>(p: P) -> u8 { *p.lock().unwrap() } +/// Writes a byte through a pointer's `DerefMut`. +fn write_through>(mut p: P, by: u8) { + *p ^= by; +} + +/// Replaces an `Arc` through a pointer's `DerefMut`. +fn replace_through>>(mut p: P, by: u8) { + *p = Arc::new(by); +} + fn locked_by(n: u8) -> Mutex { Mutex::new(n.wrapping_add(3)) } @@ -210,6 +220,22 @@ pub fn objects(data: &[u8], out: &mut Vec) { // A mutable reference to each, read through the shared `Deref`. out.extend_from_slice(&[through_arc(&mut Arc::clone(&held)), through(&mut pinned)]); out.push(through_mutex(&mut locked_by(by))); + // Written through `DerefMut` of a pinned or plain mutable reference, and of a guard. + let mut byte = by; + *Pin::new(&mut byte) ^= 0x5a; + write_through(Pin::new(&mut byte), 0x21); + write_through(&mut byte, 0x0c); + out.push(byte); + let mut replaced = Arc::new(by); + *Pin::new(&mut replaced) = Arc::new(by.wrapping_add(1)); + out.push(*replaced); + replace_through(Pin::new(&mut replaced), by.wrapping_add(2)); + out.push(*replaced); + replace_through(&mut replaced, by.wrapping_add(3)); + out.push(*replaced); + let guarded = Mutex::new(Arc::new(by)); + replace_through(guarded.lock().unwrap(), by.wrapping_add(4)); + out.push(**guarded.lock().unwrap()); let mut owned = Arc::clone(&held); let lent = Pin::new(&mut owned); out.extend_from_slice(&[Arc::strong_count(&*lent) as u8, **lent]); diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index 2b36d6497..a5740ac12 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -610,6 +610,15 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { // The receiver passes as what it points at, as std's `&mut self` does. let values = self.operands(false, &[true], args, result, span, out); let place = self.b.call_def(def_id, gargs, values, span, out); + // A `&mut`, or a pin of one, lends the `rt.Place` it is, whose value is at `v`. + let lent = targ(0).map(|t| match t.kind() { + ty::Adt(adt, args) if Std::of(tcx, adt.did()) == Some(Std::Pin) => args.type_at(0), + _ => t, + }); + let place = match lent { + Some(lent) if self.b.t.place_pointee(lent).is_some() => place.member("v"), + _ => place, + }; if !place.is_path() { if self.b.t.object_kind(*pointee).is_some() && !self.b.t.needs_place(*pointee) { return done(place, true); diff --git a/ts/src/lower/task.rs b/ts/src/lower/task.rs index 265dbc08c..022bd2fcf 100644 --- a/ts/src/lower/task.rs +++ b/ts/src/lower/task.rs @@ -176,19 +176,10 @@ pub fn call<'tcx>( ("Clone", "clone") if self_std == Some(Std::Weak) => Expr::rt("weakClone", args), // A waker is the executor's, whose `clone` is its own. ("Clone", "clone") if self_std == Some(Std::Waker) => first(args).member("clone").call(vec![]), - // A reference to one of these is the object it points at, which its own `Deref` gives: - // read out of a `&mut` that is an `rt.Place`, whose place `DerefMut` lends on. - ("Deref", "deref") | ("DerefMut", "deref_mut") if let Some(ty) = self_ty.filter(|t| t.is_ref()) => { - match self_std { - Some(Std::Arc | Std::Mutex | Std::MutexGuard | Std::Ref | Std::RefMut | Std::Pin) - if name == "deref" => - { - b.t.pointee_value(ty, first(args)) - } - Some(Std::Arc | Std::Mutex | Std::MutexGuard | Std::Ref | Std::RefMut | Std::Pin) => first(args), - _ => return Err(args), - } - } + // A reference is the object it points at, which its own `Deref` gives: read out of a + // `&mut` that is an `rt.Place`, whose place `DerefMut` lends on. + ("Deref", "deref") if let Some(ty) = self_ty.filter(|t| t.is_ref()) => b.t.pointee_value(ty, first(args)), + ("DerefMut", "deref_mut") if self_ty.is_some_and(|t| t.is_ref()) => first(args), ("Deref", "deref") | ("DerefMut", "deref_mut") => match self_std { Some(Std::Arc | Std::Mutex | Std::MutexGuard | Std::Ref | Std::RefMut) => first(args).member("value"), // A pin is its pointer, which derefs as it does: an `Arc` to the value it holds, and From 0fee740dd6dbd3cedb428f2090ddcce2d70a2096 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:06:48 -0700 Subject: [PATCH 2/2] test(ts): regenerate objects.ts after #241's place fold Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/objects.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ts/fixtures/lang/expected/objects.ts b/ts/fixtures/lang/expected/objects.ts index 56bc3d2cd..f5d0ea27e 100644 --- a/ts/fixtures/lang/expected/objects.ts +++ b/ts/fixtures/lang/expected/objects.ts @@ -72,9 +72,8 @@ export function writeThrough$u8(p: rt.Place, by: rt.u8): void { /** Replaces an `Arc` through a pointer's `DerefMut`. */ export function replaceThrough$Arc_u8(p: rt.Place>, by: rt.u8): void { const _3: rt.Arc = new rt.Arc(by); - const _4: rt.Place> = p; - _4.v[Symbol.dispose](); - _4.v = _3; + p.v[Symbol.dispose](); + p.v = _3; } /** Replaces an `Arc` through a pointer's `DerefMut`. */