Issue #2621: A grammar action instantiated for two different V gets the conversions of the other instance (NK1343)

Open and answer this issue on GitHub.

ready · opened 2026-11-21 · updated 2026-11-21 · 0 comments

Found while building the TOML reader for the compiler (#2602), map fields. Area: grammar, lowering.

Probe (target/fast/nikaia run mm.nika, binary from develop at eef833583)

use std::collections
struct Doc { a: collections::BTreeMap[String, String], b: collections::BTreeMap[String, i64], }
grammar M {
	rule WS = "" -> { }
	entry rule read[T] -> T = v:Value[T] eof -> { v }
	rule KCh -> i64 = x:any if(x != '=' && x != '\n' && x != ':' && x != ';') -> { 1 }
	rule Ch -> i64 = x:any if(x != '"') -> { 1 }
	rule Key -> String = s:text(KCh+) ":" -> { s }
	rule Value[T] -> T = match T::kind {
		Text => "\"" s:text(Ch*) "\"" -> { s }
		I64 => d:dec[i64](digit+) -> { d }
		Map(K, V) => es:list(Entry[V], "\n") -> {
			let mut m: T = collections::BTreeMap()
			for (k, v) in es {
				m.insert(k.clone(), v.clone())
			}
			m
		}
		Struct(_) => m:members[T](Key, Value[_], ";") -> { m }
	}
	rule Entry[V] -> (String, V) = k:text(KCh+) "=" v:Value[V] -> { (k, v) }
}
fn main() throws {
	let d: Doc = M::read[Doc]("a:x=\"1\";b:y=2") catch {
		println(f"error: {error}")
		return
	}
	println(f"{d.a.len()} {d.b.len()}")
}

Prints (four errors, all on line 15, m.insert(k.clone(), v.clone())):

error[NK1343]: Internal compiler error.
    = note: Rustc output: the trait bound `str: nikaia_std::num::Value` is not satisfied.
error[NK1343]: Internal compiler error.
    = note: Rustc output: the trait bound `str: Copy` is not satisfied.
error[NK1343]: Internal compiler error.
    = note: Rustc output: type `i64` cannot be dereferenced.
error[NK1343]: Internal compiler error.
    = note: Rustc output: mismatched types.

With only one map field (either one) the same grammar reads it correctly; tests/compiler/tests/grammar_kind_map.nika only passes because it uses one grammar per value type.

Expected

Prints 1 1. ADR-grammar-typed-read-v1 D1: each call of a rule that takes types reads a copy of it “for the type the copy is for”, so the Map(K, V) arm for V = String and the one for V = i64 are two separate actions, each checked and lowered for its own V.

Cause (suspected)

grammar_types_put_block (crates/nikaia-std/src/compiler/grammar_types.nika:863) copies the action with block.clone(), and the copy keeps the original’s node ids. The checker’s facts are kept per node id, so the second copy’s facts (owned or view, Copy or not) overwrite the first copy’s, and the emitter lowers both copies with one set of facts. Settling check: give the copy fresh ids (node_id::NodeId::fresh()) and run the probe again.

To build

  1. Give every expression and statement in a copied action, guard and bound fresh node ids. One walk over the copy, in grammar_types.nika (beside grammar_types_walk_block), run from grammar_types_put_block.
  2. Add test tests/compiler/tests/grammar_kind_map_two_values.nika: the probe above, with .stdout 1 1, run under --both-settings.

Done when

The probe prints 1 1 under both settings, and grammar_kind_map still passes.

Files: grammar_types.nika, its lowered file, the new test. Unchanged: the members emission.

Back to Open Issues