checkForEachBinding static method
The value to bind to a for-each loop variable for one iteration, checked against the variable's written type.
SCD63: a typed for-each variable used to bind whatever the iterable
produced — for (final int x in [1, 'two', 3]) ran its body with a String
in a variable its own declaration rules out, and the body then computed a
wrong value rather than failing (x + 1 concatenated). Real Dart raises
TypeError on the offending element, after the earlier iterations have
run, which is what this reproduces.
Every for-each execution path calls this or resolveForEachBinding: the
visitor's three (statement, collection-literal element, await-for item
list) and the async state machine's and sync generator's own copies in
this file. They are separate implementations of one construct, so a check
added to any subset of them would hold only for the loops that happen to
run on that path — and whether a given loop runs on the state machine's
path depends on nothing more visible than whether its enclosing function
is async.
Implementation
static Object? checkForEachBinding(
Environment env,
SAstNode? loopVariable,
Object? element,
) {
final binding = resolveForEachBinding(env, loopVariable);
return binding == null ? element : binding.bind(env, element);
}