Merge pull request #8859 from ethereum/noRuntimeForImmutable

trigger error when runtimeCode is called on contracts with immutables
This commit is contained in:
chriseth 2020-05-07 16:08:27 +02:00 committed by GitHub
commit 3212cb6caa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 4 deletions

View File

@ -10,7 +10,7 @@ Compiler Features:
Bugfixes:
* ABI: Skip ``private`` or ``internal`` constructors.
* Type Checker: Disallow accessing ``runtimeCode`` for contract types that contain immutable state variables.

View File

@ -2663,9 +2663,19 @@ bool TypeChecker::visit(MemberAccess const& _memberAccess)
))
{
annotation.isPure = true;
m_scope->annotation().contractDependencies.insert(
&dynamic_cast<ContractType const&>(*magicType->typeArgument()).contractDefinition()
);
ContractType const& accessedContractType = dynamic_cast<ContractType const&>(*magicType->typeArgument());
m_scope->annotation().contractDependencies.insert(&accessedContractType.contractDefinition());
if (
memberName == "runtimeCode" &&
!accessedContractType.immutableVariables().empty()
)
m_errorReporter.typeError(
9274_error,
_memberAccess.location(),
"\"runtimeCode\" is not available for contracts containing immutable variables."
);
if (contractDependenciesAreCyclic(*m_scope))
m_errorReporter.typeError(
4224_error,

View File

@ -0,0 +1,9 @@
contract A {
address public immutable user = address(0x0);
}
contract Test {
function test() public pure returns(bytes memory) {
return type(A).creationCode;
}
}

View File

@ -0,0 +1,11 @@
contract A {
address public immutable user = address(0x0);
}
contract Test {
function test() public pure returns(bytes memory) {
return type(A).runtimeCode;
}
}
// ----
// TypeError: (153-172): "runtimeCode" is not available for contracts containing immutable variables.

View File

@ -0,0 +1,13 @@
contract Base {
address public immutable user = address(0x0);
}
contract Derived is Base {}
contract Test {
function test() public pure returns(bytes memory) {
return type(Derived).runtimeCode;
}
}
// ----
// TypeError: (185-210): "runtimeCode" is not available for contracts containing immutable variables.