Factor out AST node for function return types. (#912)

This enables us to stop treating the return type as a Pattern (which is really isn't), treat return types more consistently with other static types in the typechecker, and drop ReturnTypeContext.

Additional changes:
- Merge TypeCheckFunDef with TypeOfFunDef.
- Handle implicit conversions in `return` statements.
- Require function type literals to have an explicit `->`.
- Move consistency check for omitted returns from TypeChecker to ResolveControlFlow.
This commit is contained in:
Geoff Romer
2021-11-12 11:30:31 -08:00
committed by GitHub
parent a4aff26821
commit d854fb93cb
11 changed files with 255 additions and 229 deletions
+3 -2
View File
@@ -198,9 +198,10 @@ class Return : public Statement {
// structure of the AST: the return value is not a child of this node,
// but an ancestor.
auto function() const -> const FunctionDeclaration& { return **function_; }
auto function() -> FunctionDeclaration& { return **function_; }
// Can only be called once, by ResolveControlFlow.
void set_function(Nonnull<const FunctionDeclaration*> function) {
void set_function(Nonnull<FunctionDeclaration*> function) {
CHECK(!function_.has_value());
function_ = function;
}
@@ -208,7 +209,7 @@ class Return : public Statement {
private:
Nonnull<Expression*> expression_;
bool is_omitted_expression_;
std::optional<Nonnull<const FunctionDeclaration*>> function_;
std::optional<Nonnull<FunctionDeclaration*>> function_;
};
class While : public Statement {