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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions quest/m3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ generic std gap is planned here even when moq never calls it.
- [A map built with BuildHasherDefault](/quest/m3/ts-build-hasher-default.md) - `FxHashMap`-style maps, of `BuildHasherDefault` over a crate's own `Hasher`, translate
- [Equality of hash maps and sets](/quest/m3/ts-hash-map-eq.md) - `==` of two hash maps or sets translates as std's
- [A name or an operator argument at its own Rust span](/quest/m3/ts-argument-spans.md) - a `tsc` error on a plain-name or operator argument names that argument's Rust, not the method it is passed to
- [A string Cow's variant](/quest/m3/ts-cow-str-variant.md) - matching a `Cow<str>`'s `Borrowed` or `Owned`, and `to_mut`'s change of it, translate as std's
42 changes: 42 additions & 0 deletions quest/m3/ts-cow-str-variant.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# [L] A string Cow's variant

## Goal

[rust2-ts](/ts/README.md) translates matching `Cow::Borrowed` and `Cow::Owned` of a
`Cow<str>`, and `into_owned` and `to_mut` keep std's variant: `to_mut` of a borrowed one
makes it owned. Differential tests against native Rust observe the variant wherever std
decides it, including `String::from_utf8_lossy`, which borrows valid input and owns
replaced text. `Cow::is_borrowed` and `is_owned` are unstable and stay refused.

## Plan

Decided with the maintainer on 2026-11-03: plan this, found while translating
`from_utf8_lossy` in [#537](https://github.com/moq-dev/rust2/pull/537).

Today a `Cow<str>` is a plain JS string ([#105](https://github.com/moq-dev/rust2/pull/105)),
so it keeps no variant. Code that reads one is refused with `library::cow_variant`, at a
match or `if let` of its discriminant (`mir/place.rs` and `mir/rvalue.rs`), at a read of a
variant's field (`mir/place.rs`), and at `is_borrowed` and `is_owned` (`library.rs`). The
`reject` fixture's `strs.rs` holds these cases. `to_mut` is the same place and
`into_owned` the same text, which is right only while nothing reads the variant.

Representation options:

- A wrapper only where some match observes the variant: a whole-program pass finds every
`Cow<str>` whose variant some match or field read can see, and only those values become
a `{ borrowed, text }`-like object. Every other `Cow<str>` stays a string. It costs
nothing where nothing observes, but values flow through generics, fields, collections,
and calls, so the pass has to follow each one and refuse where it can't.
- A wrapper everywhere: every `Cow<str>` is an object. It is simple and uniform, but costs
an allocation and an indirection on every use, and changes `Display`, `==`, hashing,
keys, `Deref`, and the conversions the plain string gets for free.
- Others found along the way, such as a variant kept beside the string only in the
locals and fields that need it.

Measure each candidate against the plain string on the TS bench (`just ts bench`) and
on moq's translated codec (`just ts moq`) before choosing. Ask the maintainer if no
option is free where nothing observes the variant.

`Cow<[T]>` already keeps its variant (`rt.Borrowed`, or a `ByteSlice` against a
`ByteVec`), which may suggest a shape. The tests belong in the `lang/cows` and `lang/utf8`
fixtures. The `reject` fixture's `Cow<str>` cases move to them.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Not every Cow<str> case in reject/src/strs.rs can move: cow_borrowed asks is_borrowed on a Cow<str>, which this quest keeps refused. Worth scoping this to the cases the quest resolves (the match), leaving cow_borrowed in place.

Technical details
# reject fixture's Cow<str> cases can't all move

## Affected sites
- quest/m3/ts-cow-str-variant.md:42 — "The `reject` fixture's `Cow<str>` cases move to them" sweeps in `cow_borrowed`, which stays refused.

## Required outcome
- The plan should say only the variant-matching case (`cow_matched`) moves to `lang/cows`; `cow_borrowed` (`is_borrowed` on a `Cow<str>`) stays in `reject` because `is_borrowed` remains refused per the Goal.

## Suggested approach
- "The `reject` fixture's matching case moves there" or naming `cow_matched` directly.