From 04b5eace4f338b5ee6d77fe38875194dddb34c15 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 13:08:19 -0700 Subject: [PATCH 1/2] chore(quest): claim quest/m2/ts-aliases.md Quest-Claim: 0c06b5a34fda696f From 1540a91919b63ff963ffb4756c4fa7d349ee46b1 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 13:42:59 -0700 Subject: [PATCH 2/2] fix(ts): check every held value against the names its statement writes A temporary held back for its one read is flushed before any statement that assigns a name it reads, found from the statement itself rather than a local each caller passes. An `rt.Place` through a fixed object holds each index the path goes through, not only the last. Neither changes any fixture or moq's output: the lang fixture's `holds` case covers each hold site, reassigning a name between the hold and the read, in and across loop iterations. Co-Authored-By: Claude Opus 5.5 --- quest/m2/README.md | 1 - quest/m2/ts-aliases.md | 28 --- ts/fixtures/lang/expected/harness.ts | 95 ++++---- ts/fixtures/lang/expected/holds.Row.ts | 7 + ts/fixtures/lang/expected/holds.ts | 325 +++++++++++++++++++++++++ ts/fixtures/lang/src/harness.rs | 5 +- ts/fixtures/lang/src/holds.rs | 212 ++++++++++++++++ ts/fixtures/lang/src/lib.rs | 5 +- ts/src/lower/mir/call.rs | 10 +- ts/src/lower/mir/mod.rs | 67 +++-- ts/src/lower/mir/place.rs | 32 ++- ts/src/lower/mir/rvalue.rs | 2 +- ts/src/lower/mir/scalar.rs | 8 +- ts/src/lower/mir/step.rs | 2 +- 14 files changed, 674 insertions(+), 125 deletions(-) delete mode 100644 quest/m2/ts-aliases.md create mode 100644 ts/fixtures/lang/expected/holds.Row.ts create mode 100644 ts/fixtures/lang/expected/holds.ts create mode 100644 ts/fixtures/lang/src/holds.rs diff --git a/quest/m2/README.md b/quest/m2/README.md index c32ab1dd8..11ba5abcd 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -29,7 +29,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export/README - [Byte slices without objects](/quest/m2/ts-byte-views/README.md) - reading through a translated cursor allocates no object per read - [Async in the TypeScript translator](/quest/m2/ts-async.md) - moq-net's and kio's async code translates with Rust's lazy futures - [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values -- [Held values that name a reassigned variable](/quest/m2/ts-aliases.md) - a held place or folded value reads what Rust read, in and across loop iterations - [Translate moq-net's model to TypeScript](/quest/m2/ts-model/README.md) - the model translates with kio, moq-clock, and moq-pattern, for moq to compare against WASM - [Translate against the prebuilt std](/quest/m2/ts-std-build.md) - measure whether `-Zbuild-std` is still needed now that panics abort, and drop it if not - [Waiting: Swift 6.4 in nixpkgs-unstable](/quest/m2/uniffi-swift-arm.md) - the nightly UniFFI fixtures move back to arm diff --git a/quest/m2/ts-aliases.md b/quest/m2/ts-aliases.md deleted file mode 100644 index 3255d4243..000000000 --- a/quest/m2/ts-aliases.md +++ /dev/null @@ -1,28 +0,0 @@ -# [M] Held values that name a reassigned variable - -## Goal - -Wherever rust2-ts holds an expression back instead of reading it once, the held value -reads what Rust read when it was made, even after a variable it names is assigned again, -including on a later iteration of a loop. Proven by differential tests against native Rust -that reassign each name between the hold and the read, in and across loop iterations. - -## Plan - -Collected on 2026-10-28 from three reports of one shape: - -- `stable_path` holds only the outermost object of a place, so an index inside it - (`a[i].b[j]` with `i` reassigned while the borrow lives) is read live - ([#176](https://github.com/kixelated/rust2/pull/176)). -- `alias()`'s copy arm treats a held-back source local as stable, though its folded value - may name a variable assigned again ([#148](https://github.com/kixelated/rust2/pull/148)). - No miscompile was found through it, but it is #148's bug in another place. -- Values made in a loop that share one JS binding across iterations where each Rust - iteration has its own value. - -Audit every place the lowering folds or holds an expression rather than fixing these one -by one: the rule is one check of which names a held value reads and whether any can be -written before the read. #148's review also asked for differential coverage of reborrows -through a reassigned reference and of indexed borrows. - -Public API: the translator's output. diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 40db61d85..4ff8bf202 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -20,6 +20,7 @@ import { edits, bytes } from "./edits.ts"; import { floats } from "./floats.ts"; import { ints, strings, derived, written, counts, rounding, panics } from "./fmt.ts"; import { methods } from "./generics.ts"; +import { holds } from "./holds.ts"; import { u8Op, u16Op, u32Op, u64Op, u128Op, usizeOp, i8Op, i16Op, i32Op, i64Op, i128Op, isizeOp, i8Neg, i16Neg, i32Neg, i64Neg, i128Neg, isizeNeg, cast as ints_cast, convert } from "./ints.ts"; import { adaptors, consumers, skips, ranked, ranges, options, own, values } from "./iters.ts"; import { keys, overrides } from "./keys.ts"; @@ -40,99 +41,99 @@ import { wrappers } from "./wrappers.ts"; * case added at the end moves no other. */ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void][] { - return copy$str_forabfnu8au8bmutstdvecVecu8s([["ints::u8_op", u8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u16_op", u16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u32_op", u32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u64_op", u64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u128_op", u128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::usize_op", usizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i8_op", i8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i16_op", i16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i32_op", i32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i64_op", i64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i128_op", i128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::isize_op", isizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::neg", neg] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::cast", cast] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::convert", (_270: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void => { + return copy$str_forabfnu8au8bmutstdvecVecu8s([["ints::u8_op", u8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u16_op", u16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u32_op", u32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u64_op", u64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u128_op", u128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::usize_op", usizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i8_op", i8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i16_op", i16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i32_op", i32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i64_op", i64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i128_op", i128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::isize_op", isizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::neg", neg] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::cast", cast] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::convert", (_273: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void => { convert(data, out); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::copies", (_271: rt.u8, data2: rt.ByteSlice, out2: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::copies", (_274: rt.u8, data2: rt.ByteSlice, out2: rt.ByteVec): void => { shapes_copies(data2, out2); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::loops", (_272: rt.u8, data3: rt.ByteSlice, out3: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::loops", (_275: rt.u8, data3: rt.ByteSlice, out3: rt.ByteVec): void => { loops(data3, out3); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::patterns", (_273: rt.u8, data4: rt.ByteSlice, out4: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::patterns", (_276: rt.u8, data4: rt.ByteSlice, out4: rt.ByteVec): void => { patterns(data4, out4); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::closures", (_274: rt.u8, data5: rt.ByteSlice, out5: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::closures", (_277: rt.u8, data5: rt.ByteSlice, out5: rt.ByteVec): void => { closures(data5, out5); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::discriminants", (_275: rt.u8, data6: rt.ByteSlice, out6: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::discriminants", (_278: rt.u8, data6: rt.ByteSlice, out6: rt.ByteVec): void => { discriminants(data6, out6); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::library", (_276: rt.u8, data7: rt.ByteSlice, out7: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::library", (_279: rt.u8, data7: rt.ByteSlice, out7: rt.ByteVec): void => { library(data7, out7); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::ints", ints] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::strings", (_277: rt.u8, data8: rt.ByteSlice, out8: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::ints", ints] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::strings", (_280: rt.u8, data8: rt.ByteSlice, out8: rt.ByteVec): void => { strings(data8, out8); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::derived", (_278: rt.u8, data9: rt.ByteSlice, out9: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::derived", (_281: rt.u8, data9: rt.ByteSlice, out9: rt.ByteVec): void => { derived(data9, out9); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::written", (_279: rt.u8, data10: rt.ByteSlice, out10: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::written", (_282: rt.u8, data10: rt.ByteSlice, out10: rt.ByteVec): void => { written(data10, out10); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::panics", panics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["generics::methods", (_280: rt.u8, data11: rt.ByteSlice, out11: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::panics", panics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["generics::methods", (_283: rt.u8, data11: rt.ByteSlice, out11: rt.ByteVec): void => { methods(data11, out11); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::adaptors", adaptors] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::consumers", consumers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranges", (_281: rt.u8, data12: rt.ByteSlice, out12: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::adaptors", adaptors] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::consumers", consumers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranges", (_284: rt.u8, data12: rt.ByteSlice, out12: rt.ByteVec): void => { ranges(data12, out12); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::options", (_282: rt.u8, data13: rt.ByteSlice, out13: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::options", (_285: rt.u8, data13: rt.ByteSlice, out13: rt.ByteVec): void => { options(data13, out13); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::own", (_283: rt.u8, data14: rt.ByteSlice, out14: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::own", (_286: rt.u8, data14: rt.ByteSlice, out14: rt.ByteVec): void => { own(data14, out14); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::values", (_284: rt.u8, data15: rt.ByteSlice, out15: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::values", (_287: rt.u8, data15: rt.ByteSlice, out15: rt.ByteVec): void => { values(data15, out15); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::order", (_285: rt.u8, data16: rt.ByteSlice, out16: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::order", (_288: rt.u8, data16: rt.ByteSlice, out16: rt.ByteVec): void => { order(data16, out16); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::hash_maps", hashMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::sets", sets] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::btree_maps", btreeMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::deques", (_286: rt.u8, data17: rt.ByteSlice, out17: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::hash_maps", hashMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::sets", sets] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::btree_maps", btreeMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::deques", (_289: rt.u8, data17: rt.ByteSlice, out17: rt.ByteVec): void => { deques(data17, out17); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::flows", (_287: rt.u8, data18: rt.ByteSlice, out18: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::flows", (_290: rt.u8, data18: rt.ByteSlice, out18: rt.ByteVec): void => { flows(data18, out18); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::keys", keys] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chars::chars", chars] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::strs", strs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::vecs", (_288: rt.u8, data19: rt.ByteSlice, out19: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::keys", keys] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chars::chars", chars] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::strs", strs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::vecs", (_291: rt.u8, data19: rt.ByteSlice, out19: rt.ByteVec): void => { vecs(data19, out19); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::slices", slices] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::stores", (_289: rt.u8, data20: rt.ByteSlice, out20: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::slices", slices] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::stores", (_292: rt.u8, data20: rt.ByteSlice, out20: rt.ByteVec): void => { stores(data20, out20); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chunks::chunks", chunks] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cows::cows", cows] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["durations::durations", durations] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["combinators::combinators", combinators] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["splits::splits", splits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["nested::nested", nested] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["refs::refs", (_290: rt.u8, data21: rt.ByteSlice, out21: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chunks::chunks", chunks] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cows::cows", cows] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["durations::durations", durations] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["combinators::combinators", combinators] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["splits::splits", splits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["nested::nested", nested] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["refs::refs", (_293: rt.u8, data21: rt.ByteSlice, out21: rt.ByteVec): void => { refs(data21, out21); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_291: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_294: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { counts(data22, out22); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_292: rt.u8, _310: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_295: rt.u8, _310: rt.ByteSlice, out23: rt.ByteVec): void => { rounding(out23); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_293: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_296: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { dyns(data23, out24); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cells::cells", cells] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::shared", (_294: rt.u8, data24: rt.ByteSlice, out25: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cells::cells", cells] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::shared", (_297: rt.u8, data24: rt.ByteSlice, out25: rt.ByteVec): void => { shared(data24, out25); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranked", ranked] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["captures::captures", captures] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::locks", (_295: rt.u8, data25: rt.ByteSlice, out26: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranked", ranked] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["captures::captures", captures] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::locks", (_298: rt.u8, data25: rt.ByteSlice, out26: rt.ByteVec): void => { locks(data25, out26); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::relock", (_296: rt.u8, data26: rt.ByteSlice, out27: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::relock", (_299: rt.u8, data26: rt.ByteSlice, out27: rt.ByteVec): void => { relock(data26, out27); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locals::locals", (_297: rt.u8, data27: rt.ByteSlice, out28: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locals::locals", (_2100: rt.u8, data27: rt.ByteSlice, out28: rt.ByteVec): void => { locals(data27, out28); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::rwlocks", (_298: rt.u8, data28: rt.ByteSlice, out29: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::rwlocks", (_2101: rt.u8, data28: rt.ByteSlice, out29: rt.ByteVec): void => { rwlocks(data28, out29); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["borrows::borrows", (_299: rt.u8, data29: rt.ByteSlice, out30: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["borrows::borrows", (_2102: rt.u8, data29: rt.ByteSlice, out30: rt.ByteVec): void => { borrows(data29, out30); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["defaults::defaults", (_2100: rt.u8, data30: rt.ByteSlice, out31: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["defaults::defaults", (_2103: rt.u8, data30: rt.ByteSlice, out31: rt.ByteVec): void => { defaults(data30, out31); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::arcs", (_2101: rt.u8, data31: rt.ByteSlice, out32: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::arcs", (_2104: rt.u8, data31: rt.ByteSlice, out32: rt.ByteVec): void => { arcs(data31, out32); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::rcs", (_2102: rt.u8, data32: rt.ByteSlice, out33: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::rcs", (_2105: rt.u8, data32: rt.ByteSlice, out33: rt.ByteVec): void => { rcs(data32, out33); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_arcs", (_2103: rt.u8, data33: rt.ByteSlice, out34: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_arcs", (_2106: rt.u8, data33: rt.ByteSlice, out34: rt.ByteVec): void => { cyclicArcs(data33, out34); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_rcs", (_2104: rt.u8, data34: rt.ByteSlice, out35: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_rcs", (_2107: rt.u8, data34: rt.ByteSlice, out35: rt.ByteVec): void => { cyclicRcs(data34, out35); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::anys", (_2105: rt.u8, data35: rt.ByteSlice, out36: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::anys", (_2108: rt.u8, data35: rt.ByteSlice, out36: rt.ByteVec): void => { anys(data35, out36); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::fns", (_2106: rt.u8, data36: rt.ByteSlice, out37: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::fns", (_2109: rt.u8, data36: rt.ByteSlice, out37: rt.ByteVec): void => { fns(data36, out37); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::copies", (_2107: rt.u8, data37: rt.ByteSlice, out38: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::copies", (_2110: rt.u8, data37: rt.ByteSlice, out38: rt.ByteVec): void => { copies(data37, out38); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::lists", (_2108: rt.u8, data38: rt.ByteSlice, out39: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::lists", (_2111: rt.u8, data38: rt.ByteSlice, out39: rt.ByteVec): void => { lists(data38, out39); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::nested", (_2109: rt.u8, data39: rt.ByteSlice, out40: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::nested", (_2112: rt.u8, data39: rt.ByteSlice, out40: rt.ByteVec): void => { pointers_nested(data39, out40); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::places", (_2110: rt.u8, data40: rt.ByteSlice, out41: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::places", (_2113: rt.u8, data40: rt.ByteSlice, out41: rt.ByteVec): void => { places(data40, out41); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::sentinels", (_2111: rt.u8, data41: rt.ByteSlice, out42: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::sentinels", (_2114: rt.u8, data41: rt.ByteSlice, out42: rt.ByteVec): void => { sentinels(data41, out42); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::shared", (_2112: rt.u8, data42: rt.ByteSlice, out43: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::shared", (_2115: rt.u8, data42: rt.ByteSlice, out43: rt.ByteVec): void => { pointers_shared(data42, out43); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::unchecked", (_2113: rt.u8, data43: rt.ByteSlice, out44: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::unchecked", (_2116: rt.u8, data43: rt.ByteSlice, out44: rt.ByteVec): void => { unchecked(data43, out44); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::bytes", bytes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["arrays::from_fns", (_2114: rt.u8, data44: rt.ByteSlice, out45: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::bytes", bytes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["arrays::from_fns", (_2117: rt.u8, data44: rt.ByteSlice, out45: rt.ByteVec): void => { fromFns(data44, out45); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::optionals", optionals] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["objects::objects", (_2115: rt.u8, data45: rt.ByteSlice, out46: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::optionals", optionals] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["objects::objects", (_2118: rt.u8, data45: rt.ByteSlice, out46: rt.ByteVec): void => { objects(data45, out46); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]).map(copy$str_forabfnu8au8bmutstdvecVecu8); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["holds::holds", holds] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]).map(copy$str_forabfnu8au8bmutstdvecVecu8); } /** Run one case. */ diff --git a/ts/fixtures/lang/expected/holds.Row.ts b/ts/fixtures/lang/expected/holds.Row.ts new file mode 100644 index 000000000..3e5493ecd --- /dev/null +++ b/ts/fixtures/lang/expected/holds.Row.ts @@ -0,0 +1,7 @@ +// Generated by rust2-ts. Do not edit. + +export interface Row { + cells: Uint8Array; +} + +//# sourceMappingURL=holds.Row.ts.map diff --git a/ts/fixtures/lang/expected/holds.ts b/ts/fixtures/lang/expected/holds.ts new file mode 100644 index 000000000..e68553a61 --- /dev/null +++ b/ts/fixtures/lang/expected/holds.ts @@ -0,0 +1,325 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { copy$u8s } from "./lib.ts"; +import { unwrapOr$Option_u8, mapOr$Option_usize_u8_consumers_closure8 } from "./core/option.ts"; +import type * as Row from "./holds.Row.ts"; + +export function set(x: rt.Place, v: rt.u8): void { + x.v = v; +} + +export function whole(p: [string, Row.Row, rt.u8]): rt.u8 { + return (rt.stringLen(p[0]) & 0xff) ^ p[1].cells[1] ^ p[2]; +} + +export function holds(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const a: rt.u8 = unwrapOr$Option_u8(rt.byteSliceFirst(data), 0); + const b: rt.u8 = unwrapOr$Option_u8(rt.byteSliceGet(data, 1), 0); + const _10: rt.u8 = rt.u8Rem(kind, 7); + if (_10 === 0) { + places(a, b, data, out); + } else if (_10 === 1) { + indexed(data, out); + } else if (_10 === 2) { + reborrows(a, b, data, out); + } else if (_10 === 3) { + copies(a, data, out); + } else if (_10 === 4) { + closures(a, b, data, out); + } else if (_10 === 5) { + parts(a, b, data, out); + } else { + ranges(data, out); + } +} + +/** + * A `&mut` through nested indexes lent as an `rt.Place`, each index assigned again while + * it lives, and one through a reference assigned another. + */ +export function places(a: rt.u8, b: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const item = copy$u8s(rt.bytes(4)); + const grid: Uint8Array[] = Array.from({ length: 4 }, (): Uint8Array => { + return copy$u8s(item); + }); + const rows: Row.Row[] = [{ cells: rt.bytes(4) }, { cells: rt.bytes(4) }, { cells: rt.bytes(4) }]; + const _16: [rt.usize, rt.usize] = [rt.u8Rem(a, 4) as rt.usize, rt.u8Rem(b, 4) as rt.usize]; + let i: rt.usize = _16[0]; + let j: rt.usize = _16[1]; + for (const k of rt.byteSliceView(data)) { + const _30: rt.usize = i; + if (!(_30 < 4)) { + rt.panic("index out of bounds: the len is " + 4 + " but the index is " + _30); + } + const _32: rt.usize = j; + if (!(_32 < 4)) { + rt.panic("index out of bounds: the len is " + 4 + " but the index is " + _32); + } + const _35: rt.usize = rt.usizeRem(i, 3); + if (!(_35 < 3)) { + rt.panic("index out of bounds: the len is " + 3 + " but the index is " + _35); + } + const _39: rt.usize = j; + if (!(_39 < 4)) { + rt.panic("index out of bounds: the len is " + 4 + " but the index is " + _39); + } + i = rt.usizeRem(rt.usizeAdd(i, k as rt.usize), 4); + j = rt.usizeRem(rt.usizeAdd(j, 1), 4); + set(rt.place(() => grid[_30][_32], (value) => grid[_30][_32] = value), k); + set(rt.place(() => rows[_35].cells[_39], (value2) => rows[_35].cells[_39] = value2), k ^ 1); + } + rt.iterExtendBytes(out, rt.iterFlatten(rt.iterMap(rt.iterSlice(grid), rt.byteSliceFrom), (value: rt.ByteSlice) => rt.iterSlice(rt.byteSliceView(value)))); + for (const _61 of rows) { + rt.iterExtendBytes(out, rt.iterSlice(_61.cells)); + } + const x: Uint8Array = rt.bytes(4).fill(a); + const y: Uint8Array = rt.bytes(4).fill(b); + let r: rt.ByteSlice = rt.byteSliceFrom(x); + const _72: rt.usize = rt.u8Rem(a, 4) as rt.usize; + const _76: rt.usize = r.length; + if (!(_72 < _76)) { + rt.panic("index out of bounds: the len is " + _76 + " but the index is " + _72); + } + const borrowed = r; + const borrowed2 = r.start + _72; + const s: rt.Place = rt.place(() => borrowed.data[borrowed2], (value3) => borrowed.data[borrowed2] = value3); + r = rt.byteSliceFrom(y); + set(s, 9); + const _84: rt.usize = r.length; + if (!(0 < _84)) { + rt.panic("index out of bounds: the len is " + _84 + " but the index is " + 0); + } + const _80: rt.u8 = rt.u8WrappingAdd(r.data[r.start], 1); + const _88: rt.usize = r.length; + if (!(0 < _88)) { + rt.panic("index out of bounds: the len is " + _88 + " but the index is " + 0); + } + r.data[r.start] = _80; + rt.iterExtendBytes(out, rt.iterSlice(x)); + rt.iterExtendBytes(out, rt.iterSlice(y)); +} + +/** + * Shared and mutable borrows of an element, the index assigned again before the read, + * and kept across iterations. + */ +export function indexed(data: rt.ByteSlice, out: rt.ByteVec): void { + let i: rt.usize = 0; + const kept: rt.u8[] = []; + while (i < data.length) { + const _9: rt.usize = i; + rt.idx(data, _9); + i = rt.usizeAdd(i, 1); + rt.byteVecPush(out, data.data[data.start + _9]); + kept.push(data.data[data.start + _9]); + } + rt.iterExtendBytes(out, rt.iterSlice(kept)); + const v: rt.ByteVec = rt.byteSliceToByteVec(data); + let i2: rt.usize = 0; + while (i2 < v.length) { + const at = rt.idx(v, i2); + i2 = rt.usizeAdd(i2, 2); + const _31: rt.u8 = rt.u8WrappingAdd(v.data[at], i2 & 0xff); + v.data[at] = _31; + } + rt.iterExtendBytes(out, rt.iterSlice(rt.byteVecView(v))); +} + +/** + * A reborrow through a reference that is then assigned another, shared and mutable, in a + * loop. + */ +export function reborrows(a: rt.u8, b: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let x: rt.u8 = a; + let y: rt.u8 = b; + let r: rt.Place = rt.place(() => x, (value) => x = value); + const s: rt.Place = r; + r = rt.place(() => y, (value2) => y = value2); + s.v = rt.u8WrappingAdd(s.v, 1); + r.v = rt.u8WrappingAdd(r.v, 2); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(x, y))); + let r2: rt.u8 = a; + const kept: rt.u8[] = []; + for (const _25 of rt.byteSliceView(data)) { + const s2: rt.u8 = r2; + r2 = _25; + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(s2, r2))); + kept.push(s2); + } + rt.iterExtendBytes(out, rt.iterSlice(kept)); +} + +/** + * A copy of a reference whose source is assigned another, and of an `Option` a range's + * `next` assigns again. + */ +export function copies(a: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let r: rt.u8 = a; + const kept: rt.u8[] = []; + for (const _9 of rt.byteSliceView(data)) { + const s: rt.u8 = r; + r = _9; + kept.push(s); + } + rt.iterExtendBytes(out, rt.iterSlice(kept)); + const range: rt.Range = new rt.Range(0, data.length); + let cur: rt.usize | undefined = range.start < range.end ? range.start++ : undefined; + const some: boolean = cur !== undefined; + const copied: rt.usize | undefined = cur; + cur = range.start < range.end ? range.start++ : undefined; + rt.byteVecPush(out, some ? 1 : 0); + rt.byteVecPush(out, mapOr$Option_usize_u8_consumers_closure8(copied, 255, (c: rt.usize): rt.u8 => { + return c & 0xff; + })); + while (true) { + const was: rt.usize | undefined = cur; + cur = range.start < range.end ? range.start++ : undefined; + if (was !== undefined) { + rt.byteVecPush(out, was & 0xff); + rt.byteVecPush(out, mapOr$Option_usize_u8_consumers_closure8(cur, 255, (c2: rt.usize): rt.u8 => { + return c2 & 0xff; + })); + } else { + break; + } + } +} + +/** + * Closures made in a loop that capture a reference to what that iteration reads, called + * after it: an element, a payload, a field, and a value a loop breaks with. + */ +export function closures(a: rt.u8, b: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let _31!: rt.usize; + let _47!: rt.usize; + const arr: Uint8Array = Uint8Array.of(a, b, 3, 4, 5, 6, 7, 8); + const rows: Row.Row[] = [{ cells: rt.bytes(4).fill(a) }, { cells: rt.bytes(4).fill(b) }, { cells: rt.bytes(4).fill(7) }]; + const v: rt.ByteVec = rt.byteSliceToByteVec(data); + const fs: Array<() => rt.u8> = []; + for (const _17 of rt.byteSliceView(data)) { + fs.push((): rt.u8 => { + return _17; + }); + } + const _28: rt.usize = v.length; + let iterStart: rt.usize = 0; + const iterEnd: rt.usize = _28; + while ((_31 = iterStart++) < iterEnd) { + const at = rt.idx(v, _31); + const _39: () => rt.u8 = (): rt.u8 => { + return rt.u8WrappingAdd(v.data[at], 1); + }; + fs.push(_39); + } + const _43: rt.usize = rt.byteSliceFrom(arr).length; + let rangeStart: rt.usize = 0; + const rangeEnd: rt.usize = _43; + while ((_47 = rangeStart++) < rangeEnd) { + const i2: rt.usize = _47; + if (!(i2 < 8)) { + rt.panic("index out of bounds: the len is " + 8 + " but the index is " + i2); + } + fs.push((): rt.u8 => { + return arr[i2]; + }); + } + const iter3: rt.Iter<[rt.usize, rt.u8]> = rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))); + while (true) { + const _61: [rt.usize, rt.u8] | rt.Done = iter3.next(); + if (_61 === rt.DONE) { + break; + } + const captured = _61[0]; + const _68: () => rt.u8 = (): rt.u8 => { + return rt.u8WrappingAdd(_61[1], captured & 0xff); + }; + fs.push(_68); + } + const itS: Row.Row[] = rows; + let itI: number = 0; + while (true) { + const _74: Row.Row | rt.Done = itI < itS.length ? itS[itI++] : rt.DONE; + if (_74 !== rt.DONE) { + const cells: rt.ByteSlice = rt.byteSliceFrom(_74.cells); + fs.push((): rt.u8 => { + return cells.data[cells.start + 1]; + }); + } else { + break; + } + } + for (const _86 of rt.byteSliceView(data)) { + let n: rt.u8 = _86; + while (!(rt.u8Rem(n, 3) === 0)) { + n = rt.u8WrappingAdd(n, 1); + } + const _96: rt.usize = rt.usizeRem(_86 as rt.usize, 3); + if (!(_96 < 3)) { + rt.panic("index out of bounds: the len is " + 3 + " but the index is " + _96); + } + const captured2 = n; + fs.push((): rt.u8 => { + return rows[_96].cells[0] ^ captured2; + }); + } + for (const _111 of fs) { + rt.byteVecPush(out, _111()); + } + rt.dropAll(fs); +} + +/** + * A tuple of references and a number, read by its fields and whole after the variables + * it was made of are assigned again, and one made in a loop from a variable it assigns. + */ +export function parts(a: rt.u8, b: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const rows: Row.Row[] = [{ cells: rt.bytes(4).fill(a) }, { cells: rt.bytes(4).fill(b) }]; + let s: string = "ab"; + let x: Row.Row = rows[0]; + let n: rt.u8 = a; + const _17: string = s; + const _18: Row.Row = x; + const pair: [string, Row.Row, rt.u8] = [_17, _18, n]; + s = "zzz"; + x = rows[1]; + n = rt.u8WrappingAdd(n, 1); + rt.byteVecExtendFromSlice(out, rt.stringAsBytes(_17)); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(_18.cells[0], pair[2], whole(pair)))); + rt.byteVecExtendFromSlice(out, rt.stringAsBytes(s)); + rt.byteVecPush(out, x.cells[0] ^ n); + let total: rt.u8 = 0; + for (const _47 of rt.byteSliceView(data)) { + const p: [rt.u8, rt.u8] = [n, _47]; + n = rt.u8WrappingMul(n, 3); + total = rt.u8WrappingAdd(rt.u8WrappingAdd(total, p[0]), _47); + } + rt.byteVecPush(out, total); +} + +/** A range's start read, then the range stepped, once and in a loop. */ +export function ranges(data: rt.ByteSlice, out: rt.ByteVec): void { + let _16!: rt.usize; + const range: rt.Range = new rt.Range(0, data.length); + const before: rt.usize = range.start; + const first: rt.usize | undefined = range.start < range.end ? range.start++ : undefined; + rt.byteVecPush(out, before & 0xff); + rt.byteVecPush(out, mapOr$Option_usize_u8_consumers_closure8(first, 255, (f: rt.usize): rt.u8 => { + return f & 0xff; + })); + const range2: rt.Range = new rt.Range(0, data.length); + while (true) { + const before2: rt.usize = range2.start; + const _16Some: boolean = range2.start < range2.end; + if (_16Some) { + _16 = range2.start++; + } + if (_16Some) { + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(before2 & 0xff, _16 & 0xff))); + } else { + break; + } + } +} + +//# sourceMappingURL=holds.ts.map diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 4491c4916..e6fbd8ad6 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -3,8 +3,8 @@ use crate::{ arrays, atomics, borrows, captures, cells, chars, chunks, collections, combinators, counts, cows, defaults, - durations, dyns, edits, floats, fmt, generics, ints, iters, keys, locals, locks, nested, objects, pointers, refs, - shapes, splits, strs, utf8, wrappers, + durations, dyns, edits, floats, fmt, generics, holds, ints, iters, keys, locals, locks, nested, objects, pointers, + refs, shapes, splits, strs, utf8, wrappers, }; /// The native side's entry: [`run`], except that a case starting with 0xff prints a @@ -112,6 +112,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("arrays::from_fns", |_, data, out| arrays::from_fns(data, out)), ("collections::optionals", collections::optionals), ("objects::objects", |_, data, out| objects::objects(data, out)), + ("holds::holds", holds::holds), ] } diff --git a/ts/fixtures/lang/src/holds.rs b/ts/fixtures/lang/src/holds.rs new file mode 100644 index 000000000..9e3328f90 --- /dev/null +++ b/ts/fixtures/lang/src/holds.rs @@ -0,0 +1,212 @@ +//! Values translation holds back rather than reads once: a borrow read as its place, a +//! place lent as an `rt.Place`, a copy of a reference, an aggregate read by its parts, a +//! closure's captures, and a temporary folded into its read. Each case assigns a variable +//! the held value names between the hold and the read, in a loop and across its +//! iterations, and the read must see what Rust read when the value was made. + +struct Row { + cells: [u8; 4], +} + +fn set(x: &mut u8, v: u8) { + *x = v; +} + +fn whole(p: (&str, &Row, u8)) -> u8 { + p.0.len() as u8 ^ p.1.cells[1] ^ p.2 +} + +pub fn holds(kind: u8, data: &[u8], out: &mut Vec) { + let a = data.first().copied().unwrap_or(0); + let b = data.get(1).copied().unwrap_or(0); + match kind % 7 { + 0 => places(a, b, data, out), + 1 => indexed(data, out), + 2 => reborrows(a, b, data, out), + 3 => copies(a, data, out), + 4 => closures(a, b, data, out), + 5 => parts(a, b, data, out), + _ => ranges(data, out), + } +} + +/// A `&mut` through nested indexes lent as an `rt.Place`, each index assigned again while +/// it lives, and one through a reference assigned another. +fn places(a: u8, b: u8, data: &[u8], out: &mut Vec) { + let mut grid = [[0u8; 4]; 4]; + let mut rows = [Row { cells: [0; 4] }, Row { cells: [0; 4] }, Row { cells: [0; 4] }]; + let (mut i, mut j) = (usize::from(a % 4), usize::from(b % 4)); + for &k in data { + let cell = &mut grid[i][j]; + let row = &mut rows[i % 3].cells[j]; + i = (i + usize::from(k)) % 4; + j = (j + 1) % 4; + set(cell, k); + set(row, k ^ 1); + } + out.extend(grid.iter().flatten()); + for row in &rows { + out.extend(row.cells); + } + let mut x = [a; 4]; + let mut y = [b; 4]; + let mut r: &mut [u8] = &mut x; + let s = &mut r[usize::from(a % 4)]; + r = &mut y; + set(s, 9); + r[0] = r[0].wrapping_add(1); + out.extend(x); + out.extend(y); +} + +/// Shared and mutable borrows of an element, the index assigned again before the read, +/// and kept across iterations. +fn indexed(data: &[u8], out: &mut Vec) { + let mut i = 0; + let mut kept = Vec::new(); + while i < data.len() { + let r = &data[i]; + i += 1; + out.push(*r); + kept.push(r); + } + out.extend(kept.into_iter().copied()); + let mut v = data.to_vec(); + let mut i = 0; + while i < v.len() { + let r = &mut v[i]; + i += 2; + *r = r.wrapping_add(i as u8); + } + out.extend(v); +} + +/// A reborrow through a reference that is then assigned another, shared and mutable, in a +/// loop. +fn reborrows(a: u8, b: u8, data: &[u8], out: &mut Vec) { + let (mut x, mut y) = (a, b); + let mut r: &mut u8 = &mut x; + let s: &mut u8 = &mut *r; + r = &mut y; + *s = s.wrapping_add(1); + *r = r.wrapping_add(2); + out.extend([x, y]); + let mut r: &u8 = &a; + let mut kept = Vec::new(); + for k in data { + let s: &u8 = &*r; + r = k; + out.extend([*s, *r]); + kept.push(s); + } + out.extend(kept.into_iter().copied()); +} + +/// A copy of a reference whose source is assigned another, and of an `Option` a range's +/// `next` assigns again. +fn copies(a: u8, data: &[u8], out: &mut Vec) { + let mut r: &u8 = &a; + let mut kept = Vec::new(); + for k in data { + let s = r; + r = k; + kept.push(s); + } + out.extend(kept.into_iter().copied()); + let mut range = 0..data.len(); + let mut cur = range.next(); + let some = cur.is_some(); + let copied = cur; + cur = range.next(); + out.push(u8::from(some)); + out.push(copied.map_or(0xff, |c| c as u8)); + loop { + let was = cur; + cur = range.next(); + let Some(w) = was else { break }; + out.push(w as u8); + out.push(cur.map_or(0xff, |c| c as u8)); + } +} + +/// Closures made in a loop that capture a reference to what that iteration reads, called +/// after it: an element, a payload, a field, and a value a loop breaks with. +fn closures(a: u8, b: u8, data: &[u8], out: &mut Vec) { + let arr = [a, b, 3, 4, 5, 6, 7, 8]; + let rows = [Row { cells: [a; 4] }, Row { cells: [b; 4] }, Row { cells: [7; 4] }]; + let v = data.to_vec(); + let mut fs: Vec u8 + '_>> = Vec::new(); + for k in data { + fs.push(Box::new(move || *k)); + } + for i in 0..v.len() { + let r = &v[i]; + fs.push(Box::new(move || r.wrapping_add(1))); + } + let mut range = 0..arr.len(); + loop { + let Some(i) = range.next() else { break }; + let r = &arr[i]; + fs.push(Box::new(move || *r)); + } + for (i, x) in data.iter().enumerate() { + fs.push(Box::new(move || x.wrapping_add(i as u8))); + } + let mut it = rows.iter(); + while let Some(row) = it.next() { + let cells = &row.cells; + fs.push(Box::new(move || cells[1])); + } + for k in data { + let mut n = *k; + let row: &Row = loop { + if n % 3 == 0 { + break &rows[usize::from(*k) % 3]; + } + n = n.wrapping_add(1); + }; + fs.push(Box::new(move || row.cells[0] ^ n)); + } + for f in &fs { + out.push(f()); + } +} + +/// A tuple of references and a number, read by its fields and whole after the variables +/// it was made of are assigned again, and one made in a loop from a variable it assigns. +fn parts(a: u8, b: u8, data: &[u8], out: &mut Vec) { + let rows = [Row { cells: [a; 4] }, Row { cells: [b; 4] }]; + let mut s: &str = "ab"; + let mut x: &Row = &rows[0]; + let mut n = a; + let pair = (s, x, n); + s = "zzz"; + x = &rows[1]; + n = n.wrapping_add(1); + out.extend_from_slice(pair.0.as_bytes()); + out.extend([pair.1.cells[0], pair.2, whole(pair)]); + out.extend_from_slice(s.as_bytes()); + out.push(x.cells[0] ^ n); + let mut total = 0u8; + for k in data { + let p = (n, *k); + n = n.wrapping_mul(3); + total = total.wrapping_add(p.0).wrapping_add(p.1); + } + out.push(total); +} + +/// A range's start read, then the range stepped, once and in a loop. +fn ranges(data: &[u8], out: &mut Vec) { + let mut range = 0..data.len(); + let before = range.start; + let first = range.next(); + out.push(before as u8); + out.push(first.map_or(0xff, |f| f as u8)); + let mut range = 0..data.len(); + loop { + let before = range.start; + let Some(i) = range.next() else { break }; + out.extend([before as u8, i as u8]); + } +} diff --git a/ts/fixtures/lang/src/lib.rs b/ts/fixtures/lang/src/lib.rs index 73905488f..7b2a88e52 100644 --- a/ts/fixtures/lang/src/lib.rs +++ b/ts/fixtures/lang/src/lib.rs @@ -7,8 +7,8 @@ //! and `Result`'s combinators from core's MIR, `Option`s of values that can already be //! `undefined`, atomics, trait objects, one-time cells, mutexes, `Cell`s, `RefCell`s, and //! read-write locks, the defaults of std types, the counts of shared pointers, arrays -//! `array::from_fn` makes, and maps of -//! `Option`s. +//! `array::from_fn` makes, maps of `Option`s, and values held back past a write of a +//! variable they name. //! //! `harness::run` is the entry point both sides of the differential test run, and //! `fmt::Fault` an error only `caller.ts` prints. @@ -32,6 +32,7 @@ mod floats; mod fmt; mod generics; pub mod harness; +mod holds; mod ints; mod iters; mod keys; diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index d8c73b132..a00385e06 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -70,7 +70,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { }; let read = self.uses[dest.local] > 1; let held = self.range_next(name, recv, stepped, read, &mut pre); - self.emit(pre, None, false, out); + self.emit(pre, false, out); if let Some((flag, more)) = held { self.hold_flag(flag, more); } @@ -90,7 +90,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { Some(Some(done)) => done, // Done: the destination is set. Some(None) => { - self.emit(pre, None, false, out); + self.emit(pre, false, out); return; } None => ( @@ -114,7 +114,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { _ => value, }; if !pre.is_empty() { - self.emit(pre, None, false, out); + self.emit(pre, false, out); } // What an `Iterator::next` returns, by the protocol, unless a `next` made it already. let value = match dest.as_local() { @@ -140,7 +140,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } None => { let stmts = self.store(dest, value, span); - self.emit(stmts, None, false, out); + self.emit(stmts, false, out); } } } @@ -695,7 +695,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { Expr::Member(..) | Expr::Index(..) => { let name = self.b.fresh("arg"); let stmt = StmtKind::decl(false, name.clone(), None, Some(value)).at(None); - self.emit(vec![stmt], None, false, out); + self.emit(vec![stmt], false, out); Expr::ident(name) } value => self.path(value, "arg", out), diff --git a/ts/src/lower/mir/mod.rs b/ts/src/lower/mir/mod.rs index c553279e0..6db80de7a 100644 --- a/ts/src/lower/mir/mod.rs +++ b/ts/src/lower/mir/mod.rs @@ -1753,10 +1753,9 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { // Emission /// Emit the statements `stmts`, which together are one MIR statement, folding in the - /// pending temporaries they read once where that keeps every effect in order. - /// `writes` is the local the statement assigns whole. If `hold`, nothing runs now: - /// the statements are a value being held back itself. - fn emit(&mut self, stmts: Vec, writes: Option<&str>, hold: bool, out: &mut Vec) { + /// pending temporaries they read once where that keeps every effect in order. If + /// `hold`, nothing runs now: the statements are a value being held back itself. + fn emit(&mut self, stmts: Vec, hold: bool, out: &mut Vec) { if self.pending.is_empty() { out.extend(stmts); return; @@ -1816,18 +1815,18 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { .filter(|&i| fold[i]) .map(|i| self.pending[i].order) .min(); + // A temporary held past a write of a name it reads would read the new value. + let written = assigned(&stmts); let mut flush = Vec::new(); for (i, p) in self.pending.iter().enumerate() { if fold[i] { continue; } let read = count[i] > 0; + let overwritten = written.contains(&p.name) || p.reads.iter().any(|r| written.contains(r)); let keep = match hold { - true => !read, - false => { - let overwritten = writes.is_some_and(|w| w == p.name || p.reads.iter().any(|r| r == w)); - !read && !p.effect && !p.memory && !overwritten - } + true => !read && !overwritten, + false => !read && !p.effect && !p.memory && !overwritten, }; let after = first_folded.is_some_and(|f| p.order > f); if !keep { @@ -1917,7 +1916,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { let mut stmts = pre; stmts.extend(exprs.into_iter().map(|e| StmtKind::Expr(e).at(None))); let mut emitted = Vec::new(); - self.emit(stmts, None, hold, &mut emitted); + self.emit(stmts, hold, &mut emitted); let folded = emitted .drain(emitted.len() - n..) .map(|s| match s.kind { @@ -2100,7 +2099,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { if self.uses[local] == 0 { // Nothing reads it: only its effects run. if effect { - self.emit(vec![StmtKind::Expr(value).at(None)], None, false, out); + self.emit(vec![StmtKind::Expr(value).at(None)], false, out); } return; } @@ -2135,7 +2134,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } self.assigned[local] = true; let stmt = StmtKind::Expr(Expr::ident(&name).assign(value)).at(None); - self.emit(vec![stmt], Some(&name), false, out); + self.emit(vec![stmt], false, out); // An aggregate assigned once and never changed: its operands, as emitted, // read in place of its fields. if parts.is_some() @@ -2151,7 +2150,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } Slot::Unit => { if effect { - self.emit(vec![StmtKind::Expr(value).at(None)], None, false, out); + self.emit(vec![StmtKind::Expr(value).at(None)], false, out); } } Slot::Alias(_) | Slot::Discr(_) | Slot::Flag(_) => unreachable!("set by their own assignment"), @@ -2192,7 +2191,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { StatementKind::SetDiscriminant { place, variant_index } => { let target = self.set_discriminant(**place, *variant_index, span); if let Some(stmt) = target { - self.emit(vec![stmt], None, false, out); + self.emit(vec![stmt], false, out); } } StatementKind::Intrinsic(intrinsic) => match &**intrinsic { @@ -2342,16 +2341,16 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { _ => None, }; if !pre.is_empty() { - self.emit(pre, None, false, out); + self.emit(pre, false, out); } self.set_local(local, value, effect, memory, parts, out); return; } if !pre.is_empty() { - self.emit(pre, None, false, out); + self.emit(pre, false, out); } let stmts = self.store(place, value, span); - self.emit(stmts, None, false, out); + self.emit(stmts, false, out); } /// Hold `value` in a path that reads it again for free. @@ -2362,7 +2361,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } let name = self.b.fresh(base); let stmt = StmtKind::decl(false, name.clone(), None, Some(value)).at(None); - self.emit(vec![stmt], None, false, out); + self.emit(vec![stmt], false, out); Expr::ident(name) } @@ -2419,7 +2418,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } }; let loc = self.b.t.loc(span); - self.emit(vec![StmtKind::Return(value).at(loc)], None, false, out); + self.emit(vec![StmtKind::Return(value).at(loc)], false, out); self.flush(out); Exit::End } @@ -2428,7 +2427,6 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { TerminatorKind::Unreachable | TerminatorKind::Return => { self.emit( vec![StmtKind::Expr(Expr::rt("unreachable", vec![Expr::undefined()])).at(None)], - None, false, out, ); @@ -2454,7 +2452,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { .at(None), ]; } - self.emit(glue, None, false, out); + self.emit(glue, false, out); } else if self.b.t.drops(ty) { let mut pre = Vec::new(); let value = self.place_expr(*place, span, &mut pre); @@ -2465,7 +2463,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { if let Some(dropped) = dropped { pre.push(StmtKind::Expr(dropped).at(None)); } - self.emit(pre, None, false, out); + self.emit(pre, false, out); } Exit::Goto(target.as_usize()) } @@ -2675,11 +2673,11 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { .at(self.b.t.loc(span)), ); } - self.emit(pre, None, false, out); + self.emit(pre, false, out); return; } pre.push(StmtKind::Expr(Expr::rt("idx", vec![slice, index])).at(self.b.t.loc(span))); - self.emit(pre, None, false, out); + self.emit(pre, false, out); return; } let mut pre = Vec::new(); @@ -2708,7 +2706,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { // An assert that always fails, such as a coroutine's after it returned. if matches!(&failed, Expr::Lit(t) if t == "true") { pre.push(StmtKind::Expr(panic).at(self.b.t.loc(span))); - self.emit(pre, None, false, out); + self.emit(pre, false, out); return; } pre.push( @@ -2719,7 +2717,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } .at(self.b.t.loc(span)), ); - self.emit(pre, None, false, out); + self.emit(pre, false, out); } /// The panic of an assert that fails: the runtime's own function for a coroutine @@ -3286,6 +3284,23 @@ fn scan_reads(stmt: &Stmt, out: &mut Vec) { } } +/// The names `stmts` assign, anywhere in them. +fn assigned(stmts: &[Stmt]) -> FxHashSet { + let mut names = FxHashSet::default(); + for stmt in stmts { + visit_stmt(stmt, &mut |e| { + visit_expr(e, &mut |e| { + if let Expr::Assign(target, _) | Expr::Postfix(target, _) = e + && let Expr::Ident(name) = &**target + { + names.insert(name.clone()); + } + }) + }); + } + names +} + fn reads_of(e: &Expr, out: &mut Vec) { match e { Expr::Ident(name) => out.push(name.clone()), diff --git a/ts/src/lower/mir/place.rs b/ts/src/lower/mir/place.rs index b594bc672..8ef4a97af 100644 --- a/ts/src/lower/mir/place.rs +++ b/ts/src/lower/mir/place.rs @@ -909,22 +909,18 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { self.stable_path(target, true, span, out) } - /// `target`, a path, with the index it goes through held where it is now, and the + /// `target`, a path, with each index it goes through held where it is now, and the /// object too unless it is `fixed`, which can't change while borrowed. pub(super) fn stable_path(&mut self, target: Expr, fixed: bool, span: Span, out: &mut Vec) -> Expr { self.materialize(&target, out); - let mut base = |fx: &mut Self, e: Expr| match fixed { - true => e, - false => fx.held_now(e, out), - }; match target { Expr::Located(_, e) => self.stable_path(*e, fixed, span, out), // A payload `Option::insert` lends, read as its type. Expr::As(e, ty) => Expr::As(Box::new(self.stable_path(*e, fixed, span, out)), ty), Expr::Ident(_) | Expr::This => target, - Expr::Member(object, key) => Expr::Member(Box::new(base(self, *object)), key), + Expr::Member(object, key) => Expr::Member(Box::new(self.stable_object(*object, fixed, out)), key), Expr::Index(object, i) => { - let object = base(self, *object); + let object = self.stable_object(*object, fixed, out); let i = self.held_now(*i, out); Expr::Index(Box::new(object), Box::new(i)) } @@ -932,6 +928,26 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } } + /// `object`, a path's object, held where it is now unless it is `fixed`, in which case + /// only the indices it goes through are, as the borrow fixes the objects but not the + /// variables they are indexed by. + fn stable_object(&mut self, object: Expr, fixed: bool, out: &mut Vec) -> Expr { + if !fixed { + return self.held_now(object, out); + } + match object { + Expr::Located(loc, e) => Expr::Located(loc, Box::new(self.stable_object(*e, true, out))), + Expr::As(e, ty) => Expr::As(Box::new(self.stable_object(*e, true, out)), ty), + Expr::Member(e, key) => Expr::Member(Box::new(self.stable_object(*e, true, out)), key), + Expr::Index(e, i) => { + let e = self.stable_object(*e, true, out); + let i = self.held_now(*i, out); + Expr::Index(Box::new(e), Box::new(i)) + } + e => e, + } + } + /// `e`, evaluated now into a constant unless reading it later reads the same. fn held_now(&mut self, e: Expr, out: &mut Vec) -> Expr { if e.is_constant() || matches!(e, Expr::This) || self.stable_name(&e) { @@ -939,7 +955,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } let name = self.b.fresh("borrowed"); let stmt = StmtKind::decl(false, name.clone(), None, Some(e)).at(None); - self.emit(vec![stmt], None, false, out); + self.emit(vec![stmt], false, out); Expr::ident(name) } diff --git a/ts/src/lower/mir/rvalue.rs b/ts/src/lower/mir/rvalue.rs index bdd295cfd..8061797a2 100644 --- a/ts/src/lower/mir/rvalue.rs +++ b/ts/src/lower/mir/rvalue.rs @@ -717,7 +717,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { // Typed, so a reassigned literal keeps its integer's type. let ts_ty = constant.then(|| self.b.t.held_ty(ty, span)); let stmt = StmtKind::decl(rebinds, name.clone(), ts_ty, Some(value)).at(None); - self.emit(vec![stmt], None, false, out); + self.emit(vec![stmt], false, out); Expr::ident(name) } }; diff --git a/ts/src/lower/mir/scalar.rs b/ts/src/lower/mir/scalar.rs index 8277b4032..a533dbf16 100644 --- a/ts/src/lower/mir/scalar.rs +++ b/ts/src/lower/mir/scalar.rs @@ -1114,7 +1114,7 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { els: Vec::new(), } .at(None); - self.emit(vec![check], None, false, out); + self.emit(vec![check], false, out); } } parts.extend([pure(d.clone(), ds), pure(s.clone(), ss), pure(mid, steady)]); @@ -1343,7 +1343,7 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { self.steady.insert(name.clone()); self.scalar_binds.push((name.clone(), ty)); let stmt = StmtKind::Expr(Expr::ident(&name).assign(value)).at(None); - self.emit(vec![stmt], Some(&name), false, out); + self.emit(vec![stmt], false, out); (Expr::ident(name), true) } @@ -1380,7 +1380,7 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { true => { let name = self.b.fresh("part"); let stmt = StmtKind::decl(false, name.clone(), None, Some(value)).at(None); - self.emit(vec![stmt], None, false, out); + self.emit(vec![stmt], false, out); Some(Expr::ident(name)) } false => Some(value), @@ -1403,7 +1403,7 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { .expect("a local held as its parts") .assigned = true; let stmt = StmtKind::Expr(Expr::ident(var).assign(value)).at(None); - self.emit(vec![stmt], Some(var), false, out); + self.emit(vec![stmt], false, out); } } diff --git a/ts/src/lower/mir/step.rs b/ts/src/lower/mir/step.rs index 3839732b3..58119d720 100644 --- a/ts/src/lower/mir/step.rs +++ b/ts/src/lower/mir/step.rs @@ -265,7 +265,7 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { continue; } let stmt = StmtKind::Expr(Expr::ident(&var).assign(value)).at(None); - self.emit(vec![stmt], Some(&var), false, out); + self.emit(vec![stmt], false, out); } }