From 44093f2ed63782e02ee3611e701c92b2bdf9bd0c Mon Sep 17 00:00:00 2001 From: a3d4 Date: Wed, 6 Apr 2022 01:34:58 +0200 Subject: [PATCH] Fix MSVC Debug crash --- libyul/optimiser/StructuralSimplifier.cpp | 43 +++++++++++++---------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/libyul/optimiser/StructuralSimplifier.cpp b/libyul/optimiser/StructuralSimplifier.cpp index ca8f94239..0b80c42cd 100644 --- a/libyul/optimiser/StructuralSimplifier.cpp +++ b/libyul/optimiser/StructuralSimplifier.cpp @@ -93,26 +93,31 @@ void StructuralSimplifier::operator()(Block& _block) void StructuralSimplifier::simplify(std::vector& _statements) { - util::GenericVisitor visitor{ - util::VisitorFallback{}, - [&](If& _ifStmt) -> OptionalStatements { - if (expressionAlwaysTrue(*_ifStmt.condition)) - return {std::move(_ifStmt.body.statements)}; - else if (expressionAlwaysFalse(*_ifStmt.condition)) - return {vector{}}; - return {}; - }, - [&](Switch& _switchStmt) -> OptionalStatements { - if (std::optional const constExprVal = hasLiteralValue(*_switchStmt.expression)) - return replaceConstArgSwitch(_switchStmt, constExprVal.value()); - return {}; - }, - [&](ForLoop& _forLoop) -> OptionalStatements { - if (expressionAlwaysFalse(*_forLoop.condition)) - return {std::move(_forLoop.pre.statements)}; - return {}; - } + // Explicit local variables ifLambda, switchLambda, forLoopLambda are created to avoid MSVC C++17 Debug test crash + // (Run-Time Check Failure #2 - Stack around the variable '....' was corrupted). + // As soon as the issue is fixed, this workaround can be removed. + auto ifLambda = [&](If& _ifStmt) -> OptionalStatements + { + if (expressionAlwaysTrue(*_ifStmt.condition)) + return {std::move(_ifStmt.body.statements)}; + else if (expressionAlwaysFalse(*_ifStmt.condition)) + return {vector{}}; + return {}; }; + auto switchLambda = [&](Switch& _switchStmt) -> OptionalStatements + { + if (std::optional const constExprVal = hasLiteralValue(*_switchStmt.expression)) + return replaceConstArgSwitch(_switchStmt, constExprVal.value()); + return {}; + }; + auto forLoopLambda = [&](ForLoop& _forLoop) -> OptionalStatements + { + if (expressionAlwaysFalse(*_forLoop.condition)) + return {std::move(_forLoop.pre.statements)}; + return {}; + }; + + util::GenericVisitor visitor{util::VisitorFallback{}, ifLambda, switchLambda, forLoopLambda}; util::iterateReplacing( _statements,