Skip to content

Returning a Copy element of an owned array parameter is refused as a view: fn arr(xs: [i32; 2]): xs[0] #2334

Closed

Description

@ehartford

Spec: §3.4/D27 (element access observes; an owned demand of a Copy element materializes a value), §9 inferred return type.

Repro:

fn arr(xs: [i32; 2]): xs[0]
fn main:
    print(arr([7, 8]))

Expected: prints 7. The parameter owns the array; xs[0] is an i32 (Copy), and the inferred return is an owned i32.

Actual (installed compiler and D114 stage1 alike):

error: returned view may outlive its origin 'xs'
 --> arrret.w:1:23

The inferred return type keeps the element view instead of materializing the Copy value at the return demand. Found while checking literal-argument shapes for D114 (#2327); not caused by it.

Workaround: annotate the return type (-> i32).

Activity

  1. ehartford commented on Oct 23, 2026

    @ehartford
    ContributorAuthor

    By design under D22. The D22 acceptance cells (promoted 2026-10-18) rule that an inferred return preserves a view: test/compile_errors/err_d22_inferred_return_preserves_view.w (fn find(map: &HashMap[i32, i32]): map.get(1).unwrap() infers &i32 and keeps its origin) and behav_d22_exact_carrier_contextual_copy.w ("inferred returns ... preserve references"). A view materializes its Copy value only where an owned demand is established, and an unannotated return establishes none; so fn arr(xs: [i32; 2]): xs[0] infers &i32 viewing the consumed xs, and the escape diagnostic is correct. The written -> i32 is the guardrail that names the copy. A fix that materialized it (branch issues-b) broke both cells and was reverted; the mixed join of a view return beside an owned return (#2325) is the owned value and is fixed there.

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