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
+14 -4
View File
@@ -45,6 +45,19 @@ void Declaration::Print(llvm::raw_ostream& out) const {
}
}
void ReturnTerm::Print(llvm::raw_ostream& out) const {
switch (kind_) {
case ReturnKind::Omitted:
return;
case ReturnKind::Auto:
out << "-> auto";
return;
case ReturnKind::Expression:
out << "-> " << **type_expression_;
return;
}
}
void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
out << "fn " << name_ << " ";
if (!deduced_parameters_.empty()) {
@@ -60,10 +73,7 @@ void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
}
out << "]";
}
out << *param_pattern_;
if (!is_omitted_return_type_) {
out << " -> " << *return_type_;
}
out << *param_pattern_ << return_term_;
if (body_) {
out << " {\n";
(*body_)->PrintDepth(depth, out);