Issue #907: Lowering: [..].contains(s.trim()) on a list of literals is accepted by the checker, rustc refuses it (Mismatched types)

Open and answer this issue on GitHub.

ready · opened 2026-10-28 · updated 2026-11-03 · 2 comments

Found in rs2nika R242 (67dfd5a).

Probe

fn main() {
    let t: String = " u8 "
    println(f"{["u8", "i64"].contains(t.trim())}")
}

nikaia check accepts it, nikaia run fails in rustc: error: Mismatched types. Expected: true.

To build

  1. A test tests/compiler/tests/contains_trimmed_view.nika with the probe and true as its .stdout.
  2. Lower the argument of contains on a list of literals so that a view (trim()) meets the element type (&str against &&str, or the list lowered as [&str] with the argument borrowed accordingly).

Generated by Claude Code

Comments

keywan-ghadami, 2026-10-29

The same happens with any call as the argument, not only .trim():

fn word(n: i64) -> String {
    return f"w{n}"
}

fn main() {
    let w = word(1)
    println(f"{["w1", "w2"].contains(w)}")        // fine
    println(f"{["w1", "w2"].contains(word(2))}")  // rustc: Mismatched types, expected `&&str`, found `&String`
}

Met in the converter (tr_expr.nika, ["filter_map", ..].contains(self.src(..))).


Generated by Claude Code

keywan-ghadami, 2026-11-03

A second probe, likely the same root (the argument of contains is not borrowed when it is an expression rather than a name), this time on numbers. Found while probing the forms a CDCL solver needs (neg = lit ^ 1); target/fast/nikaia at 31e2ea7:

fn main() {
    let xs: Vec[u32] = [4, 5]
    let lit: u32 = 4
    println(f"{xs.contains(lit ^ 1)} {xs.contains(lit + 1)}")
}

nikaia check accepts it; nikaia run fails in rustc twice: Mismatched types … Consider borrowing here. Expected: true true. With let neg = lit ^ 1 first, xs.contains(neg) works.

Addition to To build: a test with this probe and true true as its .stdout; lower every non-place argument of contains (an arithmetic expression, a call) as a borrow of a temporary (xs.contains(&(lit ^ 1))), not only the trimmed view of the first probe.


Generated by Claude Code

Back to Open Issues