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

Add lint for holding RefCell Ref across an await #6029

Merged
merged 10 commits into from
Oct 25, 2020

Conversation

Daniel-B-Smith
Copy link

Fixes #6008

This introduces the lint await_holding_refcell_ref. For async functions, we iterate
over all types in generator_interior_types and look for core::cell::Ref or core::cell::RefMut. If we find one then we emit a lint.

Heavily cribs from: #5439

changelog: introduce the await_holding_refcell_ref lint

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @yaahc (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 10, 2020
@bors
Copy link
Collaborator

bors commented Sep 13, 2020

☔ The latest upstream changes (presumably #6036) made this pull request unmergeable. Please resolve the merge conflicts.

Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@Daniel-B-Smith
Copy link
Author

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@rustbot
Copy link
Collaborator

rustbot commented Sep 15, 2020

Error: Label S-waiting-on-review can only be set by Rust team members

Please let @rust-lang/release know if you're having trouble with this bot.

yaahc
yaahc previously requested changes Sep 15, 2020
Copy link
Member

@yaahc yaahc left a comment

Choose a reason for hiding this comment

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

looks great, just one test I think we should add before we can merge this. Also, I went ahead and created a zulip topic to discuss the lint group this should go in.

/// }
/// ```
pub AWAIT_HOLDING_REFCELL_REF,
pedantic,
Copy link
Member

@yaahc yaahc Sep 15, 2020

Choose a reason for hiding this comment

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

I think this might be worth putting in correctness, I notice that the mutex PR you linked is also pedantic. We should definitely be consistent, but that might mean creating a diff PR to move both lints from pedantic to correctness after this PR merges.

Copy link
Author

Choose a reason for hiding this comment

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

My vote would be to make a separate PR changing both at the same time as that would give us time to do a crater run and would be easier to roll back in case of false positives.

tests/ui/await_holding_refcell_ref.rs Show resolved Hide resolved
Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

This LGTM. Regarding moving this to correctness: It doesn't really matter if we do it in a separate PR or just in a separate commit. I would even prefer to just add a commit including everything that is necessary for moving it to correctness in this PR.

Comment on lines 81 to 83
fn is_refcell_ref(cx: &LateContext<'_>, def_id: DefId) -> bool {
match_def_path(cx, def_id, &paths::REFCELL_REF) || match_def_path(cx, def_id, &paths::REFCELL_REFMUT)
}
Copy link
Member

Choose a reason for hiding this comment

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

Since this and the error messages are the only things that differ from the await_holding_lock lint, I think we should implement those two lints in the same module. You can choose the name of the module ;).

@flip1995
Copy link
Member

flip1995 commented Oct 1, 2020

r? @flip1995

reassigning this, since @yaahc stepped down as a reviewer to focus on the error handling project group

@rust-highfive rust-highfive assigned flip1995 and unassigned yaahc Oct 1, 2020
@flip1995 flip1995 added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Oct 1, 2020
@flip1995
Copy link
Member

ping from triage @Daniel-B-Smith. Only few changes are left to implement here. Is there anything I can help you with to get this over the finish line?

@Daniel-B-Smith
Copy link
Author

Thanks for the ping. I got distracted with some other work. I've merged the lints into one module and marked them as correctness in a separate commit.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Thanks! A little clean up left. Also, could you please squash the two commits 4ea9af3 and 4a3084e into one?

Comment on lines 137 to 101
use AsyncGeneratorKind::{Block, Closure, Fn};
if let Some(GeneratorKind::Async(Block | Closure | Fn)) = body.generator_kind {
let body_id = BodyId {
hir_id: body.value.hir_id,
};
let def_id = cx.tcx.hir().body_owner_def_id(body_id);
let typeck_results = cx.tcx.typeck(def_id);
Copy link
Member

Choose a reason for hiding this comment

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

this is exactly the same code as in line 55. You can just combine the two lint passes.

Copy link
Author

Choose a reason for hiding this comment

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

Done

@@ -90,3 +90,79 @@ fn is_mutex_guard(cx: &LateContext<'_>, def_id: DefId) -> bool {
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_READ_GUARD)
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_WRITE_GUARD)
}

declare_clippy_lint! {
Copy link
Member

Choose a reason for hiding this comment

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

NIT: usually lint declarations are on top of the file

Copy link
Author

Choose a reason for hiding this comment

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

Done

|
LL | / let guard = x.lock().unwrap();
LL | | baz().await
LL | | }
| |_____^

error: aborting due to 4 previous errors
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await.
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to keep the tests separated.

Copy link
Author

Choose a reason for hiding this comment

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

Done. Unfortunately, I don't think I was able to preserve the git history when they were split. I could attempt to squash the commit where I restore the split into the commit where I merged the two files.

Copy link
Author

@Daniel-B-Smith Daniel-B-Smith left a comment

Choose a reason for hiding this comment

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

Squashed the two correctness commits into one

@@ -90,3 +90,79 @@ fn is_mutex_guard(cx: &LateContext<'_>, def_id: DefId) -> bool {
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_READ_GUARD)
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_WRITE_GUARD)
}

declare_clippy_lint! {
Copy link
Author

Choose a reason for hiding this comment

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

Done

Comment on lines 137 to 101
use AsyncGeneratorKind::{Block, Closure, Fn};
if let Some(GeneratorKind::Async(Block | Closure | Fn)) = body.generator_kind {
let body_id = BodyId {
hir_id: body.value.hir_id,
};
let def_id = cx.tcx.hir().body_owner_def_id(body_id);
let typeck_results = cx.tcx.typeck(def_id);
Copy link
Author

Choose a reason for hiding this comment

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

Done

|
LL | / let guard = x.lock().unwrap();
LL | | baz().await
LL | | }
| |_____^

error: aborting due to 4 previous errors
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await.
Copy link
Author

Choose a reason for hiding this comment

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

Done. Unfortunately, I don't think I was able to preserve the git history when they were split. I could attempt to squash the commit where I restore the split into the commit where I merged the two files.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Thanks! Can you rebase on top of master, to get rid of the merge commit? You will have to force push, but this is ok.

If you want you can squash some commits, but that is not mandatory (make sure to keep 6b682db in a separate commit though).

@Daniel-B-Smith
Copy link
Author

Rebased

@flip1995
Copy link
Member

@bors r+

Thanks!

@bors
Copy link
Collaborator

bors commented Oct 25, 2020

📌 Commit 4d33225 has been approved by flip1995

@bors
Copy link
Collaborator

bors commented Oct 25, 2020

⌛ Testing commit 4d33225 with merge a675778...

@bors
Copy link
Collaborator

bors commented Oct 25, 2020

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing a675778 to master...

@bors bors merged commit a675778 into rust-lang:master Oct 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint for using await while holding RefCell Ref/RefMut
6 participants