Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/parser/cxx/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3790,6 +3790,8 @@ auto Parser::parse_return_statement(StatementAST*& yyast) -> bool {
parse_expr_or_braced_init_list(ast->expression, ExprContext{});

expect(TokenKind::T_SEMICOLON, ast->semicolonLoc);

check(ast);
}

return true;
Expand Down Expand Up @@ -9808,6 +9810,17 @@ void Parser::check(ExpressionAST* ast) {
check(ast);
}

void Parser::check(StatementAST* ast) {
if (binder_.inTemplate()) return;
auto returnStatement = ast_cast<ReturnStatementAST>(ast);
if (!returnStatement) return;

TypeChecker check{unit};
check.setScope(scope());
check.setReportErrors(config().checkTypes);
check.checkReturnStatement(returnStatement);
}

auto Parser::getFunction(Scope* scope, const Name* name, const Type* type)
-> FunctionSymbol* {
auto parentScope = scope;
Expand Down
1 change: 1 addition & 0 deletions src/parser/cxx/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ class Parser final {
void setScope(ScopedSymbol* symbol);

void check(ExpressionAST* ast);
void check(StatementAST* ast);

// lookup

Expand Down
24 changes: 24 additions & 0 deletions src/parser/cxx/type_checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2262,4 +2262,28 @@ auto TypeChecker::Visitor::check_pseudo_destructor_access(
return true;
}

void TypeChecker::checkReturnStatement(ReturnStatementAST* ast) {
const Type* targetType = nullptr;
for (auto current = scope_; current; current = current->parent()) {
if (auto function = symbol_cast<FunctionSymbol>(current->owner())) {
if (auto functionType = type_cast<FunctionType>(function->type())) {
targetType = functionType->returnType();
}
break;
}

if (auto lambda = symbol_cast<LambdaSymbol>(current->owner())) {
if (auto functionType = type_cast<FunctionType>(lambda->type())) {
targetType = functionType->returnType();
}
break;
}
}

if (!targetType) return;

Visitor visitor{*this};
(void)visitor.implicit_conversion(ast->expression, targetType);
}

} // namespace cxx
2 changes: 2 additions & 0 deletions src/parser/cxx/type_checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class TypeChecker {

void check(ExpressionAST* ast);

void checkReturnStatement(ReturnStatementAST* ast);

private:
struct Visitor;

Expand Down