Skip to content

Stop calling source_span query in significant drop order code #139461

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

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions compiler/rustc_middle/src/ty/significant_drop_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,25 +143,11 @@ pub fn ty_dtor_span<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Span> {
| ty::UnsafeBinder(_) => None,

ty::Adt(adt_def, _) => {
let did = adt_def.did();
let try_local_did_span = |did: DefId| {
if let Some(local) = did.as_local() {
tcx.source_span(local)
} else {
tcx.def_span(did)
}
};
let dtor = if let Some(dtor) = tcx.adt_destructor(did) {
dtor.did
} else if let Some(dtor) = tcx.adt_async_destructor(did) {
return Some(tcx.source_span(dtor.impl_did));
if let Some(dtor) = tcx.adt_destructor(adt_def.did()) {
Some(tcx.def_span(tcx.parent(dtor.did)))
} else {
return Some(try_local_did_span(did));
};
let def_key = tcx.def_key(dtor);
let Some(parent_index) = def_key.parent else { return Some(try_local_did_span(dtor)) };
let parent_did = DefId { index: parent_index, krate: dtor.krate };
Some(try_local_did_span(parent_did))
Some(tcx.def_span(adt_def.did()))
}
}
ty::Coroutine(did, _)
| ty::CoroutineWitness(did, _)
Expand Down
176 changes: 44 additions & 132 deletions tests/ui/drop/drop-order-comparisons.e2021.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,23 @@ LL | | }, e.mark(3), e.ok(4));
note: `#3` invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: `#1` invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: `_v` invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: `#2` invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: most of the time, changing drop order is harmless; inspect the `impl Drop`s for side effects like releasing locks or sending messages
note: the lint level is defined here
--> $DIR/drop-order-comparisons.rs:28:25
Expand Down Expand Up @@ -95,21 +79,13 @@ LL | | }, e.mark(1), e.ok(4));
note: `#2` invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: `#1` invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: most of the time, changing drop order is harmless; inspect the `impl Drop`s for side effects like releasing locks or sending messages

warning: relative drop order changing in Rust 2024
Expand All @@ -135,21 +111,13 @@ LL | | }, e.mark(1), e.ok(4));
note: `#2` invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: `#1` invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: most of the time, changing drop order is harmless; inspect the `impl Drop`s for side effects like releasing locks or sending messages

warning: relative drop order changing in Rust 2024
Expand All @@ -175,21 +143,13 @@ LL | | }, e.mark(2), e.ok(3));
note: `#2` invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: `#1` invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: most of the time, changing drop order is harmless; inspect the `impl Drop`s for side effects like releasing locks or sending messages

warning: relative drop order changing in Rust 2024
Expand All @@ -215,21 +175,13 @@ LL | | }, e.mark(2), e.ok(3));
note: `#2` invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: `#1` invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: most of the time, changing drop order is harmless; inspect the `impl Drop`s for side effects like releasing locks or sending messages

warning: `if let` assigns a shorter lifetime since Edition 2024
Expand All @@ -245,12 +197,8 @@ LL | _ = (if let Ok(_) = e.ok(4).as_ref() {
note: value invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: the value is now dropped here in Edition 2024
--> $DIR/drop-order-comparisons.rs:127:5
|
Expand Down Expand Up @@ -279,12 +227,8 @@ LL | _ = (if let Ok(_) = e.err(4).as_ref() {} else {
note: value invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: the value is now dropped here in Edition 2024
--> $DIR/drop-order-comparisons.rs:145:44
|
Expand Down Expand Up @@ -312,12 +256,8 @@ LL | if let Ok(_) = e.err(4).as_ref() {} else {
note: value invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: the value is now dropped here in Edition 2024
--> $DIR/drop-order-comparisons.rs:247:43
|
Expand Down Expand Up @@ -345,12 +285,8 @@ LL | if let true = e.err(9).is_ok() {} else {
note: value invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: the value is now dropped here in Edition 2024
--> $DIR/drop-order-comparisons.rs:352:41
|
Expand Down Expand Up @@ -378,12 +314,8 @@ LL | if let Ok(_v) = e.err(8) {} else {
note: value invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: the value is now dropped here in Edition 2024
--> $DIR/drop-order-comparisons.rs:355:35
|
Expand Down Expand Up @@ -411,12 +343,8 @@ LL | if let Ok(_) = e.err(7) {} else {
note: value invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: the value is now dropped here in Edition 2024
--> $DIR/drop-order-comparisons.rs:358:34
|
Expand Down Expand Up @@ -444,12 +372,8 @@ LL | if let Ok(_) = e.err(6).as_ref() {} else {
note: value invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: the value is now dropped here in Edition 2024
--> $DIR/drop-order-comparisons.rs:361:43
|
Expand Down Expand Up @@ -477,12 +401,8 @@ LL | if let Ok(_v) = e.err(5) {} else {
note: value invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: the value is now dropped here in Edition 2024
--> $DIR/drop-order-comparisons.rs:365:35
|
Expand Down Expand Up @@ -510,12 +430,8 @@ LL | if let Ok(_) = e.err(4) {} else {
note: value invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: the value is now dropped here in Edition 2024
--> $DIR/drop-order-comparisons.rs:368:34
|
Expand Down Expand Up @@ -543,12 +459,8 @@ LL | if let Ok(_) = e.err(4).as_ref() {} else {
note: value invokes this custom destructor
--> $DIR/drop-order-comparisons.rs:571:1
|
LL | / impl<'b> Drop for LogDrop<'b> {
LL | | fn drop(&mut self) {
LL | | self.0.mark(self.1);
LL | | }
LL | | }
| |_^
LL | impl<'b> Drop for LogDrop<'b> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: the value is now dropped here in Edition 2024
--> $DIR/drop-order-comparisons.rs:404:43
|
Expand Down
8 changes: 2 additions & 6 deletions tests/ui/drop/lint-if-let-rescope-gated.edition2021.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ LL | if let Some(_value) = Droppy.get() {
note: value invokes this custom destructor
--> $DIR/lint-if-let-rescope-gated.rs:14:1
|
LL | / impl Drop for Droppy {
LL | | fn drop(&mut self) {
LL | | println!("dropped");
LL | | }
LL | | }
| |_^
LL | impl Drop for Droppy {
| ^^^^^^^^^^^^^^^^^^^^
help: the value is now dropped here in Edition 2024
--> $DIR/lint-if-let-rescope-gated.rs:30:5
|
Expand Down
8 changes: 2 additions & 6 deletions tests/ui/drop/lint-if-let-rescope-with-macro.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ LL | | };
note: value invokes this custom destructor
--> $DIR/lint-if-let-rescope-with-macro.rs:22:1
|
LL | / impl Drop for Droppy {
LL | | fn drop(&mut self) {
LL | | println!("dropped");
LL | | }
LL | | }
| |_^
LL | impl Drop for Droppy {
| ^^^^^^^^^^^^^^^^^^^^
help: the value is now dropped here in Edition 2024
--> $DIR/lint-if-let-rescope-with-macro.rs:12:38
|
Expand Down
Loading
Loading