E3043

E3043 — A for loop over a reference to a type with no method to iterate it by. The loop calls iter() for for x in &c and iter_mut() for for x in &mut c, as Rust's collections do, so the type needs the one the loop asks for.

What the compiler reports

Error[E3043] at {{.*}}for_loop_over_a_reference_without_iter.vx:24:13: a `for` loop over a reference to Bag calls its `iter()` method, and Bag has none
Error[E3043] at {{.*}}for_loop_over_a_reference_without_iter.vx:31:12: a `for` loop over a reference to Bag calls its `iter_mut()` method, and Bag has none

The fragments the test suite holds the compiler to. An ellipsis marks text the test does not constrain; the emitted message also carries a source location.

A program that triggers it

//
// Part of the Vx Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
// `for x in &b` calls `b.iter()`, and `Bag` has no such method, so the loop is refused.
// The same through a variable that holds the reference.

struct Bag {
  n : i64,
}

fn main() -> i32 {
  let mut b = Bag {
    n : 3i64
  };
  let mut s = 0i64;
  for x in &b {
    s = s + 1i64;
  }
  let mut c = Bag {
    n : 4i64
  };
  let r = &mut c;
  for x in r {
    s = s + 1i64;
  }
  return 0;
}

From tests/frontend/fail/for_loop_over_a_reference_without_iter.vx, which asserts this diagnostic on every commit.

Related