Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2139. Add generator functions element type tests #2153

Merged
merged 6 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
eernstg marked this conversation as resolved.
Show resolved Hide resolved
/// 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 it is a compile-time error if an element type of a
/// synchronous generator function `f` is not `U`, where `S` is a union-free
/// type of the declared return type of `f` and `S` implements `Iterable<U>`
eernstg marked this conversation as resolved.
Show resolved Hide resolved
/// @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();
}
49 changes: 49 additions & 0 deletions Language/Functions/element_type_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 it is a compile-time error if an element type of a
/// synchronous generator function `f` is not `U`, where `S` is a union-free
/// type of the declared return type of `f` and `S` implements `Iterable<U>`
eernstg marked this conversation as resolved.
Show resolved Hide resolved
/// @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();
}
30 changes: 30 additions & 0 deletions Language/Functions/element_type_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 a run-time type of a return value of a synchronous
/// generator function
/// @author [email protected]

import "dart:async";

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

main() {
FutureOr<Iterable<int>?> o = foo() as dynamic;
o as FutureOr<Iterable<int>?>;
eernstg marked this conversation as resolved.
Show resolved Hide resolved
}
41 changes: 41 additions & 0 deletions Language/Functions/element_type_A01_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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>`
eernstg marked this conversation as resolved.
Show resolved Hide resolved
/// @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>?>>>();
if (o is Future<Iterable<int>?>) {
eernstg marked this conversation as resolved.
Show resolved Hide resolved
print("No");
} else if (o == null) {
print("No");
} else {
o.expectStaticType<Exactly<Iterable<int>>>();
eernstg marked this conversation as resolved.
Show resolved Hide resolved
Expect.isTrue(o is Iterable<int>);
}
}
50 changes: 50 additions & 0 deletions Language/Functions/element_type_A01_t05.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 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 runtime type of an 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>`. Test the
/// run-time types of objects returned by a synchronous generator function
eernstg marked this conversation as resolved.
Show resolved Hide resolved
/// @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.iterableElementsRuntimeIsNot<num?>(f1());
eernstg marked this conversation as resolved.
Show resolved Hide resolved
Expect.iterableElementsRuntimeIsNot<int>(f1());
Expect.iterableElementsRuntimeIsNot<Object>(f1());
Expect.iterableElementsRuntimeIsNot<Object?>(f1());
Expect.iterableElementsRuntimeIsNot<dynamic>(f1());
Expect.iterableElementsRuntimeIs<int?>(f1());

Expect.iterableElementsRuntimeIsNot<num?>(f2());
Expect.iterableElementsRuntimeIsNot<num>(f2());
Expect.iterableElementsRuntimeIsNot<int?>(f2());
Expect.iterableElementsRuntimeIsNot<Object>(f2());
Expect.iterableElementsRuntimeIsNot<Object?>(f2());
Expect.iterableElementsRuntimeIsNot<dynamic>(f2());
Expect.iterableElementsRuntimeIs<int>(f2());
}
51 changes: 51 additions & 0 deletions Language/Functions/element_type_A01_t06.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 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 runtime type of an 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>`. Test the
/// run-time types of objects returned by a synchronous generator function
eernstg marked this conversation as resolved.
Show resolved Hide resolved
/// @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.iterableElementsRuntimeIsNot<num?>(f1());
eernstg marked this conversation as resolved.
Show resolved Hide resolved
Expect.iterableElementsRuntimeIsNot<int>(f1());
Expect.iterableElementsRuntimeIsNot<Object>(f1());
Expect.iterableElementsRuntimeIsNot<Object?>(f1());
Expect.iterableElementsRuntimeIsNot<dynamic>(f1());
Expect.iterableElementsRuntimeIs<int?>(f1());

Expect.iterableElementsRuntimeIsNot<num?>(f2());
Expect.iterableElementsRuntimeIsNot<num>(f2());
Expect.iterableElementsRuntimeIsNot<int?>(f2());
Expect.iterableElementsRuntimeIsNot<Object>(f2());
Expect.iterableElementsRuntimeIsNot<Object?>(f2());
Expect.iterableElementsRuntimeIsNot<dynamic>(f2());
Expect.iterableElementsRuntimeIs<int>(f2());
}
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 it is a compile-time error if an element type of a
/// synchronous generator function `f` is not `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();
}
50 changes: 50 additions & 0 deletions Language/Functions/element_type_A02_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 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 it is a compile-time error if an element type of an
/// asynchronous generator function `f` is not `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<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