-
Notifications
You must be signed in to change notification settings - Fork 29
#2956. Add more type void tests
#2997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
Language/Functions/Type_of_a_Function/return_type_t04.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
34
Language/Functions/function_body_short_syntax_A03_t04.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 [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); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 likeList<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 isFutureOr<void> (except that expressions of typevoidcan'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.