Skip to content

Commit

Permalink
dart-lang#2956. Add more type void tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Nov 25, 2024
1 parent 991e2f1 commit 9bde0f1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Language/Functions/Type_of_a_Function/return_type_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 If a function declaration does not declare a return type
/// explicitly, its return type is dynamic, unless it is a constructor, in which
/// case it is not considered to have a return type, or it is a setter or
/// operator []=, in which case its return type is void
///
/// @description Checks that if a setter or operator `[]=` declaration does not
/// declare a return type then the return type is `void` and it's a compile-time
/// error to return a value from this declaration.
/// @author sgrekhov22@gmail.com
set globalSetter(int _) {
return 0;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}

class C {
operator []=(int index, Object? value) {
return 0;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}
static set staticSetter(int _) {
return 0;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}
set instanceSetter(int _) {
return 0;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}
}

main() {
globalSetter = 1;
print(C);
}
34 changes: 34 additions & 0 deletions Language/Functions/function_body_short_syntax_A03_t04.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 A function body is either:
/// ...
/// - of the form `=> e` or the form `async => e`, which both return the value
/// of the expression `e` as if by a `return e`. ... Let `T` be the declared
/// return type of the function that has this body. It is a compile-time error
/// if one of the following conditions hold:
/// – The function is synchronous, `T` is not `void`, and it would have been a
/// compile-time error to declare the function with the body `{ return e; }`
/// rather than `=> e`.
///
/// @description Checks that it is not an error to declare a synchronous
/// function with a short syntax and the return type `FutureOr<void>`.
/// @author sgrekhov22@qmail.com
import 'dart:async';

FutureOr<void> f1() => 1;
FutureOr<void> f2() => null;
FutureOr<void> f3<T>(T t) => t;

FutureOr<void> f4() {
return 1;
}

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

0 comments on commit 9bde0f1

Please sign in to comment.