diff --git a/quest/m3/README.md b/quest/m3/README.md index f96a26967..1f39cf3d6 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -17,7 +17,7 @@ generic std gap is planned here even when moq never calls it. - [Relink without rebuilding](/quest/m3/relink.md) - measure upstream's skip of dependency-only rebuilds on moq's edits once it reaches nightly - [Unsafe code the translator can model](/quest/m3/ts-unsafe.md) - a dependency using only `unsafe` the translator can model translates from source - [The capacity of a BytesMut](/quest/m3/ts-bytes-capacity.md) - `BytesMut::capacity` and what reads it translate with `bytes`' values -- [Trait objects](/quest/m3/ts-dyn.md) - `dyn Error`, `dyn Iterator`, `dyn Buf`, and `Arc` translate, so a consumer neither rewrites nor binds them +- [Trait objects](/quest/m3/ts-dyn.md) - `dyn Error`, `dyn Iterator`, and `dyn Buf` translate, so a consumer neither rewrites nor binds them - [Placeholders written to a crate's own fmt::Write](/quest/m3/ts-fmt-calls.md) - `write!` with placeholders into a crate's own writer translates, its `write_str` seeing each call std makes - [Waiting: type-check in process with tsrs](/quest/m3/ts-tsrs.md) - rust2-ts checks bindings through tsrs instead of `tsc` once it is a stable library - [Drop order in std's folds and iterators](/quest/m3/ts-drop-order.md) - the values that drop still refused in std's consumers, flattens, and collects drop as std's do diff --git a/quest/m3/ts-dyn.md b/quest/m3/ts-dyn.md index 681418e6a..93aa324a4 100644 --- a/quest/m3/ts-dyn.md +++ b/quest/m3/ts-dyn.md @@ -4,7 +4,7 @@ [rust2-ts](/ts/README.md) translates the trait objects of std's and mapped crates' traits that moq still refuses (`dyn Error`, `dyn Iterator`, and `dyn bytes::Buf`), and behind any -pointer std coerces to one, such as `Arc`, with Rust's dispatch and drop order, proven by differential tests against native Rust, so a consumer +pointer std coerces to one, with Rust's dispatch and drop order, proven by differential tests against native Rust, so a consumer neither rewrites nor binds them. ## Plan @@ -12,16 +12,14 @@ neither rewrites nor binds them. Decided with the maintainer on 2026-10-27: a stock crate translates with no annotations or config, so trait objects move from never to not yet. -- `&dyn Debug` and `&dyn Display` translate as functions writing their value, `dyn Future` +- `dyn Debug` and `dyn Display` translate as functions writing their value, behind `&`, + `Box`, `Arc`, and `Rc`, `dyn Future` as an `rt.Future`, `dyn Any` as the class a downcast checks for, `dyn Fn*` as the closure itself, and the translated crates' traits as an object with the trait's methods. Build the rest on those. - `Error` needs `source` and its supertraits' `Debug` and `Display` on the object, and its `downcast_ref` can check classes as `dyn Any`'s does; moq's are thiserror's `source`. - `Iterator` can be an `rt.Iter`, and `bytes::Buf` the `rt.ByteSlice` it reads. -- `Arc` is refused where it is built, found by - [#247](https://github.com/moq-dev/rust2/pull/247) on 2026-10-29: `Arc`, `Rc`, and `Box` - of a trait object take the same object as a `&dyn`, behind the pointer's own counts. - Generic traits and associated types name their arguments in the object's type. - `Any::type_id` and `TypeId` have no JS value yet, and an upcast to `dyn Any` would need the class of the value's own type. diff --git a/ts/README.md b/ts/README.md index 24d506ebf..6a74ceda7 100644 --- a/ts/README.md +++ b/ts/README.md @@ -227,7 +227,9 @@ translate, as do `Display` and `Debug` impls, derived and thiserror's included. runtime ports `core::fmt`, so the text matches native Rust's byte for byte. A placeholder becomes a function writing its value, and `fmt::Arguments` an array of literal pieces and such functions: `rt.fmtWriteFmt(f, ["code=", (f) => rt.u16Display(code, f)])`. A `&dyn -Debug` or `&dyn Display` is such a function too. Format specs take every option but `{:p}`: +Debug` or `&dyn Display` is such a function too, and so is a `Box` of one, `rt.fmtOwned` +of the value, which drops it as the box drops, and an `Arc` or `Rc` of one, which the last +pointer drops, as an `Arc` of the crate's own trait object does. Format specs take every option but `{:p}`: fill, alignment, `+`, `#`, `0`, `x?` and `X?`, a width and a precision, each also from an argument, and `x`, `X`, `o`, `b`, `e`, and `E` for integers, `e` and `E` for floats. An `f64` prints std's digits: the shortest that read back as it, the greater of two as close where @@ -470,7 +472,8 @@ a call calls as it does the closure, and which a box drops as it does the closur that `Fn` or `FnMut` calls, boxed as a `dyn FnOnce`, is `rt.once` of it, which drops it after the call, as an `FnOnce` closure's body drops what it owns, and a box of a reference to one is `rt.lent` of it, which never drops it. A trait object of std's -or a mapped crate's trait other than `&dyn Debug`, `&dyn Display`, `dyn Future`, +or a mapped crate's trait other than `dyn Debug` and `dyn Display` behind `&`, `Box`, +`Arc`, or `Rc`, `dyn Future`, `dyn Any`, and the function traits, of a generic trait or one with an associated type, of a byte array, or behind any other pointer, such as a `Weak` or a struct's last field, is refused, and so are an upcast to `dyn Any` and diff --git a/ts/fixtures/lang/expected/fmt.Dropper.ts b/ts/fixtures/lang/expected/fmt.Dropper.ts new file mode 100644 index 000000000..15990962c --- /dev/null +++ b/ts/fixtures/lang/expected/fmt.Dropper.ts @@ -0,0 +1,37 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +/** Logs its tag as it drops, and writes it padded by `Display`. */ +export class Dropper { + tag: rt.u8; + log: rt.Arc>; + + constructor(tag: rt.u8, log: rt.Arc>) { + this.tag = tag; + this.log = log; + } + + [Symbol.dispose](): void { + drop$ops_Drop(this); + this.log[Symbol.dispose](); + } +} + +export function fmt$fmt_Debug(self: Dropper, f: rt.Formatter): rt.Result { + return rt.debugStructFinishNonExhaustive(rt.debugStructField(rt.fmtDebugStruct(f, "Dropper"), "tag", (f: rt.Formatter) => rt.u8Debug(self.tag, f))); +} + +export function fmt$fmt_Display(self: Dropper, f: rt.Formatter): rt.Result { + const arg = self.tag; + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.u8Display(arg, f)]; + return rt.fmtPad(f, rt.fmtFormat(["n", args2[0]])); +} + +export function drop$ops_Drop(self: Dropper): void { + const _5: rt.RefCell = rt.refCellBorrowMut(self.log.value); + rt.byteVecPush(_5.value, self.tag); + rt.refMutRelease(_5); +} + +//# sourceMappingURL=fmt.Dropper.ts.map diff --git a/ts/fixtures/lang/expected/fmt.ts b/ts/fixtures/lang/expected/fmt.ts index db283be35..ec7fb96f6 100644 --- a/ts/fixtures/lang/expected/fmt.ts +++ b/ts/fixtures/lang/expected/fmt.ts @@ -8,6 +8,7 @@ import * as fmt_Bare from "./fmt.Bare.ts"; import * as Braced from "./fmt.Braced.ts"; import * as Capped from "./fmt.Capped.ts"; import * as Clip from "./fmt.Clip.ts"; +import * as Dropper from "./fmt.Dropper.ts"; import * as Fails from "./fmt.Fails.ts"; import * as Hex from "./fmt.Hex.ts"; import * as Kind from "./fmt.Kind.ts"; @@ -2200,4 +2201,104 @@ export function writes(data: rt.ByteSlice, out: rt.ByteVec): void { put(out, twice._0); } +export function objects(data: rt.ByteSlice, out: rt.ByteVec): void { + let _218: boolean = false; + const log: rt.Arc> = new rt.Arc(new rt.RefCell(new rt.ByteVec())); + const dropper: (a0: rt.u8) => Dropper.Dropper = (tag: rt.u8): Dropper.Dropper => { + return new Dropper.Dropper(tag, rt.arcClone(log)); + }; + const dropped: (a0: rt.ByteVec) => undefined = (out2: rt.ByteVec): undefined => { + const _6: rt.RefCell = rt.refCellBorrowMut(log.value); + const taken = _6.value; + _6.value = new rt.ByteVec(); + const tags: rt.ByteVec = taken; + rt.refMutRelease(_6); + const args18: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtList(rt.byteVecView(tags), f, rt.u8Debug)]; + put(out2, rt.fmtFormat([args18[0], " "])); + }; + const shown: rt.Fmt[] = []; + const written: rt.Fmt[] = []; + const iter: rt.Iter<[rt.usize, rt.u8]> = rt.iterEnumerate(rt.iterSlice(rt.byteSliceView(data))); + while (true) { + const _17: [rt.usize, rt.u8] | rt.Done = iter.next(); + if (_17 === rt.DONE) { + break; + } + const i: rt.usize = _17[0]; + const x: rt.u8 = _17[1]; + const _22: rt.u8 = rt.u8Rem(x, 4); + if (_22 === 0) { + const _25: rt.Fmt = rt.fmtOwned(dropper(x), (value: Dropper.Dropper, f: rt.Formatter) => Dropper.fmt$fmt_Debug(value, f), rt.drop); + shown.push(_25); + } else if (_22 === 1) { + const _31: rt.Fmt = rt.fmtOwned([x, i] as [rt.u8, rt.usize], (value: [rt.u8, rt.usize], f: rt.Formatter) => rt.fmtTuple([(f: rt.Formatter) => rt.u8Debug(value[0], f), (f: rt.Formatter) => rt.usizeDebug(value[1], f)], f)); + shown.push(_31); + } else if (_22 === 2) { + const _35: rt.Fmt = rt.fmtOwned(dropper(x), (value: Dropper.Dropper, f: rt.Formatter) => Dropper.fmt$fmt_Display(value, f), rt.drop); + written.push(_35); + } else { + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.u8Display(x, f)]; + const _41: rt.Fmt = rt.fmtOwned(rt.fmtFormat(["s", args2[0]]), (value: string, f: rt.Formatter) => rt.fmtPad(f, value)); + written.push(_41); + } + } + for (const _55 of shown) { + const args4: rt.Fmt[] = [(f: rt.Formatter) => _55(f)]; + put(out, rt.fmtFormat([args4[0], " ", rt.fmtSpec({ alternate: true }, args4[0]), " "])); + } + for (const _74 of written) { + const args6: rt.Fmt[] = [(f: rt.Formatter) => _74(f)]; + put(out, rt.fmtFormat([args6[0], " [", rt.fmtSpec({ align: ">", width: 6 }, args6[0]), "] [", rt.fmtSpec({ align: "^", width: 7 }, args6[0]), "] "])); + } + const up: rt.Fmt = rt.fmtOwned(data.length, rt.usizeDebug); + const args8: rt.Fmt[] = [(f: rt.Formatter) => up(f)]; + put(out, rt.fmtFormat([args8[0], " "])); + rt.dropAll(written); + dropped(out); + rt.vecTruncate(shown, 1, rt.drop); + dropped(out); + if (!(data.length >= 2)) { + rt.drop(up); + rt.dropAll(shown); + log[Symbol.dispose](); + } else { + const _121: rt.Arc<[rt.u8, rt.u8]> = new rt.Arc([data.data[data.start], data.data[data.start + 1]] as [rt.u8, rt.u8]); + const shared: rt.Arc = new rt.ArcDyn(_121, rt.fmtOwned(_121.value, (value: [rt.u8, rt.u8], f: rt.Formatter) => rt.fmtTuple([(f: rt.Formatter) => rt.u8Debug(value[0], f), (f: rt.Formatter) => rt.u8Debug(value[1], f)], f))); + const kept: rt.Arc = rt.arcClone(shared); + const _135: rt.usize = kept.strong; + const args10: rt.Fmt[] = [(f: rt.Formatter) => shared.value(f), (f: rt.Formatter) => rt.usizeDisplay(_135, f)]; + put(out, rt.fmtFormat([args10[0], " ", args10[1], " "])); + const counted: rt.Arc = new rt.Arc(dropper(data.data[data.start])); + const _149: rt.Arc = rt.arcClone(counted); + const asDisplay: rt.Arc = new rt.ArcDyn(_149, rt.fmtOwned(_149.value, (value: Dropper.Dropper, f: rt.Formatter) => Dropper.fmt$fmt_Display(value, f), rt.drop)); + _218 = true; + const asDebug: rt.Arc = new rt.ArcDyn(counted, rt.fmtOwned(counted.value, (value: Dropper.Dropper, f: rt.Formatter) => Dropper.fmt$fmt_Debug(value, f), rt.drop)); + const _160: rt.usize = asDebug.strong; + const args12: rt.Fmt[] = [(f: rt.Formatter) => asDisplay.value(f), (f: rt.Formatter) => rt.usizeDisplay(_160, f)]; + put(out, rt.fmtFormat([rt.fmtSpec({ align: "<", width: 5 }, args12[0]), "| ", args12[1], " "])); + asDisplay[Symbol.dispose](); + dropped(out); + if (rt.u8Rem(data.data[data.start + 1], 2) === 0) { + _218 = false; + asDebug[Symbol.dispose](); + } else { + const args14: rt.Fmt[] = [(f: rt.Formatter) => asDebug.value(f)]; + put(out, rt.fmtFormat([args14[0], " "])); + } + dropped(out); + shared[Symbol.dispose](); + const _198: rt.usize = kept.strong; + const args16: rt.Fmt[] = [(f: rt.Formatter) => kept.value(f), (f: rt.Formatter) => rt.usizeDisplay(_198, f)]; + put(out, rt.fmtFormat([args16[0], " ", args16[1]])); + if (_218) { + asDebug[Symbol.dispose](); + } + _218 = false; + kept[Symbol.dispose](); + rt.drop(up); + rt.dropAll(shown); + log[Symbol.dispose](); + } +} + //# sourceMappingURL=fmt.ts.map diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index c41a03df1..500fec154 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -27,7 +27,7 @@ import { edits as edits_edits, bytes as edits_bytes } from "./edits.ts"; import { eqs } from "./errors.ts"; import { fields as fields_fields } from "./fields.ts"; import { floats, mulAdds as floats_mulAdds, formats as floats_formats, parses as floats_parses } from "./floats.ts"; -import { ints, strings, derived, written as fmt_written, counts as fmt_counts, rounding, panics as fmt_panics, wrapped as fmt_wrapped, collections, enums, iters as fmt_iters, writes as fmt_writes } from "./fmt.ts"; +import { ints, strings, derived, written as fmt_written, counts as fmt_counts, rounding, panics as fmt_panics, wrapped as fmt_wrapped, collections, enums, iters as fmt_iters, writes as fmt_writes, objects as fmt_objects } from "./fmt.ts"; import { folds } from "./folds.ts"; import { methods, runs } from "./generics.ts"; import { values as hashes_values, random, bytes as hashes_bytes, own as hashes_own } from "./hashes.ts"; @@ -43,7 +43,7 @@ import { locals } from "./locals.ts"; import { locks, relock, rwlocks, mapped, mutexMapped, printed as locks_printed, downgraded } from "./locks.ts"; import { u8s as methods_u8s, u16s as methods_u16s, u32s as methods_u32s, u64s as methods_u64s, u128s as methods_u128s, usizes as methods_usizes, i8s as methods_i8s, i16s as methods_i16s, i32s as methods_i32s, i64s as methods_i64s, i128s as methods_i128s, isizes as methods_isizes } from "./methods.ts"; import { nested as nested_nested } from "./nested.ts"; -import { objects } from "./objects.ts"; +import { objects as objects_objects } from "./objects.ts"; import { orders, bytes as orders_bytes, tuples as orders_tuples, held as orders_held, btrees } from "./orders.ts"; import { lists as pointers_lists, shared as pointers_shared, places as pointers_places, unchecked, nested as pointers_nested, copies as pointers_copies, sentinels, cells as pointers_cells, rebound as pointers_rebound, lapsed, writers, made as pointers_made, segments, uninitRuns, iteratedRuns, besideWritten, waiters, taken as pointers_taken, bytes as pointers_bytes, loaded, reattached, partWritten, aimed } from "./pointers.ts"; import { refs, toRefs, adapted as refs_adapted, compared } from "./refs.ts"; @@ -165,7 +165,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["edits::bytes", edits_bytes as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["arrays::from_fns", (_2147: rt.u8, data47: rt.ByteSlice, out48: rt.ByteVec): undefined => { fromFns(data47, out48); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["collections::optionals", optionals as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["objects::objects", (_2148: rt.u8, data48: rt.ByteSlice, out49: rt.ByteVec): undefined => { - objects(data48, out49); + objects_objects(data48, out49); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["holds::holds", holds as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["fmt::wrapped", (_2149: rt.u8, data49: rt.ByteSlice, out50: rt.ByteVec): undefined => { fmt_wrapped(data49, out50); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["refs::to_refs", (_2150: rt.u8, data50: rt.ByteSlice, out51: rt.ByteVec): undefined => { @@ -284,6 +284,8 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) hashes_own(data99, out100); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["refs::compared", (a0159: rt.u8, a1159: rt.ByteSlice, a2159: rt.ByteVec): undefined => { compared(a0159, a1159.data, a1159.start, a1159.length, a2159); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["fmt::objects", (_2200: rt.u8, data100: rt.ByteSlice, out101: rt.ByteVec): undefined => { + fmt_objects(data100, out101); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined]]; } diff --git a/ts/fixtures/lang/src/fmt.rs b/ts/fixtures/lang/src/fmt.rs index eef1f2b03..736897f03 100644 --- a/ts/fixtures/lang/src/fmt.rs +++ b/ts/fixtures/lang/src/fmt.rs @@ -1,10 +1,13 @@ //! Formatting: `format!` and `write!` with every option, derived and written `Debug` and -//! `Display`, and panics that format their messages. +//! `Display`, and panics that format their messages. A `Box`, `Arc`, or `Rc` of a +//! `dyn Debug` or `dyn Display`, written with options, dropping what it owns as the last +//! pointer drops. #![allow(dead_code, reason = "fields only `Debug` reads")] +use std::cell::RefCell; use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque}; -use std::fmt; +use std::fmt::{self, Debug, Display}; use std::ops::{Range, RangeToInclusive}; use std::rc::Rc; use std::sync::Arc; @@ -1114,3 +1117,84 @@ pub fn writes(data: &[u8], out: &mut Vec) { result(out, twice.write_char('ß')); put(out, &twice.0); } + +/// Logs its tag as it drops, and writes it padded by `Display`. +struct Dropper { + tag: u8, + log: Rc>>, +} + +impl Debug for Dropper { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Dropper") + .field("tag", &self.tag) + .finish_non_exhaustive() + } +} + +impl Display for Dropper { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.pad(&format!("n{}", self.tag)) + } +} + +impl Drop for Dropper { + fn drop(&mut self) { + self.log.borrow_mut().push(self.tag); + } +} + +pub fn objects(data: &[u8], out: &mut Vec) { + let log = Rc::new(RefCell::new(Vec::new())); + let dropper = |tag: u8| Dropper { tag, log: log.clone() }; + let dropped = |out: &mut Vec| { + let tags = std::mem::take(&mut *log.borrow_mut()); + put(out, &format!("{tags:?} ")); + }; + + // Boxed, of each kind, written with options, then dropped in order. + let mut shown: Vec> = Vec::new(); + let mut written: Vec> = Vec::new(); + for (i, &x) in data.iter().enumerate() { + match x % 4 { + 0 => shown.push(Box::new(dropper(x))), + 1 => shown.push(Box::new((x, i))), + 2 => written.push(Box::new(dropper(x))), + _ => written.push(Box::new(format!("s{x}"))), + } + } + for s in &shown { + put(out, &format!("{s:?} {s:#?} ")); + } + for w in &written { + put(out, &format!("{w} [{w:>6}] [{w:^7}] ")); + } + let sent: Box = Box::new(data.len()); + let up: Box = sent; + put(out, &format!("{up:?} ")); + drop(written); + dropped(out); + shown.truncate(1); + dropped(out); + + // Shared: the value drops with the last pointer, whichever it is. + let [a, b, ..] = data else { + return; + }; + let shared: Arc = Arc::new((*a, *b)); + let kept = shared.clone(); + put(out, &format!("{shared:?} {} ", Arc::strong_count(&kept))); + let counted: Rc = Rc::new(dropper(*a)); + let as_display: Rc = counted.clone(); + let as_debug: Rc = counted; + put(out, &format!("{as_display:<5}| {} ", Rc::strong_count(&as_debug))); + drop(as_display); + dropped(out); + match b % 2 { + 0 => drop(as_debug), + _ => put(out, &format!("{as_debug:?} ")), + } + dropped(out); + drop(shared); + put(out, &format!("{kept:?} {}", Arc::strong_count(&kept))); +} diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index ada2a70f3..fcd9c7526 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -290,6 +290,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("borrows::leaks", |_, data, out| borrows::leaks(data, out)), ("hashes::own", |_, data, out| hashes::own(data, out)), ("refs::compared", refs::compared), + ("fmt::objects", |_, data, out| fmt::objects(data, out)), ] } diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index dd592533f..2a452c3a8 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -1619,6 +1619,12 @@ export type Fmt = (f: Formatter) => FmtResult; /** A type's `Debug` or `Display`, as a function of its values. */ export type Debug = (value: T, f: Formatter) => FmtResult; +/** `Box` or the like of `value`, written by `write`, which drops it by `drop` as it drops. */ +export function fmtOwned(value: T, write: Debug, drop?: (value: T) => void): Fmt { + const fmt = (f: Formatter) => write(value, f); + return drop === undefined ? fmt : Object.assign(fmt, { [Symbol.dispose]: () => drop(value) }); +} + /** `fmt::Arguments`: literal pieces, and the values written between them. */ export type Arguments = readonly (string | Fmt)[]; diff --git a/ts/src/lower/dynamic.rs b/ts/src/lower/dynamic.rs index 2d73493a2..ecbc12081 100644 --- a/ts/src/lower/dynamic.rs +++ b/ts/src/lower/dynamic.rs @@ -420,6 +420,22 @@ impl<'tcx> Body<'_, '_, 'tcx> { if let Some(f) = self.t.dyn_fn(object) { return self.fn_object(value, from, f.kind); } + // A box of a value to write, as a `&dyn Debug` of it is, which owns and drops it. + if let Some(tr) = principal(object).and_then(|p| super::fmt::Trait::of(self.t.tcx, p)) { + return match pointee.kind() { + _ if principal(pointee) == principal(object) => value, + _ if pointee.is_trait() || !from.is_box() => unsupported(self), + _ if self.t.polymorphic() && pointee.has_param() => Expr::undefined(), + _ => { + let write = self.fmt_fn(tr, pointee, span); + let args = [value, write] + .into_iter() + .chain(self.t.drop_fn(pointee, span)) + .collect(); + Expr::rt("fmtOwned", args) + } + }; + } if self.t.object(object, span).is_none() { return Expr::undefined(); }