Skip to content
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

Allow named parameters in mapping types #13384

Merged
merged 1 commit into from
Jan 9, 2023
Merged

Conversation

zemse
Copy link
Contributor

@zemse zemse commented Aug 11, 2022

Closes #11407

  • Allows providing an optional name/identifier just after the key type and the value type.
  • Identifier defaults to empty string "".
  • The optional identifier if provided is set to the "name" field in JSON ABI.
  • Test cases are added for parser and ABI generator

Please let me know of any changes that are needed.

@zemse zemse dismissed stale reviews from ghost via 48df82f August 13, 2022 10:46
@@ -10,4 +10,6 @@ contract Error2 {
mapping (address => uint balances; // missing ) before "balances"
}
// ----
// ParserError 6635: (417-425): Expected ')' but got identifier
// ParserError 6635: (425-426): Expected ')' but got ';'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some existing error recovery tests change their behavior because a syntax that was illegal before (i.e. ability to place an identifier next to type in mapping) is legal now, hence the parser passes further and generates a different error.

Copy link
Member

@hrkrshnn hrkrshnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks quite good! Some small comments.

We were wondering with the annotations, if it makes sense to have the function call options syntax for mappings. For example, for mapping (address user => uint balance) balanceOf, would the syntax balanceOf[{user: 0xaddress}] be allowed? It's not needed to implement it in this PR, but worth thinking about.

Also, it's missing a changelog entry.

libsolidity/ast/AST.h Outdated Show resolved Hide resolved
libsolidity/parsing/Parser.cpp Outdated Show resolved Hide resolved
libsolidity/parsing/Parser.cpp Outdated Show resolved Hide resolved
libsolidity/parsing/Parser.cpp Show resolved Hide resolved
test/libsolidity/ABIJson/mapping.sol Show resolved Hide resolved
@hrkrshnn
Copy link
Member

Can you add a semantic test as well? https://github.com/ethereum/solidity/blob/develop/test/libsolidity/semanticTests/getters/mapping.sol here is an example

@zemse zemse dismissed a stale review via cf2056e August 17, 2022 11:43
@hrkrshnn
Copy link
Member

Could you also add an example in the documentation?

@zemse
Copy link
Contributor Author

zemse commented Aug 18, 2022

Thanks for the review! I've made the changes.

function call options syntax for mappings

This might be tricky for nested mappings: allowances[{owner: 0xAddr}][{spender: 0xAddr}]. Somehow having it together could be good allowances[{owner: 0xAddr, spender: 0xAddr}], however the names are optional hence this together syntax might not work. But yeah having it makes sense for improving code readability, does not affect any ABI or bytecode. I'd prefer adding it in a separate PR unless it is needed in this PR.

@zemse zemse requested a review from hrkrshnn August 18, 2022 05:33
Copy link
Contributor

@Marenz Marenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already looking very good.
Additionally to the comments, I am wondering if it makes sense to make it an optional<ASTPointer<ASTString>>.
Would love to hear @cameel and @christianparpart on that topic

docs/types/mapping-types.rst Outdated Show resolved Hide resolved
libsolidity/parsing/Parser.cpp Show resolved Hide resolved
@cameel
Copy link
Member

cameel commented Aug 24, 2022

Additionally to the comments, I am wondering if it makes sense to make it an optional<ASTPointer<ASTString>>.

Depends if we prefer to treat it as a missing name or an empty name. That will show up in the AST.

But overall optional would make it clearer that this is even possible. And since names need to be unique, having to treat "" as an exception is not that great.

Also, I can't see any place in the AST where we'd have a single optional name but in a few cases we do have vectors of names and that's closer to optional than to having an empty string when the name is missing.

@zemse
Copy link
Contributor Author

zemse commented Sep 12, 2022

I have rebased over the latest develop branch to fix the conflicts.

Edit: I'm not able to request reviews from multiple people. cc: @hrkrshnn, @Marenz, @cameel, @nishant-sachdeva. (Sorry for tagging).

@zemse zemse requested review from Marenz and hrkrshnn and removed request for hrkrshnn, nishant-sachdeva and Marenz September 12, 2022 18:21
@hrkrshnn
Copy link
Member

Will rebase now

Changelog.md Outdated Show resolved Hide resolved
libsolidity/ast/AST.h Outdated Show resolved Hide resolved
@nikola-matic nikola-matic self-assigned this Nov 21, 2022
@nikola-matic
Copy link
Collaborator

I've pushed an empty commit, squash and pushed in the hopes of restarting the build successfully, but unfortunately we seem to have CI problems, which will hopefully be resolved soon. In fact, as soon as that happens, I'll restart the build to see whether everything passes, and then I'll resume reviewing.

Co-authored-by: Hari <[email protected]>

test: add parser and abi test cases

docs: add example on using named parameters for mappings

- Add changelog

feat: update antlr grammar to allow named parameters in mappings

fix: prevent conflicting mapping parameter names

ref: change order of mapping initializers

test: update expectations and fix build

test: add more tests

fix: use common error & code for conflicting params

fix: issue with accessing nested mapping

test: add conflicting params tests for more nested levels

Update libsolidity/analysis/DeclarationTypeChecker.cpp

Co-authored-by: Nikola Matić <[email protected]>

fix: error reported with the same code twice

test: add more tests for 3 level nested mapping

Address review comments
Copy link
Collaborator

@nikola-matic nikola-matic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed the remaining issues, rebased and squashed, and am satisfied with the state of the PR at the moment, and am thus approving. @cameel or @ekpyron, please take over for the final review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Allow specifying parameter name in public mappings
8 participants