From ee002b4773c3b4e31d3ae849f7b4d61ab20a9370 Mon Sep 17 00:00:00 2001 From: Daniel Kofanov Date: Wed, 25 Jan 2023 14:26:22 +0300 Subject: [PATCH] Fix CheckDirectEval For each 'continue' triggered within the loops there will be a default-initialized element in the 'literals' vector, which is incorrect. Instead, space for vector should be reserved and elements should be emplaced one-by-one. Signed-off-by: Daniel Kofanov --- binder/scope.cpp | 17 +++++------------ compiler/core/emitter.cpp | 1 + 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/binder/scope.cpp b/binder/scope.cpp index 973869c45..5e2fe718d 100644 --- a/binder/scope.cpp +++ b/binder/scope.cpp @@ -256,7 +256,8 @@ void VariableScope::CheckDirectEval(compiler::CompilerContext *compilerCtx) } } - std::vector literals(LexicalSlots() + constBindings); + std::vector literals; + literals.reserve(LexicalSlots() + constBindings); if (constBindings == 0U) { for (const auto &[name, variable] : bindings_) { @@ -264,26 +265,18 @@ void VariableScope::CheckDirectEval(compiler::CompilerContext *compilerCtx) continue; } - literals[variable->AsLocalVariable()->LexIdx()] = compiler::Literal(name); + literals.emplace_back(compiler::Literal(name)); } } else { - std::vector bindings(LexicalSlots()); - for (const auto &[name, variable] : bindings_) { (void)name; if (!variable->LexicalBound()) { continue; } - - bindings[variable->AsLocalVariable()->LexIdx()] = variable; - } - - uint32_t buffIndex = 0; - for (const auto *variable : bindings) { if (variable->Declaration()->IsConstDecl()) { - literals[buffIndex++] = compiler::Literal(true); + literals.emplace_back(compiler::Literal(true)); } - literals[buffIndex++] = compiler::Literal(variable->Name()); + literals.emplace_back(compiler::Literal(variable->Name())); } } diff --git a/compiler/core/emitter.cpp b/compiler/core/emitter.cpp index d854586e2..605c0579d 100644 --- a/compiler/core/emitter.cpp +++ b/compiler/core/emitter.cpp @@ -98,6 +98,7 @@ static LiteralPair TransformLiteral(const compiler::Literal *literal) break; } default: + UNREACHABLE(); break; } -- Gitee