-
Notifications
You must be signed in to change notification settings - Fork 6.1k
UDVT: Allow internal function types as underlying type #16604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
k06a
wants to merge
1
commit into
argotorg:develop
Choose a base branch
from
k06a:feature/udvt-fp
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
test/libsolidity/semanticTests/userDefinedValueType/function_type_internal.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| library Hooks { | ||
| type Callback is function(uint256) returns (uint256); | ||
| } | ||
|
|
||
| contract C { | ||
| Hooks.Callback cb; | ||
|
|
||
| function double(uint256 x) internal pure returns (uint256) { | ||
| return x * 2; | ||
| } | ||
|
|
||
| function triple(uint256 x) internal pure returns (uint256) { | ||
| return x * 3; | ||
| } | ||
|
|
||
| function setCb(Hooks.Callback _cb) internal { | ||
| cb = _cb; | ||
| } | ||
|
|
||
| function setup() public { | ||
| setCb(Hooks.Callback.wrap(double)); | ||
| } | ||
|
|
||
| function callCb(uint256 x) public returns (uint256) { | ||
| function(uint256) returns (uint256) fn = Hooks.Callback.unwrap(cb); | ||
| return fn(x); | ||
| } | ||
|
|
||
| function wrapAndCall(uint256 x) public returns (uint256) { | ||
| Hooks.Callback wrapped = Hooks.Callback.wrap(triple); | ||
| function(uint256) returns (uint256) fn = Hooks.Callback.unwrap(wrapped); | ||
| return fn(x); | ||
| } | ||
| } | ||
| // ---- | ||
| // setup() -> | ||
| // callCb(uint256): 5 -> 10 | ||
| // wrapAndCall(uint256): 7 -> 21 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test/libsolidity/syntaxTests/userDefinedValueType/forward_reference_err.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| function f(MyIntB x) pure {} | ||
| type MyIntB is MyIntB; | ||
| // ---- | ||
| // TypeError 8657: (44-50): The underlying type for a user defined value type has to be an elementary value type. | ||
| // TypeError 8657: (44-50): The underlying type for a user defined value type has to be an elementary value type or an internal function type. |
27 changes: 27 additions & 0 deletions
27
test/libsolidity/syntaxTests/userDefinedValueType/function_type_internal.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| type Callback is function(uint256) returns (bool); | ||
|
|
||
| library Hooks { | ||
| type BeforeSwap is function(uint256) returns (bool); | ||
| } | ||
|
|
||
| contract C { | ||
| Hooks.BeforeSwap cb; | ||
|
|
||
| function set(Hooks.BeforeSwap _cb) internal { | ||
| cb = _cb; | ||
| } | ||
|
|
||
| function wrapUnwrap() internal pure { | ||
| function(uint256) returns (bool) raw; | ||
| Callback wrapped = Callback.wrap(raw); | ||
| function(uint256) returns (bool) unwrapped = Callback.unwrap(wrapped); | ||
| unwrapped; | ||
| } | ||
|
|
||
| function wrapUnwrapLib() internal pure { | ||
| function(uint256) returns (bool) raw; | ||
| Hooks.BeforeSwap wrapped = Hooks.BeforeSwap.wrap(raw); | ||
| function(uint256) returns (bool) unwrapped = Hooks.BeforeSwap.unwrap(wrapped); | ||
| unwrapped; | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
test/libsolidity/syntaxTests/userDefinedValueType/non_value_type_contract_err.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| contract C {} | ||
| type MyContract is C; | ||
| // ---- | ||
| // TypeError 8657: (33-34): The underlying type for a user defined value type has to be an elementary value type. | ||
| // TypeError 8657: (33-34): The underlying type for a user defined value type has to be an elementary value type or an internal function type. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
test/libsolidity/syntaxTests/userDefinedValueType/non_value_type_function_err.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| type MyFunction is function(uint) returns (uint); | ||
| type MyFunction is function(uint) external returns (uint); | ||
| // ---- | ||
| // TypeError 8657: (19-49): The underlying type for a user defined value type has to be an elementary value type. | ||
| // TypeError 8657: (19-58): The underlying type of the user defined value type "MyFunction" has to be an internal function type, but "function (uint256) external returns (uint256)" is an external function type. |
2 changes: 1 addition & 1 deletion
2
test/libsolidity/syntaxTests/userDefinedValueType/non_value_type_mapping_err.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| type MyInt is mapping(uint => uint); | ||
| // ---- | ||
| // TypeError 8657: (14-35): The underlying type for a user defined value type has to be an elementary value type. | ||
| // TypeError 8657: (14-35): The underlying type for a user defined value type has to be an elementary value type or an internal function type. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test/libsolidity/syntaxTests/userDefinedValueType/recursive_err.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| type MyInt1 is MyInt2; | ||
| type MyInt2 is MyInt1; | ||
| // ---- | ||
| // TypeError 8657: (15-21): The underlying type for a user defined value type has to be an elementary value type. | ||
| // TypeError 8657: (15-21): The underlying type for a user defined value type has to be an elementary value type or an internal function type. |
2 changes: 1 addition & 1 deletion
2
test/libsolidity/syntaxTests/userDefinedValueType/recursive_function_parameter_err.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| type MyFunction is function(MyFunction) external returns(MyFunction); | ||
| // ---- | ||
| // TypeError 8657: (19-69): The underlying type for a user defined value type has to be an elementary value type. | ||
| // TypeError 8657: (19-69): The underlying type of the user defined value type "MyFunction" has to be an internal function type, but "function (MyFunction) external returns (MyFunction)" is an external function type. |
2 changes: 1 addition & 1 deletion
2
test/libsolidity/syntaxTests/userDefinedValueType/self_reference_err.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| type MyInt is MyInt; | ||
| // ---- | ||
| // TypeError 8657: (14-19): The underlying type for a user defined value type has to be an elementary value type. | ||
| // TypeError 8657: (14-19): The underlying type for a user defined value type has to be an elementary value type or an internal function type. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doubt that implementation for this will be this short. There are many corner cases around function types, so if you allow them to masquerade as UDVTs, we have to basically replicate all their analysis checks in all of the UDVT cases. Otherwise some safeguards could be bypassed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @cameel! Thanks for your thorough analysis of the impact. I’m curious if we could apply the first two checks to canonical types? It would be great if we could achieve constant arrays of function pointers for O(1) dispatch tables.