Skip to content

clone() is unknown on a tuple, an array, Option and Result #2161

Closed

Description

@ehartford

Today (v0.15.3.0, main 6b0c99d):

let t = ("a".to_upper(), 1)
let u = t.clone()              // unknown method 'clone' for type '(str, i32)'
let o: Option[str] = Some("x".to_upper())
let p = o.clone()              // unknown method 'clone' for type 'Option[str]'
let arr = ["a".to_upper(), "b".to_upper()]
let brr = arr.clone()          // unknown method 'clone' for type '[2]str'

Spec: §11.8 gives derive(Clone) ("all fields are Clone: field-by-field clone") for declared types. Nothing says a tuple or an array of Clone elements is Clone, and Option/Result in lib/std declare no Clone. D44 and §13.3 make clone the spelling of an independent collection (m.keys() |> map(it.clone()) |> collect[Vec]()), so a Vec[(str, i32)] has no way to produce owned pairs from a traversal.

Why now: it blocks #2145 (v.iter() yielding views): xs.iter() |> map(it.clone()) |> collect[HashMap[str, i32]]() over Vec[(str, i32)] is the conforming spelling and does not compile.

Proposed:

  1. Option[T] and Result[T, E] are Clone when their arguments are: two impls in lib/std, plain With.
  2. A tuple and a fixed array are Clone when every element is: this needs one sentence in §11.8 (or §4.3) from Eric, and a structural clone in the compiler shaped like == on an enum or Vec with a str payload compares representation bytes: Option[str] == Some("a") is false #2137's structural equality (Sema records each part's clone; codegen walks the type).

Activity

  1. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    Option and Result are Clone on main a57d2d2 (#2164). Tuples and fixed arrays still have no clone: that needs a spec sentence (asked of Eric).

  2. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by stack #2196 (merged as efb765c), battery green on a21e326; the regression test for it landed with the fix.

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