Skip to content

Commit af2ce8b

Browse files
committed
don't drop types with no drop glue when tailcalling
this is required as otherwise drops of `&mut` refs count as a usage of a 'two-phase temporary' causing an ICE.
1 parent 99768c8 commit af2ce8b

10 files changed

+12
-12
lines changed

compiler/rustc_mir_build/src/builder/scope.rs

+9
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
785785
let local =
786786
place.as_local().unwrap_or_else(|| bug!("projection in tail call args"));
787787

788+
if !self.local_decls[local].ty.needs_drop(self.tcx, self.typing_env()) {
789+
return None;
790+
}
791+
788792
Some(DropData { source_info, local, kind: DropKind::Value })
789793
}
790794
Operand::Constant(_) => None,
@@ -795,6 +799,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
795799
self.scopes.scopes.iter().rev().nth(1).unwrap().region_scope,
796800
DUMMY_SP,
797801
);
802+
let typing_env = self.typing_env();
798803
let unwind_drops = &mut self.scopes.unwind_drops;
799804

800805
// the innermost scope contains only the destructors for the tail call arguments
@@ -805,6 +810,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
805810
let source_info = drop_data.source_info;
806811
let local = drop_data.local;
807812

813+
if !self.local_decls[local].ty.needs_drop(self.tcx, typing_env) {
814+
continue;
815+
}
816+
808817
match drop_data.kind {
809818
DropKind::Value => {
810819
// `unwind_to` should drop the value that we're about to

tests/mir-opt/tail_call_drops.f.ElaborateDrops.panic-abort.diff

-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
bb6: {
6767
+ _8 = const false;
6868
StorageDead(_4);
69-
StorageDead(_3);
7069
drop(_2) -> [return: bb7, unwind: bb12];
7170
}
7271

tests/mir-opt/tail_call_drops.f.ElaborateDrops.panic-unwind.diff

-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
bb6: {
6767
+ _8 = const false;
6868
StorageDead(_4);
69-
StorageDead(_3);
7069
- drop(_2) -> [return: bb7, unwind continue];
7170
+ drop(_2) -> [return: bb7, unwind: bb12];
7271
}

tests/mir-opt/tail_call_drops.f.built.after.panic-abort.mir

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ fn f() -> () {
6363

6464
bb6: {
6565
StorageDead(_4);
66-
StorageDead(_3);
6766
drop(_2) -> [return: bb7, unwind: bb17];
6867
}
6968

tests/mir-opt/tail_call_drops.f.built.after.panic-unwind.mir

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ fn f() -> () {
6363

6464
bb6: {
6565
StorageDead(_4);
66-
StorageDead(_3);
6766
drop(_2) -> [return: bb7, unwind: bb17];
6867
}
6968

tests/mir-opt/tail_call_drops.f_with_arg.ElaborateDrops.panic-abort.diff

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
bb8: {
8181
+ _12 = const false;
8282
StorageDead(_6);
83-
StorageDead(_5);
8483
drop(_4) -> [return: bb9, unwind: bb16];
8584
}
8685

tests/mir-opt/tail_call_drops.f_with_arg.ElaborateDrops.panic-unwind.diff

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
bb8: {
8181
+ _12 = const false;
8282
StorageDead(_6);
83-
StorageDead(_5);
8483
drop(_4) -> [return: bb9, unwind: bb16];
8584
}
8685

tests/mir-opt/tail_call_drops.f_with_arg.built.after.panic-abort.mir

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ fn f_with_arg(_1: String, _2: String) -> () {
7777

7878
bb8: {
7979
StorageDead(_6);
80-
StorageDead(_5);
8180
drop(_4) -> [return: bb9, unwind: bb23];
8281
}
8382

tests/mir-opt/tail_call_drops.f_with_arg.built.after.panic-unwind.mir

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ fn f_with_arg(_1: String, _2: String) -> () {
7777

7878
bb8: {
7979
StorageDead(_6);
80-
StorageDead(_5);
8180
drop(_4) -> [return: bb9, unwind: bb23];
8281
}
8382

tests/ui/explicit-tail-calls/ctfe-arg-bad-borrow.stderr

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ error[E0597]: `local` does not live long enough
44
LL | let local = Type;
55
| ----- binding `local` declared here
66
LL | become takes_borrow(&local);
7-
| ^^^^^^ borrowed value does not live long enough
8-
LL |
9-
LL | }
10-
| - `local` dropped here while still borrowed
7+
| ^^^^^^- `local` dropped here while still borrowed
8+
| |
9+
| borrowed value does not live long enough
1110

1211
error: aborting due to 1 previous error
1312

0 commit comments

Comments
 (0)