dart-lang/sdk

Support generalized void

Closed this issue · 3 comments

The reserved word void denotes a type, but it has received special treatment in Dart 1.x: it was allowed only as a function return type in the grammar. void will now be allowed in several other locations, e.g., as a type annotation and as an actual type argument in a generic type or an invocation of a generic function. Note that it is still not allowed to use void in certain other contexts; for instance, it can not be used in implements void, with void, nor int foo<T extends void>(T t) {..};.

Note that the generalized void feature contains two phases: In the first phase, it is allowed to use void in many locations where it is currently not allowed, but direct usage of the value of a void expression is prohibited in most situations. That is, when an expression of type void is evaluated, the result must generally be discarded.

In the second phase, support for detecting that an implicit cast can cause the static analysis of a certain expression to "forget" voidness is added. This issue is only concerned with the first phase.

For more details, please consult the feature specification (which is also only concerned with the first phase).

Tracking issues

For many tools, the common front end Fasta will provide support for most or all of this feature. Hence, support in Fasta is a core step in the implementation of this feature. Tool teams are therefore encouraged to coordinate the efforts with the work on Fasta.

Note that an important test for this feature has been adjusted in this commit: void_type_usage_test.dart. It used to mark some statements of the form return e; as compile-time errors. By mistake, these statements were actually OK: e has static type void and the enclosing function had return type void, and that combination is allowed.

The commit fixes this problem: A new function testReturnToVoid has been added to test the situation where "return voidThing" is allowed, and all other test... functions have had their return type changed such that their expectation that it is a compile-time error is now correct.

Support has landed in Fasta with 9bdf248 and dart2js will get it for free. All that's left is to update the language specification.

The rules about generalized void have been added to the language specification a long time ago.