From 9bde0f14e26fc9fba0cca80000b7900f089a4d7f Mon Sep 17 00:00:00 2001 From: sgrekhov Date: Mon, 25 Nov 2024 15:17:58 +0200 Subject: [PATCH] #2956. Add more type `void` tests --- .../Type_of_a_Function/return_type_t04.dart | 46 +++++++++++++++++++ .../function_body_short_syntax_A03_t04.dart | 34 ++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 Language/Functions/Type_of_a_Function/return_type_t04.dart create mode 100644 Language/Functions/function_body_short_syntax_A03_t04.dart diff --git a/Language/Functions/Type_of_a_Function/return_type_t04.dart b/Language/Functions/Type_of_a_Function/return_type_t04.dart new file mode 100644 index 0000000000..18d66bccee --- /dev/null +++ b/Language/Functions/Type_of_a_Function/return_type_t04.dart @@ -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); +} diff --git a/Language/Functions/function_body_short_syntax_A03_t04.dart b/Language/Functions/function_body_short_syntax_A03_t04.dart new file mode 100644 index 0000000000..b09604b5c2 --- /dev/null +++ b/Language/Functions/function_body_short_syntax_A03_t04.dart @@ -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`. +/// @author sgrekhov22@qmail.com + +import 'dart:async'; + +FutureOr f1() => 1; +FutureOr f2() => null; +FutureOr f3(T t) => t; + +FutureOr f4() { + return 1; +} + +main() { + print(f1); + print(f2); + print(f3); + print(f4); +}