Issue #3017: let mut x = mutparam ?? return … lowers to a shared borrow (NK1343)

Open and answer this issue on GitHub.

ready · opened 2026-12-01 · updated 2026-12-01 · 0 comments

Found: a let mut bound from a mut optional parameter by ?? return is lowered as a shared reference. Mutating a field of it is refused by rustc (NK1343, internal compiler error), and the checker also warns wrongly that the mut is not needed.

Probe:

use std::collections

struct Book { at: collections::BTreeSet[String] }

fn f2(mut b: Book?, go: bool) -> i64 {
	let mut book = b ?? return 0
	if go {
		book.at.insert("x")
	}
	return 2
}

fn main() throws {
	let mut b: Book? = Book { at: collections::BTreeSet() }
	println(f"{f2(b, true)}")
}

Output:

warning: Variable does not need to be mutable.   (a.nika:13: let mut book = b ?? return 0)
error[NK1343]: Internal compiler error.          (a.nika:15: book.at.insert("x"))
  Rustc output: cannot borrow `book.at` as mutable, as it is behind a `&` reference.

The same body with mut book: Book as the parameter (no optional, no ??) works. It is the same in origin/develop’s binary and in develop’s, so it is not a regression.

Expected: prints 2, with no warning.

To build:

  1. Find where the binding of let mut x = opt ?? return … takes its mutability and ownership from the optional parameter. The lowering emits a & binding (or an as_ref()), where mut on the binding should give an owned value or a &mut.
  2. Fix the mut-needed analysis so that a field mutation through the binding counts as a use of mut.
  3. Add a test in tests/compiler/tests/ with the probe and its .stdout.

Done when: the probe prints 2 with no warning, and the test is in the suite.


Generated by Claude Code

Back to Open Issues