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

[SR-13950] Replace uses of find(x) != end() idiom with contains(x) for Set-like types #56347

Closed
typesanitizer opened this issue Dec 9, 2020 · 7 comments
Labels
compiler The Swift compiler in itself good first issue Good for newcomers task

Comments

@typesanitizer
Copy link

Previous ID SR-13950
Radar rdar://problem/72160853
Original Reporter @typesanitizer
Type Task
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Task, StarterBug
Assignee mininny (JIRA)
Priority Medium

md5: 361ae0b10fe3e9e35324139c472df244

Issue Description:

A little while back, LLVM Set-like data types got contains(t) methods which make code more readable. Many places in the compiler use the find(a) != .end() idiom to check for membership (and in some places find(a) == .end() to check if the item is not present).

It would be nice to update the code to use contains(a) to improve the readability.

It's probably easier to manually audit the code for places calling find(...) and end(). That said, if you want to have extra fun, you could try creating a clang-tidy check which automates this. There are various YouTube tutorials and blog posts on clang-tidy, and it has a "REPL" clang-query which can be used to interactively explore the AST.

Then you can run clang-tidy on the codebase. See docs/DebuggingTheCompiler.md on how to do that.

This change shouldn't require any additional tests.

@typesanitizer
Copy link
Author

Feel free to ask questions here and/or ping me for review on GitHub (@ varungandhi-apple on GitHub).

@typesanitizer
Copy link
Author

@swift-ci create

@swift-ci
Copy link
Collaborator

Comment by Minhyuk Kim (JIRA)

Hi theindigamer (JIRA User), I'm trying to take on this issue, and I have a question. 🙂

I was able to change find usages to contains for c++ data structures. But for DenseMap type from llvm(which is used most frequently), I was not able to find contains method. For convention and the scope of this ticket, would it be okay for me to add contains method to llvm::DenseMap?

@typesanitizer
Copy link
Author

For this bug report, I think it's fine if we address things for sets only, since that has already landed in LLVM and we have that available on LLVM's swift/main branch.

The "problem" with DenseMap is that LLVM has many types for hashtables (as it does for sets) and it would be nice to fix all of those at once. If you are going to submit that change to LLVM, and are not sure about the review process/landing changes, you can ask David Blaikie (dblaikie on Phabricator) for review and guidance. Once changes have landed in LLVM and are available on the swift/main branch, we can file a separate task to update Swift; I don't think it is worth waiting on that to land to not update usages for set-like types. Moreover, the larger the Swift-side PR becomes, the more likely you are to run into merge conflicts, which will get tedious quickly.

@swift-ci
Copy link
Collaborator

Comment by Minhyuk Kim (JIRA)

Thanks for the further explanation! 🙂

I went ahead and changed all other usages(changes to llvm could come later i guess) of `find(t) == .end()` to `contains(t)` with the changes from the c+20 to set data-types. But I found that swift's default c+ std for build is c+14, and the build doesn't run unless I change it to c20 in the cmake file. Is it okay for me to commit as-is, or are there any other configurations for using c+20 that I may be missing?

@typesanitizer
Copy link
Author

We can't bump the version to C++20 right now. This bug report applies only to uses of LLVM's Set-like data types, not for the standard library's set/unordered_set. Also, note that the polarity is the other way: so find(t) != end() (not ==) is equivalent to contains(t).

@typesanitizer
Copy link
Author

PR merged. #35229

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself good first issue Good for newcomers task
Projects
None yet
Development

No branches or pull requests

2 participants