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

Port improved unused_unsafe check to -Zthir_unsafeck #99379

Closed

Conversation

LeSeulArtichaut
Copy link
Contributor

Rebase of dc1056a as proposed in #93678 (comment), cc @steffahn.

r? @nagisa, do you want to review this as a followup of #93678?

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jul 17, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 17, 2022
@steffahn
Copy link
Member

I’ll take a look through the further changes made to the MIR-version that came up during review after I had created dc1056a and see if any of those apply to this code as-well. I can probably start looking into this tomorrow or Tuesday.

Haven’t looked at the diffs so far or tried the rebase myself; I assume this is at the moment just a rebase and there weren’t any hard to solve merge conflicts?

@LeSeulArtichaut
Copy link
Contributor Author

I’ll take a look through the further changes made to the MIR-version that came up during review after I had created dc1056a and see if any of those apply to this code as-well. I can probably start looking into this tomorrow or Tuesday.

Thanks a lot, that'd be great if you still have the squashed commits. I've tried to resolve the differences I could spot but that's nowhere near a guarantee.

Haven’t looked at the diffs so far or tried the rebase myself; I assume this is at the moment just a rebase and there weren’t any hard to solve merge conflicts?

Exactly, just enough for the code to compile and for the tests to pass.

@steffahn
Copy link
Member

steffahn commented Jul 17, 2022

that'd be great if you still have the squashed commits

They’re on GitHub, too. Just look for the “force-pushed” messages in the PR to get the relevant commit hashes to look at (I’d assume that 40b1efa45e79b5925906b632f4a8c109107c36b2 is the relevant before-squashing and before-rebasing commit); commits can also be fetched and checked out on the command-line just like branches (git fetch REMOTE_NAME COMMIT_HASH and git checkout COMMIT_HASH1).

Footnotes

  1. IIRC you can use any of the GitHub forks as REMOTE_NAME when fetching from GitHub, the set of commits available should be the same between forks; so assuming you have some GitHub fork of rustc configured as origin, you can use git fetch origin 40b1efa4; git checkout 40b1efa4, then inspect the history with git log, look at the dates and so on… (at least that’s what I am going to try)

@LeSeulArtichaut
Copy link
Contributor Author

Okay, so I believe we're looking at 59e19357dfc54744980f256a4105e701f7dd99b1...99a1aa904aceb120220a46954c5d270290d4b471.

  • 3be74c3b52adb7272d27562c3c5fc0fcec7672bc is now irrelevant
  • I've included relevant patches from c4638d8abad4e33db849cfbcfb7f2b27a0228e9c and 99a1aa904aceb120220a46954c5d270290d4b471
  • the rest is specific to the MIR implementation or touches rustc_middle and therefore is already applied in master

@nagisa
Copy link
Member

nagisa commented Jul 23, 2022

Can’t currently dedicate the significant amount of attention to reviewing this. Reassigning, but might be able to come back to this later if this doesn’t get reviewed by the time autumn rolls around.

r? rust-lang/compiler

@michaelwoerister
Copy link
Member

Not really my ballpark, sorry. r? rust-lang/compiler

@LeSeulArtichaut
Copy link
Contributor Author

r? rust-lang/compiler

@bors
Copy link
Contributor

bors commented Aug 19, 2022

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

Copy link
Contributor

@cjgillot cjgillot left a comment

Choose a reason for hiding this comment

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

I left some questions. Mainly: why do you need to design this as a second pass over the HIR, and not report this straight while visiting THIR?

used_unsafe_blocks: visitor.used_unsafe_blocks,
context: if body_unsafety.is_unsafe() { Context::UnsafeFn(hir_id) } else { Context::Safe },
};
intravisit::Visitor::visit_body(&mut visitor2, tcx.hir().body(tcx.hir().body_owned_by(hir_id)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this traversal happen on HIR and not THIR?

}
(Context::UnsafeBlock(hir_id), Some(_)) => UnusedUnsafe::InUnsafeBlock(hir_id),
};
report_unused_unsafe(self.tcx, block.hir_id, unused_unsafe);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't we report this at the end of UnsafetyVisitor::in_safety_context?

hir_context,
used_unsafe_blocks: self.used_unsafe_blocks,
..*self
};
closure_visitor.visit_expr(&closure_thir[expr]);
// Unsafe blocks can be used in closures, make sure to take it into account
self.safety_context = closure_visitor.safety_context;
Copy link
Contributor

Choose a reason for hiding this comment

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

safety_context has a stack-like behaviour. Should this be an assert_eq?

Comment on lines -82 to -84
| ------ ^^^^^^ unnecessary `unsafe` block
| |
| because it's nested under this `unsafe` block
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a shame we have this output regression here

Comment on lines +1 to +6
error: unnecessary `unsafe` block
--> $DIR/lint-unused-unsafe.rs:1054:29
|
LL | let _ = async { unsafe {
| ^^^^^^ unnecessary `unsafe` block
|
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't pointing at the reason, the async fn in 1051. Or is it because there's another unsafe block within it before calling the unsafe fn? Either way, we should be pointing at the reason this is unnecessary, otherwise people will have to either already know what the problem is or start changing things at random.

Comment on lines +40 to +44
error: unnecessary `unsafe` block
--> $DIR/lint-unused-unsafe.rs:1068:29
|
LL | let _ = async { unsafe {
| ^^^^^^ unnecessary `unsafe` block
Copy link
Contributor

Choose a reason for hiding this comment

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

Another case, it should point at the enclosing async fn

Comment on lines +46 to +53
error: unnecessary `unsafe` block
--> $DIR/lint-unused-unsafe.rs:1069:33
|
LL | async unsafe fn async_blocks() {
| ------------------------------ because it's nested under this `unsafe` fn
...
LL | let _ = async { unsafe { let _ = async { unsf() }; }};
| ^^^^^^ unnecessary `unsafe` block
Copy link
Contributor

Choose a reason for hiding this comment

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

Interestingly, this points at the async fn, even though it is nested inside of another unsafe block.

Comment on lines +134 to +138
error: unnecessary `unsafe` block
--> $DIR/lint-unused-unsafe.rs:21:13
|
LL | fn bad1() { unsafe {} }
| ^^^^^^ unnecessary `unsafe` block
Copy link
Contributor

Choose a reason for hiding this comment

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

This should mention that within the unsafe block there are no unsafe operations, so the block is unnecessary.

@LeSeulArtichaut
Copy link
Contributor Author

Thanks a lot for the reviews! Unfortunately I won't have time to continue working on this, sorry...

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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants