mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[cond-expr] don't allow conditional as left value for the first stage
This commit is contained in:
parent
b003290638
commit
bf7daf0814
@ -748,55 +748,37 @@ bool TypeChecker::visit(Conditional const& _conditional)
|
||||
{
|
||||
expectType(_conditional.condition(), BoolType());
|
||||
|
||||
_conditional.trueExpression().accept(*this);
|
||||
_conditional.falseExpression().accept(*this);
|
||||
|
||||
TypePointer const& trueType = type(_conditional.trueExpression());
|
||||
TypePointer const& falseType = type(_conditional.falseExpression());
|
||||
|
||||
// we fake it as an equal operator, but any other comparison operator can work.
|
||||
TypePointer commonType = trueType->binaryOperatorResult(Token::Equal, falseType);
|
||||
if (!commonType)
|
||||
{
|
||||
typeError(
|
||||
_conditional.location(),
|
||||
"True expression's type " +
|
||||
trueType->toString() +
|
||||
" doesn't match false expression's type " +
|
||||
falseType->toString() +
|
||||
"."
|
||||
);
|
||||
// even we can't find a common type, we have to set a type here,
|
||||
// otherwise the upper statement will not be able to check the type.
|
||||
commonType = trueType;
|
||||
}
|
||||
|
||||
_conditional.annotation().type = commonType;
|
||||
|
||||
if (_conditional.annotation().lValueRequested)
|
||||
{
|
||||
requireLValue(_conditional.trueExpression());
|
||||
requireLValue(_conditional.falseExpression());
|
||||
typeError(
|
||||
_conditional.location(),
|
||||
"Conditional expression as left value is not supported yet."
|
||||
);
|
||||
|
||||
TypePointer const& trueType = type(_conditional.trueExpression());
|
||||
TypePointer const& falseType = type(_conditional.falseExpression());
|
||||
|
||||
// as a left value, we require exact match to prevent subtle conversion issues.
|
||||
if (*trueType != *falseType)
|
||||
typeError(
|
||||
_conditional.location(),
|
||||
"True expression's type " +
|
||||
trueType->toString() +
|
||||
" doesn't match false expression's type " +
|
||||
falseType->toString() +
|
||||
"."
|
||||
);
|
||||
|
||||
_conditional.annotation().type = trueType;
|
||||
_conditional.annotation().isLValue = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_conditional.trueExpression().accept(*this);
|
||||
_conditional.falseExpression().accept(*this);
|
||||
|
||||
TypePointer const& trueType = type(_conditional.trueExpression());
|
||||
TypePointer const& falseType = type(_conditional.falseExpression());
|
||||
|
||||
// we fake it as an equal operator, but any other comparison operator can work.
|
||||
TypePointer commonType = trueType->binaryOperatorResult(Token::Equal, falseType);
|
||||
if (!commonType)
|
||||
{
|
||||
typeError(
|
||||
_conditional.location(),
|
||||
"True expression's type " +
|
||||
trueType->toString() +
|
||||
" doesn't match false expression's type " +
|
||||
falseType->toString() +
|
||||
"."
|
||||
);
|
||||
// even we can't find a common type, we have to set a type here,
|
||||
// otherwise the upper statement will not be able to check the type.
|
||||
commonType = trueType;
|
||||
}
|
||||
|
||||
_conditional.annotation().type = commonType;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -128,22 +128,6 @@ BOOST_AUTO_TEST_CASE(conditional_expression_multiple)
|
||||
BOOST_CHECK(callContractFunction("f(uint256)", u256(40)) == toBigEndian(u256(10)));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(conditional_expression_as_left_value)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract test {
|
||||
function f(uint x) returns(uint d) {
|
||||
uint y = 1;
|
||||
uint z = 1;
|
||||
(x > 10 ? y : z) = 3;
|
||||
return y ** z;
|
||||
}
|
||||
})";
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("f(uint256)", u256(20)) == toBigEndian(u256(3)));
|
||||
BOOST_CHECK(callContractFunction("f(uint256)", u256(5)) == toBigEndian(u256(1)));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(conditional_expression_with_return_values)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
|
@ -2972,12 +2972,14 @@ BOOST_AUTO_TEST_CASE(invalid_different_types_for_conditional_expression)
|
||||
BOOST_CHECK(expectError(text) == Error::Type::TypeError);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(invalid_left_value_in_conditional_expression)
|
||||
BOOST_AUTO_TEST_CASE(left_value_in_conditional_expression_not_supported_yet)
|
||||
{
|
||||
char const* text = R"(
|
||||
contract C {
|
||||
function f() {
|
||||
(true ? 3 : 5) = 1;
|
||||
uint x;
|
||||
uint y;
|
||||
(true ? x : y) = 1;
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
Loading…
Reference in New Issue
Block a user