blob: 2cbe72aecd9c8cf3e835d4035951309545e10412 [file] [log] [blame]
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// @assertion
/// A collection element of the form:
///
/// for (<keyword> <pattern> in <expression>) <element>
///
/// Where <keyword> is var or final is treated like so:
/// 1. Let I be the static type of <expression>, inferred using context type
/// schema Iterable<P> where P is the context type schema of <pattern>.
/// 2. Calculate the element type of I:
/// i. If I implements Iterable<T> for some T then E is T.
/// ii. Else if I is dynamic then E is dynamic.
/// iii. Else it is a compile-time error.
/// 3. Type check <pattern> with matched value type E.
/// 4. If there are no compile-time errors, then execution proceeds as the
/// following code, where id1 and id2 are fresh identifiers and append() is
/// an operation to add an element to the surrounding collection being built:
/// ```dart
/// I id1 = <expression>;
/// Iterator<E> id2 = id1.iterator;
/// while (id2.moveNext()) {
/// <keyword> <pattern> = id2.current;
/// append(<element>);
/// }
/// ```
/// @description Checks that if `I` is `dynamic` and runtime type of `I` is
/// `Iterable<T>` where `T` is not assignable to `<pattern>` required type then
/// a run-time error occurs. Test a map literal
/// @author sgrekhov22@gmail.com
import "../../Utils/expect.dart";
import "patterns_lib.dart";
main() {
Expect.throws(() {
var m = {
for (var (int v1) in <num>[1, 2, 3.14] as dynamic) "k$v1": v1
};
});
Expect.throws(() {
var m = {
for (final <int>[v2] in <List<num>>[[1], [2], [3]] as dynamic) "k$v2": v2
};
});
Expect.throws(() {
var m = {
for (var <String, int>{"k1": v3} in
<Map<String, num>>[{"k1": 1}] as dynamic) "k$v3": v3
};
});
Expect.throws(() {
var m = {
for (final (int v4,) in <(num,)>[(1.1,)] as dynamic) "k$v4": v4
};
});
Expect.throws(() {
var m = {
for (var (n: int v5) in <({num n})>[(n: 2.1)] as dynamic) "k$v5": v5
};
});
Expect.throws(() {
var m = {
for (var Square<Centimeter>(area: v6) in [Circle(1)] as dynamic)
"k$v6": v6
};
});
Expect.throws(() {
var m = {
for (final Square<Meter>(area: v7) in
<Square<Centimeter>>[Square<Centimeter>(1)] as dynamic) "k$v7": v7
};
});
}