Skip to content

Commit

Permalink
Fixes #2139. Add generator functions element type tests (#2153)
Browse files Browse the repository at this point in the history
Add generator functions element type tests
  • Loading branch information
sgrekhov authored Aug 10, 2023
1 parent d31a3b6 commit ccd6eae
Show file tree
Hide file tree
Showing 21 changed files with 819 additions and 8 deletions.
57 changes: 57 additions & 0 deletions Language/Functions/element_type_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 We define the union-free type derived from a type T as follows:
/// If T is of the form S? or the form FutureOr<S> then the union-free type
/// derived from T is the union-free type derived from S. Otherwise, the
/// union-free type derived from T is T
///
/// We define the element type of a generator function `f` as follows:
/// Let S be the union-free type derived from the declared return type of f. If
/// f is a synchronous generator and S implements Iterable<U> for some U then
/// the element type of f is U.
///
/// @description Check that if the declared return type of a synchronous
/// generator function `f` is `T`, and `S` is the union-free type derived from
/// `T`, and `S` is of the form `Iterable<U>`, it is a compile-time error to
/// yield an expression whose static type isn't assignable to `U`.
/// @author [email protected]
import "dart:async";

Iterable<num?>? f1() sync* {
yield 1;
yield 3.14;
yield null; // Ok, element type is `num?`
}

