diff --git a/src/program.cc b/src/program.cc index 8b7b4bc136..269c738ce7 100644 --- a/src/program.cc +++ b/src/program.cc @@ -47,6 +47,7 @@ Program::Program(ProgramBuilder&& builder) { TypeDeterminer td(&builder); if (!td.Determine()) { diagnostics_.add_error(td.error()); + is_valid_ = false; } } @@ -60,7 +61,7 @@ Program::Program(ProgramBuilder&& builder) { builder.AST().GlobalVariables()); sem_ = std::move(builder.Sem()); symbols_ = std::move(builder.Symbols()); - diagnostics_ = std::move(builder.Diagnostics()); + diagnostics_.add(std::move(builder.Diagnostics())); builder.MarkAsMoved(); if (!is_valid_ && !diagnostics_.contains_errors()) { diff --git a/src/program_test.cc b/src/program_test.cc index 75dde4925f..e958c1b56c 100644 --- a/src/program_test.cc +++ b/src/program_test.cc @@ -19,6 +19,7 @@ #include "gmock/gmock.h" #include "src/ast/function.h" +#include "src/ast/return_statement.h" #include "src/ast/test_helper.h" #include "src/ast/variable.h" #include "src/type/alias_type.h" @@ -126,6 +127,18 @@ TEST_F(ProgramTest, IsValid_Invalid_Function) { EXPECT_FALSE(program.IsValid()); } +TEST_F(ProgramTest, IsValid_Invalid_UnknownVar) { + Func("main", ast::VariableList{}, nullptr, + ast::StatementList{ + create(Expr("unknown_ident")), + }, + ast::FunctionDecorationList{}); + + Program program(std::move(*this)); + EXPECT_FALSE(program.IsValid()); + EXPECT_NE(program.Diagnostics().count(), 0u); +} + TEST_F(ProgramTest, IsValid_GeneratesError) { AST().AddGlobalVariable(nullptr);