Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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 [email protected]

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>`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true. The type FutureOr<void> (which is a bad type anyway, but we still need to test it) does not get special treatment. In particular, FutureOr<void> isn't "a kind of void". (Similarly, List<void> doesn't get special treatment, so there is no error if we do things like List<Object?> xs = <void>['secret!'];.)

FutureOr<void> is a top type, though, so we can return anything we want when the return type of a non-asynchronous function is FutureOr<void> (except that expressions of type voidcan't be used unless the situation is on the allowlist, and the allowlist doesn't say anything aboutFutureOr`).

It looks like the analyzer emits warnings, but that's not part of the language definition, so we shouldn't interfere with that.

/// @author [email protected]

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);
}