diff options
author | Alex Beregszaszi <alex@rtfs.hu> | 2018-04-20 08:31:30 +0800 |
---|---|---|
committer | Alex Beregszaszi <alex@rtfs.hu> | 2018-04-20 18:58:16 +0800 |
commit | 069ea38916a96dfb194cf8d2a0f5e07f9a586bf3 (patch) | |
tree | cfea4a296c46b053466efad84c71dffda7f800f4 | |
parent | 676732776eaf087a17b79bc97213c84746dabd09 (diff) | |
download | dexon-solidity-069ea38916a96dfb194cf8d2a0f5e07f9a586bf3.tar dexon-solidity-069ea38916a96dfb194cf8d2a0f5e07f9a586bf3.tar.gz dexon-solidity-069ea38916a96dfb194cf8d2a0f5e07f9a586bf3.tar.bz2 dexon-solidity-069ea38916a96dfb194cf8d2a0f5e07f9a586bf3.tar.lz dexon-solidity-069ea38916a96dfb194cf8d2a0f5e07f9a586bf3.tar.xz dexon-solidity-069ea38916a96dfb194cf8d2a0f5e07f9a586bf3.tar.zst dexon-solidity-069ea38916a96dfb194cf8d2a0f5e07f9a586bf3.zip |
Make literals an error for tight packing (experimental 0.5.0)
-rw-r--r-- | Changelog.md | 2 | ||||
-rw-r--r-- | libsolidity/analysis/TypeChecker.cpp | 26 | ||||
-rw-r--r-- | test/libsolidity/syntaxTests/tight_packing_literals_050.sol | 26 |
3 files changed, 45 insertions, 9 deletions
diff --git a/Changelog.md b/Changelog.md index 207e2c21..0fbce7c5 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,7 +2,7 @@ Features: * Optimizer: Remove unnecessary masking of the result of known short instructions (``ADDRESS``, ``CALLER``, ``ORIGIN`` and ``COINBASE``). - + * Type Checker: Make literals (without explicit type casting) an error for tight packing as experimental 0.5.0 feature. Bugfixes: diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 72d29762..66b0af58 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -1648,6 +1648,8 @@ bool TypeChecker::visit(FunctionCall const& _functionCall) else _functionCall.annotation().type = make_shared<TupleType>(returnTypes); + bool const v050 = m_scope->sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::V050); + if (auto functionName = dynamic_cast<Identifier const*>(&_functionCall.expression())) { if (functionName->name() == "sha3" && functionType->kind() == FunctionType::Kind::SHA3) @@ -1674,14 +1676,22 @@ bool TypeChecker::visit(FunctionCall const& _functionCall) { /* If no mobile type is available an error will be raised elsewhere. */ if (literal->mobileType()) - m_errorReporter.warning( - arguments[i]->location(), - "The type of \"" + - argType->toString() + - "\" was inferred as " + - literal->mobileType()->toString() + - ". This is probably not desired. Use an explicit type to silence this warning." - ); + { + if (v050) + m_errorReporter.typeError( + arguments[i]->location(), + "Cannot perform packed encoding for a literal. Please convert it to an explicit type first." + ); + else + m_errorReporter.warning( + arguments[i]->location(), + "The type of \"" + + argType->toString() + + "\" was inferred as " + + literal->mobileType()->toString() + + ". This is probably not desired. Use an explicit type to silence this warning." + ); + } } } } diff --git a/test/libsolidity/syntaxTests/tight_packing_literals_050.sol b/test/libsolidity/syntaxTests/tight_packing_literals_050.sol new file mode 100644 index 00000000..617af543 --- /dev/null +++ b/test/libsolidity/syntaxTests/tight_packing_literals_050.sol @@ -0,0 +1,26 @@ +pragma experimental "v0.5.0"; +contract C { + function f() pure public returns (bytes32) { + return keccak256(1); + } + function g() pure public returns (bytes32) { + return sha3(1); + } + function h() pure public returns (bytes32) { + return sha256(1); + } + function j() pure public returns (bytes32) { + return ripemd160(1); + } + function k() pure public returns (bytes) { + return abi.encodePacked(1); + } +} + +// ---- +// TypeError: (117-118): Cannot perform packed encoding for a literal. Please convert it to an explicit type first. +// Warning: (191-198): "sha3" has been deprecated in favour of "keccak256" +// TypeError: (196-197): Cannot perform packed encoding for a literal. Please convert it to an explicit type first. +// TypeError: (277-278): Cannot perform packed encoding for a literal. Please convert it to an explicit type first. +// TypeError: (361-362): Cannot perform packed encoding for a literal. Please convert it to an explicit type first. +// TypeError: (450-451): Cannot perform packed encoding for a literal. Please convert it to an explicit type first. |