...
Number: | GEP-8 |
Title: | Static Type Checking |
Version: | 78 |
Type: | Feature |
Status: | Draft |
Leader: | Cédric Champeau |
Created: | 2011-10-08 |
Last modification: | 20112012-1102-0921 |
Abstract: Static Type Checking
...
Implementation details
Development branch
The code is currently in heavy development, so make sure that you checkout the code from Git. The code is in Since Groovy 2.0-beta-2, code has been merged into master branch. However, if heavy developments are done on the type checker, it is advisable to work on the grumpy branch. It adds an AST transformation named TypeChecked. If set, then the AST transformation will perform type inference and store type information in AST nodes metadata. Eventually, if errors are found, it will add errors to the compiler through a dedicated addStaticTypeError method which basically does the same as the traditional addError method but prefixes the messages with a "Static type checking" message. This is done to help the developer determine whether the error he is seeing is a "plain Groovy" error, or an error thrown by the STC mode.
...
Feature | Example | Behaviour | Status | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
Method does not exist |
| Complains about undefined method | Implemented | |||||||
Property does not exist |
| Complains about undefined property "y" | Implemented | |||||||
Assignment type checking |
| Assigning a String to an int is forbidden | Implemented | |||||||
Incompatible binary expressions |
| Checks that arguments of a binary expression are compatible (here, no 'plus' method is available | Implemented | |||||||
Possible loss of precision (1/2) |
| Complains about possible loss of precision | Implemented | |||||||
Possible loss of precision (2/2) |
| Will not complain because '2' can be represented as an int | Implemented | |||||||
Arrays components |
| Cannot assign an int value in an array of type String[] | Implemented | |||||||
Method return type check |
| Ensures that assignments are compatible with method return type | Implemented | |||||||
Explicit return type checking |
| Ensures that returned value is compatible with declared return type | Implemented | |||||||
Implicit return type checking |
| Ensures that returned value is compatible with declared return type | Implemented | |||||||
Implicit toString() |
| Implicit call to toString() | Implemented | |||||||
Basic type inference |
| Method calls as well as property access are checked against inferred type | Implemented | |||||||
Basic flow analysis |
| Last method call will not complain because type of 'o' at this point can be inferred | Implemented | |||||||
Instance of |
| Casts should not be necessary when type can be inferred from a previous instanceof check | Implemented | |||||||
DefaultGroovyMethods support |
| Method calls can be resolved against Groovy extension methods | In progress (no type inference for closure arguments)Implemented | |||||||
with |
| Static type checking should be aware of the "with" structure | In progressImplemented | |||||||
Categories |
| Compiler should be aware that extension method is found in a category | N/A (support will be limited as category support is inherently dynamic) | |||||||
Groovy list constructor |
| Type checks the arguments and the number of arguments | Implemented | |||||||
Groovy map constructor |
| Type checks the properties and checks for wrong property names | Implemented | |||||||
Closure parameter types |
| Type checking the arguments when calling a closure | Implemented | |||||||
Closure return type inference |
| Closure return type can be inferred from block | Implemented | |||||||
Method return type inference |
| Return type can be inferred from a method if the method is itself annotated with @TypeChecked (or class is annotated with @TypeChecked) | Implemented | |||||||
Multiple assignments |
| In case of inline declaration, type check arguments. | Implemented | |||||||
Multiple assignments from a variable |
| In case of inline declaration, type check arguments. | Unsupported | |||||||
Generics |
| Type checking of generic parameters | Mostly implemented (some edge cases may fail)Implemented | |||||||
Spread operator |
| Type checking against component type | Implemented | |||||||
| Closure shared variables |
| Type check assignments of closure shared variables. The type checker is required to perform a two-pass verification, in order to check that method calls on a closure shared variables belong to the lowest upper bound of all assignment types. | Implemented |
Open discussions
Closure parameter type inference
...
Are not properly recognized. You have to explicitly set the type of the "it" parameter inside the closure. It is because the expected parameter types of closures are unknown at compile time. There is a discussion about how to add this type information to source code so that the inference engine can deal with them properly. The implementation of closure parameter type inference requires a change to the method signatures. It will probably not belong to the initial release of the type checker.
Unification Types
In cases of for example "x instanceof A || x instanceof B" with A and B being unrelated we could still make an artificial union kind of type, that contains everthing present in A and B, to allow those kinds of method calls. The alternative to this is to allow only methods from Object here, which is less interesintg. This typing can also be used for multicatch, ensuring that a method call is only valid if it exists on each of the exceptions for the multicatch. In the current implementation (Okt-14-2011) the multicatch is already expanded at the point @TypeChecked will check. Meaning effectively this already represents a kind of union type, as the same code is in each catch block and thus the method call would fail, if the method is not available on each type. The proposed behaviour is therefore to align the instanceof case with multicatch.
...
