Skip to content

D111 non-compliance: an owned str demand does not materialize a &str through Ok-wrap, collect, or a &&str join #2271

Closed

Description

@ehartford

D111 (docs/meetings, Eric 2026-10-08): "str is a value. Passing a str always copies. Code using str never sees 'use of moved value' and never needs .clone()." D22 §6.2: an owned demand is met by a copy of a Copy pointee.

The D111 test/example sweep left these .clone()s in place because removing them does not compile. Each is a demand for an owned str that the compiler does not meet:

  1. Implicit Ok tail (behav_implicit_result_tail_ownership):
fn direct(text: &str) -> Result[str, str]: text

error: return type mismatch. A plain -> str return copies; the implicit Ok-wrap does not carry the demand to its payload.

  1. collect of &str views into an owned collection (map_snapshot_cases, behav_d44_map_traversal_views, issue58_hashmap_keys_str, da_2165_collect_duplicate_key):
fn keys(xs: &Vec[str]) -> Vec[str]: xs.iter() |> map(it) |> collect[Vec]()

error: return type mismatch (the pipeline is Vec[&str]). collect[HashSet[str]]() over &str items: collect[HashSet[T]] element type does not match iterator element type. With an inferred element, the generic instance in std/collections.w:773 reports a view escape (view out may originate from x). D44 spells the owned collection map(it.clone()); under D111 the .clone() is ceremony, and the demand (the declared Vec[str], HashSet[str]) should copy each element.

  1. A &&str arm meeting a str result (behav_1810_str_view_lookups):
fn first_some(o: &Option[&str]) -> str:
    match o:
        Some(s) => s
        None => "none"

error: match expression of type &&str cannot produce str. A single &str arm works; contextual Copy materialization stops after one dereference.

Each site still carries its .clone() on the D111 stack; the census counts them (with run tools/ceremony_census.w --sites str-clone).

Activity

  1. added a commit that references this issue on Oct 11, 2026
  2. ehartford commented on Oct 11, 2026

    @ehartford
    ContributorAuthor

    State on branch d115-d120 (commit 02bf556), checked by running, not by notes:

    Shapes 1 (implicit Ok tail, fn direct(text: &str) -> Result[str, str]: text) and 3 (a &&str arm meeting a str result) compile on main since #2337; their test clones are gone (behav_implicit_result_tail_ownership, behav_1810_str_view_lookups). collect[HashSet[str]]() over a List's &str items also works (da_2165 clones gone).

    Still open, one shape: m.keys() |> map(it) |> collect[List]() and the annotated let ks: List[str] = … over a map's key or value views. Two separate defects behind it:

    • iter_collect[T, I: Iter[T]] builds a List[&str] the user cannot spell (var out: List[&str] is refused as an ephemeral reference in a container); under D111/D22 §6.2 the collection stores its element, so the owned demand should copy each &str into a List[str]. The generic that does that needs a view-element bound (I: Iter[&T]), which the parser rejects today (expected pattern).
    • Through Mapped.next() (a result returned by an indirect call (self.f)(x)) the view's origin is lost, so inside the iter_collect instance Rule 6 reports view out may originate from x; the same pipeline over a ListIter records no origin at all and passes silently. A view returned by a call through a fn(&T) -> &U value should originate from its view arguments (the D122 principle applied to callable values).
      The .clone()s in issue58_hashmap_keys_str (both), behav_d44_map_traversal_views and lib/map_snapshot_cases stay until those land.
  3. added a commit that references this issue on Oct 11, 2026
  4. ehartford commented on Oct 13, 2026

    @ehartford
    ContributorAuthor

    Re-checked on main 811a777 (2026-10-12), by running:

    • The parser half is ** gone**: fn c[T, I: Iter[&T]](iter: I) -> List[T] with the copying body parses, checks, and runs over m.keys() (prints the right count). The "expected pattern" in the earlier comment reproduces only when the parameter is named it — that name is the pipeline placeholder and cannot be declared (filed as Parser: a parameter named it fails with "expected pattern" and no hint that it is reserved #2382 for the diagnostic wording). Whether a parser fix also landed since, the bound itself is fine today.
    • Two defects remain, both in the collect[List]() path over map key/value views:
      1. let ks: List[str] = m.keys() |> collect[List]() → "type mismatch in binding": collect builds List[&str]; the owned List[str] demand does not select/copy per element (the hand-rolled Iter[&T] collect above shows the shape std needs).
      2. m.keys() |> map(it) |> collect[List]() → Rule 6 inside std iter_collect (collections.w:760, "view out may originate from x"): a view returned through the callable value in Mapped.next() loses its origin — the D122 principle for callable values, as the last comment said.

    So the remaining work is: origin propagation through an indirect call whose callee type is fn(&T) -> &U, and an owned-demand collect for view elements in std. The four .clone() sites stay until both land.

  5. added a commit that references this issue on Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions