diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 1ca8dd193..76d184fc1 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -608,6 +608,20 @@ bool TypeChecker::visit(VariableDeclaration const& _variable) } } + auto fixedPoint = dynamic_cast(_variable.annotation().type); + if (fixedPoint) + { + bigint fixedPointTypeSize = boost::multiprecision::pow(bigint(2), fixedPoint->numBits()); + bigint digitSize = boost::multiprecision::pow(bigint(10), fixedPoint->fractionalDigits()); + if (fixedPointTypeSize <= digitSize) + m_errorReporter.typeError( + 5108_error, + _variable.location(), + "Invalid fixed point type " + fixedPoint->toString(true) + ": 10^" + + std::to_string(fixedPoint->fractionalDigits()) + " does not fit in 2^" + + std::to_string(fixedPoint->numBits()) + " bits." + ); + } return false; } diff --git a/test/libsolidity/syntaxTests/fixed_point/type_size.sol b/test/libsolidity/syntaxTests/fixed_point/type_size.sol new file mode 100644 index 000000000..fa206c87f --- /dev/null +++ b/test/libsolidity/syntaxTests/fixed_point/type_size.sol @@ -0,0 +1,8 @@ +contract A +{ + function s128x18() public pure returns (ufixed16x16 x) { + x = 0; + } +} +// ---- +// TypeError 5108: (57-70): Invalid fixed point type ufixed16x16: 10^16 does not fit in 2^16 bits.