Skip to content

Commit

Permalink
dart-lang#2139. Add generator functions element type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Jul 19, 2023
1 parent ed5f316 commit 311a09a
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Language/Functions/element_type_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 of a type `T` as follows:
/// If `T` is of the form `S?` or the form `FutureOr<S>` then the union-free
/// type of `T` is the union-free type of `S`. Otherwise, the union-free type
/// of `T` is T.
///
/// We define the element type of a generator function `f` as follows:
/// Let `S` be the union-free type of 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 element type of a synchronous generator function `f`
/// is `U`, where `S` is a union-free type of the declared return type of `f`
/// and `S` implements `Iterable<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();
}
57 changes: 57 additions & 0 deletions Language/Functions/element_type_A02_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 of a type `T` as follows:
/// If `T` is of the form `S?` or the form `FutureOr<S>` then the union-free
/// type of `T` is the union-free type of `S`. Otherwise, the union-free type
/// of `T` is T.
///
/// We define the element type of a generator function `f` as follows:
/// Let `S` be the union-free type of 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 element type of an asynchronous generator function
/// `f` is `U`, where `S` is a union-free type of the declared return type of
/// `f` and `S` implements `Stream<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();
}
54 changes: 54 additions & 0 deletions Language/Functions/element_type_A03_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 of a type `T` as follows:
/// If `T` is of the form `S?` or the form `FutureOr<S>` then the union-free
/// type of `T` is the union-free type of `S`. Otherwise, the union-free type
/// of `T` is T.
///
/// We define the element type of a generator function `f` as follows:
/// Let `S` be the union-free type of the declared return type of `f`.
/// ...
/// Otherwise, if `f` is a generator (synchronous or asynchronous) and `S` is a
/// supertype of `Object` then the element type of `f` is dynamic
///
/// @description Check that element type of a synchronous generator function `f`
/// is `dynamic`, if `S` is a union-free type of the declared return type of `f`
/// and `S` is a supertype of `Object`
/// @author [email protected]
import "dart:async";

Object? f1() sync* {
yield 1;
yield 3.14;
yield null;
yield "42";
}

Object f2() sync* {
yield 1;
yield null;
}

FutureOr<Object> f3() sync* {
yield 1;
yield null;
yield Future<Null>.value(null);
yield Future<num>.value(1);
}

FutureOr<Object?>? f4() sync* {
yield 1;
yield null;
yield Future<Null>.value(null);
yield Future<num>.value(1);
}

main() {
f1();
f2();
f3();
f4();
}
54 changes: 54 additions & 0 deletions Language/Functions/element_type_A03_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 of a type `T` as follows:
/// If `T` is of the form `S?` or the form `FutureOr<S>` then the union-free
/// type of `T` is the union-free type of `S`. Otherwise, the union-free type
/// of `T` is T.
///
/// We define the element type of a generator function `f` as follows:
/// Let `S` be the union-free type of the declared return type of `f`.
/// ...
/// Otherwise, if `f` is a generator (synchronous or asynchronous) and `S` is a
/// supertype of Object then the element type of `f` is dynamic
///
/// @description Check that element type of an asynchronous generator function
/// `f` is `dynamic`, if `S` is a union-free type of the declared return type of
/// `f` and `S` is a supertype of `Object`
/// @author [email protected]
import "dart:async";

Object? f1() sync* {
yield 1;
yield 3.14;
yield null;
yield "42";
}

Object f2() sync* {
yield 1;
yield null;
}

FutureOr<Object> f3() sync* {
yield 1;
yield null;
yield Future<Null>.value(null);
yield Future<num>.value(1);
}

FutureOr<Object?>? f4() sync* {
yield 1;
yield null;
yield Future<Null>.value(null);
yield Future<num>.value(1);
}

main() {
f1();
f2();
f3();
f4();
}

0 comments on commit 311a09a

Please sign in to comment.