From 00b64c07c6dd89ad9efbe0ea6ea3b098063febbc Mon Sep 17 00:00:00 2001 From: rune-scape Date: Sat, 27 Jul 2024 17:02:27 -0700 Subject: [PATCH] Added error messages to some compilation errors --- modules/gdscript/gdscript_compiler.cpp | 47 +++++++++++++++++++------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/modules/gdscript/gdscript_compiler.cpp b/modules/gdscript/gdscript_compiler.cpp index d8b44a558f67..fa8d544add1f 100644 --- a/modules/gdscript/gdscript_compiler.cpp +++ b/modules/gdscript/gdscript_compiler.cpp @@ -120,8 +120,8 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D #ifdef DEBUG_ENABLED if (unlikely(!GDScriptLanguage::get_singleton()->get_global_map().has(result.native_type))) { - ERR_PRINT(vformat(R"(GDScript bug: Native class "%s" not found.)", result.native_type)); - result.native_type = Object::get_class_static(); + _set_error(vformat(R"(GDScript bug (please report): Native class "%s" not found.)", result.native_type), nullptr); + return GDScriptDataType(); } #endif } break; @@ -161,6 +161,7 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D script = GDScriptCache::get_shallow_script(p_datatype.script_path, err, p_owner->path); if (err) { _set_error(vformat(R"(Could not find script "%s": %s)", p_datatype.script_path, error_names[err]), nullptr); + return GDScriptDataType(); } } @@ -193,7 +194,7 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D break; case GDScriptParser::DataType::RESOLVING: case GDScriptParser::DataType::UNRESOLVED: { - ERR_PRINT("Parser bug: converting unresolved type."); + _set_error("Parser bug (please report): converting unresolved type.", nullptr); return GDScriptDataType(); } } @@ -725,6 +726,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code return GDScriptCodeGenerator::Address(); } } else { + _set_error("Compiler bug (please report): incorrect callee type in call node!", call->callee); r_error = ERR_COMPILATION_FAILED; return GDScriptCodeGenerator::Address(); } @@ -1275,7 +1277,11 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code } } else { // Regular assignment. - ERR_FAIL_COND_V_MSG(assignment->assignee->type != GDScriptParser::Node::IDENTIFIER, GDScriptCodeGenerator::Address(), "Expected the assignee to be an identifier here."); + if (assignment->assignee->type != GDScriptParser::Node::IDENTIFIER) { + _set_error("Compiler bug (please report): Expected the assignee to be an identifier here.", assignment->assignee); + r_error = ERR_COMPILATION_FAILED; + return GDScriptCodeGenerator::Address(); + } GDScriptCodeGenerator::Address member; bool is_member = false; bool has_setter = false; @@ -1416,7 +1422,9 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code return result; } break; default: { - ERR_FAIL_V_MSG(GDScriptCodeGenerator::Address(), "Bug in bytecode compiler, unexpected node in parse tree while parsing expression."); // Unreachable code. + _set_error("Compiler bug (please report): Unexpected node in parse tree while parsing expression.", p_expression); // Unreachable code. + r_error = ERR_COMPILATION_FAILED; + return GDScriptCodeGenerator::Address(); } break; } } @@ -1853,7 +1861,10 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_match_pattern(CodeGen &c } return p_previous_test; } - ERR_FAIL_V_MSG(p_previous_test, "Reaching the end of pattern compilation without matching a pattern."); + + _set_error("Compiler bug (please report): Reaching the end of pattern compilation without matching a pattern.", p_pattern); + r_error = ERR_COMPILATION_FAILED; + return p_previous_test; } List GDScriptCompiler::_add_block_locals(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block) { @@ -2221,7 +2232,8 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Sui codegen.generator->pop_temporary(); } } else { - ERR_FAIL_V_MSG(ERR_INVALID_DATA, "Bug in bytecode compiler, unexpected node in parse tree while parsing statement."); // Unreachable code. + _set_error("Compiler bug (please report): unexpected node in parse tree while parsing statement.", s); // Unreachable code. + return ERR_INVALID_DATA; } } break; } @@ -2696,12 +2708,18 @@ Error GDScriptCompiler::_prepare_compilation(GDScript *p_script, const GDScriptP GDScriptDataType base_type = _gdtype_from_datatype(p_class->base_type, p_script, false); - ERR_FAIL_COND_V_MSG(base_type.native_type == StringName(), ERR_BUG, vformat(R"(Failed to get base class for "%s")", p_script->path)); + if (base_type.native_type == StringName()) { + _set_error(vformat(R"(Parser bug (please report): Empty native type in base class "%s")", p_script->path), p_class); + return ERR_BUG; + } int native_idx = GDScriptLanguage::get_singleton()->get_global_map()[base_type.native_type]; p_script->native = GDScriptLanguage::get_singleton()->get_global_array()[native_idx]; - ERR_FAIL_COND_V(p_script->native.is_null(), ERR_BUG); + if (p_script->native.is_null()) { + _set_error("Compiler bug (please report): script native type is null!", nullptr); + return ERR_BUG; + } // Inheritance switch (base_type.kind) { @@ -2711,7 +2729,8 @@ Error GDScriptCompiler::_prepare_compilation(GDScript *p_script, const GDScriptP case GDScriptDataType::GDSCRIPT: { Ref base = Ref(base_type.script_type); if (base.is_null()) { - return ERR_COMPILATION_FAILED; + _set_error("Compiler bug (please report): base script type is null!", nullptr); + return ERR_BUG; } if (main_script->has_class(base.ptr())) { @@ -2746,7 +2765,7 @@ Error GDScriptCompiler::_prepare_compilation(GDScript *p_script, const GDScriptP p_script->member_indices = base->member_indices; } break; default: { - _set_error("Parser bug: invalid inheritance.", nullptr); + _set_error("Parser bug (please report): invalid inheritance.", nullptr); return ERR_BUG; } break; } @@ -3259,7 +3278,11 @@ Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_scri GDScriptCache::add_static_script(p_script); } - return GDScriptCache::finish_compiling(main_script->path); + err = GDScriptCache::finish_compiling(main_script->path); + if (err) { + _set_error(R"(Failed to compile depended scripts.)", nullptr); + } + return err; } String GDScriptCompiler::get_error() const {