Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ac30192

Browse files
author
Lu Guanqun
committed
[cond-expr] add a test for different types
1 parent 1cd3288 commit ac30192

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

libsolidity/analysis/TypeChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ bool TypeChecker::visit(Conditional const& _conditional)
759759
commonType = trueType;
760760
else
761761
// we fake it as an equal operator, but any other comparison operator can work.
762-
TypePointer commonType = trueType->binaryOperatorResult(Token::Equal, falseType);
762+
commonType = trueType->binaryOperatorResult(Token::Equal, falseType);
763763
if (!commonType)
764764
{
765765
typeError(

test/libsolidity/SolidityEndToEndTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,21 @@ BOOST_AUTO_TEST_CASE(conditional_expression_storage_memory)
174174
BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(u256(2)));
175175
}
176176

177+
BOOST_AUTO_TEST_CASE(conditional_expression_different_types)
178+
{
179+
char const* sourceCode = R"(
180+
contract test {
181+
function f(bool cond) returns (uint) {
182+
uint8 x = 0xcd;
183+
uint16 y = 0xabab;
184+
return cond ? x : y;
185+
}
186+
}
187+
)";
188+
compileAndRun(sourceCode);
189+
BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(u256(0xcd)));
190+
BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(u256(0xabab)));
191+
}
177192
BOOST_AUTO_TEST_CASE(recursive_calls)
178193
{
179194
char const* sourceCode = "contract test {\n"

0 commit comments

Comments
 (0)