Skip to content

let s: &T = x moves x instead of binding a view (typed-let borrow over an ident) #1244

Closed

Description

@ehartford

Spec §3.8 / D22: an annotation demands what it says, and &T is a borrow. The checker marks the initializer ident MOVED regardless of a reference annotation (SemaCheck.w let path: mark_moved_if_consumed(value) runs unless field_let_binds_as_view matches, which only covers field accesses), and MirLower lowers it as a plain use.

use std.fs
fn main:
    let x = read_file("m.w").unwrap()
    let s: &str = x
    print(f"{s.len()} {x.len()}")      // error: use of moved value

Same for let s: &Vec[str] = x. Over a global it is worse: let s: &str = g inside a function yields invalid MIR before codegen: use rvalue type is incompatible with assign destination, and the move mark on the global leaks into every later function body (#1242).

Expected: s is a view of x's place; x stays live; no move, no copy.

Found while fixing #1242; fix follows in its own ownership batch.

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