Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 122 additions & 65 deletions ts/fixtures/lang/expected/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@ export function throughMutex$Mutex_u8$2(p: rt.Mutex<rt.u8>): rt.u8 {
return _0;
}

/** Writes a byte through a pointer's `DerefMut`. */
export function writeThrough$Pin_u8(p: rt.Place<rt.u8>, by: rt.u8): void {
p.v = p.v ^ by;
}

/** Writes a byte through a pointer's `DerefMut`. */
export function writeThrough$u8(p: rt.Place<rt.u8>, 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<rt.Arc<rt.u8>>, by: rt.u8): void {
const _3: rt.Arc<rt.u8> = 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<rt.Arc<rt.u8>>, by: rt.u8): void {
const _3: rt.Arc<rt.u8> = 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<rt.Arc<rt.u8>>, by: rt.u8): void {
const _3: rt.Arc<rt.u8> = new rt.Arc(by);
const _4: rt.Place<rt.Arc<rt.u8>> = 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.u8>): rt.Place<rt.u8> {
return ((a0: rt.Place<rt.u8>): rt.Place<rt.u8> => {
Expand Down Expand Up @@ -186,7 +219,7 @@ export function counts(out: rt.ByteVec, arcs: rt.Arc<rt.u8>[], items: rt.Arc<rt.
}

export function objects(data: rt.ByteSlice, out: rt.ByteVec): void {
let _572!: rt.Waker;
let _625!: rt.Waker;
const by: rt.u8 = unwrapOr$Option_u8(rt.byteSliceFirst(data), 5);
const one: rt.Arc<rt.u8> = new rt.Arc(by);
const two: rt.Arc<rt.u8> = new rt.Arc(rt.u8WrappingAdd(by, 1));
Expand Down Expand Up @@ -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.u8> = 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<rt.u8> = new rt.Arc(by);
const _220: rt.Arc<rt.u8> = new rt.Arc(rt.u8WrappingAdd(by, 1));
const _222: rt.Place<rt.Arc<rt.u8>> = 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<rt.Arc<rt.u8>> = new rt.Mutex(new rt.Arc(by));
replaceThrough$MutexGuard_Arc_u8(rt.mutexLock(guarded), rt.u8WrappingAdd(by, 4));
const _257: rt.Mutex<rt.Arc<rt.u8>> = rt.mutexLock(guarded);
rt.byteVecPush(out, _257.value.value);
rt.mutexUnlock(_257);
let pinnedByte: rt.u8 = by;
const _208: rt.Place<rt.u8> = pinVia(rt.place(() => pinnedByte, (value7) => pinnedByte = value7));
_208.v = _208.v ^ 90;
const _261: rt.Place<rt.u8> = pinVia(rt.place(() => pinnedByte, (value13) => pinnedByte = value13));
_261.v = _261.v ^ 90;
rt.byteVecPush(out, pinnedByte);
let owned: rt.Arc<rt.u8> = rt.arcClone(held);
const lent2: rt.Place<rt.Arc<rt.u8>> = rt.place(() => owned, (value8) => owned = value8);
const lent2: rt.Place<rt.Arc<rt.u8>> = rt.place(() => owned, (value14) => owned = value14);
rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(lent2.v.strong & 0xff, lent2.v.value)));
const locked: rt.Mutex<rt.Arc<rt.u8>[]> = new rt.Mutex(v);
const _234: rt.Mutex<rt.Arc<rt.u8>[]> = rt.mutexLock(locked);
const taken3 = _234.value;
_234.value = [];
const _287: rt.Mutex<rt.Arc<rt.u8>[]> = rt.mutexLock(locked);
const taken3 = _287.value;
_287.value = [];
const taken: rt.Arc<rt.u8>[] = taken3;
rt.mutexUnlock(_234);
rt.mutexUnlock(_287);
counts(out, [one, two], taken);
const _245: rt.Arc<rt.u8>[] = [rt.arcClone(two)];
const _251: rt.Mutex<rt.Arc<rt.u8>[]> = rt.mutexLock(locked);
rt.dropAll(_251.value);
_251.value = _245;
rt.mutexUnlock(_251);
const _298: rt.Arc<rt.u8>[] = [rt.arcClone(two)];
const _304: rt.Mutex<rt.Arc<rt.u8>[]> = rt.mutexLock(locked);
rt.dropAll(_304.value);
_304.value = _298;
rt.mutexUnlock(_304);
rt.dropAll(taken);
const _256: rt.Arc<rt.u8>[] = [one, two];
const _264: rt.Mutex<rt.Arc<rt.u8>[]> = rt.mutexLock(locked);
counts(out, _256, _264.value);
rt.mutexUnlock(_264);
const _309: rt.Arc<rt.u8>[] = [one, two];
const _317: rt.Mutex<rt.Arc<rt.u8>[]> = rt.mutexLock(locked);
counts(out, _309, _317.value);
rt.mutexUnlock(_317);
const cell: rt.RefCell<rt.Arc<rt.u8>[]> = new rt.RefCell([rt.arcClone(one)]);
const _275: rt.RefCell<rt.Arc<rt.u8>[]> = rt.refCellBorrowMut(cell);
const taken4 = _275.value;
_275.value = [];
const _328: rt.RefCell<rt.Arc<rt.u8>[]> = rt.refCellBorrowMut(cell);
const taken4 = _328.value;
_328.value = [];
const taken2: rt.Arc<rt.u8>[] = taken4;
rt.refMutRelease(_275);
const _277: rt.Arc<rt.u8>[] = [rt.arcClone(two), rt.arcClone(two)];
const _285: rt.RefCell<rt.Arc<rt.u8>[]> = rt.refCellBorrowMut(cell);
rt.dropAll(_285.value);
_285.value = _277;
rt.refMutRelease(_285);
rt.refMutRelease(_328);
const _330: rt.Arc<rt.u8>[] = [rt.arcClone(two), rt.arcClone(two)];
const _338: rt.RefCell<rt.Arc<rt.u8>[]> = rt.refCellBorrowMut(cell);
rt.dropAll(_338.value);
_338.value = _330;
rt.refMutRelease(_338);
counts(out, [one, two], taken2);
const _296: rt.Arc<rt.u8>[] = [one, two];
const _304: rt.RefCell<rt.Arc<rt.u8>[]> = rt.refCellBorrow(cell);
counts(out, _296, _304.value);
rt.refRelease(_304);
const _349: rt.Arc<rt.u8>[] = [one, two];
const _357: rt.RefCell<rt.Arc<rt.u8>[]> = rt.refCellBorrow(cell);
counts(out, _349, _357.value);
rt.refRelease(_357);
const sets: rt.HashMap<rt.u8, rt.BTreeSet<rt.u8>> = rt.hashNew() as rt.HashMap<rt.u8, rt.BTreeSet<rt.u8>>;
const _310: rt.Iter<rt.u8> = rt.iterSlice(rt.byteSliceView(data));
const _309: rt.BTreeSet<rt.u8> = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet<rt.u8>, _310, true);
rt.hashInsert(sets, by, _309);
const _312: rt.BTreeSet<rt.u8> | 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.u8>, rt.iterSlice(_319), true))) & 0xff);
const _363: rt.Iter<rt.u8> = rt.iterSlice(rt.byteSliceView(data));
const _362: rt.BTreeSet<rt.u8> = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet<rt.u8>, _363, true);
rt.hashInsert(sets, by, _362);
const _365: rt.BTreeSet<rt.u8> | 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.u8>, rt.iterSlice(_372), true))) & 0xff);
}
const _326: Uint8Array = Uint8Array.of(by);
const _325: rt.BTreeSet<rt.u8> = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet<rt.u8>, rt.iterSlice(_326), true);
const _330: rt.u8 = rt.u8WrappingAdd(by, 1);
Object.assign(rt.entryOrInsertWith(rt.entry(sets, _330), (): rt.BTreeSet<rt.u8> => {
const _379: Uint8Array = Uint8Array.of(by);
const _378: rt.BTreeSet<rt.u8> = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet<rt.u8>, rt.iterSlice(_379), true);
const _383: rt.u8 = rt.u8WrappingAdd(by, 1);
Object.assign(rt.entryOrInsertWith(rt.entry(sets, _383), (): rt.BTreeSet<rt.u8> => {
return rt.btreeNew(rt.cmp) as rt.BTreeSet<rt.u8>;
}), _325);
}), _378);
const keys: [rt.u8, rt.u8][] = rt.iterToArray(rt.iterMap(rt.hashIter(sets), (_22: [rt.u8, rt.BTreeSet<rt.u8>]): [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.u8>[] = [rt.arcClone(one)];
const w: Window.Window = new Window.Window(by, _351);
const _404: rt.Arc<rt.u8>[] = [rt.arcClone(one)];
const w: Window.Window = new Window.Window(by, _404);
const other: rt.Arc<rt.u8>[] = [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<rt.u8>[] = swapIn(other, two);
counts(out, [one, two], mine);
Expand All @@ -375,8 +430,8 @@ export function objects(data: rt.ByteSlice, out: rt.ByteVec): void {
rt.dropAll(takeAll(w.held));
}
const pinnedAll: rt.Arc<rt.u8>[] = [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);
Expand All @@ -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.u8> = rt.dequeExtend(rt.dequeNew() as rt.VecDeque<rt.u8>, rt.iterSlice(_462));
const _515: Uint8Array = Uint8Array.of(by);
const q: rt.VecDeque<rt.u8> = rt.dequeExtend(rt.dequeNew() as rt.VecDeque<rt.u8>, rt.iterSlice(_515));
const map: rt.HashMap<rt.u8, rt.u8> = rt.hashNew() as rt.HashMap<rt.u8, rt.u8>;
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));
Expand All @@ -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) {
Expand All @@ -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<Wakes.Wakes>) => 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]();
Expand All @@ -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]();
Expand Down
26 changes: 26 additions & 0 deletions ts/fixtures/lang/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ fn through_mutex<P: std::ops::Deref<Target = Mutex<u8>>>(p: P) -> u8 {
*p.lock().unwrap()
}

