Allow UserDefinedValueType.uwrap (and wrap) as RHS of constant decl

Needed to make `MyType.unwrap` and `MyType.unwrap` as pure in the process. This change affected some
existing tests ("statement has no effect").
This commit is contained in:
hrkrshnn 2021-09-13 11:07:44 +02:00
parent dea1b9ec79
commit a1d4d0125d
6 changed files with 33 additions and 2 deletions

View File

@ -2916,7 +2916,10 @@ bool TypeChecker::visit(MemberAccess const& _memberAccess)
// TODO some members might be pure, but for example `address(0x123).balance` is not pure
// although every subexpression is, so leaving this limited for now.
if (auto tt = dynamic_cast<TypeType const*>(exprType))
if (tt->actualType()->category() == Type::Category::Enum)
if (
tt->actualType()->category() == Type::Category::Enum ||
tt->actualType()->category() == Type::Category::UserDefinedValueType
)
annotation.isPure = true;
if (
auto const* functionType = dynamic_cast<FunctionType const*>(exprType);

View File

@ -3499,7 +3499,9 @@ bool FunctionType::isPure() const
m_kind == Kind::ABIEncodeWithSelector ||
m_kind == Kind::ABIEncodeWithSignature ||
m_kind == Kind::ABIDecode ||
m_kind == Kind::MetaType;
m_kind == Kind::MetaType ||
m_kind == Kind::Wrap ||
m_kind == Kind::Unwrap;
}
TypePointers FunctionType::parseElementaryTypeVector(strings const& _types)

View File

@ -0,0 +1,13 @@
type T is int224;
pragma solidity >= 0.0.0;
contract C {
T constant public s = T.wrap(int224(165521356710917456517261742455526507355687727119203895813322792776));
T constant public t = s;
int224 constant public u = T.unwrap(t);
}
// ====
// compileViaYul: also
// ----
// s() -> 165521356710917456517261742455526507355687727119203895813322792776
// t() -> 165521356710917456517261742455526507355687727119203895813322792776
// u() -> 165521356710917456517261742455526507355687727119203895813322792776

View File

@ -0,0 +1,8 @@
contract C {
type MyInt is int;
MyInt constant mi = MyInt.wrap(5);
// This is currently unsupported.
uint[MyInt.unwrap(mi)] arr;
}
// ----
// TypeError 5462: (122-138): Invalid array length, expected integer literal or constant expression.

View File

@ -6,3 +6,5 @@ function f() pure {
MyAddress.wrap(address(5));
}
// ----
// Warning 6133: (73-87): Statement has no effect.
// Warning 6133: (93-119): Statement has no effect.

View File

@ -7,3 +7,6 @@ function f() pure {
MyUInt8.wrap(50);
}
// ----
// Warning 6133: (75-101): Statement has no effect.
// Warning 6133: (107-122): Statement has no effect.
// Warning 6133: (128-144): Statement has no effect.