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

Reduce false positives of tail-expr-drop-order from consumed values (attempt #2) #131326

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

dingxiangfei2009
Copy link
Contributor

@dingxiangfei2009 dingxiangfei2009 commented Oct 6, 2024

r? @nikomatsakis

Tracked by #123739.

Related to #129864 but not replacing, yet.

Related to #130836.

This is an implementation of the approach suggested in the Zulip stream. 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.

@rustbot rustbot added 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. labels Oct 6, 2024
@rustbot
Copy link
Collaborator

rustbot commented Oct 6, 2024

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in coverage instrumentation.

cc @Zalathar

This PR changes Stable MIR

cc @oli-obk, @celinval, @ouz-a

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

Some changes occurred in match lowering

cc @Nadrieril

@dingxiangfei2009
Copy link
Contributor Author

I still have two issues.

  • I am not sure if using tcx.ty_string_with_limit is the way to go, because it doesn't print fully qualified path names for ADTs yet. I still need to find out how one can configure PrettyPrinter::pretty_print_type for this.
  • I found it hard to reason about lints when futures are involved.

About the futures

Take the crate sqlx-macros-core @ 0.8.1 as an example. In src/database/mod.rs:57:71 the future awaited involves generics B, C and E, T on separate occasions. I suppose it would be much more helpful if we can refer to the code where those types are involved.

error: this value has significant drop implementation that will have a different drop order from that of Edition 2021, whose type `Pin<Box<dyn Future<Output = Result<<DB as Database>::Connection, Error>> + Send>>` drops `Ptr` while dropping
  --> src/database/mod.rs:57:71
   |
57 |                     miss.insert(DB::Connection::connect(database_url).await?)
   |                                 --------------------------------------^^^^^- this local binding may observe changes in drop order under Edition 2024, whose type `ControlFlow<std::result::Result<Infallible, sqlx_core::Error>, <DB as sqlx_core::database::Database>::Connection>` drops `B, C` while dropping
   |
   = warning: this changes meaning in Rust 2024
   = note: for more information, see issue #123739 <https://github.com/rust-lang/rust/issues/123739>
   = note: requested on the command line with `-D tail-expr-drop-order`

error: this value has significant drop implementation that will have a different drop order from that of Edition 2021, whose type `Pin<Box<dyn Future<Output = Result<<DB as Database>::Connection, Error>> + Send>>` drops `Ptr` while dropping
  --> src/database/mod.rs:57:71
   |
57 |                     miss.insert(DB::Connection::connect(database_url).await?)
   |                                 --------------------------------------^^^^^- this local binding may observe changes in drop order under Edition 2024, whose type `std::result::Result<Infallible, sqlx_core::Error>` drops `E, T` while dropping
   |
   = warning: this changes meaning in Rust 2024
   = note: for more information, see issue #123739 <https://github.com/rust-lang/rust/issues/123739>

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@dingxiangfei2009
Copy link
Contributor Author

@rustbot labels +A-edition-2024

@rustbot rustbot added the A-edition-2024 Area: The 2024 edition label Oct 7, 2024
@rust-log-analyzer

This comment has been minimized.

@dingxiangfei2009
Copy link
Contributor Author

@bors try

@bors
Copy link
Contributor

bors commented Oct 7, 2024

⌛ Trying commit 8f09388 with merge 1c6d72d...

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 7, 2024
…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.
@bors
Copy link
Contributor

bors commented Oct 7, 2024

☀️ Try build successful - checks-actions
Build commit: 1c6d72d (1c6d72d356cc2668e49375eb9391c0b95cb18594)

@dingxiangfei2009
Copy link
Contributor Author

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 7, 2024
…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.
@bors
Copy link
Contributor

bors commented Oct 7, 2024

⌛ Trying commit 80c5c8f with merge fa33670...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Oct 7, 2024

☀️ Try build successful - checks-actions
Build commit: fa33670 (fa3367093b28d0fb18394b7fd734d540fcd30c47)

@dingxiangfei2009
Copy link
Contributor Author

dingxiangfei2009 commented Oct 7, 2024

@compiler-errors Shall we give it a crater run with run mode=check-only p=1 crates=https://gist.githubusercontent.com/compiler-errors/cc84423a4a9fbc5eb598383fe2555467/raw/a11fe3433fefddaab0038ee75cd2e0ab8852748a/crates start=master#0c227e91aa956d20861deb2bb25083d8a4d094e1 end=try#fa3367093b28d0fb18394b7fd734d540fcd30c47+rustflags=-Dtail_expr_drop_order? 🙇

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

Overall this seems much better than before! Minimally invasive, which is good.

{
visitor.terminating_scopes.insert(tail_expr.hir_id.local_id);
// Note: we are unconditionally adding this information so that we can run
Copy link
Contributor

Choose a reason for hiding this comment

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

The word "unconditionally" is confusing here, since clearly this insert call is conditioned on several things (edition, lint level, etc). I think I know what you mean by it, perhaps rewrite to

// If this temporary scope will be changing once the codebase adopts Rust 2024,
// and we are linting about possible semantic changes that would result,
// then record this node-id in the field `backwards_incompatible_scope`
// for future reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Applied. The original comment was added when I thought it was right to insert the linting info under all circumstances. Given that we only want to lint when edition migration is run, I changed the condition when the linting info is inserted.

compiler/rustc_middle/src/mir/syntax.rs Show resolved Hide resolved
/// Lifetime for temporaries as expected.
/// This should be `None` in a constant context.
pub temp_lifetime: Option<region::Scope>,
/// Backward-incompatible lifetime for future editions
Copy link
Contributor

Choose a reason for hiding this comment

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

I would say:

/// If `Some(lt)`, indicates that the lifetime of this temporary will change to `lt` in a future edition.
/// If `None`, then no changes are expected, or lints are disabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Applied

compiler/rustc_middle/src/ty/rvalue_scopes.rs Show resolved Hide resolved
@dingxiangfei2009
Copy link
Contributor Author

cc @traviscross for visibility

@dingxiangfei2009
Copy link
Contributor Author

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 8, 2024
…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.
@dingxiangfei2009 dingxiangfei2009 force-pushed the issue-130836-attempt-2 branch 2 times, most recently from bb15b7c to 2f5657b Compare October 20, 2024 22:37
Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

OK, left some comments, but also a few questions.

compiler/rustc_middle/src/mir/syntax.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_transform/messages.ftl Show resolved Hide resolved
compiler/rustc_mir_transform/messages.ftl Outdated Show resolved Hide resolved
compiler/rustc_mir_transform/messages.ftl Outdated Show resolved Hide resolved
compiler/rustc_mir_transform/messages.ftl Outdated Show resolved Hide resolved
compiler/rustc_ty_utils/src/needs_drop.rs Show resolved Hide resolved
compiler/rustc_ty_utils/src/needs_drop.rs Outdated Show resolved Hide resolved
compiler/rustc_ty_utils/src/needs_drop.rs Outdated Show resolved Hide resolved
@bors
Copy link
Contributor

bors commented Oct 23, 2024

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

@dingxiangfei2009
Copy link
Contributor Author

dingxiangfei2009 commented Oct 30, 2024

@rustbot ready

  • The new query is reworked so that we don't run this manual recursion into coroutine. The documentation gives a warning, too, on at which stage the query can be used.
  • The message is reworked and the drop span is corrected. Specifically, the language is clearer by avoiding use of the indefinitive determiners in the message.

There are merge conflicts but I would prefer to get feedback first for the general framework and outlook of the lint messages.

dingxiangfei2009 and others added 11 commits November 6, 2024 04:52
take 2

open up coroutines

tweak the wordings

the lint works up until 2021

We were missing one case, for ADTs, which was
causing `Result` to yield incorrect results.

only include field spans with significant types

deduplicate and eliminate field spans

switch to emit spans to impl Drops

Co-authored-by: Niko Matsakis <nikomat@amazon.com>
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Nov 6, 2024

Some changes occurred to the CTFE machinery

cc @rust-lang/wg-const-eval

@traviscross traviscross added the F-shorter_tail_lifetimes `#![feature(shorter_tail_lifetimes)]` label Nov 7, 2024
@ehuss ehuss mentioned this pull request Nov 8, 2024
Copy link
Contributor

@celinval celinval left a comment

Choose a reason for hiding this comment

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

Since the new statement has no semantic, can we just handle it as a Nop in StableMir? Thanks!

Comment on lines +154 to +159
mir::StatementKind::BackwardIncompatibleDropHint { place, reason } => {
stable_mir::mir::StatementKind::BackwardIncompatibleDropHint {
place: place.stable(tables),
reason: reason.stable(tables),
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this has no semantics, can we translate it to Nop for now?

Suggested change
mir::StatementKind::BackwardIncompatibleDropHint { place, reason } => {
stable_mir::mir::StatementKind::BackwardIncompatibleDropHint {
place: place.stable(tables),
reason: reason.stable(tables),
}
}
mir::StatementKind::BackwardIncompatibleDropHint { .. } => stable_mir::mir::StatementKind::Nop,

Comment on lines +454 to +465
impl<'tcx> Stable<'tcx> for mir::BackwardIncompatibleDropReason {
type T = stable_mir::mir::BackwardIncompatibleDropReason;

fn stable(&self, _tables: &mut Tables<'_>) -> Self::T {
match self {
mir::BackwardIncompatibleDropReason::Edition2024 => {
stable_mir::mir::BackwardIncompatibleDropReason::Edition2024
}
}
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

then we can cleanup all the other code that was added to StableMIR.

Suggested change
impl<'tcx> Stable<'tcx> for mir::BackwardIncompatibleDropReason {
type T = stable_mir::mir::BackwardIncompatibleDropReason;
fn stable(&self, _tables: &mut Tables<'_>) -> Self::T {
match self {
mir::BackwardIncompatibleDropReason::Edition2024 => {
stable_mir::mir::BackwardIncompatibleDropReason::Edition2024
}
}
}
}

@@ -448,9 +448,15 @@ pub enum StatementKind {
Coverage(Coverage),
Intrinsic(NonDivergingIntrinsic),
ConstEvalCounter,
BackwardIncompatibleDropHint { place: Place, reason: BackwardIncompatibleDropReason },
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
BackwardIncompatibleDropHint { place: Place, reason: BackwardIncompatibleDropReason },
/// This has no semantics. E.g.: BackwardIncompatibleDropHint is translated to Nop.

Comment on lines +455 to +459
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum BackwardIncompatibleDropReason {
Edition2024,
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub enum BackwardIncompatibleDropReason {
Edition2024,
}

Comment on lines +104 to +106
StatementKind::BackwardIncompatibleDropHint { place, reason: _ } => {
writeln!(writer, "backwards incompatible drop({place:?});")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
StatementKind::BackwardIncompatibleDropHint { place, reason: _ } => {
writeln!(writer, "backwards incompatible drop({place:?});")
}

Comment on lines +227 to +229
StatementKind::BackwardIncompatibleDropHint { .. }
| StatementKind::ConstEvalCounter
| StatementKind::Nop => {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
StatementKind::BackwardIncompatibleDropHint { .. }
| StatementKind::ConstEvalCounter
| StatementKind::Nop => {}
StatementKind::ConstEvalCounter | StatementKind::Nop => {}

@bors
Copy link
Contributor

bors commented Nov 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2024 Area: The 2024 edition F-shorter_tail_lifetimes `#![feature(shorter_tail_lifetimes)]` 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.