/// Writes a byte through a pointer's `DerefMut`.
fn write_through<P: std::ops::DerefMut<Target = u8>>(mut p: P, by: u8) {
*p ^= by;
}

/// Replaces an `Arc` through a pointer's `DerefMut`.
fn replace_through<P: std::ops::DerefMut<Target = Arc<u8>>>(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;
Expand Down Expand Up @@ -216,6 +226,22 @@ pub fn objects(data: &[u8], out: &mut Vec<u8>) {
// 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);
Expand Down
9 changes: 9 additions & 0 deletions ts/src/lower/mir/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Resolve nested Pin wrappers before choosing the writable path

The new logic peels only one Pin. The existing fixture helper can also be called as write_through(Pin::new(Pin::new(&mut byte)), 1): both Pins erase recursively to rt.Place<u8> and both constructors are identity mappings. However, the outer DerefMut mapping returns that Place, while lent here is still Pin<&mut u8>, for which place_pointee returns None. The alias therefore points at the wrapper itself instead of its .v, so *p ^= by still fails to update byte (and emits the same invalid Place/scalar operation this PR fixes for one Pin). Resolve the nested pointer representation consistently and add this case to the differential fixture.

};
if !place.is_path() {
if self.b.t.object_kind(*pointee).is_some() && !self.b.t.needs_place(*pointee) {
return done(place, true);
Expand Down
17 changes: 4 additions & 13 deletions ts/src/lower/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down