Merge pull request #11015 from ethereum/fixSelectorForInternal

[Sol->Yul] Provide selector for some internal functions.
This commit is contained in:
chriseth 2021-03-02 12:24:53 +01:00 committed by GitHub
commit ce5c5970d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 1 deletions

View File

@ -1689,9 +1689,15 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
functionType.kind() == FunctionType::Kind::DelegateCall
)
define(IRVariable{_memberAccess}, IRVariable(_memberAccess.expression()).part("functionSelector"));
else if (functionType.kind() == FunctionType::Kind::Declaration)
else if (
functionType.kind() == FunctionType::Kind::Declaration ||
// In some situations, internal function types also provide the "selector" member.
// See Types.cpp for details.
functionType.kind() == FunctionType::Kind::Internal
)
{
solAssert(functionType.hasDeclaration(), "");
solAssert(functionType.declaration().isPartOfExternalInterface(), "");
define(IRVariable{_memberAccess}) << formatNumber(functionType.externalIdentifier() << 224) << "\n";
}
else

View File

@ -0,0 +1,11 @@
contract B {
function g() public {}
}
contract C is B {
bytes4 constant s2 = B.g.selector;
function f() external pure returns (bytes4) { return s2; }
}
// ====
// compileViaYul: also
// ----
// f() -> 0xe2179b8e00000000000000000000000000000000000000000000000000000000

View File

@ -0,0 +1,14 @@
contract B {
function ext() external {}
function pub() public {}
}
contract C is B {
function test() public returns (bytes4, bytes4, bytes4, bytes4) {
return (B.ext.selector, B.pub.selector, this.ext.selector, pub.selector);
}
}
// ====
// compileViaYul: true
// ----
// test() -> 0xcf9f23b500000000000000000000000000000000000000000000000000000000, 0x7defb41000000000000000000000000000000000000000000000000000000000, 0xcf9f23b500000000000000000000000000000000000000000000000000000000, 0x7defb41000000000000000000000000000000000000000000000000000000000

View File

@ -0,0 +1,14 @@
contract B {
function ext() external {}
function pub() public {}
}
contract D {
function test() public returns (bytes4, bytes4) {
return (B.ext.selector, B.pub.selector);
}
}
// ====
// compileViaYul: true
// ----
// test() -> 0xcf9f23b500000000000000000000000000000000000000000000000000000000, 0x7defb41000000000000000000000000000000000000000000000000000000000

View File

@ -0,0 +1,26 @@
contract B {
function ext() external {}
function pub() public {}
}
contract C is B {
function test() public pure {
B.ext.selector;
B.pub.selector;
this.ext.selector;
pub.selector;
}
}
contract D {
function test() public pure {
B.ext.selector;
B.pub.selector;
}
}
// ----
// Warning 6133: (136-150): Statement has no effect.
// Warning 6133: (160-174): Statement has no effect.
// Warning 6133: (184-201): Statement has no effect.
// Warning 6133: (289-303): Statement has no effect.
// Warning 6133: (313-327): Statement has no effect.