diff --git a/quest/m2/ts-drop.md b/quest/m2/ts-drop.md index 00fb28c76..bfcccf4d6 100644 --- a/quest/m2/ts-drop.md +++ b/quest/m2/ts-drop.md @@ -17,25 +17,24 @@ blocks of `-C panic=unwind` MIR, which a function runs from a `catch`. A value w `drop_in_place` shim. A runtime function that may drop what std's would (a collection's `clear`, `unwrap_or`'s fallback, an adaptor's rejects) takes a dropper for each such type after its own arguments (`drop_fn` and `droppers` in `drop.rs`), and a consumer -that takes an iterator by value takes `rt.drop` for it. The task fixture's `drops.rs` -and `drops/iters.rs` check these against native Rust, natively unwinding too. - -What still fails translation, by the census at the pinned moq revision (moq-net's, then -kio's): - -- A closure that owns a value that drops, dropped without a call that takes it (17, 0), - as `Option::map` drops it on `None` and `Result::map` on an error. An arrow function - has no `[Symbol.dispose]`; one that owns droppable captures needs one, and so do the - adaptors that hold it (`map`'s, `filter`'s), which drop it after their iterator, as - std's fields do. - -Refused with none in the census, each where std's order is one JS's can't follow yet: +that takes an iterator by value takes `rt.drop` for it. A closure that owns a value that +drops carries `[Symbol.dispose]` (`rt.closure`), and one that `Fn` or `FnMut` call drops +after a call through `FnOnce` (`rt.callOnce`, and `rt.once` for std's). The task +fixture's `drops.rs`, `drops/iters.rs`, and `drops/closures.rs` check these against +native Rust, natively unwinding too. + +Nothing that drops fails translation in the census at the pinned moq revision. Refused +with none in the census, each where std's order is one JS's can't follow yet: +a closure that owns a value that drops, given to std's iterators or a collection's +methods, which hold or call it (an adaptor drops it after its iterator, as std's fields +do, and each consumer where its `fold` path does); `into_keys` and `into_values` of a hash map whose other half drops (`fold` drops it after the half it yields, `next` before); `zip` of two `TrustedRandomAccess` iterators of items that drop; a `flatten` whose inner iterator holds items once ended, or that a `fold` reads where both its source and inner iterators drop; a B-tree's `collect` of entries that drop, sorted by the crate's own `Ord`; a `collect` in place of a `zip` -whose second iterator drops; and `DebugList::entries` of items that drop. +whose second iterator drops; `successors` of items that drop, which holds the next one; +and `DebugList::entries` of items that drop. Watch the cost: a function whose cleanup does something JS can see sets a variable before each call that can panic and wraps its body in a `try`; measure any change on diff --git a/ts/README.md b/ts/README.md index d326c29aa..c93f2237c 100644 --- a/ts/README.md +++ b/ts/README.md @@ -84,7 +84,7 @@ in `rt/str.ts`, collections in `rt/collections.ts`, ownership and async in | `HashMap`, `HashSet`, `BTreeMap`, `BTreeSet`, `VecDeque` | `rt.HashMap` and so on, each a plain object whose functions are the runtime's: `rt.hashGet(map, key)` | | `Ordering`, `DefaultHasher` | `rt.Ordering`, which is `-1`, `0`, or `1`; `rt.Hasher` | | `Duration` | `rt.Duration`: `{ secs, nanos }`, std's whole seconds as an `rt.u64` and the nanoseconds past them, never changed once made | -| closure | an arrow function | +| closure | an arrow function, with `[Symbol.dispose]` if it owns a value that drops | | constant | its value, inlined as Rust inlines it | | a type that drops | a class with `[Symbol.dispose]`: its `Drop` impl, then its fields | | a type's function | a function of the type's module, taking `self` first: `Decoder.slice(dec, 4)` after `import * as Decoder` | @@ -412,8 +412,9 @@ to hand-written TS ([bindings](#bindings)). ## Not yet -These fail translation: floats, statics, async closures, values that drop in a closure dropped without a call that -takes it or in an iterator ([Drop](/quest/m2/ts-drop.md)), a `Cow` of other than `str`, +These fail translation: floats, statics, async closures, a closure that owns a value +that drops given to std's iterators or collections, and values that drop in some +iterators ([Drop](/quest/m2/ts-drop.md)), a `Cow` of other than `str`, a collection holding a type that can be `undefined`, as `()` and `Option` can, a collection's `capacity`, looking up a key by another type it borrows as through the crate's own `Borrow`, a `&mut` to a value JS copies that std makes, other than a map's, or hands a closure, as `iter_mut` and diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index 3a7e890b4..846a14f13 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -1,7 +1,7 @@ # Census of `reject-fixture` -- Refusals: 87 -- Function instances: 77 +- Refusals: 86 +- Function instances: 73 - Generic functions no instance reached, lowered over their type parameters: 2 ## By construct @@ -10,26 +10,25 @@ | ---: | --- | --- | | 10 | unsupported call to `_` | fixtures/reject/src/lib.rs:28:4 | | 6 | `_` is not supported: its `None` and `Some` would both be `undefined` | fixtures/reject/src/lib.rs:23:15 | -| 4 | a variant of `_`, which is not supported: either is the same JS string | fixtures/reject/src/lib.rs:407:2 | +| 5 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | fixtures/reject/src/lib.rs:45:11 | +| 4 | a variant of `_`, which is not supported: either is the same JS string | fixtures/reject/src/lib.rs:397:2 | | 4 | an assignment through a reference to `_` | fixtures/reject/src/lib.rs:109:2 | -| 4 | dropping a closure that owns a value that drops, which is not supported yet | fixtures/reject/src/lib.rs:46:1 | | 4 | unsupported type `_` | fixtures/reject/src/lib.rs:7:14 | | 3 | `_` with a mutable reference to a value JS copies, which its mapping would copy | fixtures/reject/src/lib.rs:17:2 | -| 3 | dropping a closure that owns a value that drops, which is not supported yet, in `_` | fixtures/reject/src/lib.rs:378:12 | +| 3 | dropping `_`, which is not supported yet | fixtures/reject/src/lib.rs:45:18 | | 3 | replacing the value behind a reference to `_` | fixtures/reject/src/lib.rs:93:2 | | 2 | `_` is not supported: it would be pending two ways with one value | fixtures/reject/src/lib.rs:113:20 | | 2 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | fixtures/reject/src/lib.rs:265:16 | | 2 | a field of `_` | fixtures/reject/src/lib.rs:281:7 | | 2 | an iterator of the crate implementing `_`, which is not supported yet | fixtures/reject/src/lib.rs:172:1 | | 2 | constructing `_` from another crate | fixtures/reject/src/lib.rs:158:23 | -| 2 | dropping `_`, which is not supported yet | fixtures/reject/src/lib.rs:399:79 | | 2 | overriding `_`, which std's adaptors call in place of `next` or beside it | fixtures/reject/src/lib.rs:152:2 | | 1 | `Binary` formatting, as in `{:b}`, is not supported | fixtures/reject/src/lib.rs:70:3 | | 1 | `LowerExp` formatting, as in `{:e}`, is not supported | fixtures/reject/src/lib.rs:72:3 | | 1 | `Octal` formatting, as in `{:o}`, is not supported | fixtures/reject/src/lib.rs:71:3 | | 1 | `Pointer` formatting, as in `{:p}`, is not supported | fixtures/reject/src/lib.rs:75:3 | | 1 | `_` holds `_`, which is `undefined` as `None` is, which is not supported | fixtures/reject/src/lib.rs:286:6 | -| 1 | `_` is not supported yet | fixtures/reject/src/lib.rs:418:18 | +| 1 | `_` is not supported yet | fixtures/reject/src/lib.rs:408:18 | | 1 | `_` of `_` by `_`, which is not supported yet | fixtures/reject/src/lib.rs:309:6 | | 1 | `_` of `_`, which drops `_`, which is not supported yet | fixtures/reject/src/lib.rs:51:13 | | 1 | `_` through a held type's own, which is not supported yet | fixtures/reject/src/lib.rs:341:2 | @@ -42,16 +41,16 @@ | 1 | a constant of type `_` | fixtures/reject/src/lib.rs:88:2 | | 1 | a copy of a closure that changes what it captured | fixtures/reject/src/lib.rs:275:14 | | 1 | a mutable borrow of a subslice of `_`, which JS would copy, having no views | fixtures/reject/src/lib.rs:244:13 | -| 1 | a mutable reference into a `Result`'s payload, passed on or kept | fixtures/reject/src/lib.rs:456:12 | +| 1 | a mutable reference into a `Result`'s payload, passed on or kept | fixtures/reject/src/lib.rs:446:12 | | 1 | a precision, as in `{:.2}`, is not supported | fixtures/reject/src/lib.rs:67:3 | | 1 | a printable type must implement `Display` or `Debug` | fixtures/reject/src/lib.rs:100:1 | | 1 | a scalar constant of type `_` | fixtures/reject/src/lib.rs:8:2 | | 1 | a width or precision from an argument, as in `{:1$}`, is not supported | fixtures/reject/src/lib.rs:74:3 | | 1 | a zero-sized constant of type `_` | fixtures/reject/src/lib.rs:60:2 | -| 1 | an iterator that owns `_`, which drops, which is not supported yet | fixtures/reject/src/lib.rs:401:13 | +| 1 | an iterator that owns `_`, which drops, which is not supported yet | fixtures/reject/src/lib.rs:391:13 | | 1 | arithmetic on `_` | fixtures/reject/src/lib.rs:8:2 | | 1 | comparing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:291:52 | -| 1 | constructing `_` | fixtures/reject/src/lib.rs:425:53 | +| 1 | constructing `_` | fixtures/reject/src/lib.rs:415:53 | | 1 | hashing `_` as a key, which is not supported yet | fixtures/reject/src/lib.rs:291:52 | | 1 | hex `Debug`, as in `{:x?}`, is not supported | fixtures/reject/src/lib.rs:73:3 | | 1 | the `+` flag of a format spec is not supported | fixtures/reject/src/lib.rs:68:3 | @@ -63,46 +62,49 @@ The type or module each refused type or path belongs to. | Count | API | First | | ---: | --- | --- | -| 7 | `std::borrow::Cow` | fixtures/reject/src/lib.rs:407:2 | +| 7 | `std::borrow::Cow` | fixtures/reject/src/lib.rs:397:2 | | 6 | `Option` | fixtures/reject/src/lib.rs:23:15 | | 6 | `f64` | fixtures/reject/src/lib.rs:7:14 | +| 5 | `{closure}` | fixtures/reject/src/lib.rs:45:11 | +| 4 | `Iterator` | fixtures/reject/src/lib.rs:45:11 | | 3 | `std::collections::HashMap` | fixtures/reject/src/lib.rs:51:13 | -| 3 | `std::option::Option` | fixtures/reject/src/lib.rs:286:6 | +| 3 | `std::iter::Map` | fixtures/reject/src/lib.rs:45:18 | | 3 | `std::slice` | fixtures/reject/src/lib.rs:28:4 | | 3 | `std::sync::Arc` | fixtures/reject/src/lib.rs:109:2 | -| 2 | `Iterator` | fixtures/reject/src/lib.rs:152:2 | | 2 | `Meters` | fixtures/reject/src/lib.rs:93:2 | | 2 | `Noisy` | fixtures/reject/src/lib.rs:51:13 | | 2 | `Poll` | fixtures/reject/src/lib.rs:113:20 | | 2 | `std::iter::Iterator` | fixtures/reject/src/lib.rs:265:16 | -| 2 | `std::iter::Map` | fixtures/reject/src/lib.rs:158:23 | -| 2 | `std::iter::Take` | fixtures/reject/src/lib.rs:399:79 | +| 2 | `std::iter::Take` | fixtures/reject/src/lib.rs:389:79 | | 2 | `std::ops::Range` | fixtures/reject/src/lib.rs:193:8 | +| 2 | `std::option::Option` | fixtures/reject/src/lib.rs:286:6 | | 2 | `std::pin::Pin` | fixtures/reject/src/lib.rs:129:2 | -| 2 | `std::result::Result` | fixtures/reject/src/lib.rs:382:12 | | 2 | `std::vec::Vec` | fixtures/reject/src/lib.rs:291:52 | | 2 | `u8` | fixtures/reject/src/lib.rs:88:2 | | 1 | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:172:1 | +| 1 | `Entry` | fixtures/reject/src/lib.rs:380:13 | | 1 | `FusedIterator` | fixtures/reject/src/lib.rs:226:1 | | 1 | `HashMap` | fixtures/reject/src/lib.rs:309:6 | | 1 | `Hasher` | fixtures/reject/src/lib.rs:317:9 | | 1 | `IntToFloat` | fixtures/reject/src/lib.rs:104:2 | | 1 | `[u16; 1]` | fixtures/reject/src/lib.rs:258:22 | | 1 | `dyn std::ops::Fn(u8) -> u8` | fixtures/reject/src/lib.rs:11:16 | +| 1 | `from_fn` | fixtures/reject/src/lib.rs:390:13 | | 1 | `into_keys` | fixtures/reject/src/lib.rs:51:13 | -| 1 | `std::collections::hash_map::Entry` | fixtures/reject/src/lib.rs:446:15 | +| 1 | `std::collections::hash_map::Entry` | fixtures/reject/src/lib.rs:436:15 | | 1 | `std::iter::Chain` | fixtures/reject/src/lib.rs:185:51 | | 1 | `std::iter::Flatten` | fixtures/reject/src/lib.rs:234:26 | | 1 | `std::iter::IntoIterator` | fixtures/reject/src/lib.rs:17:2 | | 1 | `std::iter::Rev` | fixtures/reject/src/lib.rs:179:14 | -| 1 | `std::mem` | fixtures/reject/src/lib.rs:44:18 | +| 1 | `std::mem` | fixtures/reject/src/lib.rs:44:29 | | 1 | `std::ops::RangeFrom` | fixtures/reject/src/lib.rs:281:48 | | 1 | `std::ops::RangeTo` | fixtures/reject/src/lib.rs:281:7 | | 1 | `std::slice::Iter` | fixtures/reject/src/lib.rs:139:11 | | 1 | `std::str` | fixtures/reject/src/lib.rs:370:4 | | 1 | `std::string::String` | fixtures/reject/src/lib.rs:82:10 | | 1 | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:134:2 | -| 1 | `std::time::Duration` | fixtures/reject/src/lib.rs:433:2 | +| 1 | `std::time::Duration` | fixtures/reject/src/lib.rs:423:2 | +| 1 | `successors` | fixtures/reject/src/lib.rs:389:15 | | 1 | `u32` | fixtures/reject/src/lib.rs:244:13 | | 1 | `u64` | fixtures/reject/src/lib.rs:104:2 | | 1 | `{async closure}` | fixtures/reject/src/lib.rs:60:2 | @@ -112,22 +114,22 @@ The type or module each refused type or path belongs to. | Count | Construct | Type or path | First | | ---: | --- | --- | --- | | 6 | `_` is not supported: its `None` and `Some` would both be `undefined` | `Option` | fixtures/reject/src/lib.rs:23:15 | -| 4 | a variant of `_`, which is not supported: either is the same JS string | `std::borrow::Cow` | fixtures/reject/src/lib.rs:407:2 | +| 4 | a variant of `_`, which is not supported: either is the same JS string | `std::borrow::Cow` | fixtures/reject/src/lib.rs:397:2 | | 3 | unsupported type `_` | `f64` | fixtures/reject/src/lib.rs:7:14 | | 2 | `_` is not supported: it would be pending two ways with one value | `Poll` | fixtures/reject/src/lib.rs:113:20 | | 2 | an assignment through a reference to `_` | `std::pin::Pin` | fixtures/reject/src/lib.rs:129:2 | -| 2 | dropping `_`, which is not supported yet | `std::iter::Take` | fixtures/reject/src/lib.rs:399:79 | +| 2 | dropping `_`, which is not supported yet | `std::iter::Take` | fixtures/reject/src/lib.rs:389:79 | | 2 | replacing the value behind a reference to `_` | `Meters` | fixtures/reject/src/lib.rs:93:2 | | 1 | `_` holds `_`, which is `undefined` as `None` is, which is not supported | `std::collections::HashMap`, `std::option::Option` | fixtures/reject/src/lib.rs:286:6 | -| 1 | `_` is not supported yet | `std::borrow::Cow` | fixtures/reject/src/lib.rs:418:18 | +| 1 | `_` is not supported yet | `std::borrow::Cow` | fixtures/reject/src/lib.rs:408:18 | | 1 | `_` of `_` by `_`, which is not supported yet | `HashMap::contains_key`, `std::collections::HashMap`, `u8` | fixtures/reject/src/lib.rs:309:6 | | 1 | `_` of `_`, which drops `_`, which is not supported yet | `into_keys`, `std::collections::HashMap`, `Noisy` | fixtures/reject/src/lib.rs:51:13 | | 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::iter::Iterator::count` | fixtures/reject/src/lib.rs:265:38 | | 1 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | `std::iter::Iterator::filter` | fixtures/reject/src/lib.rs:265:16 | | 1 | `_` through a held type's own, which is not supported yet | `::ne` | fixtures/reject/src/lib.rs:341:2 | -| 1 | `_` with a mutable reference to a value JS copies, which its mapping would copy | `std::collections::hash_map::Entry::and_modify` | fixtures/reject/src/lib.rs:446:15 | +| 1 | `_` with a mutable reference to a value JS copies, which its mapping would copy | `std::collections::hash_map::Entry::and_modify` | fixtures/reject/src/lib.rs:436:15 | | 1 | `_` with a mutable reference to a value JS copies, which its mapping would copy | `std::iter::IntoIterator::into_iter` | fixtures/reject/src/lib.rs:17:2 | -| 1 | `_` with a mutable reference to a value JS copies, which its mapping would copy | `std::slice::last_mut` | fixtures/reject/src/lib.rs:439:21 | +| 1 | `_` with a mutable reference to a value JS copies, which its mapping would copy | `std::slice::last_mut` | fixtures/reject/src/lib.rs:429:21 | | 1 | `_`, which is not supported yet | `Hasher::write` | fixtures/reject/src/lib.rs:317:9 | | 1 | `fold` of `_`, which folds a `flatten` whose source and iterators both drop, which is not supported yet | `std::iter::Flatten` | fixtures/reject/src/lib.rs:234:26 | | 1 | a `_` cast from `_` to `_` | `IntToFloat`, `u64`, `f64` | fixtures/reject/src/lib.rs:104:2 | @@ -144,25 +146,28 @@ The type or module each refused type or path belongs to. | 1 | an assignment through a reference to `_` | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:134:2 | | 1 | an iterator of the crate implementing `_`, which is not supported yet | `DoubleEndedIterator` | fixtures/reject/src/lib.rs:172:1 | | 1 | an iterator of the crate implementing `_`, which is not supported yet | `FusedIterator` | fixtures/reject/src/lib.rs:226:1 | -| 1 | an iterator that owns `_`, which drops, which is not supported yet | `Noisy` | fixtures/reject/src/lib.rs:401:13 | +| 1 | an iterator that owns `_`, which drops, which is not supported yet | `Noisy` | fixtures/reject/src/lib.rs:391:13 | | 1 | arithmetic on `_` | `f64` | fixtures/reject/src/lib.rs:8:2 | | 1 | comparing `_` as a key, which is not supported yet | `std::vec::Vec` | fixtures/reject/src/lib.rs:291:52 | -| 1 | constructing `_` | `std::borrow::Cow` | fixtures/reject/src/lib.rs:425:53 | +| 1 | constructing `_` | `std::borrow::Cow` | fixtures/reject/src/lib.rs:415:53 | | 1 | constructing `_` from another crate | `std::iter::Map` | fixtures/reject/src/lib.rs:158:23 | | 1 | constructing `_` from another crate | `std::iter::Rev` | fixtures/reject/src/lib.rs:179:14 | -| 1 | dropping a closure that owns a value that drops, which is not supported yet, in `_` | `std::option::Option::map` | fixtures/reject/src/lib.rs:378:12 | -| 1 | dropping a closure that owns a value that drops, which is not supported yet, in `_` | `std::result::Result::and_then` | fixtures/reject/src/lib.rs:386:12 | -| 1 | dropping a closure that owns a value that drops, which is not supported yet, in `_` | `std::result::Result::map_err` | fixtures/reject/src/lib.rs:382:12 | +| 1 | dropping `_`, which is not supported yet | `std::iter::Map` | fixtures/reject/src/lib.rs:45:18 | | 1 | hashing `_` as a key, which is not supported yet | `std::vec::Vec` | fixtures/reject/src/lib.rs:291:52 | | 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::fold` | fixtures/reject/src/lib.rs:152:2 | | 1 | overriding `_`, which std's adaptors call in place of `next` or beside it | `Iterator::size_hint` | fixtures/reject/src/lib.rs:206:2 | +| 1 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | `{closure}`, `Entry::or_insert_with` | fixtures/reject/src/lib.rs:380:13 | +| 1 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | `{closure}`, `Iterator::any` | fixtures/reject/src/lib.rs:381:11 | +| 1 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | `{closure}`, `Iterator::map` | fixtures/reject/src/lib.rs:45:11 | +| 1 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | `{closure}`, `from_fn::from_fn` | fixtures/reject/src/lib.rs:390:13 | +| 1 | passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet | `{closure}`, `successors::successors` | fixtures/reject/src/lib.rs:389:15 | | 1 | replacing the value behind a reference to `_` | `[u16; 1]` | fixtures/reject/src/lib.rs:258:22 | -| 1 | unsupported call to `_` | `::deref` | fixtures/reject/src/lib.rs:419:2 | -| 1 | unsupported call to `_` | `::eq` | fixtures/reject/src/lib.rs:463:2 | +| 1 | unsupported call to `_` | `::deref` | fixtures/reject/src/lib.rs:409:2 | +| 1 | unsupported call to `_` | `::eq` | fixtures/reject/src/lib.rs:453:2 | | 1 | unsupported call to `_` | `::cycle` | fixtures/reject/src/lib.rs:139:11 | | 1 | unsupported call to `_` | `::write_fmt` | fixtures/reject/src/lib.rs:82:10 | -| 1 | unsupported call to `_` | `::add_assign` | fixtures/reject/src/lib.rs:433:2 | -| 1 | unsupported call to `_` | `std::mem::size_of_val` | fixtures/reject/src/lib.rs:44:18 | +| 1 | unsupported call to `_` | `::add_assign` | fixtures/reject/src/lib.rs:423:2 | +| 1 | unsupported call to `_` | `std::mem::size_of_val` | fixtures/reject/src/lib.rs:44:29 | | 1 | unsupported call to `_` | `std::ops::Range::contains` | fixtures/reject/src/lib.rs:193:8 | | 1 | unsupported call to `_` | `std::slice::copy_within` | fixtures/reject/src/lib.rs:359:4 | | 1 | unsupported call to `_` | `std::slice::sort` | fixtures/reject/src/lib.rs:28:4 | @@ -174,15 +179,15 @@ The type or module each refused type or path belongs to.
unsupported call to `_` (10) - fixtures/reject/src/lib.rs:28:4: unsupported call to `std::slice::::sort` -- fixtures/reject/src/lib.rs:44:18: unsupported call to `std::mem::size_of_val::` +- fixtures/reject/src/lib.rs:44:29: unsupported call to `std::mem::size_of_val::` - fixtures/reject/src/lib.rs:82:10: unsupported call to `::write_fmt` - fixtures/reject/src/lib.rs:139:11: unsupported call to ` as std::iter::Iterator>::cycle` - fixtures/reject/src/lib.rs:193:8: unsupported call to `std::ops::Range::::contains::` - fixtures/reject/src/lib.rs:359:4: unsupported call to `core::slice::::copy_within::>` - fixtures/reject/src/lib.rs:370:4: unsupported call to `core::str::::split::<[char; 2]>` -- fixtures/reject/src/lib.rs:419:2: unsupported call to ` as std::ops::Deref>::deref` -- fixtures/reject/src/lib.rs:433:2: unsupported call to `::add_assign` -- fixtures/reject/src/lib.rs:463:2: unsupported call to ` as std::cmp::PartialEq>::eq` +- fixtures/reject/src/lib.rs:409:2: unsupported call to ` as std::ops::Deref>::deref` +- fixtures/reject/src/lib.rs:423:2: unsupported call to `::add_assign` +- fixtures/reject/src/lib.rs:453:2: unsupported call to ` as std::cmp::PartialEq>::eq`
@@ -197,12 +202,22 @@ The type or module each refused type or path belongs to. +
passing `_`, a closure that owns a value that drops, to `_`, which is not supported yet (5) + +- fixtures/reject/src/lib.rs:45:11: passing `{closure@fixtures/reject/src/lib.rs:44:10: 44:23}`, a closure that owns a value that drops, to `Iterator::map`, which is not supported yet +- fixtures/reject/src/lib.rs:380:13: passing `{closure@fixtures/reject/src/lib.rs:380:28: 380:35}`, a closure that owns a value that drops, to `Entry::or_insert_with`, which is not supported yet +- fixtures/reject/src/lib.rs:381:11: passing `{closure@fixtures/reject/src/lib.rs:381:15: 381:24}`, a closure that owns a value that drops, to `Iterator::any`, which is not supported yet +- fixtures/reject/src/lib.rs:389:15: passing `{closure@fixtures/reject/src/lib.rs:389:48: 389:56}`, a closure that owns a value that drops, to `successors::successors`, which is not supported yet +- fixtures/reject/src/lib.rs:390:13: passing `{closure@fixtures/reject/src/lib.rs:390:32: 390:39}`, a closure that owns a value that drops, to `from_fn::from_fn`, which is not supported yet + +
+
a variant of `_`, which is not supported: either is the same JS string (4) -- fixtures/reject/src/lib.rs:407:2: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string -- fixtures/reject/src/lib.rs:408:30: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string -- fixtures/reject/src/lib.rs:409:27: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string -- fixtures/reject/src/lib.rs:414:2: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string +- fixtures/reject/src/lib.rs:397:2: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string +- fixtures/reject/src/lib.rs:398:30: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string +- fixtures/reject/src/lib.rs:399:27: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string +- fixtures/reject/src/lib.rs:404:2: a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string
@@ -215,15 +230,6 @@ The type or module each refused type or path belongs to. -
dropping a closure that owns a value that drops, which is not supported yet (4) - -- fixtures/reject/src/lib.rs:46:1: dropping a closure that owns a value that drops, which is not supported yet -- fixtures/reject/src/lib.rs:397:18: dropping a closure that owns a value that drops, which is not supported yet -- fixtures/reject/src/lib.rs:399:15: dropping a closure that owns a value that drops, which is not supported yet -- fixtures/reject/src/lib.rs:400:13: dropping a closure that owns a value that drops, which is not supported yet - -
-
unsupported type `_` (4) - fixtures/reject/src/lib.rs:7:14: unsupported type `f64` @@ -236,16 +242,16 @@ The type or module each refused type or path belongs to.
`_` with a mutable reference to a value JS copies, which its mapping would copy (3) - fixtures/reject/src/lib.rs:17:2: `std::iter::IntoIterator::into_iter` with a mutable reference to a value JS copies, which its mapping would copy -- fixtures/reject/src/lib.rs:439:21: `core::slice::::last_mut` with a mutable reference to a value JS copies, which its mapping would copy -- fixtures/reject/src/lib.rs:446:15: `std::collections::hash_map::Entry::<'a, K, V, A>::and_modify` with a mutable reference to a value JS copies, which its mapping would copy +- fixtures/reject/src/lib.rs:429:21: `core::slice::::last_mut` with a mutable reference to a value JS copies, which its mapping would copy +- fixtures/reject/src/lib.rs:436:15: `std::collections::hash_map::Entry::<'a, K, V, A>::and_modify` with a mutable reference to a value JS copies, which its mapping would copy
-
dropping a closure that owns a value that drops, which is not supported yet, in `_` (3) +
dropping `_`, which is not supported yet (3) -- fixtures/reject/src/lib.rs:378:12: dropping a closure that owns a value that drops, which is not supported yet, in `std::option::Option::::map` -- fixtures/reject/src/lib.rs:382:12: dropping a closure that owns a value that drops, which is not supported yet, in `std::result::Result::::map_err` -- fixtures/reject/src/lib.rs:386:12: dropping a closure that owns a value that drops, which is not supported yet, in `std::result::Result::::and_then` +- fixtures/reject/src/lib.rs:45:18: dropping `std::iter::Map, {closure@fixtures/reject/src/lib.rs:44:10: 44:23}>`, which is not supported yet +- fixtures/reject/src/lib.rs:389:79: dropping `std::iter::Take>`, which is not supported yet +- fixtures/reject/src/lib.rs:390:62: dropping `std::iter::Take>`, which is not supported yet
@@ -292,13 +298,6 @@ The type or module each refused type or path belongs to.
-
dropping `_`, which is not supported yet (2) - -- fixtures/reject/src/lib.rs:399:79: dropping `std::iter::Take>`, which is not supported yet -- fixtures/reject/src/lib.rs:400:62: dropping `std::iter::Take>`, which is not supported yet - -
-
overriding `_`, which std's adaptors call in place of `next` or beside it (2) - fixtures/reject/src/lib.rs:152:2: overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it @@ -338,7 +337,7 @@ The type or module each refused type or path belongs to.
`_` is not supported yet (1) -- fixtures/reject/src/lib.rs:418:18: `std::borrow::Cow<'_, [u8]>` is not supported yet +- fixtures/reject/src/lib.rs:408:18: `std::borrow::Cow<'_, [u8]>` is not supported yet
@@ -416,7 +415,7 @@ The type or module each refused type or path belongs to.
a mutable reference into a `Result`'s payload, passed on or kept (1) -- fixtures/reject/src/lib.rs:456:12: a mutable reference into a `Result`'s payload, passed on or kept +- fixtures/reject/src/lib.rs:446:12: a mutable reference into a `Result`'s payload, passed on or kept
@@ -452,7 +451,7 @@ The type or module each refused type or path belongs to.
an iterator that owns `_`, which drops, which is not supported yet (1) -- fixtures/reject/src/lib.rs:401:13: an iterator that owns `Noisy`, which drops, which is not supported yet +- fixtures/reject/src/lib.rs:391:13: an iterator that owns `Noisy`, which drops, which is not supported yet
@@ -470,7 +469,7 @@ The type or module each refused type or path belongs to.
constructing `_` (1) -- fixtures/reject/src/lib.rs:425:53: constructing `std::borrow::Cow<'_, std::sync::Arc>` +- fixtures/reject/src/lib.rs:415:53: constructing `std::borrow::Cow<'_, std::sync::Arc>`
diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index bd5953e39..6a5d50716 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -12,8 +12,9 @@ unsupported type `dyn std::ops::Fn(u8) -> u8` at fixtures/reject/src/lib.rs:11:1 unsupported call to `std::slice::::sort` at fixtures/reject/src/lib.rs:28:4 `Option>` is not supported: its `None` and `Some` would both be `undefined` at fixtures/reject/src/lib.rs:32:2 `Option>` is not supported: its `None` and `Some` would both be `undefined` at fixtures/reject/src/lib.rs:32:42 -unsupported call to `std::mem::size_of_val::` at fixtures/reject/src/lib.rs:44:18 -dropping a closure that owns a value that drops, which is not supported yet at fixtures/reject/src/lib.rs:46:1 +unsupported call to `std::mem::size_of_val::` at fixtures/reject/src/lib.rs:44:29 +passing `{closure@fixtures/reject/src/lib.rs:44:10: 44:23}`, a closure that owns a value that drops, to `Iterator::map`, which is not supported yet at fixtures/reject/src/lib.rs:45:11 +dropping `std::iter::Map, {closure@fixtures/reject/src/lib.rs:44:10: 44:23}>`, which is not supported yet at fixtures/reject/src/lib.rs:45:18 `into_keys` of `std::collections::HashMap`, which drops `Noisy`, which is not supported yet at fixtures/reject/src/lib.rs:51:13 a zero-sized constant of type `{async closure@fixtures/reject/src/lib.rs:59:10: 59:18}` at fixtures/reject/src/lib.rs:60:2 a precision, as in `{:.2}`, is not supported at fixtures/reject/src/lib.rs:67:3 @@ -58,30 +59,28 @@ comparing `std::vec::Vec` as a key, which is not supported yet at fixtures/r `HashMap::contains_key` of `std::collections::HashMap` by `u8`, which is not supported yet at fixtures/reject/src/lib.rs:309:6 unsupported call to `core::slice::::copy_within::>` at fixtures/reject/src/lib.rs:359:4 a coercion to `std::sync::Arc<[u8]>` at fixtures/reject/src/lib.rs:364:2 -a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:407:2 -a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:409:27 -a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:408:30 -a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:414:2 -`std::borrow::Cow<'_, [u8]>` is not supported yet at fixtures/reject/src/lib.rs:418:18 -unsupported call to ` as std::ops::Deref>::deref` at fixtures/reject/src/lib.rs:419:2 -constructing `std::borrow::Cow<'_, std::sync::Arc>` at fixtures/reject/src/lib.rs:425:53 +a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:397:2 +a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:399:27 +a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:398:30 +a variant of `std::borrow::Cow<'_, str>`, which is not supported: either is the same JS string at fixtures/reject/src/lib.rs:404:2 +`std::borrow::Cow<'_, [u8]>` is not supported yet at fixtures/reject/src/lib.rs:408:18 +unsupported call to ` as std::ops::Deref>::deref` at fixtures/reject/src/lib.rs:409:2 +constructing `std::borrow::Cow<'_, std::sync::Arc>` at fixtures/reject/src/lib.rs:415:53 unsupported call to `core::str::::split::<[char; 2]>` at fixtures/reject/src/lib.rs:370:4 -dropping a closure that owns a value that drops, which is not supported yet at fixtures/reject/src/lib.rs:397:18 -dropping a closure that owns a value that drops, which is not supported yet at fixtures/reject/src/lib.rs:399:15 -dropping `std::iter::Take>`, which is not supported yet at fixtures/reject/src/lib.rs:399:79 -dropping a closure that owns a value that drops, which is not supported yet at fixtures/reject/src/lib.rs:400:13 -dropping `std::iter::Take>`, which is not supported yet at fixtures/reject/src/lib.rs:400:62 -an iterator that owns `Noisy`, which drops, which is not supported yet at fixtures/reject/src/lib.rs:401:13 -unsupported call to `::add_assign` at fixtures/reject/src/lib.rs:433:2 -`core::slice::::last_mut` with a mutable reference to a value JS copies, which its mapping would copy at fixtures/reject/src/lib.rs:439:21 -`std::collections::hash_map::Entry::<'a, K, V, A>::and_modify` with a mutable reference to a value JS copies, which its mapping would copy at fixtures/reject/src/lib.rs:446:15 -a mutable reference into a `Result`'s payload, passed on or kept at fixtures/reject/src/lib.rs:456:12 -unsupported call to ` as std::cmp::PartialEq>::eq` at fixtures/reject/src/lib.rs:463:2 +passing `{closure@fixtures/reject/src/lib.rs:380:28: 380:35}`, a closure that owns a value that drops, to `Entry::or_insert_with`, which is not supported yet at fixtures/reject/src/lib.rs:380:13 +passing `{closure@fixtures/reject/src/lib.rs:381:15: 381:24}`, a closure that owns a value that drops, to `Iterator::any`, which is not supported yet at fixtures/reject/src/lib.rs:381:11 +passing `{closure@fixtures/reject/src/lib.rs:389:48: 389:56}`, a closure that owns a value that drops, to `successors::successors`, which is not supported yet at fixtures/reject/src/lib.rs:389:15 +dropping `std::iter::Take>`, which is not supported yet at fixtures/reject/src/lib.rs:389:79 +passing `{closure@fixtures/reject/src/lib.rs:390:32: 390:39}`, a closure that owns a value that drops, to `from_fn::from_fn`, which is not supported yet at fixtures/reject/src/lib.rs:390:13 +dropping `std::iter::Take>`, which is not supported yet at fixtures/reject/src/lib.rs:390:62 +an iterator that owns `Noisy`, which drops, which is not supported yet at fixtures/reject/src/lib.rs:391:13 +unsupported call to `::add_assign` at fixtures/reject/src/lib.rs:423:2 +`core::slice::::last_mut` with a mutable reference to a value JS copies, which its mapping would copy at fixtures/reject/src/lib.rs:429:21 +`std::collections::hash_map::Entry::<'a, K, V, A>::and_modify` with a mutable reference to a value JS copies, which its mapping would copy at fixtures/reject/src/lib.rs:436:15 +a mutable reference into a `Result`'s payload, passed on or kept at fixtures/reject/src/lib.rs:446:12 +unsupported call to ` as std::cmp::PartialEq>::eq` at fixtures/reject/src/lib.rs:453:2 overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:152:2 an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:172:1 overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:206:2 an iterator of the crate implementing `FusedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:226:1 `Hasher::write`, which is not supported yet at fixtures/reject/src/lib.rs:317:9 -dropping a closure that owns a value that drops, which is not supported yet, in `std::option::Option::::map` at fixtures/reject/src/lib.rs:378:12 -dropping a closure that owns a value that drops, which is not supported yet, in `std::result::Result::::map_err` at fixtures/reject/src/lib.rs:382:12 -dropping a closure that owns a value that drops, which is not supported yet, in `std::result::Result::::and_then` at fixtures/reject/src/lib.rs:386:12 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index c8809f478..68cdbd1e6 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -38,11 +38,11 @@ impl Drop for Noisy { fn drop(&mut self) {} } -/// Dropping the closure would drop what it captured, which lives in an arrow function. -pub fn owning_closure() -> u8 { +/// A closure that owns a value that drops, which `map` would hold but never drop. +pub fn owning_closure(v: &[u8]) -> usize { let n = Noisy(1); - let f = move || std::mem::size_of_val(&n) as u8; - f() + let f = move |x: &u8| *x + std::mem::size_of_val(&n) as u8; + v.iter().map(f).count() } /// std's `fold` of `into_keys` drops each value after its key, where `next` drops it first. @@ -370,36 +370,26 @@ pub fn split_chars(s: &str) -> usize { s.split(['a', 'b']).count() } -/// `Option::map` drops its closure on `None`, as `Result::map_err` does on `Ok` and -/// `Result::and_then` on `Err`, and with it what it captured, which lives in an arrow -/// function. -pub fn discarded_closure(a: Option, b: Result) -> u8 { - let (m, n, o) = (Noisy(1), Noisy(2), Noisy(3)); - let a = a.map(move |x| { - drop(m); - x - }); - let c = b.map_err(move |e| { - drop(n); - e - }); - let d = b.and_then(move |v| { - drop(o); - Ok(v) - }); - a.unwrap_or(0) + c.unwrap_or(0) + d.unwrap_or(0) +/// Closures that own a value that drops, which `any` and `or_insert_with` would call but +/// never drop. +pub fn called_closure(v: &[u8], m: &mut std::collections::HashMap) -> bool { + fn id(n: &Noisy) -> u8 { + n.0 + } + let (a, b) = (Noisy(1), Noisy(2)); + m.entry(1).or_insert_with(move || b); + v.iter().any(move |&x| x == id(&a)) } /// Each drops its closure, and the `Noisy` it owns, when it is done with it, as /// `successors` does the item it holds next. -pub fn owning_lazy(cond: bool) -> usize { - let (a, b, c) = (Noisy(1), Noisy(2), Noisy(3)); - let then = cond.then(move || a).is_some() as usize; +pub fn owning_lazy() -> usize { + let (b, c) = (Noisy(2), Noisy(3)); // Through a reference, each owns its `Noisy` whole rather than a copy of its field. let walked = std::iter::successors(Some(0u8), move |_| Some((&b).0)).take(2).count(); let made = std::iter::from_fn(move || Some((&c).0)).take(2).count(); let held = std::iter::successors(Some(Noisy(4)), |_| None).count(); - then + walked + made + held + walked + made + held } /// A `Cow` is a JS string, which keeps no variant to match on or ask about. diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index 93e9c18fa..5765d60e8 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -48,7 +48,7 @@ roots = [ "cow_bytes", "cow_made", "split_chars", - "discarded_closure", + "called_closure", "owning_lazy", "duration_assign", "last_bumped", diff --git a/ts/fixtures/task/expected/core/option.ts b/ts/fixtures/task/expected/core/option.ts index ed016ac19..8ab57220e 100644 --- a/ts/fixtures/task/expected/core/option.ts +++ b/ts/fixtures/task/expected/core/option.ts @@ -35,6 +35,42 @@ export function unwrapOr$Option_u64(self: rt.u64 | undefined, default_: rt.u64): return self; } +export function map$Option_u32_u32_blasted_closure3(self: rt.u32 | undefined, f: (a0: rt.u32) => rt.u32): rt.u32 | undefined { + let _0!: rt.u32 | undefined; + let _8: boolean = false; + _8 = true; + if (self === undefined) { + _0 = undefined; + } else { + const x: rt.u32 = self; + _8 = false; + _0 = f(x); + } + if (_8) { + rt.drop(f); + return _0; + } + return _0; +} + +export function map$Option_u32_u32_discarded_closure16(self: rt.u32 | undefined, f: (a0: rt.u32) => rt.u32): rt.u32 | undefined { + let _0!: rt.u32 | undefined; + let _8: boolean = false; + _8 = true; + if (self === undefined) { + _0 = undefined; + } else { + const x: rt.u32 = self; + _8 = false; + _0 = rt.callOnce(f, x); + } + if (_8) { + rt.drop(f); + return _0; + } + return _0; +} + export function mapOr$Option_Guard_u32_drained_closure4(self: Guard.Guard | undefined, default_: rt.u32, f: (a0: Guard.Guard) => rt.u32): rt.u32 { if (self === undefined) { return default_; @@ -87,6 +123,24 @@ export function mapOr$Option_u32_Guard_options_closure2(self: rt.u32 | undefined } } +export function mapOr$Option_u32_u32_discarded_closure12(self: rt.u32 | undefined, default_: rt.u32, f: (a0: rt.u32) => rt.u32): rt.u32 { + let _0!: rt.u32; + let _8: boolean = false; + _8 = true; + if (self === undefined) { + _0 = default_; + } else { + const t: rt.u32 = self; + _8 = false; + _0 = f(t); + } + if (_8) { + rt.drop(f); + return _0; + } + return _0; +} + export function okOr$Option_Guard_Guard(self: Guard.Guard | undefined, err: Guard.Guard): rt.Result { let _0!: rt.Result; let _6: boolean = false; diff --git a/ts/fixtures/task/expected/core/result.ts b/ts/fixtures/task/expected/core/result.ts index 7c3d3e2ea..9017338ae 100644 --- a/ts/fixtures/task/expected/core/result.ts +++ b/ts/fixtures/task/expected/core/result.ts @@ -33,6 +33,24 @@ export function err$Result_Guard_Guard(self: rt.Result return _0; } +export function map$Result_u32_u32_u32_discarded_closure4(self: rt.Result, op: (a0: rt.u32) => rt.u32): rt.Result { + let _0!: rt.Result; + let _9: boolean = false; + _9 = true; + if (self.ok) { + const t: rt.u32 = self.value; + _9 = false; + _0 = rt.Ok(op(t)); + } else { + _0 = self; + } + if (_9) { + rt.drop(op); + return _0; + } + return _0; +} + export function mapOrElse$Result_Guard_Guard_u32_options_closure9_options_closure10(self: rt.Result, default_: (a0: Guard.Guard) => rt.u32, f: (a0: Guard.Guard) => rt.u32): rt.u32 { if (self.ok) { return f(self.value); @@ -40,6 +58,39 @@ export function mapOrElse$Result_Guard_Guard_u32_options_closure9_options_closur return default_(self.error); } +export function mapErr$Result_u32_u32_u32_discarded_closure6(self: rt.Result, op: (a0: rt.u32) => rt.u32): rt.Result { + let _0!: rt.Result; + let _9: boolean = false; + _9 = true; + if (self.ok) { + _0 = self; + } else { + const e: rt.u32 = self.error; + _9 = false; + _0 = rt.Err(op(e)); + } + if (_9) { + rt.drop(op); + return _0; + } + return _0; +} + +export function inspectErr$Result_u32_u32_discarded_closure10(self: rt.Result, f: (a0: rt.u32) => void): rt.Result { + let _8: boolean = false; + _8 = true; + if (!self.ok) { + _8 = false; + f(self.error); + } + const _0: rt.Result = self; + if (_8) { + rt.drop(f); + return _0; + } + return _0; +} + export function inspectErr$Result_u8_Key_tripped_closure6(self: rt.Result, f: (a0: Key.Key) => void): rt.Result { let _9!: boolean; let unwind: number = 0; @@ -70,6 +121,24 @@ export function andThen$Result_Guard_Guard_Guard_options_closure8(self: rt.Resul return self; } +export function andThen$Result_u32_u32_u32_discarded_closure8(self: rt.Result, op: (a0: rt.u32) => rt.Result): rt.Result { + let _0!: rt.Result; + let _8: boolean = false; + _8 = true; + if (self.ok) { + const t: rt.u32 = self.value; + _8 = false; + _0 = op(t); + } else { + _0 = self; + } + if (_8) { + rt.drop(op); + return _0; + } + return _0; +} + export function unwrapOr$Result_Guard_Guard(self: rt.Result, default_: Guard.Guard): Guard.Guard { let _0!: Guard.Guard; let unwind: number = 0; diff --git a/ts/fixtures/task/expected/drops.ts b/ts/fixtures/task/expected/drops.ts index 4bded1017..3f3c53038 100644 --- a/ts/fixtures/task/expected/drops.ts +++ b/ts/fixtures/task/expected/drops.ts @@ -4,6 +4,7 @@ import * as rt from "./rt.ts"; import { drop$Arc_Wrap_Pair_Held, drop$Arc_str_Arc_str, drop$Bomb_Guard, drop$Guard_Entries_Bomb_Guard_Held, drop$Held, drop$Mutex_Held, drop$Option_Box_Held, drop$Option_Guard_Guard, drop$Option_Held, drop$Pair_Held, drop$Result_Guard_Guard, drop$Result_u8_Key, drop$Wrap_Arc_Held, drop$Wrap_Guard, drop$Wrap_Held, drop$Wrap_Pair_Held, drop$Wrap_Vec_Wrap_Held } from "./lib.ts"; import { isSomeAnd$Option_Guard_options_closure7, unwrapOr$Option_Guard, unwrapOr$Option_u64, mapOr$Option_u32_Guard_options_closure2, okOr$Option_Guard_Guard, filter$Option_Guard_options_closure4, orElse$Option_Guard_options_closure6, zip$Option_Guard_Guard, clone$Clone_Option_Arc_Guard } from "./core/option.ts"; import { ok$Result_Guard_Guard, err$Result_Guard_Guard, mapOrElse$Result_Guard_Guard_u32_options_closure9_options_closure10, inspectErr$Result_u8_Key_tripped_closure6, andThen$Result_Guard_Guard_Guard_options_closure8, unwrapOr$Result_Guard_Guard } from "./core/result.ts"; +import { scoped, discarded, blasted } from "./drops/closures.ts"; import { looped, collected, drained, extended, bombed, spent } from "./drops/iters.ts"; import * as Bomb from "./drops.Bomb.ts"; import * as Entries from "./drops.Entries.ts"; @@ -21,7 +22,7 @@ import * as Log from "./log.Log.ts"; import * as Guard from "./model.Guard.ts"; /** - * Run scenario `arg % 19`, panicking at the point `arg / 19 % 8` names, if any: what + * Run scenario `arg % 22`, panicking at the point `arg / 22 % 8` names, if any: what * every value std holds drops, where Rust drops it, and in Rust's order. */ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { @@ -84,7 +85,7 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { _380 = false; _379 = false; _378 = false; - const at: rt.u8 = rt.u8Rem(rt.u8Div(arg, 19), 8); + const at: rt.u8 = rt.u8Rem(rt.u8Div(arg, 22), 8); const guard: (a0: rt.u32) => Guard.Guard = (n: rt.u32): Guard.Guard => { return Guard.new(rt.u32Add(id, n), Log.clone$Clone(log)); }; @@ -94,8 +95,14 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { rt.panicFmt(["d", args18[0], " at ", args18[1]]); } }; - const _13: rt.u8 = rt.u8Rem(arg, 19); - if (_13 === 13) { + const _13: rt.u8 = rt.u8Rem(arg, 22); + if (_13 === 19) { + scoped(log, id, at); + } else if (_13 === 20) { + discarded(log, id, at); + } else if (_13 === 21) { + blasted(log, id, at); + } else if (_13 === 13) { looped(log, id, at); } else if (_13 === 14) { collected(log, id, at); @@ -127,23 +134,23 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { grown(log, id, at, false); } else if (_13 === 0) { v = []; - unwind = 216; + unwind = 219; const _19 = { tag: "One", _0: guard(1) } as Held.Held; v.push(_19); v.push({ tag: "Empty" } as Held.Held); _29 = guard(2); - unwind = 215; + unwind = 218; const _32: Guard.Guard = guard(3); - unwind = 216; + unwind = 219; v.push({ tag: "Two", _0: _29, _1: _32 } as Held.Held); - unwind = 214; + unwind = 217; held_ = v; check(1); arc = new rt.Arc({ tag: "One", _0: guard(4) } as Held.Held); - unwind = 235; + unwind = 238; _386 = true; shared = rt.arcClone(arc); - unwind = 213; + unwind = 216; _386 = false; rt.arcDrop(arc, drop$Held); const part4 = shared.strong; @@ -151,7 +158,7 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { Log.note(log, rt.fmtFormat(["arc ", args2[0]])); const part5 = shared.strong; cloned = rt.arcClone(shared); - unwind = 233; + unwind = 236; _385 = true; const args4: rt.Fmt[] = [(f: rt.Formatter) => rt.usizeDisplay(part5, f)]; Log.note(log, rt.fmtFormat(["counted ", args4[0]])); @@ -159,22 +166,22 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { rt.arcDrop(cloned, drop$Held); check(2); v2 = []; - unwind = 212; + unwind = 215; const _85 = { tag: "One", _0: guard(5) } as Held.Held; v2.push(_85); - unwind = 233; + unwind = 236; mutex = new rt.Mutex(v2); - unwind = 231; + unwind = 234; _384 = true; _91 = guard(6); - unwind = 211; + unwind = 214; const _94: Guard.Guard = guard(7); - unwind = 231; + unwind = 234; boxed = { tag: "Two", _0: _91, _1: _94 } as Held.Held; - unwind = 229; + unwind = 232; _383 = true; const _98: Held.Held = { tag: "One", _0: guard(8) }; - unwind = 210; + unwind = 213; maybe = _98; check(3); const _112: rt.usize = held_.length; @@ -187,9 +194,9 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { _384 = false; rt.dropAll(mutex.value, drop$Held); check(5); - unwind = 229; + unwind = 232; drop$Option_Box_Held(maybe); - unwind = 235; + unwind = 238; _383 = false; _384 = false; _385 = false; @@ -200,57 +207,57 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { } else if (_13 === 1) { const _133: rt.u32 = rt.u32Add(id, 10); _135 = Log.clone$Clone(log); - unwind = 209; + unwind = 212; const _137: Guard.Guard = guard(11); - unwind = 208; + unwind = 211; wrapped = new Wrap.Wrap({ id: _133, log: _135, inner: { tag: "One", _0: _137 } as Held.Held }); const _141: rt.u32 = rt.u32Add(id, 20); _143 = Log.clone$Clone(log); - unwind = 207; + unwind = 210; const _144: Guard.Guard = guard(21); - unwind = 206; + unwind = 209; guarded = new Wrap.Wrap({ id: _141, log: _143, inner: _144 }); check(1); _152 = guard(31); - unwind = 205; + unwind = 208; const _155: Guard.Guard = guard(32); - unwind = 204; + unwind = 207; _151 = { tag: "Two", _0: _152, _1: _155 } as Held.Held; const _158: Guard.Guard = guard(33); - unwind = 227; + unwind = 230; _382 = true; pair = new Pair.Pair({ first: _151, second: _158 }); v3 = []; - unwind = 203; + unwind = 206; _166 = guard(41); - unwind = 202; + unwind = 205; const _169: Guard.Guard = guard(42); - unwind = 203; + unwind = 206; v3.push(new Pair.Pair({ first: _166, second: _169 })); _175 = guard(43); - unwind = 201; + unwind = 204; const _178: Guard.Guard = guard(44); - unwind = 203; + unwind = 206; v3.push(new Pair.Pair({ first: _175, second: _178 })); - unwind = 199; + unwind = 202; pairs = v3; _borrowed = new Named.Named({ tag: "Borrowed", _0: "text" } as Text.Text); const _186: Guard.Guard = guard(45); - unwind = 198; + unwind = 201; _owned = new Named.Named({ tag: "Owned", _0: _186 } as Text.Text); check(2); const _193: rt.u32 = rt.u32Add(id, 50); _195 = Log.clone$Clone(log); - unwind = 197; + unwind = 200; v4 = []; - unwind = 196; + unwind = 199; const _201: rt.u32 = rt.u32Add(id, 60); _203 = Log.clone$Clone(log); - unwind = 195; + unwind = 198; const _205: Guard.Guard = guard(61); - unwind = 196; + unwind = 199; v4.push(new Wrap.Wrap({ id: _201, log: _203, inner: { tag: "One", _0: _205 } as Held.Held })); - unwind = 194; + unwind = 197; nested = new Wrap.Wrap({ id: _193, log: _195, inner: v4 }); check(3); _382 = false; @@ -261,15 +268,15 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { const _228: rt.u32 = rt.u32Add(rt.u32Add(Guard.id(pairs[rt.idx(pairs, 1)].first), Guard.id(pairs[rt.idx(pairs, 0)].second)), nested.inner.length as rt.u32); const args8: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(_222, f), (f: rt.Formatter) => rt.u32Display(_225, f), (f: rt.Formatter) => rt.u32Display(_228, f)]; Log.note(log, rt.fmtFormat(["wraps ", args8[0], " ", args8[1], " ", args8[2]])); - unwind = 198; + unwind = 201; drop$Wrap_Vec_Wrap_Held(nested); - unwind = 199; + unwind = 202; rt.drop(_owned); - unwind = 200; + unwind = 203; rt.drop(_borrowed); - unwind = 227; + unwind = 230; rt.dropAll(pairs); - unwind = 208; + unwind = 211; _382 = false; drop$Wrap_Guard(guarded); unwind = 0; @@ -277,22 +284,22 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { } else if (_13 === 2) { const _251: rt.u32 = rt.u32Add(id, 70); _253 = Log.clone$Clone(log); - unwind = 193; + unwind = 196; const _256: Guard.Guard = guard(71); - unwind = 192; + unwind = 195; _255 = { tag: "One", _0: _256 } as Held.Held; const _259: Guard.Guard = guard(72); unwind = 0; shared2 = new rt.Arc(new Wrap.Wrap({ id: _251, log: _253, inner: new Pair.Pair({ first: _255, second: _259 }) })); - unwind = 225; + unwind = 228; _381 = true; v5 = []; - unwind = 191; + unwind = 194; const _266: rt.Arc>> = rt.arcClone(shared2); v5.push(_266); const _270: rt.Arc>> = rt.arcClone(shared2); v5.push(_270); - unwind = 190; + unwind = 193; clones = v5; check(1); _381 = false; @@ -304,36 +311,36 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { const part3 = !bail(log, id, at).ok; const args12: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(part3, f)]; Log.note(log, rt.fmtFormat(["bailed ", args12[0]])); - unwind = 225; + unwind = 228; rt.dropAll(clones, drop$Arc_Wrap_Pair_Held); _381 = false; } else { _308 = guard(81); - unwind = 189; + unwind = 192; const _311: Guard.Guard = guard(82); unwind = 0; arc2 = new rt.Arc(new rt.Mutex({ tag: "Two", _0: _308, _1: _311 } as Held.Held)); - unwind = 223; + unwind = 226; _380 = true; other = rt.arcClone(arc2); - unwind = 188; + unwind = 191; check(1); _380 = false; rt.arcDrop(arc2, drop$Mutex_Held); check(2); const _327: rt.Mutex = other.value; - unwind = 187; + unwind = 190; const held2: boolean = Held.isSome(_327.value); - unwind = 188; + unwind = 191; const args14: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtBool(held2, f)]; Log.note(log, rt.fmtFormat(["locked ", args14[0]])); lent = guard(83); - unwind = 186; + unwind = 189; borrowed = new rt.Arc(lent); - unwind = 221; + unwind = 224; _379 = true; again = rt.arcClone(borrowed); - unwind = 219; + unwind = 222; _378 = true; _379 = false; rt.arcDrop(borrowed); @@ -343,11 +350,11 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { const part = Guard.id(lent); const args16: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part, f)]; Log.note(log, rt.fmtFormat(["lent ", args16[0]])); - unwind = 188; + unwind = 191; _378 = false; _379 = false; rt.drop(lent); - unwind = 223; + unwind = 226; rt.arcDrop(other, drop$Mutex_Held); _380 = false; } @@ -377,136 +384,136 @@ export function run(log: Log.Log, id: rt.u32, arg: rt.u8): void { if (unwind === 0) { throw e; } - if (unwind === 186) { + if (unwind === 189) { break b4; } - if (unwind === 187) { + if (unwind === 190) { break b3; } - if (unwind === 188) { + if (unwind === 191) { break b3; } - if (unwind === 189) { + if (unwind === 192) { rt.drop(_308); break b1; } - if (unwind === 190) { + if (unwind === 193) { rt.dropAll(clones, drop$Arc_Wrap_Pair_Held); break b5; } - if (unwind === 191) { + if (unwind === 194) { rt.dropAll(v5, drop$Arc_Wrap_Pair_Held); break b5; } - if (unwind === 192) { + if (unwind === 195) { drop$Held(_255); break b6; } - if (unwind === 193) { + if (unwind === 196) { break b6; } - if (unwind === 194) { + if (unwind === 197) { drop$Wrap_Vec_Wrap_Held(nested); break b12; } - if (unwind === 195) { + if (unwind === 198) { rt.drop(_203); break b14; } - if (unwind === 196) { + if (unwind === 199) { break b14; } - if (unwind === 197) { + if (unwind === 200) { break b13; } - if (unwind === 198) { + if (unwind === 201) { break b12; } - if (unwind === 199) { + if (unwind === 202) { break b11; } - if (unwind === 200) { + if (unwind === 203) { break b10; } - if (unwind === 201) { + if (unwind === 204) { rt.drop(_175); break b15; } - if (unwind === 202) { + if (unwind === 205) { rt.drop(_166); break b15; } - if (unwind === 203) { + if (unwind === 206) { break b15; } - if (unwind === 204) { + if (unwind === 207) { drop$Held(_151); break b8; } - if (unwind === 205) { + if (unwind === 208) { rt.drop(_152); break b8; } - if (unwind === 206) { + if (unwind === 209) { break b8; } - if (unwind === 207) { + if (unwind === 210) { rt.drop(_143); break b7; } - if (unwind === 208) { + if (unwind === 211) { break b7; } - if (unwind === 209) { + if (unwind === 212) { rt.drop(_135); break b1; } - if (unwind === 210) { + if (unwind === 213) { drop$Option_Box_Held(maybe); break b21; } - if (unwind === 211) { + if (unwind === 214) { rt.drop(_91); break b20; } - if (unwind === 212) { + if (unwind === 215) { rt.dropAll(v2, drop$Held); break b19; } - if (unwind === 213) { + if (unwind === 216) { break b18; } - if (unwind === 214) { + if (unwind === 217) { break b16; } - if (unwind === 215) { + if (unwind === 218) { rt.drop(_29); break b22; } - if (unwind === 216) { + if (unwind === 219) { break b22; } - if (unwind === 219) { + if (unwind === 222) { if (_378) { rt.arcDrop(again); } - } else if (unwind !== 221) { - if (unwind === 223) { + } else if (unwind !== 224) { + if (unwind === 226) { break b2; } - if (unwind === 225) { + if (unwind === 228) { break b5; } - if (unwind === 227) { + if (unwind === 230) { break b9; } - if (unwind === 229) { + if (unwind === 232) { break b21; } - if (unwind === 231) { + if (unwind === 234) { break b20; } - if (unwind === 233) { + if (unwind === 236) { break b19; } break b17; diff --git a/ts/fixtures/task/expected/drops/closures.Holder.ts b/ts/fixtures/task/expected/drops/closures.Holder.ts new file mode 100644 index 000000000..5853f49dd --- /dev/null +++ b/ts/fixtures/task/expected/drops/closures.Holder.ts @@ -0,0 +1,31 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "../rt.ts"; +import type * as Guard from "../model.Guard.ts"; + +/** Hold `f`, which drops with it. */ +export class Holder { + f: F; + guard: Guard.Guard; + + constructor(fields: { f: F; guard: Guard.Guard }) { + this.f = fields.f; + this.guard = fields.guard; + } + + [Symbol.dispose](): void { + let dropped: number = 0; + try { + rt.drop(this.f); + dropped = 1; + rt.drop(this.guard); + } catch (e) { + if (dropped < 1) { + rt.drop(this.guard); + } + throw e; + } + } +} + +//# sourceMappingURL=closures.Holder.ts.map diff --git a/ts/fixtures/task/expected/drops/closures.ts b/ts/fixtures/task/expected/drops/closures.ts new file mode 100644 index 000000000..ece40ccd6 --- /dev/null +++ b/ts/fixtures/task/expected/drops/closures.ts @@ -0,0 +1,1459 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "../rt.ts"; +import { map$Option_u32_u32_blasted_closure3, map$Option_u32_u32_discarded_closure16, mapOr$Option_u32_u32_discarded_closure12 } from "../core/option.ts"; +import { map$Result_u32_u32_u32_discarded_closure4, mapErr$Result_u32_u32_u32_discarded_closure6, inspectErr$Result_u32_u32_discarded_closure10, andThen$Result_u32_u32_u32_discarded_closure8 } from "../core/result.ts"; +import * as Holder from "../drops/closures.Holder.ts"; +import * as Bomb from "../drops.Bomb.ts"; +import * as Log from "../log.Log.ts"; +import * as Guard from "../model.Guard.ts"; + +/** Call `f` once, which consumes it. */ +export function once$u32_blasted_closure4(f: () => rt.u32): rt.u32 { + return f(); +} + +/** Call `f` once, which consumes it. */ +export function once$u32_blasted_closure5(f: () => rt.u32): rt.u32 { + return rt.callOnce(f); +} + +/** Call `f` once, which consumes it. */ +export function once$u32_scoped_closure4(f: () => rt.u32): rt.u32 { + return f(); +} + +/** Call `f` once, which consumes it. */ +export function once$u32_scoped_closure5(f: () => rt.u32): rt.u32 { + return f(); +} + +/** Call `f` once, which consumes it. */ +export function once$u32_scoped_closure6(f: () => rt.u32): rt.u32 { + return rt.callOnce(f); +} + +/** Call `f` once, which consumes it. */ +export function once$u32_scoped_closure9(f: () => rt.u32): rt.u32 { + return rt.callOnce(f); +} + +/** Call `f` with each of `n` and `n + 1`, which borrows it, and drop it. */ +export function twice$scoped_closure11(n: rt.u32, f: (a0: rt.u32) => rt.u32): rt.u32 { + let unwind: number = 0; + try { + unwind = 7; + const _11: rt.u32 = rt.u32Add(f(n), f(rt.u32Add(n, 1))); + unwind = 0; + rt.drop(f); + return _11; + } catch (e) { + if (unwind === 0) { + throw e; + } + rt.drop(f); + throw e; + } +} + +/** + * The id of `bomb`, which a closure that calls it captures whole, where one that reads + * its field captures only that. + */ +export function bombId(bomb: Bomb.Bomb): rt.u32 { + return bomb.id; +} + +/** + * What closures that own guards drop, panicking at point `at`, if any, some inside a + * closure's body. + */ +export function scoped(log: Log.Log, id: rt.u32, at: rt.u8): void { + let a!: Guard.Guard; + let b!: Guard.Guard; + let _12!: Guard.Guard; + let unused!: () => rt.u32; + let read!: (a0: rt.u32) => rt.u32; + let e!: Guard.Guard; + let f!: Guard.Guard; + let holder!: Holder.Holder<(a0: rt.u32) => rt.u32>; + let _110!: (a0: rt.u32) => rt.u32; + let outer!: () => rt.u32; + let push!: (a0: Guard.Guard) => rt.usize; + let _179!: boolean; + let _180!: boolean; + let _181!: boolean; + let _182!: boolean; + let _183!: boolean; + let _184!: boolean; + let unwind: number = 0; + try { + _184 = false; + _183 = false; + _182 = false; + _181 = false; + _180 = false; + _179 = false; + const guard: (a0: rt.u32) => Guard.Guard = (n: rt.u32): Guard.Guard => { + return Guard.new(rt.u32Add(id, n), Log.clone$Clone(log)); + }; + const check: (a0: rt.u8) => void = (point: rt.u8): void => { + if (point === at) { + const args16: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(id, f), (f: rt.Formatter) => rt.u8Display(point, f)]; + rt.panicFmt(["d", args16[0], " at ", args16[1]]); + } + }; + _12 = guard(1); + unwind = 74; + const _15: Guard.Guard = guard(2); + unwind = 83; + _184 = true; + a = _12; + _183 = true; + b = _15; + _184 = false; + _183 = false; + _182 = true; + const captured = a; + const captured2 = b; + unused = rt.closure((): rt.u32 => { + return rt.u32Add(Guard.id(captured), Guard.id(captured2)); + }, (): void => { + let dropped: number = 0; + try { + rt.drop(captured); + dropped = 1; + rt.drop(captured2); + } catch (e) { + if (dropped < 1) { + rt.drop(captured2); + } + throw e; + } + }); + const c: Guard.Guard = guard(3); + unwind = 71; + const captured3 = c; + read = rt.closure((n2: rt.u32): rt.u32 => { + return rt.u32Add(n2, Guard.id(captured3)); + }, (): void => { + rt.drop(captured3); + }); + const part = rt.u32Add(read(1), read(2)); + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part, f)]; + Log.note(log, rt.fmtFormat(["read ", args2[0]])); + check(1); + _182 = false; + rt.drop(unused); + const captured4 = guard(4); + const part2 = once$u32_scoped_closure4(rt.closure((): rt.u32 => { + let _172!: boolean; + let unwind2: number = 0; + try { + unwind2 = 11; + _172 = false; + _172 = true; + const n3: rt.u32 = Guard.id(captured4); + _172 = false; + rt.drop(captured4); + const args18: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(n3, f)]; + Log.note(log, rt.fmtFormat(["moved ", args18[0]])); + return n3; + } catch (e3) { + if (unwind2 === 0) { + throw e3; + } + if (_172) { + rt.drop(captured4); + } + throw e3; + } + }, (): void => { + rt.drop(captured4); + })); + const args4: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part2, f)]; + Log.note(log, rt.fmtFormat(["moved ", args4[0]])); + e = guard(5); + const captured5 = check; + const captured6 = e; + const part3 = once$u32_scoped_closure5(rt.closure((): rt.u32 => { + let unwind3: number = 0; + try { + unwind3 = 5; + captured5(2); + const _04: rt.u32 = Guard.id(captured6); + unwind3 = 0; + rt.drop(captured6); + return _04; + } catch (e4) { + if (unwind3 === 0) { + throw e4; + } + rt.drop(captured6); + throw e4; + } + }, (): void => { + rt.drop(captured6); + })); + const args6: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part3, f)]; + Log.note(log, rt.fmtFormat(["kept ", args6[0]])); + f = guard(6); + const captured7 = check; + const captured8 = f; + const part4 = once$u32_scoped_closure6(rt.closure((): rt.u32 => { + captured7(3); + return Guard.id(captured8); + }, (): void => { + rt.drop(captured8); + })); + const args8: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part4, f)]; + Log.note(log, rt.fmtFormat(["by ref ", args8[0]])); + const captured9 = guard(7); + const part5 = twice$scoped_closure11(1, rt.closure((n4: rt.u32): rt.u32 => { + return rt.u32Add(n4, Guard.id(captured9)); + }, (): void => { + rt.drop(captured9); + })); + const args10: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part5, f)]; + Log.note(log, rt.fmtFormat(["twice ", args10[0]])); + const h: Guard.Guard = guard(8); + unwind = 70; + const captured10 = h; + _110 = rt.closure((n5: rt.u32): rt.u32 => { + return rt.u32Add(n5, Guard.id(captured10)); + }, (): void => { + rt.drop(captured10); + }); + const _111: Guard.Guard = guard(9); + unwind = 81; + _181 = true; + holder = new Holder.Holder({ f: _110, guard: _111 }); + const part6 = rt.u32Add(holder.f(1), Guard.id(holder.guard)); + const args12: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part6, f)]; + Log.note(log, rt.fmtFormat(["holder ", args12[0]])); + const i: Guard.Guard = guard(10); + unwind = 79; + const captured11 = i; + _180 = true; + const captured12 = rt.closure((n6: rt.u32): rt.u32 => { + return rt.u32Add(n6, Guard.id(captured11)); + }, (): void => { + rt.drop(captured11); + }); + outer = rt.closure((): rt.u32 => { + return captured12(1); + }, (): void => { + rt.drop(captured12); + }); + check(4); + _180 = false; + const part7 = once$u32_scoped_closure9(outer); + const args14: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part7, f)]; + Log.note(log, rt.fmtFormat(["outer ", args14[0]])); + _181 = false; + rt.drop(holder); + const held: Guard.Guard[] = []; + unwind = 77; + _179 = true; + const captured13 = held; + push = rt.closure((g2: Guard.Guard): rt.usize => { + captured13.push(g2); + return captured13.length; + }, (): void => { + rt.dropAll(captured13); + }); + const part8 = guard(11); + push(part8); + const part9 = guard(12); + push(part9); + check(5); + _179 = false; + rt.drop(push); + unwind = 83; + _179 = false; + _180 = false; + _181 = false; + rt.drop(read); + _182 = false; + _183 = false; + _184 = false; + } catch (e2) { + b0: { + b1: { + b2: { + b3: { + if (unwind === 0) { + throw e2; + } + if (unwind === 70) { + rt.drop(_110); + break b2; + } + if (unwind === 71) { + break b2; + } + if (unwind === 74) { + rt.drop(_12); + break b0; + } + if (unwind === 77) { + if (_179) { + rt.drop(push); + } + } else if (unwind !== 79) { + if (unwind === 81) { + break b3; + } + break b1; + } + if (_180) { + rt.drop(outer); + } + } + if (_181) { + rt.drop(holder); + } + } + rt.drop(read); + } + if (_182) { + rt.drop(unused); + } + if (_183) { + rt.drop(b); + } + if (_184) { + rt.drop(a); + } + } + throw e2; + } +} + +/** + * What std does with a closure that owns a guard: drops it where it is not called, and + * leaves it to the call where it is. + */ +export function discarded(log: Log.Log, id: rt.u32, at: rt.u8): void { + let some!: rt.u32 | undefined; + let none!: rt.u32 | undefined; + let a!: Guard.Guard; + let b!: Guard.Guard; + let _16!: Guard.Guard; + let c!: Guard.Guard; + let d!: Guard.Guard; + let e!: Guard.Guard; + let f!: Guard.Guard; + let _47!: Guard.Guard; + let _50!: Guard.Guard; + let _53!: Guard.Guard; + let g!: Guard.Guard; + let h!: Guard.Guard; + let _84!: Guard.Guard; + let i!: Guard.Guard; + let j!: Guard.Guard; + let _98!: Guard.Guard; + let k!: Guard.Guard; + let l!: Guard.Guard; + let _124!: Guard.Guard; + let m!: Guard.Guard; + let n!: Guard.Guard; + let _150!: Guard.Guard; + let o!: Guard.Guard; + let p!: Guard.Guard; + let _177!: Guard.Guard; + let other!: (a0: rt.u32) => rt.u32; + let r!: Guard.Guard; + let s!: Guard.Guard; + let _205!: Guard.Guard; + let skipped!: () => rt.u32; + let _262!: boolean; + let _263!: boolean; + let _264!: boolean; + let _265!: boolean; + let _266!: boolean; + let _267!: boolean; + let _268!: boolean; + let _269!: boolean; + let _270!: boolean; + let _271!: boolean; + let _272!: boolean; + let _273!: boolean; + let _274!: boolean; + let _275!: boolean; + let _276!: boolean; + let _277!: boolean; + let _278!: boolean; + let _279!: boolean; + let _280!: boolean; + let _281!: boolean; + let unwind: number = 0; + try { + _281 = false; + _280 = false; + _279 = false; + _278 = false; + _277 = false; + _276 = false; + _275 = false; + _274 = false; + _273 = false; + _272 = false; + _271 = false; + _270 = false; + _269 = false; + _268 = false; + _267 = false; + _266 = false; + _265 = false; + _264 = false; + _263 = false; + _262 = false; + const guard: (a0: rt.u32) => Guard.Guard = (n2: rt.u32): Guard.Guard => { + return Guard.new(rt.u32Add(id, n2), Log.clone$Clone(log)); + }; + const check: (a0: rt.u8) => void = (point: rt.u8): void => { + if (point === at) { + const args18: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(id, f), (f: rt.Formatter) => rt.u8Display(point, f)]; + rt.panicFmt(["d", args18[0], " at ", args18[1]]); + } + }; + if (!(at < 8)) { + some = undefined; + } else { + some = id; + } + if (!(at > 8)) { + none = undefined; + } else { + none = id; + } + _16 = guard(1); + unwind = 114; + const _19: Guard.Guard = guard(2); + unwind = 142; + _281 = true; + a = _16; + _280 = true; + b = _19; + const _23: rt.u32 | undefined = some; + _281 = false; + const captured = check; + const captured2 = a; + const mapped: rt.u32 | undefined = map$Option_u32_u32_blasted_closure3(_23, rt.closure((n3: rt.u32): rt.u32 => { + let unwind2: number = 0; + try { + unwind2 = 6; + captured(1); + const _8: rt.u32 = rt.u32Add(n3, Guard.id(captured2)); + unwind2 = 0; + rt.drop(captured2); + return _8; + } catch (e3) { + if (unwind2 === 0) { + throw e3; + } + rt.drop(captured2); + throw e3; + } + }, (): void => { + rt.drop(captured2); + })); + _280 = false; + const captured3 = b; + const unmapped: rt.u32 | undefined = map$Option_u32_u32_blasted_closure3(none, rt.closure((n4: rt.u32): rt.u32 => { + let unwind3: number = 0; + try { + unwind3 = 5; + const _57: rt.u32 = rt.u32Add(n4, Guard.id(captured3)); + unwind3 = 0; + rt.drop(captured3); + return _57; + } catch (e4) { + if (unwind3 === 0) { + throw e4; + } + rt.drop(captured3); + throw e4; + } + }, (): void => { + rt.drop(captured3); + })); + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtOption(mapped, f, rt.u32Display), (f: rt.Formatter) => rt.fmtOption(unmapped, f, rt.u32Display)]; + Log.note(log, rt.fmtFormat(["map ", args2[0], " ", args2[1]])); + const ok: rt.Result = rt.Ok(id); + const err: rt.Result = rt.Err(id); + _47 = guard(3); + unwind = 112; + _50 = guard(4); + unwind = 111; + _53 = guard(5); + unwind = 110; + const _56: Guard.Guard = guard(6); + unwind = 137; + _279 = true; + c = _47; + _278 = true; + d = _50; + _277 = true; + e = _53; + _276 = true; + f = _56; + _279 = false; + const captured4 = c; + const _60: rt.Result = map$Result_u32_u32_u32_discarded_closure4(ok, rt.closure((n5: rt.u32): rt.u32 => { + let unwind4: number = 0; + try { + unwind4 = 5; + const _59: rt.u32 = rt.u32Add(n5, Guard.id(captured4)); + unwind4 = 0; + rt.drop(captured4); + return _59; + } catch (e5) { + if (unwind4 === 0) { + throw e5; + } + rt.drop(captured4); + throw e5; + } + }, (): void => { + rt.drop(captured4); + })); + _278 = false; + const captured5 = d; + const values: [rt.Result, rt.Result] = [_60, map$Result_u32_u32_u32_discarded_closure4(err, rt.closure((n6: rt.u32): rt.u32 => { + let unwind5: number = 0; + try { + unwind5 = 5; + const _510: rt.u32 = rt.u32Add(n6, Guard.id(captured5)); + unwind5 = 0; + rt.drop(captured5); + return _510; + } catch (e6) { + if (unwind5 === 0) { + throw e6; + } + rt.drop(captured5); + throw e6; + } + }, (): void => { + rt.drop(captured5); + }))]; + _277 = false; + const captured6 = e; + const _65: rt.Result = mapErr$Result_u32_u32_u32_discarded_closure6(ok, rt.closure((n7: rt.u32): rt.u32 => { + let unwind6: number = 0; + try { + unwind6 = 5; + const _511: rt.u32 = rt.u32Add(n7, Guard.id(captured6)); + unwind6 = 0; + rt.drop(captured6); + return _511; + } catch (e7) { + if (unwind6 === 0) { + throw e7; + } + rt.drop(captured6); + throw e7; + } + }, (): void => { + rt.drop(captured6); + })); + _276 = false; + const captured7 = check; + const captured8 = f; + const errors: [rt.Result, rt.Result] = [_65, mapErr$Result_u32_u32_u32_discarded_closure6(err, rt.closure((n8: rt.u32): rt.u32 => { + let unwind7: number = 0; + try { + unwind7 = 6; + captured7(2); + const _82: rt.u32 = rt.u32Add(n8, Guard.id(captured8)); + unwind7 = 0; + rt.drop(captured8); + return _82; + } catch (e8) { + if (unwind7 === 0) { + throw e8; + } + rt.drop(captured8); + throw e8; + } + }, (): void => { + rt.drop(captured8); + }))]; + const args4: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtTuple([(f: rt.Formatter) => rt.fmtResult(values[0], f, rt.u32Display, rt.u32Display), (f: rt.Formatter) => rt.fmtResult(values[1], f, rt.u32Display, rt.u32Display)], f), (f: rt.Formatter) => rt.fmtTuple([(f: rt.Formatter) => rt.fmtResult(errors[0], f, rt.u32Display, rt.u32Display), (f: rt.Formatter) => rt.fmtResult(errors[1], f, rt.u32Display, rt.u32Display)], f)]; + Log.note(log, rt.fmtFormat(["result ", args4[0], " ", args4[1]])); + _84 = guard(7); + unwind = 106; + const _87: Guard.Guard = guard(8); + unwind = 134; + _275 = true; + g = _84; + _274 = true; + h = _87; + _275 = false; + const captured9 = g; + const _91: rt.Result = andThen$Result_u32_u32_u32_discarded_closure8(ok, rt.closure((n9: rt.u32): rt.Result => { + let unwind8: number = 0; + try { + unwind8 = 5; + const _611: rt.u32 = rt.u32Add(n9, Guard.id(captured9)); + unwind8 = 0; + const _08: rt.Result = rt.Ok(_611); + rt.drop(captured9); + return _08; + } catch (e9) { + if (unwind8 === 0) { + throw e9; + } + rt.drop(captured9); + throw e9; + } + }, (): void => { + rt.drop(captured9); + })); + _274 = false; + const captured10 = h; + const chained: [rt.Result, rt.Result] = [_91, andThen$Result_u32_u32_u32_discarded_closure8(err, rt.closure((n10: rt.u32): rt.Result => { + let unwind9: number = 0; + try { + unwind9 = 5; + const _612: rt.u32 = rt.u32Add(n10, Guard.id(captured10)); + unwind9 = 0; + const _09: rt.Result = rt.Ok(_612); + rt.drop(captured10); + return _09; + } catch (e10) { + if (unwind9 === 0) { + throw e10; + } + rt.drop(captured10); + throw e10; + } + }, (): void => { + rt.drop(captured10); + }))]; + _98 = guard(9); + unwind = 104; + const _101: Guard.Guard = guard(10); + unwind = 131; + _273 = true; + i = _98; + _272 = true; + j = _101; + _273 = false; + const captured11 = i; + const _105: rt.Result = inspectErr$Result_u32_u32_discarded_closure10(ok, rt.closure((_2: rt.u32): void => { + let unwind10: number = 0; + try { + unwind10 = 9; + const part = Guard.id(captured11); + const args20: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part, f)]; + Log.note(log, rt.fmtFormat(["inspect ", args20[0]])); + unwind10 = 0; + rt.drop(captured11); + } catch (e11) { + if (unwind10 === 0) { + throw e11; + } + rt.drop(captured11); + throw e11; + } + }, (): void => { + rt.drop(captured11); + })); + _272 = false; + const captured12 = check; + const captured13 = j; + const inspected: [rt.Result, rt.Result] = [_105, inspectErr$Result_u32_u32_discarded_closure10(err, rt.closure((_22: rt.u32): void => { + let unwind11: number = 0; + try { + unwind11 = 10; + captured12(3); + const part2 = Guard.id(captured13); + const args22: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part2, f)]; + Log.note(log, rt.fmtFormat(["inspect ", args22[0]])); + unwind11 = 0; + rt.drop(captured13); + } catch (e12) { + if (unwind11 === 0) { + throw e12; + } + rt.drop(captured13); + throw e12; + } + }, (): void => { + rt.drop(captured13); + }))]; + const args6: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtTuple([(f: rt.Formatter) => rt.fmtResult(chained[0], f, rt.u32Display, rt.u32Display), (f: rt.Formatter) => rt.fmtResult(chained[1], f, rt.u32Display, rt.u32Display)], f), (f: rt.Formatter) => rt.fmtTuple([(f: rt.Formatter) => rt.fmtResult(inspected[0], f, rt.u32Display, rt.u32Display), (f: rt.Formatter) => rt.fmtResult(inspected[1], f, rt.u32Display, rt.u32Display)], f)]; + Log.note(log, rt.fmtFormat(["chained ", args6[0], " ", args6[1]])); + _124 = guard(11); + unwind = 102; + const _127: Guard.Guard = guard(12); + unwind = 128; + _271 = true; + k = _124; + _270 = true; + l = _127; + const _132: rt.u32 | undefined = some; + _271 = false; + const captured14 = check; + const captured15 = k; + const _131: rt.u32 = mapOr$Option_u32_u32_discarded_closure12(_132, 0, rt.closure((n11: rt.u32): rt.u32 => { + let unwind12: number = 0; + try { + unwind12 = 6; + captured14(4); + const _810: rt.u32 = rt.u32Add(n11, Guard.id(captured15)); + unwind12 = 0; + rt.drop(captured15); + return _810; + } catch (e13) { + if (unwind12 === 0) { + throw e13; + } + rt.drop(captured15); + throw e13; + } + }, (): void => { + rt.drop(captured15); + })); + _270 = false; + const captured16 = l; + const part3 = [_131, mapOr$Option_u32_u32_discarded_closure12(none, 0, rt.closure((n12: rt.u32): rt.u32 => { + let unwind13: number = 0; + try { + unwind13 = 5; + const _516: rt.u32 = rt.u32Add(n12, Guard.id(captured16)); + unwind13 = 0; + rt.drop(captured16); + return _516; + } catch (e14) { + if (unwind13 === 0) { + throw e14; + } + rt.drop(captured16); + throw e14; + } + }, (): void => { + rt.drop(captured16); + }))] as [rt.u32, rt.u32]; + const args8: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtTuple([(f: rt.Formatter) => rt.u32Display(part3[0], f), (f: rt.Formatter) => rt.u32Display(part3[1], f)], f)]; + Log.note(log, rt.fmtFormat(["map_or ", args8[0]])); + _150 = guard(13); + unwind = 100; + const _153: Guard.Guard = guard(14); + unwind = 125; + _269 = true; + m = _150; + _268 = true; + n = _153; + const yes: boolean = some !== undefined; + _269 = false; + const captured17 = m; + const _159: rt.u32 | undefined = rt.boolThen(yes, rt.closure((): rt.u32 => { + let unwind14: number = 0; + try { + unwind14 = 4; + const _012: rt.u32 = Guard.id(captured17); + unwind14 = 0; + rt.drop(captured17); + return _012; + } catch (e15) { + if (unwind14 === 0) { + throw e15; + } + rt.drop(captured17); + throw e15; + } + }, (): void => { + rt.drop(captured17); + }), rt.drop); + _268 = false; + const captured18 = n; + const part4 = [_159, rt.boolThen(!yes, rt.closure((): rt.u32 => { + let unwind15: number = 0; + try { + unwind15 = 4; + const _013: rt.u32 = Guard.id(captured18); + unwind15 = 0; + rt.drop(captured18); + return _013; + } catch (e16) { + if (unwind15 === 0) { + throw e16; + } + rt.drop(captured18); + throw e16; + } + }, (): void => { + rt.drop(captured18); + }), rt.drop)] as [rt.u32 | undefined, rt.u32 | undefined]; + const args10: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtTuple([(f: rt.Formatter) => rt.fmtOption(part4[0], f, rt.u32Display), (f: rt.Formatter) => rt.fmtOption(part4[1], f, rt.u32Display)], f)]; + Log.note(log, rt.fmtFormat(["then ", args10[0]])); + _177 = guard(15); + unwind = 98; + const _180: Guard.Guard = guard(16); + unwind = 121; + _267 = true; + o = _177; + _266 = true; + p = _180; + _267 = false; + const captured19 = check; + const captured20 = o; + const plus: (a0: rt.u32) => rt.u32 = rt.closure((n13: rt.u32): rt.u32 => { + captured19(5); + return rt.u32Add(n13, Guard.id(captured20)); + }, (): void => { + rt.drop(captured20); + }); + _266 = false; + _265 = true; + const captured21 = p; + other = rt.closure((n14: rt.u32): rt.u32 => { + return rt.u32Add(n14, Guard.id(captured21)); + }, (): void => { + rt.drop(captured21); + }); + const _191: rt.u32 | undefined = map$Option_u32_u32_discarded_closure16(some, plus); + _265 = false; + const _194: rt.u32 | undefined = map$Option_u32_u32_discarded_closure16(none, other); + const args12: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtOption(_191, f, rt.u32Display), (f: rt.Formatter) => rt.fmtOption(_194, f, rt.u32Display)]; + Log.note(log, rt.fmtFormat(["by ref ", args12[0], " ", args12[1]])); + _205 = guard(18); + unwind = 95; + const _208: Guard.Guard = guard(19); + unwind = 117; + _264 = true; + r = _205; + _263 = true; + s = _208; + _264 = false; + const captured22 = r; + _263 = false; + _262 = true; + const captured23 = s; + skipped = rt.closure((): rt.u32 => { + return Guard.id(captured23); + }, (): void => { + rt.drop(captured23); + }); + const _219: rt.u32 | undefined = rt.boolThen(yes, rt.once(rt.closure((): rt.u32 => { + return Guard.id(captured22); + }, (): void => { + rt.drop(captured22); + })), rt.drop); + _262 = false; + const _221: rt.u32 | undefined = rt.boolThen(!yes, rt.once(skipped), rt.drop); + const args14: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtOption(_219, f, rt.u32Display), (f: rt.Formatter) => rt.fmtOption(_221, f, rt.u32Display)]; + Log.note(log, rt.fmtFormat(["then by ref ", args14[0], " ", args14[1]])); + const captured24 = guard(17); + const _240: rt.u32 | undefined = some; + const captured25 = check; + const captured26 = rt.closure((n15: rt.u32): rt.u32 => { + return rt.u32Add(n15, Guard.id(captured24)); + }, (): void => { + rt.drop(captured24); + }); + const part5 = map$Option_u32_u32_blasted_closure3(_240, rt.closure((n16: rt.u32): rt.u32 => { + let unwind16: number = 0; + try { + unwind16 = 5; + captured25(6); + const _019: rt.u32 = captured26(n16); + unwind16 = 0; + rt.drop(captured26); + return _019; + } catch (e17) { + if (unwind16 === 0) { + throw e17; + } + rt.drop(captured26); + throw e17; + } + }, (): void => { + rt.drop(captured26); + })); + const args16: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtOption(part5, f, rt.u32Display)]; + Log.note(log, rt.fmtFormat(["nested ", args16[0]])); + check(7); + _262 = false; + _263 = false; + _264 = false; + _265 = false; + _266 = false; + _267 = false; + _268 = false; + _269 = false; + _270 = false; + _271 = false; + _272 = false; + _273 = false; + _274 = false; + _275 = false; + _276 = false; + _277 = false; + _278 = false; + _279 = false; + _280 = false; + _281 = false; + } catch (e2) { + b2: { + b3: { + b4: { + b5: { + b6: { + b7: { + b8: { + b9: { + b10: { + if (unwind === 0) { + throw e2; + } + if (unwind === 95) { + rt.drop(_205); + break b9; + } + if (unwind === 98) { + rt.drop(_177); + break b8; + } + if (unwind === 100) { + rt.drop(_150); + break b7; + } + if (unwind === 102) { + rt.drop(_124); + break b6; + } + if (unwind === 104) { + rt.drop(_98); + break b5; + } + if (unwind === 106) { + rt.drop(_84); + break b4; + } + if (unwind === 110) { + rt.drop(_53); + } else if (unwind !== 111) { + if (unwind === 112) { + break b10; + } + if (unwind === 114) { + rt.drop(_16); + break b2; + } + if (unwind === 117) { + if (_262) { + rt.drop(skipped); + } + if (_263) { + rt.drop(s); + } + if (!_264) { + break b9; + } + rt.drop(r); + break b9; + } + if (unwind === 121) { + break b9; + } + if (unwind === 125) { + break b8; + } + if (unwind === 128) { + break b7; + } + if (unwind === 131) { + break b6; + } + if (unwind === 134) { + break b5; + } + if (unwind === 137) { + break b4; + } + break b3; + } + rt.drop(_50); + } + rt.drop(_47); + break b3; + } + if (_265) { + rt.drop(other); + } + if (_266) { + rt.drop(p); + } + if (_267) { + rt.drop(o); + } + } + if (_268) { + rt.drop(n); + } + if (_269) { + rt.drop(m); + } + } + if (_270) { + rt.drop(l); + } + if (_271) { + rt.drop(k); + } + } + if (_272) { + rt.drop(j); + } + if (_273) { + rt.drop(i); + } + } + if (_274) { + rt.drop(h); + } + if (_275) { + rt.drop(g); + } + } + if (_276) { + rt.drop(f); + } + if (_277) { + rt.drop(e); + } + if (_278) { + rt.drop(d); + } + if (_279) { + rt.drop(c); + } + } + if (_280) { + rt.drop(b); + } + if (_281) { + rt.drop(a); + } + } + throw e2; + } +} + +/** + * What drops of a closure's captures when one's `Drop` panics: the rest still drop, in + * order, and the panic goes on. + */ +export function blasted(log: Log.Log, id: rt.u32, at: rt.u8): void { + let a!: Bomb.Bomb; + let b!: Bomb.Bomb; + let c!: Guard.Guard; + let _12!: Bomb.Bomb; + let _15!: Bomb.Bomb; + let f!: () => rt.u32; + let a2!: Bomb.Bomb; + let b2!: Guard.Guard; + let _37!: Bomb.Bomb; + let a3!: Bomb.Bomb; + let b3!: Guard.Guard; + let _50!: Bomb.Bomb; + let a4!: Bomb.Bomb; + let b4!: Guard.Guard; + let _62!: Bomb.Bomb; + let a5!: Bomb.Bomb; + let b5!: Guard.Guard; + let _74!: Bomb.Bomb; + let g!: () => rt.u32; + let h!: Guard.Guard; + let _90!: boolean; + let _91!: boolean; + let _92!: boolean; + let _93!: boolean; + let _94!: boolean; + let _95!: boolean; + let _96!: boolean; + let _97!: boolean; + let _98!: boolean; + let _99!: boolean; + let _100!: boolean; + let _101!: boolean; + let _102!: boolean; + let unwind: number = 0; + try { + _102 = false; + _101 = false; + _100 = false; + _99 = false; + _98 = false; + _97 = false; + _96 = false; + _95 = false; + _94 = false; + _93 = false; + _92 = false; + _91 = false; + _90 = false; + const bomb: (a0: rt.u32, a1: boolean) => Bomb.Bomb = (n: rt.u32, blows: boolean): Bomb.Bomb => { + return new Bomb.Bomb({ id: rt.u32Add(id, n), log: Log.clone$Clone(log), blows }); + }; + const guard: (a0: rt.u32) => Guard.Guard = (n2: rt.u32): Guard.Guard => { + return Guard.new(rt.u32Add(id, n2), Log.clone$Clone(log)); + }; + if (at === 0) { + _12 = bomb(1, false); + unwind = 49; + _15 = bomb(2, true); + unwind = 48; + const _18: Guard.Guard = guard(3); + unwind = 45; + _102 = true; + a = _12; + _101 = true; + b = _15; + _100 = true; + c = _18; + _102 = false; + _101 = false; + _100 = false; + const captured10 = a; + const captured11 = b; + const captured12 = c; + f = rt.closure((): rt.u32 => { + return rt.u32Add(rt.u32Add(bombId(captured10), bombId(captured11)), Guard.id(captured12)); + }, (): void => { + let dropped: number = 0; + try { + rt.drop(captured10); + dropped = 1; + rt.drop(captured11); + dropped = 2; + rt.drop(captured12); + } catch (e) { + if (dropped < 1) { + rt.drop(captured11); + } + if (dropped < 2) { + rt.drop(captured12); + } + throw e; + } + }); + const part = f(); + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part, f)]; + Log.note(log, rt.fmtFormat(["blasted ", args2[0]])); + unwind = 66; + rt.drop(f); + _100 = false; + _101 = false; + _102 = false; + } else if (at === 1) { + _37 = bomb(1, true); + unwind = 44; + const _40: Guard.Guard = guard(2); + unwind = 63; + _99 = true; + a2 = _37; + _98 = true; + b2 = _40; + _99 = false; + _98 = false; + const captured8 = a2; + const captured9 = b2; + map$Option_u32_u32_blasted_closure3(undefined as rt.u32 | undefined, rt.closure((n3: rt.u32): rt.u32 => { + let unwind3: number = 0; + try { + unwind3 = 7; + const _9: rt.u32 = rt.u32Add(rt.u32Add(n3, bombId(captured8)), Guard.id(captured9)); + unwind3 = 0; + const _07: rt.u32 = _9; + let dropped: number = 0; + try { + rt.drop(captured8); + dropped = 1; + rt.drop(captured9); + } catch (e) { + if (dropped < 1) { + rt.drop(captured9); + } + throw e; + } + return _07; + } catch (e3) { + if (unwind3 === 0) { + throw e3; + } + let dropped: number = 0; + try { + rt.drop(captured8); + dropped = 1; + rt.drop(captured9); + } catch (e) { + if (dropped < 1) { + rt.drop(captured9); + } + throw e; + } + throw e3; + } + }, (): void => { + let dropped: number = 0; + try { + rt.drop(captured8); + dropped = 1; + rt.drop(captured9); + } catch (e) { + if (dropped < 1) { + rt.drop(captured9); + } + throw e; + } + })); + _98 = false; + _99 = false; + } else if (at === 2) { + _50 = bomb(1, true); + unwind = 42; + const _53: Guard.Guard = guard(2); + unwind = 60; + _97 = true; + a3 = _50; + _96 = true; + b3 = _53; + _97 = false; + _96 = false; + const captured6 = a3; + const captured7 = b3; + once$u32_blasted_closure4(rt.closure((): rt.u32 => { + let unwind2: number = 0; + try { + unwind2 = 6; + const _69: rt.u32 = rt.u32Add(bombId(captured6), Guard.id(captured7)); + unwind2 = 0; + const _06: rt.u32 = _69; + let dropped: number = 0; + try { + rt.drop(captured6); + dropped = 1; + rt.drop(captured7); + } catch (e) { + if (dropped < 1) { + rt.drop(captured7); + } + throw e; + } + return _06; + } catch (e2) { + if (unwind2 === 0) { + throw e2; + } + let dropped: number = 0; + try { + rt.drop(captured6); + dropped = 1; + rt.drop(captured7); + } catch (e) { + if (dropped < 1) { + rt.drop(captured7); + } + throw e; + } + throw e2; + } + }, (): void => { + let dropped: number = 0; + try { + rt.drop(captured6); + dropped = 1; + rt.drop(captured7); + } catch (e) { + if (dropped < 1) { + rt.drop(captured7); + } + throw e; + } + })); + _96 = false; + _97 = false; + } else if (at === 3) { + _62 = bomb(1, true); + unwind = 40; + const _65: Guard.Guard = guard(2); + unwind = 57; + _95 = true; + a4 = _62; + _94 = true; + b4 = _65; + _95 = false; + _94 = false; + const captured4 = a4; + const captured5 = b4; + once$u32_blasted_closure5(rt.closure((): rt.u32 => { + return rt.u32Add(bombId(captured4), Guard.id(captured5)); + }, (): void => { + let dropped: number = 0; + try { + rt.drop(captured4); + dropped = 1; + rt.drop(captured5); + } catch (e) { + if (dropped < 1) { + rt.drop(captured5); + } + throw e; + } + })); + _94 = false; + _95 = false; + } else { + _74 = bomb(1, true); + unwind = 38; + const _77: Guard.Guard = guard(2); + unwind = 35; + _93 = true; + a5 = _74; + _92 = true; + b5 = _77; + _93 = false; + _92 = false; + const captured = a5; + const captured2 = b5; + _91 = true; + const captured3 = rt.closure((): rt.u32 => { + return rt.u32Add(bombId(captured), Guard.id(captured2)); + }, (): void => { + let dropped: number = 0; + try { + rt.drop(captured); + dropped = 1; + rt.drop(captured2); + } catch (e) { + if (dropped < 1) { + rt.drop(captured2); + } + throw e; + } + }); + g = rt.closure((): rt.u32 => { + return captured3(); + }, (): void => { + rt.drop(captured3); + }); + h = guard(3); + unwind = 52; + _90 = true; + _91 = false; + rt.drop(g); + _90 = false; + rt.drop(h); + _90 = false; + _91 = false; + _92 = false; + _93 = false; + } + } catch (e) { + b1: { + b2: { + b3: { + if (unwind === 0) { + throw e; + } + if (unwind === 35) { + break b2; + } + if (unwind === 38) { + rt.drop(_74); + break b1; + } + if (unwind === 40) { + rt.drop(_62); + break b1; + } + if (unwind === 42) { + rt.drop(_50); + break b1; + } + if (unwind === 44) { + rt.drop(_37); + break b1; + } + if (unwind === 45) { + rt.drop(f); + break b3; + } + if (unwind === 48) { + rt.drop(_15); + } else if (unwind !== 49) { + if (unwind === 52) { + if (!_90) { + break b2; + } + rt.drop(h); + break b2; + } + if (unwind === 57) { + if (_94) { + rt.drop(b4); + } + if (!_95) { + break b1; + } + rt.drop(a4); + break b1; + } + if (unwind === 60) { + if (_96) { + rt.drop(b3); + } + if (!_97) { + break b1; + } + rt.drop(a3); + break b1; + } + if (unwind === 63) { + if (_98) { + rt.drop(b2); + } + if (!_99) { + break b1; + } + rt.drop(a2); + break b1; + } + break b3; + } + rt.drop(_12); + break b1; + } + if (_100) { + rt.drop(c); + } + if (_101) { + rt.drop(b); + } + if (!_102) { + break b1; + } + rt.drop(a); + break b1; + } + if (_91) { + rt.drop(g); + } + if (_92) { + rt.drop(b5); + } + if (_93) { + rt.drop(a5); + } + } + throw e; + } +} + +//# sourceMappingURL=closures.ts.map diff --git a/ts/fixtures/task/expected/model.ts b/ts/fixtures/task/expected/model.ts index 0b1921d39..96befdbff 100644 --- a/ts/fixtures/task/expected/model.ts +++ b/ts/fixtures/task/expected/model.ts @@ -1405,8 +1405,10 @@ export function temporaries(log: Log.Log, id: rt.u32): void { const args8: rt.Fmt[] = [(f: rt.Formatter) => rt.usizeDisplay(part2, f)]; Log.note(log, rt.fmtFormat(["empty ", args8[0]])); const captured = guard(1100); - const part3 = ((): rt.u32 => { + const part3 = rt.closure((): rt.u32 => { return Guard.finish(captured, "closure"); + }, (): void => { + rt.drop(captured); })(); const args10: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part3, f)]; Log.note(log, rt.fmtFormat(["closed ", args10[0]])); diff --git a/ts/fixtures/task/src/drops.rs b/ts/fixtures/task/src/drops.rs index d6b637a91..2c7534315 100644 --- a/ts/fixtures/task/src/drops.rs +++ b/ts/fixtures/task/src/drops.rs @@ -21,6 +21,7 @@ macro_rules! list { }}; } +mod closures; mod iters; /// A value of a type that drops, with no `[Symbol.dispose]` of its own. @@ -63,17 +64,20 @@ pub struct Named<'a>(Text<'a>); /// order, the second after the first panics. pub struct Entries(Vec<(K, V)>); -/// Run scenario `arg % 19`, panicking at the point `arg / 19 % 8` names, if any: what +/// Run scenario `arg % 22`, panicking at the point `arg / 22 % 8` names, if any: what /// every value std holds drops, where Rust drops it, and in Rust's order. pub fn run(log: &Log, id: u32, arg: u8) { - let at = arg / 19 % 8; + let at = arg / 22 % 8; let guard = |n: u32| Guard::new(id + n, log.clone()); let check = |point: u8| { if point == at { panic!("d{id} at {point}"); } }; - match arg % 19 { + match arg % 22 { + 19 => closures::scoped(log, id, at), + 20 => closures::discarded(log, id, at), + 21 => closures::blasted(log, id, at), 13 => iters::looped(log, id, at), 14 => iters::collected(log, id, at), 15 => iters::drained(log, id, at), diff --git a/ts/fixtures/task/src/drops/closures.rs b/ts/fixtures/task/src/drops/closures.rs new file mode 100644 index 000000000..e773f30ef --- /dev/null +++ b/ts/fixtures/task/src/drops/closures.rs @@ -0,0 +1,218 @@ +//! What a closure drops of the values it owns: where it goes out of scope, where std +//! discards it uncalled, and where a call consumes it, a panic included. + +use super::Bomb; +use crate::log::Log; +use crate::model::Guard; + +/// Call `f` once, which consumes it. +fn once(f: impl FnOnce() -> R) -> R { + f() +} + +/// Call `f` with each of `n` and `n + 1`, which borrows it, and drop it. +fn twice(n: u32, mut f: impl FnMut(u32) -> u32) -> u32 { + f(n) + f(n + 1) +} + +/// The id of `bomb`, which a closure that calls it captures whole, where one that reads +/// its field captures only that. +fn bomb_id(bomb: &Bomb) -> u32 { + bomb.id +} + +/// Hold `f`, which drops with it. +struct Holder { + f: F, + guard: Guard, +} + +/// What closures that own guards drop, panicking at point `at`, if any, some inside a +/// closure's body. +pub(super) fn scoped(log: &Log, id: u32, at: u8) { + let guard = |n: u32| Guard::new(id + n, log.clone()); + let check = |point: u8| { + if point == at { + panic!("d{id} at {point}"); + } + }; + // Out of scope uncalled, or called by reference and then dropped, as the scope ends or + // a panic unwinds it. + let (a, b) = (guard(1), guard(2)); + let unused = move || a.id() + b.id(); + let c = guard(3); + let read = move |n: u32| n + c.id(); + log.note(format!("read {}", read(1) + read(2))); + check(1); + drop(unused); + // A body that moves what it owns out drops it there; one that keeps it drops it as it + // returns, or as a panic in it unwinds. + let d = guard(4); + let moved = once(move || { + let n = d.id(); + drop(d); + log.note(format!("moved {n}")); + n + }); + log.note(format!("moved {moved}")); + let e = guard(5); + let kept = once(move || { + check(2); + e.id() + }); + log.note(format!("kept {kept}")); + // One that `Fn` calls, consumed by a call through `FnOnce`, drops after it, or as a + // panic in it unwinds. + let f = guard(6); + let by_ref = move || { + check(3); + f.id() + }; + log.note(format!("by ref {}", once(by_ref))); + let g = guard(7); + log.note(format!("twice {}", twice(1, move |n| n + g.id()))); + // Held in a struct, and in a closure that owns it. + let h = guard(8); + let holder = Holder { + f: move |n: u32| n + h.id(), + guard: guard(9), + }; + log.note(format!("holder {}", (holder.f)(1) + holder.guard.id())); + let i = guard(10); + let inner = move |n: u32| n + i.id(); + let outer = move || inner(1); + check(4); + log.note(format!("outer {}", once(outer))); + drop(holder); + // Changing what it owns, through `FnMut`. + let mut held = Vec::new(); + let mut push = move |g: Guard| { + held.push(g); + held.len() + }; + push(guard(11)); + push(guard(12)); + check(5); + drop(push); +} + +/// What std does with a closure that owns a guard: drops it where it is not called, and +/// leaves it to the call where it is. +pub(super) fn discarded(log: &Log, id: u32, at: u8) { + let guard = |n: u32| Guard::new(id + n, log.clone()); + let check = |point: u8| { + if point == at { + panic!("d{id} at {point}"); + } + }; + let some = if at < 8 { Some(id) } else { None }; + let none = if at > 8 { Some(id) } else { None }; + let (a, b) = (guard(1), guard(2)); + let mapped = some.map(move |n| { + check(1); + n + a.id() + }); + let unmapped = none.map(move |n| n + b.id()); + log.note(format!("map {mapped:?} {unmapped:?}")); + let ok = Ok::(id); + let err = Err::(id); + let (c, d, e, f) = (guard(3), guard(4), guard(5), guard(6)); + let values = (ok.map(move |n| n + c.id()), err.map(move |n| n + d.id())); + let errors = ( + ok.map_err(move |n| n + e.id()), + err.map_err(move |n| { + check(2); + n + f.id() + }), + ); + log.note(format!("result {values:?} {errors:?}")); + let (g, h) = (guard(7), guard(8)); + let chained = ( + ok.and_then(move |n| Ok::(n + g.id())), + err.and_then(move |n| Ok::(n + h.id())), + ); + let (i, j) = (guard(9), guard(10)); + let inspected = ( + ok.inspect_err(move |_| log.note(format!("inspect {}", i.id()))), + err.inspect_err(move |_| { + check(3); + log.note(format!("inspect {}", j.id())) + }), + ); + log.note(format!("chained {chained:?} {inspected:?}")); + let (k, l) = (guard(11), guard(12)); + let defaults = ( + some.map_or(0, move |n| { + check(4); + n + k.id() + }), + none.map_or(0, move |n| n + l.id()), + ); + log.note(format!("map_or {defaults:?}")); + let (m, n) = (guard(13), guard(14)); + let yes = some.is_some(); + let thens = (yes.then(move || m.id()), (!yes).then(move || n.id())); + log.note(format!("then {thens:?}")); + // One that `Fn` calls, which std consumes by `FnOnce`: called, then dropped. + let (o, p) = (guard(15), guard(16)); + let plus = move |n: u32| { + check(5); + n + o.id() + }; + let other = move |n: u32| n + p.id(); + log.note(format!("by ref {:?} {:?}", some.map(plus), none.map(other))); + let (r, s) = (guard(18), guard(19)); + let kept = move || r.id(); + let skipped = move || s.id(); + log.note(format!("then by ref {:?} {:?}", yes.then(kept), (!yes).then(skipped))); + // A closure that owns one that owns a guard, as a helper's would. + let q = guard(17); + let inner = move |n: u32| n + q.id(); + log.note(format!( + "nested {:?}", + some.map(move |n| { + check(6); + inner(n) + }) + )); + check(7); +} + +/// What drops of a closure's captures when one's `Drop` panics: the rest still drop, in +/// order, and the panic goes on. +pub(super) fn blasted(log: &Log, id: u32, at: u8) { + let bomb = |n: u32, blows: bool| Bomb { + id: id + n, + log: log.clone(), + blows, + }; + let guard = |n: u32| Guard::new(id + n, log.clone()); + match at { + 0 => { + let (a, b, c) = (bomb(1, false), bomb(2, true), guard(3)); + let f = move || bomb_id(&a) + bomb_id(&b) + c.id(); + log.note(format!("blasted {}", f())); + } + 1 => { + let (a, b) = (bomb(1, true), guard(2)); + drop(None::.map(move |n| n + bomb_id(&a) + b.id())); + } + 2 => { + let (a, b) = (bomb(1, true), guard(2)); + drop(once(move || bomb_id(&a) + b.id())); + } + 3 => { + let (a, b) = (bomb(1, true), guard(2)); + let f = move || bomb_id(&a) + b.id(); + drop(once(f)); + } + _ => { + let (a, b) = (bomb(1, true), guard(2)); + let f = move || bomb_id(&a) + b.id(); + let g = move || f(); + let h = guard(3); + drop(g); + drop(h); + } + } +} diff --git a/ts/runtime/rt/task.ts b/ts/runtime/rt/task.ts index 42b48ea09..f87b0df2b 100644 --- a/ts/runtime/rt/task.ts +++ b/ts/runtime/rt/task.ts @@ -33,6 +33,30 @@ export function dropRange(values: ArrayLike, f: (value: T) => void, from: } } +// Closures. One that owns a value that drops is an arrow function with +// `[Symbol.dispose]`, which drops what it owns. Calling one that `FnOnce` calls consumes +// it, as its body drops what it owns; one that `Fn` or `FnMut` call drops after a call +// through `FnOnce`, as rustc's shim does. + +/** A closure `f`, which owns what `dispose` drops. */ +export function closure unknown>(f: F, dispose: () => void): F & Disposable { + return Object.assign(f, { [Symbol.dispose]: dispose }); +} + +/** `FnOnce::call_once` of a closure that `Fn` or `FnMut` call: `f(...args)`, then `f` drops, a panic included. */ +export function callOnce(f: (...args: A) => R, ...args: A): R { + try { + return f(...args); + } finally { + drop(f); + } +} + +/** A closure that `Fn` or `FnMut` call, as one `FnOnce` calls: a call drops it after. */ +export function once(f: (...args: A) => R): ((...args: A) => R) & Disposable { + return closure((...args: A) => callOnce(f, ...args), () => drop(f)); +} + // Shared ownership /** `Arc`: every clone is this one object, counted. */ diff --git a/ts/src/lower/drop.rs b/ts/src/lower/drop.rs index efcf6257d..b7c3b2828 100644 --- a/ts/src/lower/drop.rs +++ b/ts/src/lower/drop.rs @@ -10,6 +10,7 @@ use rustc_middle::ty::{self, AdtDef, Instance, Ty, TypeVisitableExt}; use rustc_span::Span; use super::library::Std; +use super::mir::Capture; use super::{Adt, AdtRepr, Translator}; use crate::program::{SymKind, Symbol}; use crate::ts::{self, Expr, Param, Stmt, StmtKind, Type}; @@ -134,7 +135,8 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } None => false, }, - ty::Coroutine(..) | ty::Param(_) => true, + // A closure's `[Symbol.dispose]` drops what it owns. + ty::Closure(..) | ty::Coroutine(..) | ty::Param(_) => true, _ => false, } } @@ -278,14 +280,6 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let rustc = match ty.kind() { ty::Tuple(_) => true, ty::Adt(adt, _) => adt.is_enum() || adt.did().is_local(), - ty::Closure(..) => { - // Its captures live in an arrow function's scope, where nothing outside can drop them. - self.err( - span, - "dropping a closure that owns a value that drops, which is not supported yet", - ); - return None; - } _ => false, }; if rustc && let Some(sym) = super::mir::glue::shim(self, ty, span) { @@ -343,9 +337,6 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let body = self.drop_value(Expr::ident("value"), ty, span)?; return Some(Expr::Lambda(vec![param], Box::new(body))); } - if let ty::Closure(..) = ty.kind() { - return self.drop_glue_call(Expr::undefined(), ty, span); - } super::mir::glue::shim(self, ty, span).map(Expr::Sym) } @@ -388,6 +379,23 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } } + /// Statements dropping what a closure of type `ty` owns, whose captures are `captures`: + /// each that drops, in order, the rest still dropping after one panics, as rustc's glue + /// drops a closure. Its `[Symbol.dispose]`, and what ends an `FnOnce` body that drops + /// its closure whole. + pub fn closure_glue(&mut self, ty: Ty<'tcx>, captures: &[Capture], span: Span) -> Vec { + let ty::Closure(_, args) = ty.kind() else { + unreachable!("closure glue of a closure") + }; + let mut steps = Vec::new(); + for (capture, ty) in captures.iter().zip(args.as_closure().upvar_tys()) { + if let Some(dropped) = self.drop_value(capture.value.clone(), ty, span) { + steps.push((dropped, self.drop_panics(ty, &mut Default::default()))); + } + } + unwinding(steps) + } + /// Give a class whose own fields or `Drop` impl drop its drop glue up front, so JS /// that holds one can drop it. One that drops only through a type parameter gets /// it once an instance that drops does. diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 38ba04f91..1931aa134 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -209,17 +209,6 @@ pub(super) fn iter_of<'tcx>(t: &Translator<'_, 'tcx>, ty: Ty<'tcx>) -> Option(b: &mut Body<'_, '_, 'tcx>, closure: Ty<'tcx>, span: Span) { - if b.t.drops(closure) { - b.t.err( - span, - "dropping a closure that owns a value that drops, which is not supported yet", - ); - } -} - /// Refuse iterating `ty`, an iterator, if a binding stands in for it: it is no `rt.Iter`, /// which std's adaptors and `for` take every iterator but the crate's own for. fn not_bound<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, span: Span) { @@ -280,11 +269,9 @@ pub fn call<'tcx>( return Some(Expr::rt("iterOnce", tail(args, vec![dropper]))); } ["iter", "sources", "empty", "empty"] => return Some(Expr::rt("iterEmpty", Vec::new())), - // Each owns its closure, `F`, which the runtime's never drops. - ["iter", "sources", "from_fn", "from_fn"] => { - closure_no_drops(b, targ(1)?, span); - return Some(Expr::rt("iterFromFn", args)); - } + // A closure that owns a value that drops is refused as any std mapping refuses one + // it calls but never drops. + ["iter", "sources", "from_fn", "from_fn"] => return Some(Expr::rt("iterFromFn", args)), ["iter", "sources", "successors", "successors"] => { // `rt.iterSuccessors` would leave the item it holds next undropped. let item = targ(0)?; @@ -294,7 +281,6 @@ pub fn call<'tcx>( format!("an iterator that owns `{item}`, which drops, which is not supported yet"), ); } - closure_no_drops(b, targ(1)?, span); return Some(Expr::rt("iterSuccessors", args)); } _ => {} diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 3ed9457c5..37783de3b 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -475,6 +475,62 @@ fn discards(owner: &str, name: &str) -> Option<&'static [usize]> { }) } +/// The first closure `call` calls that owns a value that drops, of those its mapping does +/// not drop where std would: not in `discarded`, the types it may drop by their index among +/// the call's type arguments. A closure that `Fn` or `FnMut` call, given to one that calls +/// it through `FnOnce`, becomes `rt.once` of it, which drops it after the call, as rustc's +/// shim does. +fn called_closures<'tcx>( + b: &mut Body<'_, '_, 'tcx>, + call: &Call<'tcx>, + discarded: Option<&[usize]>, + args: &mut [Expr], +) -> Option> { + let tcx = b.t.tcx; + let sig = tcx.fn_sig(call.def_id).instantiate_identity().skip_norm_wip(); + let inputs = tcx.instantiate_bound_regions_with_erased(sig).inputs().to_vec(); + let bounds = tcx.param_env(call.def_id).caller_bounds(); + for (i, input) in inputs.into_iter().enumerate() { + let ty::Param(param) = *input.kind() else { + continue; + }; + let ty = call.args[param.index as usize].expect_ty(); + let ty::Closure(_, closure) = ty.kind() else { + continue; + }; + // The trait it is called by: `Fn`, then `FnMut`, whichever it is bound by, or else `FnOnce`. + let Some(bound) = bounds + .clone() + .filter_map(|p| p.as_trait_clause()) + .filter(|t| t.self_ty().skip_binder() == input) + .filter_map(|t| tcx.fn_trait_kind_from_def_id(t.def_id())) + .min_by_key(|kind| match kind { + ty::ClosureKind::Fn => 0, + ty::ClosureKind::FnMut => 1, + ty::ClosureKind::FnOnce => 2, + }) + else { + continue; + }; + if !b.t.drops(ty) { + continue; + } + let index = call.args[..param.index as usize] + .iter() + .filter(|a| a.as_type().is_some()) + .count(); + if !discarded.is_some_and(|d| d.contains(&index)) { + return Some(ty); + } + if bound == ty::ClosureKind::FnOnce && closure.as_closure().kind() != ty::ClosureKind::FnOnce && i < args.len() + { + let f = std::mem::replace(&mut args[i], Expr::undefined()); + args[i] = Expr::rt("once", vec![f]); + } + } + None +} + /// The first type `call` takes by value that drops. fn dropping_operand<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>) -> Option> { let tcx = b.t.tcx; @@ -534,6 +590,32 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Vec return None; } let (owner, name) = key(tcx, call.def_id)?; + let discarded = discards(&owner, &name); + let mut args = args; + let leaked = called_closures(b, call, discarded, &mut args); + let e = mapping(b, call, owner.clone(), name.clone(), discarded, args, out)?; + if let Some(ty) = leaked { + return Some(b.err( + call.span, + format!( + "passing `{ty}`, a closure that owns a value that drops, to `{owner}::{name}`, which is not supported yet" + ), + )); + } + Some(e) +} + +/// The mapping of a call of std's `owner::name`, which may discard what `discarded` says. +fn mapping<'tcx>( + b: &mut Body<'_, '_, 'tcx>, + call: &Call<'tcx>, + owner: String, + name: String, + discarded: Option<&'static [usize]>, + args: Vec, + out: &mut Vec, +) -> Option { + let tcx = b.t.tcx; let span = call.span; let gargs = call.args; let args = match super::task::call(b, call, &owner, &name, args) { @@ -548,7 +630,6 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Vec Ok(e) => return e, Err(args) => args, }; - let discarded = discards(&owner, &name); let dropping = match discarded { Some(_) => None, None => dropping_operand(b, call), diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index 4d5d3ebc2..7118e66a8 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -282,6 +282,15 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { return self.b.call_def(inner, inner_args.skip_binder(), values, span, out); } let f = self.operand(&callee.node, span, out); + // `FnOnce::call_once` of a closure that `Fn` or `FnMut` call drops it after, + // as rustc's shim does, where an `FnOnce` closure's body drops what it owns. + if tcx.fn_trait_kind_from_def_id(trait_id) == Some(ty::ClosureKind::FnOnce) + && let ty::Closure(_, closure) = callee_ty.kind() + && closure.as_closure().kind() != ty::ClosureKind::FnOnce + && self.b.t.drops(callee_ty) + { + return Expr::rt("callOnce", [vec![f], values].concat()); + } return f.call(values); } let local = self.b.t.translated(def_id, gargs); diff --git a/ts/src/lower/mir/mod.rs b/ts/src/lower/mir/mod.rs index e3ad8ab2b..fb6a912e5 100644 --- a/ts/src/lower/mir/mod.rs +++ b/ts/src/lower/mir/mod.rs @@ -2325,7 +2325,25 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { TerminatorKind::SwitchInt { discr, targets } => self.switch(discr, targets, span, out), TerminatorKind::Drop { place, target, .. } => { let ty = place.ty(&self.mir.local_decls, self.tcx).ty; - if self.b.t.drops(ty) { + // An `FnOnce` body drops its closure whole, which is what it captured. + if let Env::Closure(captures) = &self.env + && place.local.as_u32() == 1 + && place.projection.is_empty() + && matches!(ty.kind(), ty::Closure(..)) + { + let mut glue = self.b.t.closure_glue(ty, &captures.clone(), span); + // In a block of its own, whose variable no other drop's can clash with. + if glue.iter().any(|s| matches!(s.kind, StmtKind::Decl { .. })) { + glue = vec![ + StmtKind::Block { + label: None, + body: glue, + } + .at(None), + ]; + } + self.emit(glue, None, false, out); + } else if self.b.t.drops(ty) { let mut pre = Vec::new(); let value = self.place_expr(*place, span, &mut pre); let dropped = match place.as_local().is_some_and(|l| self.stepped.contains(&l)) { diff --git a/ts/src/lower/mir/rvalue.rs b/ts/src/lower/mir/rvalue.rs index f8526f23f..553ba48c6 100644 --- a/ts/src/lower/mir/rvalue.rs +++ b/ts/src/lower/mir/rvalue.rs @@ -11,7 +11,7 @@ use super::{Capture, Env, Fx, Slot}; use crate::lower::library::Std; use crate::lower::num::{self, Int}; use crate::lower::{AdtRepr, byte_array, field_name}; -use crate::ts::{Expr, Key, Stmt, StmtKind}; +use crate::ts::{Expr, Key, Stmt, StmtKind, Type}; impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { /// The value of `rvalue`, assigned to `dest`, or `None` if it has none to assign. @@ -567,10 +567,20 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { .b .t .ret_ty(sig.output(), mir.local_decls[mir::RETURN_PLACE].source_info.span); + let ty = Ty::new_closure(tcx, def_id, args); + let glue = match self.b.t.drops(ty) { + true => Some(self.b.t.closure_glue(ty, &captures, span)), + false => None, + }; let mut inner = Fx::new(&mut *self.b, &mir, Env::Closure(captures), span); let params = inner.params(); let body = inner.lower(); - Expr::Arrow(params, ret, body) + let f = Expr::Arrow(params, ret, body); + // One that owns what drops carries the glue that drops it. + match glue { + Some(glue) => Expr::rt("closure", vec![f, Expr::Arrow(Vec::new(), Type::Builtin("void"), glue)]), + None => f, + } } } diff --git a/ts/tests/task.ts b/ts/tests/task.ts index 3ea7207bb..f9d314f30 100644 --- a/ts/tests/task.ts +++ b/ts/tests/task.ts @@ -281,5 +281,5 @@ export const cases: Uint8Array[] = [ [0, 0, 12, 0, 6, 0, 3, 5, 6, 0], [0, 0, 12, 0, 12, 1, 6, 0, 9, 1, 9, 0], // Each scenario of values std holds that drop, panicking at each point or none. - ...Array.from({ length: 152 }, (_, arg) => [11, arg]), + ...Array.from({ length: 176 }, (_, arg) => [11, arg]), ].map((script) => Uint8Array.from(script));