Skip to content

Commit

Permalink
#2956. Add more tests for the type void (#2977)
Browse files Browse the repository at this point in the history
Add more tests for the type `void`
  • Loading branch information
sgrekhov authored Nov 14, 2024
1 parent 3a07e0e commit 886cdf1
Show file tree
Hide file tree
Showing 14 changed files with 488 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Language/Types/Type_Void/implicit_usage_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2024, 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 Finally, we need to address situations involving implicit usage
/// of an object whose static type can be `void`. It is a compile-time error for
/// a for-in statement to have an iterator expression of type `T` such that
/// `Iterator<void>` is the most specific instantiation of `Iterator` that is a
/// superinterface of `T`, unless the iteration variable has type `void`.
///
/// @description Checks that it is a compile-time error if in a for-in loop type
/// of the iterator expression is `Iterator<void>` and the type of the iteration
/// variable is not `void`.
/// @author sgrekhov22@gmail.com
/// @issue 57078
void main() {
List<void> list = <void>[1, 2, 3];
for (Object? i in list) {
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
for (dynamic i in list) {
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
}
28 changes: 28 additions & 0 deletions Language/Types/Type_Void/implicit_usage_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2024, 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 Finally, we need to address situations involving implicit usage
/// of an object whose static type can be `void`. It is a compile-time error for
/// a for-in statement to have an iterator expression of type `T` such that
/// `Iterator<void>` is the most specific instantiation of `Iterator` that is a
/// superinterface of `T`, unless the iteration variable has type `void`.
///
/// @description Checks that it is not an error if in a for-in loop type of the
/// iterator expression is `Iterator<void>` and the type of the iteration
/// variable is `void`.
/// @author sgrekhov22@gmail.com
import '../../../Utils/expect.dart';

void main() {
List<void> list = <void>[1, 2, 3];
int j = 0;
for (void i in list) {
Expect.equals(++j, i as int);
}
j = 0;
for (var i in list) {
Expect.equals(++j, i as int);
}
}
29 changes: 29 additions & 0 deletions Language/Types/Type_Void/implicit_usage_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2024, 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 Finally, we need to address situations involving implicit usage
/// of an object whose static type can be `void`. ... It is a compile-time error
/// for an asynchronous for-in statement to have a stream expression of type `T`
/// such that `Stream<void>` is the most specific instantiation of `Stream` that
/// is a superinterface of `T`, unless the iteration variable has type `void`.
///
/// @description Checks that it is a compile-time error if in an asynchronous
/// for-in loop type of the iterator expression is `Stream<void>` and the type
/// of the iteration variable is not `void`.
/// @author sgrekhov22@gmail.com
/// @issue 57078
void main() async {
Stream<void> stream = Stream<void>.fromIterable([1, 2, 3]);
await for (Object? i in stream) {
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
await for (dynamic i in stream) {
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
}
28 changes: 28 additions & 0 deletions Language/Types/Type_Void/implicit_usage_A02_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2024, 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 Finally, we need to address situations involving implicit usage
/// of an object whose static type can be `void`. ... It is a compile-time error
/// for an asynchronous for-in statement to have a stream expression of type `T`
/// such that `Stream<void>` is the most specific instantiation of `Stream` that
/// is a superinterface of `T`, unless the iteration variable has type `void`.
///
/// @description Checks that it is not an error if in an asynchronous for-in
/// loop type of the iterator expression is `Stream<void>` and the type of the
/// iteration variable is `void`.
/// @author sgrekhov22@gmail.com
import '../../../Utils/expect.dart';

void main() async {
Stream<void> stream = Stream<void>.fromIterable([1, 2, 3]);
int j = 0;
await for (void i in stream) {
Expect.equals(++j, i as int);
}
j = 0;
await for (var i in stream) {
Expect.equals(++j, i as int);
}
}
41 changes: 41 additions & 0 deletions Language/Types/Type_Void/permitted_use_A10_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2024, 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 In support of the notion that the value of an expression with
/// static type `void` should be discarded, such objects can only be used in
/// specific situations: The occurrence of an expression of type `void` is a
/// compile-time error unless it is permitted according to one of the following
/// rules.
/// ...
/// - An actual parameter expression corresponding to a formal parameter whose
/// statically known type annotation is `void` may have type `void`.
///
/// @description Checks that an actual parameter expression corresponding to a
/// formal parameter whose statically known type annotation is `void` may have
/// type `void`. Test a class.
/// @author sgrekhov22@gmail.com
import '../../../Utils/expect.dart';

Object? f<X>(X x) => x;

class C<X> {
final X x;
C(this.x);
Object? foo(X x) => x;
static Object? bar<T>(T t) => t;
}

void main() {
void v = 42;
Expect.equals(42, f(v));
Expect.equals(42, f<void>(v));

Expect.equals(42, C.bar(v));
Expect.equals(42, C.bar<void>(v));

Expect.equals(42, C<void>(v).x as int);
Expect.equals(42, C(v).foo(v));
Expect.equals(42, C<void>("").foo(v));
}
34 changes: 34 additions & 0 deletions Language/Types/Type_Void/permitted_use_A10_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2024, 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 In support of the notion that the value of an expression with
/// static type `void` should be discarded, such objects can only be used in
/// specific situations: The occurrence of an expression of type `void` is a
/// compile-time error unless it is permitted according to one of the following
/// rules.
/// ...
/// - An actual parameter expression corresponding to a formal parameter whose
/// statically known type annotation is `void` may have type `void`.
///
/// @description Checks that an actual parameter expression corresponding to a
/// formal parameter whose statically known type annotation is `void` may have
/// type `void`. Test a mixin.
/// @author sgrekhov22@gmail.com
import '../../../Utils/expect.dart';

mixin M<X> {
Object? foo(X x) => x;
static Object? bar<T>(T t) => t;
}

class MA = Object with M<void>;

void main() {
void v = 42;

Expect.equals(42, M.bar(v));
Expect.equals(42, M.bar<void>(v));
Expect.equals(42, MA().foo(v));
}
37 changes: 37 additions & 0 deletions Language/Types/Type_Void/permitted_use_A10_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2024, 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 In support of the notion that the value of an expression with
/// static type `void` should be discarded, such objects can only be used in
/// specific situations: The occurrence of an expression of type `void` is a
/// compile-time error unless it is permitted according to one of the following
/// rules.
/// ...
/// - An actual parameter expression corresponding to a formal parameter whose
/// statically known type annotation is `void` may have type `void`.
///
/// @description Checks that an actual parameter expression corresponding to a
/// formal parameter whose statically known type annotation is `void` may have
/// type `void`. Test an enum.
/// @author sgrekhov22@gmail.com
import '../../../Utils/expect.dart';

const void answer = 42;

enum E<X> {
e0<void>(answer);

final X x;
const E(this.x);
Object? foo(X x) => x;
static Object? bar<T>(T t) => t;
}

void main() {
void v = 0;
Expect.equals(42, E.e0.x as int);
Expect.equals(0, E.bar<void>(v));
Expect.equals(0, E.e0.foo(v));
}
33 changes: 33 additions & 0 deletions Language/Types/Type_Void/permitted_use_A10_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2024, 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 In support of the notion that the value of an expression with
/// static type `void` should be discarded, such objects can only be used in
/// specific situations: The occurrence of an expression of type `void` is a
/// compile-time error unless it is permitted according to one of the following
/// rules.
/// ...
/// - An actual parameter expression corresponding to a formal parameter whose
/// statically known type annotation is `void` may have type `void`.
///
/// @description Checks that an actual parameter expression corresponding to a
/// formal parameter whose statically known type annotation is `void` may have
/// type `void`. Test an extension.
/// @author sgrekhov22@gmail.com
import '../../../Utils/expect.dart';

class C<T> {
}

extension Ext<X> on C<X> {
Object? foo(X x) => x;
static Object? bar<T>(T t) => t;
}

void main() {
void v = 42;
Expect.equals(42, C<void>().foo(v));
Expect.equals(42, Ext.bar<void>(v));
}
31 changes: 31 additions & 0 deletions Language/Types/Type_Void/permitted_use_A10_t05.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2024, 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 In support of the notion that the value of an expression with
/// static type `void` should be discarded, such objects can only be used in
/// specific situations: The occurrence of an expression of type `void` is a
/// compile-time error unless it is permitted according to one of the following
/// rules.
/// ...
/// - An actual parameter expression corresponding to a formal parameter whose
/// statically known type annotation is `void` may have type `void`.
///
/// @description Checks that an actual parameter expression corresponding to a
/// formal parameter whose statically known type annotation is `void` may have
/// type `void`. Test an extension type.
/// @author sgrekhov22@gmail.com
import '../../../Utils/expect.dart';

extension type ET<X>(X x) {
Object? foo(X x) => x;
static Object? bar<T>(T t) => t;
}

void main() {
void v = 42;
Expect.equals(42, ET<void>(v).x as int);
Expect.equals(42, ET<void>(v).foo(v));
Expect.equals(42, ET.bar<void>(v));
}
35 changes: 35 additions & 0 deletions Language/Types/Type_Void/permitted_use_A11_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2024, 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 In support of the notion that the value of an expression with
/// static type `void` should be discarded, such objects can only be used in
/// specific situations: The occurrence of an expression of type `void` is a
/// compile-time error unless it is permitted according to one of the following
/// rules.
/// ...
/// - In an expression of the form `e1 = e2` where `e1` is an
/// `⟨assignableExpression⟩` denoting a variable or formal parameter of type
/// `void`, `e2` may have type `void`.
///
/// @description Checks that in an expression of the form `e1 = e2` where `e1`
/// is an `⟨assignableExpression⟩` denoting a variable of type `void`, `e2` may
/// have type `void`.
/// @author sgrekhov22@gmail.com
import '../../../Utils/expect.dart';

class C<T> {
late final T t;
C();
}

void main() {
void e1 = print(42);
e1 = print("x");

void e2 = 42;
C<void> c = C<void>();
c.t = e2;
Expect.equals(42, c.t as int);
}
30 changes: 30 additions & 0 deletions Language/Types/Type_Void/permitted_use_A11_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2024, 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 In support of the notion that the value of an expression with
/// static type `void` should be discarded, such objects can only be used in
/// specific situations: The occurrence of an expression of type `void` is a
/// compile-time error unless it is permitted according to one of the following
/// rules.
/// ...
/// - In an expression of the form `e1 = e2` where `e1` is an
/// `⟨assignableExpression⟩` denoting a variable or formal parameter of type
/// `void`, `e2` may have type `void`.
///
/// @description Checks that in an expression of the form `e1 = e2` where `e1`
/// is an `⟨assignableExpression⟩` denoting a formal parameter of type `void`,
/// `e2` may have type `void`.
/// @author sgrekhov22@gmail.com
import '../../../Utils/expect.dart';

void foo(void e1) {
void e2 = 42;
e1 = e2;
Expect.equals(42, e1 as int);
}

void main() {
foo(0);
}
Loading

0 comments on commit 886cdf1

Please sign in to comment.