Iterable<num>? f2() sync* {
yield 1;
yield null;
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

FutureOr<Iterable<num>?> f3() sync* {
yield 1;
yield null;
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
yield Future<Null>.value(null);
// ^^^^^^^^^^^^^^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
yield Future<num>.value(1);
// ^^^^^^^^^^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
f1();
f2();
f3();
}
50 changes: 50 additions & 0 deletions Language/Functions/element_type_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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 We define the union-free type derived from a type T as follows:
/// If T is of the form S? or the form FutureOr<S> then the union-free type
/// derived from T is the union-free type derived from S. Otherwise, the
/// union-free type derived from T is T
///
/// We define the element type of a generator function `f` as follows:
/// Let S be the union-free type derived from the declared return type of f. If
/// f is a synchronous generator and S implements Iterable<U> for some U then
/// the element type of f is U.
///
/// @description Check that if the declared return type of a synchronous
/// generator function `f` is `T`, and `S` is the union-free type derived from
/// `T`, and `S` is of the form `Iterable<U>`, it is a compile-time error to
/// yield an expression whose static type isn't assignable to `U`.
/// @author [email protected]
import "dart:async";

Iterable<int>? f1() sync* {
yield 1;
yield 3.14;
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
yield "2";
// ^^^
// [analyzer] unspecified
// [cfe] unspecified
}

FutureOr<Iterable<int>?> f2() sync* {
yield 1;
yield 3.14;
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
yield "2";
// ^^^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
f1();
f2();
}
32 changes: 32 additions & 0 deletions Language/Functions/element_type_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 We define the union-free type derived from a type T as follows:
/// If T is of the form S? or the form FutureOr<S> then the union-free type
/// derived from T is the union-free type derived from S. Otherwise, the
/// union-free type derived from T is T
///
/// We define the element type of a generator function `f` as follows:
/// Let S be the union-free type derived from the declared return type of f. If
/// f is a synchronous generator and S implements Iterable<U> for some U then
/// the element type of f is U.
///
/// @description Check a run-time type of a return value of a synchronous
/// generator function
/// @author [email protected]
import "dart:async";
import "../../Utils/expect.dart";

FutureOr<Iterable<int>?> foo() sync* {
yield 1;
yield 2;
yield 3;
}

main() {
FutureOr<Iterable<int>?> o = foo() as dynamic;
o as FutureOr<Iterable<int>>;
Expect.isRuntimeTypeImplementsIterable<int>(o);
}
34 changes: 34 additions & 0 deletions Language/Functions/element_type_A01_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 We define the union-free type derived from a type T as follows:
/// If T is of the form S? or the form FutureOr<S> then the union-free type
/// derived from T is the union-free type derived from S. Otherwise, the
/// union-free type derived from T is T
///
/// We define the element type of a generator function `f` as follows:
/// Let S be the union-free type derived from the declared return type of f. If
/// f is a synchronous generator and S implements Iterable<U> for some U then
/// the element type of f is U.
///
/// @description Let `f` be a synchronous generator function whose declared
/// return type derives the union-free type `S`, and assume that `S` implements
/// `Iterable<U>`. Then check that the element type of `f` is `U`.
/// @author [email protected]
import "dart:async";
import "../../Utils/expect.dart";
import "../../Utils/static_type_helper.dart";

FutureOr<Iterable<int>?> foo() sync* {
yield 1;
yield 2;
yield 3;
}

main() {
var o = foo();
o.expectStaticType<Exactly<FutureOr<Iterable<int>?>>>();
Expect.isRuntimeTypeImplementsIterable<int>(o);
}
37 changes: 37 additions & 0 deletions Language/Functions/element_type_A01_t05.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 We define the union-free type derived from a type T as follows:
/// If T is of the form S? or the form FutureOr<S> then the union-free type
/// derived from T is the union-free type derived from S. Otherwise, the
/// union-free type derived from T is T
///
/// We define the element type of a generator function `f` as follows:
/// Let S be the union-free type derived from the declared return type of f. If
/// f is a synchronous generator and S implements Iterable<U> for some U then
/// the element type of f is U.
///
/// @description Let `f` be a synchronous generator function whose declared
/// return type derives the union-free type `S`, and assume that `S` implements
/// `Iterable<U>`. Then check that the element type of `f` is `U`.
/// @author [email protected]
import "../../Utils/expect.dart";

Iterable<int?>? f1() sync* {
yield 1;
yield 2;
yield null;
}

Iterable<int>? f2() sync* {
yield 1;
yield 2;
yield 3;
}

main() {
Expect.isRuntimeTypeImplementsIterable<int?>(f1());
Expect.isRuntimeTypeImplementsIterable<int>(f2());
}
38 changes: 38 additions & 0 deletions Language/Functions/element_type_A01_t06.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 We define the union-free type derived from a type T as follows:
/// If T is of the form S? or the form FutureOr<S> then the union-free type
/// derived from T is the union-free type derived from S. Otherwise, the
/// union-free type derived from T is T
///
/// We define the element type of a generator function `f` as follows:
/// Let S be the union-free type derived from the declared return type of f. If
/// f is a synchronous generator and S implements Iterable<U> for some U then
/// the element type of f is U.
///
/// @description Let `f` be a synchronous generator function whose declared
/// return type derives the union-free type `S`, and assume that `S` implements
/// `Iterable<U>`. Then check that the element type of `f` is `U`.
/// @author [email protected]
import "dart:async";
import "../../Utils/expect.dart";

FutureOr<Iterable<int?>?> f1() sync* {
yield 1;
yield 2;
yield null;
}

FutureOr<Iterable<int>?>? f2() sync* {
yield 1;
yield 2;
yield 3;
}

main() {
Expect.isRuntimeTypeImplementsIterable<int?>(f1());
Expect.isRuntimeTypeImplementsIterable<int>(f2());
}
58 changes: 58 additions & 0 deletions Language/Functions/element_type_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 We define the union-free type derived from a type T as follows:
/// If T is of the form S? or the form FutureOr<S> then the union-free type
/// derived from T is the union-free type derived from S. Otherwise, the
/// union-free type derived from T is T
///
/// We define the element type of a generator function `f` as follows:
/// Let S be the union-free type derived from the declared return type of f.
/// ...
/// If `f` is an asynchronous generator and `S` implements `Stream<U>` for some
/// `U` then the element type of `f` is `U`.
///
/// @description Check that if the declared return type of an asynchronous
/// generator function `f` is `T`, and `S` is the union-free type derived from
/// `T`, and `S` is of the form `Stream<U>`, it is a compile-time error to
/// yield an expression whose static type isn't assignable to `U`.
/// @author [email protected]
import "dart:async";

Stream<num?>? f1() async* {
yield 1;
yield 3.14;
yield null; // Ok, element type is `num?`
}

Stream<num>? f2() async* {
yield 1;
yield null;
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

FutureOr<Stream<num>?> f3() async* {
yield 1;
yield null;
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
yield Future<Null>.value(null);
// ^^^^^^^^^^^^^^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
yield Future<num>.value(1);
// ^^^^^^^^^^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
f1();
f2();
f3();
}
51 changes: 51 additions & 0 deletions Language/Functions/element_type_A02_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 We define the union-free type derived from a type T as follows:
/// If T is of the form S? or the form FutureOr<S> then the union-free type
/// derived from T is the union-free type derived from S. Otherwise, the
/// union-free type derived from T is T
///
/// We define the element type of a generator function `f` as follows:
/// Let S be the union-free type derived from the declared return type of f.
/// ...
/// If `f` is an asynchronous generator and `S` implements `Stream<U>` for some
/// `U` then the element type of `f` is `U`.
///
/// @description Check that if the declared return type of an asynchronous
/// generator function `f` is `T`, and `S` is the union-free type derived from
/// `T`, and `S` is of the form `Stream<U>`, it is a compile-time error to
/// yield an expression whose static type isn't assignable to `U`.
/// @author [email protected]
import "dart:async";

Stream<int>? f1() async* {
yield 1;
yield 3.14;
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
yield "2";
// ^^^
// [analyzer] unspecified
// [cfe] unspecified
}

FutureOr<Stream<int>?> f2() async* {
yield 1;
yield 3.14;
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
yield "2";
// ^^^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
f1();
f2();
}
Loading

0 comments on commit ccd6eae

Please sign in to comment.