Skip to content

Commit 238d3d4

Browse files
authored
#2956. Add more type void tests (#2997)
1 parent 991e2f1 commit 238d3d4

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion If a function declaration does not declare a return type
6+
/// explicitly, its return type is dynamic, unless it is a constructor, in which
7+
/// case it is not considered to have a return type, or it is a setter or
8+
/// operator []=, in which case its return type is void
9+
///
10+
/// @description Checks that if a setter or operator `[]=` declaration does not
11+
/// declare a return type then the return type is `void` and it's a compile-time
12+
/// error to return a value from this declaration.
13+
/// @author sgrekhov22@gmail.com
14+
15+
set globalSetter(int _) {
16+
return 0;
17+
// ^
18+
// [analyzer] unspecified
19+
// [cfe] unspecified
20+
}
21+
22+
class C {
23+
operator []=(int index, Object? value) {
24+
return 0;
25+
// ^
26+
// [analyzer] unspecified
27+
// [cfe] unspecified
28+
}
29+
static set staticSetter(int _) {
30+
return 0;
31+
// ^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
}
35+
set instanceSetter(int _) {
36+
return 0;
37+
// ^
38+
// [analyzer] unspecified
39+
// [cfe] unspecified
40+
}
41+
}
42+
43+
main() {
44+
globalSetter = 1;
45+
print(C);
46+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion A function body is either:
6+
/// ...
7+
/// - of the form `=> e` or the form `async => e`, which both return the value
8+
/// of the expression `e` as if by a `return e`. ... Let `T` be the declared
9+
/// return type of the function that has this body. It is a compile-time error
10+
/// if one of the following conditions hold:
11+
/// – The function is synchronous, `T` is not `void`, and it would have been a
12+
/// compile-time error to declare the function with the body `{ return e; }`
13+
/// rather than `=> e`.
14+
///
15+
/// @description Checks that it is not an error to declare a synchronous
16+
/// function with a short syntax and the return type `FutureOr<void>`.
17+
/// @author sgrekhov22@qmail.com
18+
19+
import 'dart:async';
20+
21+
FutureOr<void> f1() => 1;
22+
FutureOr<void> f2() => null;
23+
FutureOr<void> f3<T>(T t) => t;
24+
25+
FutureOr<void> f4() {
26+
return 1;
27+
}
28+
29+
main() {
30+
print(f1);
31+
print(f2);
32+
print(f3);
33+
print(f4);
34+
}

0 commit comments

Comments
 (0)