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

[significant_drop_tightening] Fix #10413 #10774

Merged
merged 2 commits into from
Jun 30, 2023
Merged

Conversation

c410-f3r
Copy link
Contributor

@c410-f3r c410-f3r commented May 14, 2023

Fix #10413

This is quite a rewrite that unfortunately took a large amount of time. I tried my best to comment what is going on to easy review but feel free to ask any question.

The problem basically is that the current algorithm is only taking into consideration single blocks which means that things like the following don't work or show unpredictable results.

let mutex = Mutex::new(1);
{
  let lock = mutex.lock().unwrap();
  {
    let _ = *lock;
  }
}

The solve the issue, each path that refers a lock is now being tracked individually.

changelog: [`significant_drop_tightening`]: Lift the restriction of only considerate single blocks

@rustbot
Copy link
Collaborator

rustbot commented May 14, 2023

r? @dswij

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 14, 2023
@c410-f3r
Copy link
Contributor Author

@dswij I can re-roll if you don't have time to review

@dswij
Copy link
Member

dswij commented May 31, 2023

Sorry for the delay, I had started to look into this but then fell a bit ill last week. I will take a look asap

@c410-f3r
Copy link
Contributor Author

Don't worry

Comment on lines 265 to 282
fn visit_block(&mut self, block: &'tcx hir::Block<'tcx>) {
self.ap.curr_block_hir_id = block.hir_id;
self.ap.curr_block_span = block.span;
for stmt in block.stmts.iter() {
self.ap.curr_stmt = Cow::Borrowed(stmt);
self.visit_stmt(stmt);
self.ap.curr_block_hir_id = block.hir_id;
self.ap.curr_block_span = block.span;
self.manage_has_expensive_expr_after_last_attr();
}
if let Some(expr) = block.expr {
self.ap.curr_stmt = Cow::Owned(dummy_stmt_expr(expr));
self.visit_expr(expr);
self.ap.curr_block_hir_id = block.hir_id;
self.ap.curr_block_span = block.span;
self.manage_has_expensive_expr_after_last_attr();
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if I understand completely, but I think this is the difference with the previous implementation? i.e. now we check for blocks and any nested block?

Copy link
Contributor Author

@c410-f3r c410-f3r Jun 6, 2023

Choose a reason for hiding this comment

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

The previous algorithm used to check all individual occurrences of Block through LateLintPass::check_block but now everything starts in the root block of fn through LateLintPass::check_fn to properly track the subsequent nested block children.

@c410-f3r
Copy link
Contributor Author

There is still related work to be done that I would like to continue pursuing with this PR merged, as such, I will try another reviewer.

r? clippy

@rustbot rustbot assigned Jarcho and unassigned dswij Jun 12, 2023
@Jarcho
Copy link
Contributor

Jarcho commented Jun 15, 2023

With a quick look this seems ok.

With that said, getting this lint to a state where it could be removed from the nursery will requiring rewriting this as a MIR lint. It needs to be able to track lifetimes for it to not have a large number of false positives. e.g.

let mut locked = foo.lock();
let borrow = &mut *locked;
let references_borrow = bar(borrow);
// and so on

@c410-f3r
Copy link
Contributor Author

Yeah, such thing will allow me to close #9399 but something tells me that it will take a long time to finish :)

@c410-f3r
Copy link
Contributor Author

@Jarcho I can re-roll again if you don't have the time to review

@Jarcho
Copy link
Contributor

Jarcho commented Jun 28, 2023

Sorry, I've been sick the past few weeks.

Since this is in the nursery I'll consider the lint still a WIP. I'll just reiterate that you can't fully fix this lint at the HIR level. You can reference needless_clone for how to write a MIR lint the needs to deal with lifetimes.

Since there doesn't seem to be any new issues added with this. @bors r+

@bors
Copy link
Collaborator

bors commented Jun 28, 2023

📌 Commit 755aa8d has been approved by Jarcho

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jun 28, 2023

⌛ Testing commit 755aa8d with merge ed9a2f0...

bors added a commit that referenced this pull request Jun 28, 2023
[significant_drop_tightening] Fix #10413

Fix #10413

This is quite a rewrite that unfortunately took a  large amount of time. I tried my best to comment what is going on to easy review but feel free to ask any question.

The problem basically is that the current algorithm is only taking into consideration single blocks which means that things like the following don't work or show unpredictable results.

```rust
let mutex = Mutex::new(1);
{
  let lock = mutex.lock().unwrap();
  {
    let _ = *lock;
  }
}
```

The solve the issue, each path that refers a lock is now being tracked individually.

```
changelog: [`significant_drop_tightening`]: Lift the restriction of only considerate single blocks
```
@bors
Copy link
Collaborator

bors commented Jun 28, 2023

💔 Test failed - checks-action_test

@c410-f3r
Copy link
Contributor Author

The CI failure has been fixed.Thank you, @Jarcho!

@Jarcho
Copy link
Contributor

Jarcho commented Jun 30, 2023

@bors retry

@Jarcho
Copy link
Contributor

Jarcho commented Jun 30, 2023

@bors r+

@bors
Copy link
Collaborator

bors commented Jun 30, 2023

📌 Commit fc832f0 has been approved by Jarcho

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jun 30, 2023

⌛ Testing commit fc832f0 with merge 73f1417...

@bors
Copy link
Collaborator

bors commented Jun 30, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Jarcho
Pushing 73f1417 to master...

@bors bors merged commit 73f1417 into rust-lang:master Jun 30, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect suggestion for has_significant_drop
5 participants