Issue #2845: A tuple read through an optional chain m.get(k)?.get(0) is handed back as a reference, not its value (NK1343)

Open and answer this issue on GitHub.

ready, bugs, queued · opened 2026-11-28 · updated 2026-11-29 · 0 comments

Found while “A held view is checked against the buffer the plan names” (#2842), by its builder.

Probe

On develop 65e66b1a (pushed), target/fast/nikaia built from it, outside the shared checkout:

use std::collections

fn first(places: collections::HashMap[String, Vec[(i64, bool)]], k: String) -> (i64, bool)? {
	return places.get(k)?.get(0)
}

fn main() throws {
	let mut m: collections::HashMap[String, Vec[(i64, bool)]] = collections::HashMap()
	m.insert("a", [(1, true)])
	let r = first(m, "a")
	println(r?.0 ?? 0)
}

nikaia run q2.nika

Expected: prints 1: the function returns a (i64, bool)?, and an element of a tuple of numbers is handed back as its value (Part I 6.5; a tuple of Copy parts is copied, as r?.0 reads it). Actual: error[NK1343]: Internal compiler error., Rustc output: “mismatched types.”

Cause

Suspected: places.get(k)?.get(0) lowers to Option<&(i64, bool)> and the return writes no .copied() / .cloned() for a declared (i64, bool)?. Not traced; start at the lowering of ?. chains (nikaia run scripts/where.nika -- emit_core_reads). To check: the same returning i64? from Vec[i64], which may already work.

Workaround to remove

crates/nikaia-std/src/compiler/emit_stmt.nika (return plan.places.get((at, bound.clone()))?.get(0)?.clone(), written by #2842’s run). The fix removes the .clone() there.

To build

  1. Rerun the probe on today’s develop.
  2. Hand back an element of a tuple of Copy parts read through ?. as its value. Test: tests/compiler/tests/a_tuple_read_through_an_optional_chain_is_handed_back.nika, the probe, .stdout 1. Remove the workaround above in the same commit.

Done when

The test prints 1 and failed before the fix, the workaround is gone; that commit carries Fixes #N. A Found: line per finding.

Files and limits

The ?. chain’s emitter, emit_stmt.nika (the workaround), their lowered files.

Facts: Lowering and lower-std

Back to Open Issues