diff --git a/ts/fixtures/lang/expected/objects.ts b/ts/fixtures/lang/expected/objects.ts index 2a79464c7..f5d0ea27e 100644 --- a/ts/fixtures/lang/expected/objects.ts +++ b/ts/fixtures/lang/expected/objects.ts @@ -59,6 +59,39 @@ 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); + p.v[Symbol.dispose](); + p.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; +} + /** A pin of a byte, made through a function pointer, that writes the byte. */ export function pinVia(p: rt.Place): rt.Place { return ((a0: rt.Place): rt.Place => { @@ -186,7 +219,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)); @@ -280,91 +313,113 @@ 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 pinnedByte: rt.u8 = by; - const _208: rt.Place = pinVia(rt.place(() => pinnedByte, (value7) => pinnedByte = value7)); - _208.v = _208.v ^ 90; + const _261: rt.Place = pinVia(rt.place(() => pinnedByte, (value13) => pinnedByte = value13)); + _261.v = _261.v ^ 90; rt.byteVecPush(out, pinnedByte); let owned: rt.Arc = rt.arcClone(held); - const lent2: rt.Place> = rt.place(() => owned, (value8) => owned = value8); + const lent2: rt.Place> = rt.place(() => owned, (value14) => owned = value14); rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(lent2.v.strong & 0xff, lent2.v.value))); const locked: rt.Mutex[]> = new rt.Mutex(v); - const _234: rt.Mutex[]> = rt.mutexLock(locked); - const taken3 = _234.value; - _234.value = []; + const _287: rt.Mutex[]> = rt.mutexLock(locked); + const taken3 = _287.value; + _287.value = []; const taken: rt.Arc[] = taken3; - rt.mutexUnlock(_234); + rt.mutexUnlock(_287); counts(out, [one, two], taken); - const _245: rt.Arc[] = [rt.arcClone(two)]; - const _251: rt.Mutex[]> = rt.mutexLock(locked); - rt.dropAll(_251.value); - _251.value = _245; - rt.mutexUnlock(_251); + const _298: rt.Arc[] = [rt.arcClone(two)]; + const _304: rt.Mutex[]> = rt.mutexLock(locked); + rt.dropAll(_304.value); + _304.value = _298; + rt.mutexUnlock(_304); rt.dropAll(taken); - const _256: rt.Arc[] = [one, two]; - const _264: rt.Mutex[]> = rt.mutexLock(locked); - counts(out, _256, _264.value); - rt.mutexUnlock(_264); + const _309: rt.Arc[] = [one, two]; + const _317: rt.Mutex[]> = rt.mutexLock(locked); + counts(out, _309, _317.value); + rt.mutexUnlock(_317); const cell: rt.RefCell[]> = new rt.RefCell([rt.arcClone(one)]); - const _275: rt.RefCell[]> = rt.refCellBorrowMut(cell); - const taken4 = _275.value; - _275.value = []; + const _328: rt.RefCell[]> = rt.refCellBorrowMut(cell); + const taken4 = _328.value; + _328.value = []; const taken2: rt.Arc[] = taken4; - rt.refMutRelease(_275); - const _277: rt.Arc[] = [rt.arcClone(two), rt.arcClone(two)]; - const _285: rt.RefCell[]> = rt.refCellBorrowMut(cell); - rt.dropAll(_285.value); - _285.value = _277; - rt.refMutRelease(_285); + rt.refMutRelease(_328); + const _330: rt.Arc[] = [rt.arcClone(two), rt.arcClone(two)]; + const _338: rt.RefCell[]> = rt.refCellBorrowMut(cell); + rt.dropAll(_338.value); + _338.value = _330; + rt.refMutRelease(_338); counts(out, [one, two], taken2); - const _296: rt.Arc[] = [one, two]; - const _304: rt.RefCell[]> = rt.refCellBorrow(cell); - counts(out, _296, _304.value); - rt.refRelease(_304); + const _349: rt.Arc[] = [one, two]; + const _357: rt.RefCell[]> = rt.refCellBorrow(cell); + counts(out, _349, _357.value); + rt.refRelease(_357); const sets: rt.HashMap> = rt.hashNew() as rt.HashMap>; - const _310: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); - const _309: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _310, true); - rt.hashInsert(sets, by, _309); - const _312: rt.BTreeSet | undefined = rt.hashGet(sets, by); - if (_312 !== undefined) { - const _319: Uint8Array = Uint8Array.of(rt.u8WrappingAdd(by, 1)); - rt.byteVecPush(out, rt.btreeLen(rt.replaceIn(_312, rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterSlice(_319), true))) & 0xff); + const _363: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); + const _362: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _363, true); + rt.hashInsert(sets, by, _362); + const _365: rt.BTreeSet | undefined = rt.hashGet(sets, by); + if (_365 !== undefined) { + const _372: Uint8Array = Uint8Array.of(rt.u8WrappingAdd(by, 1)); + rt.byteVecPush(out, rt.btreeLen(rt.replaceIn(_365, rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterSlice(_372), true))) & 0xff); } - const _326: Uint8Array = Uint8Array.of(by); - const _325: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterSlice(_326), true); - const _330: rt.u8 = rt.u8WrappingAdd(by, 1); - Object.assign(rt.entryOrInsertWith(rt.entry(sets, _330), (): rt.BTreeSet => { + const _379: Uint8Array = Uint8Array.of(by); + const _378: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterSlice(_379), true); + const _383: rt.u8 = rt.u8WrappingAdd(by, 1); + Object.assign(rt.entryOrInsertWith(rt.entry(sets, _383), (): rt.BTreeSet => { return rt.btreeNew(rt.cmp) as rt.BTreeSet; - }), _325); + }), _378); 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 _341 of keys) { - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(_341[0], _341[1]))); + for (const _394 of keys) { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(_394[0], _394[1]))); } - const _351: rt.Arc[] = [rt.arcClone(one)]; - const w: Window.Window = new Window.Window(by, _351); + const _404: rt.Arc[] = [rt.arcClone(one)]; + const w: Window.Window = new Window.Window(by, _404); 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 _369: rt.u8 = rt.u8Rem(b4, 5); - if (_369 === 0) { + const _422: rt.u8 = rt.u8Rem(b4, 5); + if (_422 === 0) { apply(w, b4); continue; } - if (_369 === 1) { + if (_422 === 1) { rt.byteVecPush(out, park(w, b4)); continue; } - if (_369 === 2) { + if (_422 === 2) { swapAll(w.held, other); continue; } - if (_369 === 3) { + if (_422 === 3) { refill(other, bytes, one, b4); continue; } - if (_369 === 4) { + if (_422 === 4) { if (rt.u8Rem(b4, 2) === 0) { const mine: rt.Arc[] = swapIn(other, two); counts(out, [one, two], mine); @@ -375,8 +430,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 _402 of rt.byteSliceView(data)) { - rt.byteVecPush(out, repinAll(pinnedAll, one, _402) & 0xff); + for (const _455 of rt.byteSliceView(data)) { + rt.byteVecPush(out, repinAll(pinnedAll, one, _455) & 0xff); } counts(out, [one, two], pinnedAll); rt.dropAll(pinnedAll); @@ -388,11 +443,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 _462: Uint8Array = Uint8Array.of(by); - const q: rt.VecDeque = rt.dequeExtend(rt.dequeNew() as rt.VecDeque, rt.iterSlice(_462)); + const _515: Uint8Array = Uint8Array.of(by); + const q: rt.VecDeque = rt.dequeExtend(rt.dequeNew() as rt.VecDeque, rt.iterSlice(_515)); const map: rt.HashMap = rt.hashNew() as rt.HashMap; - for (const _466 of rt.byteSliceView(data)) { - rt.byteVecPush(out, requeue(q, map, _466) & 0xff); + for (const _519 of rt.byteSliceView(data)) { + rt.byteVecPush(out, requeue(q, map, _519) & 0xff); } rt.iterExtendBytes(out, rt.dequeIter(q)); rt.iterExtendBytes(out, rt.hashValues(map)); @@ -406,9 +461,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 _502: rt.u16 = by as rt.u16; + const _555: rt.u16 = by as rt.u16; const array: rt.u16[] = Array.from({ length: 2 }, (): rt.u16 => { - return _502; + return _555; }); for (const x of rt.byteSliceView(data)) { if (rt.u8Rem(x, 2) === 0) { @@ -423,13 +478,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 _566 of rt.byteSliceView(data)) { - if (!(rt.u8Rem(_566, 2) === 0)) { - _572 = noop; + for (const _619 of rt.byteSliceView(data)) { + if (!(rt.u8Rem(_619, 2) === 0)) { + _625 = noop; } else { - _572 = counting; + _625 = counting; } - const old3: rt.Waker = rewake(rt.place(() => w2, (value9) => w2 = value9), _572); + const old3: rt.Waker = rewake(rt.place(() => w2, (value15) => w2 = value15), _625); old3.wakeByRef(); w2.wakeByRef(); old3[Symbol.dispose](); @@ -449,6 +504,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 4adab4fde..7cc38247f 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); +} + /// A pin of a byte, made through a function pointer, that writes the byte. fn pin_via<'a>(p: &'a mut u8) -> Pin<&'a mut u8> { let f: fn(&'a mut u8) -> Pin<&'a mut u8> = Pin::new; @@ -216,6 +226,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 pinned_byte = by; *Pin::get_mut(pin_via(&mut pinned_byte)) ^= 0x5a; out.push(pinned_byte); diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index 893087192..bcf596ba4 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -623,6 +623,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 7b12649aa..b994545a3 100644 --- a/ts/src/lower/task.rs +++ b/ts/src/lower/task.rs @@ -201,19 +201,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