Reduce false positives of tail-expr-drop-order from consumed values#129864
Reduce false positives of tail-expr-drop-order from consumed values#129864dingxiangfei2009 wants to merge 7 commits intorust-lang:masterfrom
Conversation
491fb13 to
de2d256
Compare
|
I also found that |
| type Error = !; | ||
|
|
||
| fn typeck_results(&self) -> Self::TypeckResults<'_> { | ||
| self.0.typeck(self.1) |
There was a problem hiding this comment.
This is going to be extremely expensive for the query, for the record. I would probably make some helper that caches the typeck results after the first call, or better yet just load the typeck results yourself and pass them here.
There was a problem hiding this comment.
I should drop this code, since we are not using this anymore.
| match place_with_id.place.base { | ||
| PlaceBase::Rvalue => { | ||
| self.nodes.insert(place_with_id.hir_id); | ||
| } | ||
| PlaceBase::Local(id) => { | ||
| self.nodes.insert(id); | ||
| } | ||
| PlaceBase::Upvar(upvar) => { | ||
| self.nodes.insert(upvar.var_path.hir_id); | ||
| } |
There was a problem hiding this comment.
Doesn't this count partial moves as full moves? Isn't that still not correct for drop ordering?
There was a problem hiding this comment.
Also, doesn't this not respect different paths correctly? What about consuming operations that happen on only one branch of a conditional path? I don't know if this sort of analysis is supported with ExprUseVisitor, which has no "flow" state.
There was a problem hiding this comment.
For now I don't have a good strategy towards that. How do we track partial moves and drops currently? Can we invoke machinery from borrow checker or somewhere else for such information?
I have switched to treat partial moves not as full moves. It could still get noisy but still safe.
| } | ||
| } | ||
|
|
||
| fn extract_tail_expr_consuming_nodes<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx HirIdSet { |
There was a problem hiding this comment.
Make this into a hook, not a query; I don't believe it needs any sort of caching, since that has overhead. This should also explain very clearly what it does.
|
I opened a zulip thread at https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/More.20precise.20lint.20impl.20for.20Edition.202024.20tail-expr-drop-order to discuss this lint, because as @compiler-errors mentioned I feel like this might almost need to be some kind of flow analysis. |
|
@rustbot author |
|
☔ The latest upstream changes (presumably #130357) made this pull request unmergeable. Please resolve the merge conflicts. |
de2d256 to
08ec439
Compare
|
This PR changes MIR cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt Some changes occurred in compiler/rustc_codegen_cranelift cc @bjorn3 These commits modify the If this was unintentional then you should revert the changes before this PR is merged. Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri |
| place: Place<'tcx>, | ||
| target: BasicBlock, | ||
| unwind: UnwindAction, | ||
| scope: Option<(DefId, ItemLocalId)>, |
There was a problem hiding this comment.
Please also add a comment explaining the meaning of this new field.
There was a problem hiding this comment.
Could you make this a ClearCrossCrate? Then it can just be a HirId.
08ec439 to
e71ad9f
Compare
|
@rustbot ready
|
|
I am looking at |
This comment has been minimized.
This comment has been minimized.
|
☔ The latest upstream changes (presumably #130724) made this pull request unmergeable. Please resolve the merge conflicts. |
|
cc @jieyouxu I also gave a hard thought about upvars and whether we should lint against upvars. I have arrived at the conclusion that we should not lint against the case where upvars are dropped later than the tail expression temporaries. What I mean is the following example, where fn should_we_lint_or_not() -> impl FnOnce() -> (?, ?) {
let x = LoudDropper;
move || (x.f(), LoudDropper.f())
}Upvar I am going to flip the test on this to not linting against. |
…ves-from-consumed-droppers, r=<try> Reduce false positives of tail-expr-drop-order from consumed values r? `@jieyouxu` To reduce false positives, the lint does not fire if the locals are consumed/moved, or the values with significant drop are consumed/moved at the tail expression location. For this, we rely on solving a small dataflow to determine whether a `Local` is either live, or moved/dropped. I am also printing the type involved for easier diagnosis.
| rustc_hir = { path = "../rustc_hir" } | ||
| rustc_index = { path = "../rustc_index" } | ||
| rustc_infer = { path = "../rustc_infer" } | ||
| rustc_lint = { path = "../rustc_lint" } |
There was a problem hiding this comment.
Wouldn't rustc_lint_defs suffice?
|
☀️ Try build successful - checks-actions |
|
@craterbot check p=1 crates=https://crater-reports.s3.amazonaws.com/pr-129604/retry-regressed-list.txt start=master#38352b01ae4af9300be03b805d6db68c45e51068 end=try#7014e13d5becc920d4bea3cd87942c8a13d359bf+rustflags=-Dtail_expr_drop_order |
|
🚨 Error: missing desired crates: {"dym", "maxplus", "vkopt-message-parser", "lucia", "rustoku_gui", "flp-tsl", "awsl-pest", "roast-2d", "cl-traits", "cleu-orm-derive", "future-iter", "ndsparse", "genai-custom", "restruct"} 🆘 If you have any trouble with Crater please ping |
|
@craterbot run mode=check-only p=1 crates=https://gist.githubusercontent.com/compiler-errors/cc84423a4a9fbc5eb598383fe2555467/raw/a11fe3433fefddaab0038ee75cd2e0ab8852748a/crates start=master#38352b01ae4af9300be03b805d6db68c45e51068 end=try#7014e13d5becc920d4bea3cd87942c8a13d359bf+rustflags=-Dtail_expr_drop_order |
|
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🎉 Experiment
|
…er, r=<try> [crater] validate impact of marking more types as `#[rustc_insignificant_dtor]` on tail expr drop order lint Validate impact of rust-lang#130914 on rust-lang#129864 and the tail expr drop order lint. r? `@ghost`
…, r=Amanieu Mark some more types as having insignificant dtor These were caught by rust-lang#129864 (comment), which is implementing a lint for some changes in drop order for temporaries in tail expressions. Specifically, the destructors of `CString` and the bitpacked repr for `std::io::Error` are insignificant insofar as they don't have side-effects on things like locking or synchronization; they just free memory. See some discussion on rust-lang#89144 for what makes a drop impl "significant"
Rollup merge of rust-lang#130914 - compiler-errors:insignificant-dtor, r=Amanieu Mark some more types as having insignificant dtor These were caught by rust-lang#129864 (comment), which is implementing a lint for some changes in drop order for temporaries in tail expressions. Specifically, the destructors of `CString` and the bitpacked repr for `std::io::Error` are insignificant insofar as they don't have side-effects on things like locking or synchronization; they just free memory. See some discussion on rust-lang#89144 for what makes a drop impl "significant"
…t-2, r=<try> Reduce false positives of tail-expr-drop-order from consumed values (attempt #2) r? `@nikomatsakis` Related to rust-lang#129864 but not replacing, yet. Related to rust-lang#130836. This is an implementation of the approach suggested in the [Zulip stream](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/temporary.20drop.20order.20changes). A new MIR statement `BackwardsIncompatibleDrop` is added to the MIR syntax. The lint now works by inspecting possibly live move paths before at the `BackwardsIncompatibleDrop` location and the actual drop under the current edition, which should be one before Edition 2024 in practice.
…t-2, r=<try> Reduce false positives of tail-expr-drop-order from consumed values (attempt #2) r? `@nikomatsakis` Related to rust-lang#129864 but not replacing, yet. Related to rust-lang#130836. This is an implementation of the approach suggested in the [Zulip stream](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/temporary.20drop.20order.20changes). A new MIR statement `BackwardsIncompatibleDrop` is added to the MIR syntax. The lint now works by inspecting possibly live move paths before at the `BackwardsIncompatibleDrop` location and the actual drop under the current edition, which should be one before Edition 2024 in practice.
…t-2, r=<try> Reduce false positives of tail-expr-drop-order from consumed values (attempt #2) r? `@nikomatsakis` Tracked by rust-lang#123739. Related to rust-lang#129864 but not replacing, yet. Related to rust-lang#130836. This is an implementation of the approach suggested in the [Zulip stream](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/temporary.20drop.20order.20changes). A new MIR statement `BackwardsIncompatibleDrop` is added to the MIR syntax. The lint now works by inspecting possibly live move paths before at the `BackwardsIncompatibleDrop` location and the actual drop under the current edition, which should be one before Edition 2024 in practice.
…t-2, r=<try> Reduce false positives of tail-expr-drop-order from consumed values (attempt #2) r? `@nikomatsakis` Tracked by rust-lang#123739. Related to rust-lang#129864 but not replacing, yet. Related to rust-lang#130836. This is an implementation of the approach suggested in the [Zulip stream](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/temporary.20drop.20order.20changes). A new MIR statement `BackwardsIncompatibleDrop` is added to the MIR syntax. The lint now works by inspecting possibly live move paths before at the `BackwardsIncompatibleDrop` location and the actual drop under the current edition, which should be one before Edition 2024 in practice.
…t-2, r=nikomatsakis Reduce false positives of tail-expr-drop-order from consumed values (attempt #2) r? `@nikomatsakis` Tracked by rust-lang#123739. Related to rust-lang#129864 but not replacing, yet. Related to rust-lang#130836. This is an implementation of the approach suggested in the [Zulip stream](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/temporary.20drop.20order.20changes). A new MIR statement `BackwardsIncompatibleDrop` is added to the MIR syntax. The lint now works by inspecting possibly live move paths before at the `BackwardsIncompatibleDrop` location and the actual drop under the current edition, which should be one before Edition 2024 in practice.
|
I am closing this since the second attempt achieve greater success. A PoC for detecting future borrow checking error is working. What remains is to work out a good error message and suggestion. I am working on it. 🚧 |
r? @jieyouxu
To reduce false positives, the lint does not fire if the locals are consumed/moved, or the values with significant drop are consumed/moved at the tail expression location.
For this, we rely on solving a small dataflow to determine whether a
Localis either live, or moved/dropped.I am also printing the type involved for easier diagnosis.