Issue #2875: x?.clone() on a local holding an optional reference lowers to a reference, and rustc refuses it (NK1343)

Open and answer this issue on GitHub.

bug, ready · opened 2026-11-28 · updated 2026-11-28 · 0 comments

Found while “Every held view the plan names a buffer for is checked against it” (#2848), builder’s report, at 2258b9f0.

Probe

use std::collections

fn pick(places: ref collections::BTreeMap[i64, (i64, bool)]?, i: i64) -> (i64, bool)? {
    let named = places?.get(i)
    return named?.clone()
}

fn main() {
    let mut m: collections::BTreeMap[i64, (i64, bool)] = collections::BTreeMap()
    m.insert(3, (3, true))
    match pick(m, 3) {
        Some(p) => println(f"{p.0}")
        None => println("none")
    }
}

nikaia run gives NK1343 (rustc: “expected tuple, found &(_, _)”) instead of printing 3. The lowered code is match named.as_ref() { Some(x) => Some(x.clone()) }: the .as_ref() turns the clone into a clone of the reference. The one-chain form places?.get(i)?.clone() lowers correctly.

Governing decision

.clone() hands back a value of its own (Part I, copies); the checker accepts the probe, so this is plainly meant.

Cause

Untraced. Suspected in the lowering of ?. on a local whose type is an optional reference (the .as_ref() it adds), in the emit_core_*.nika files.

To build

  1. ?.clone() on such a local lowers to a clone of the value behind the reference. Test: tests/compiler/tests/a_clone_through_an_optional_reference_is_a_value.nika, .stdout (3).

Done when

The probe prints 3, and the commit carries Fixes #N.

Files and limits

The emitter’s .nika files and their lowered files; no Rust by hand.

Facts: Lowering and lower-std

Back to Open Issues