Skip to content

Commit dbc18f6

Browse files
authored
Rollup merge of rust-lang#95953 - JakobDegen:repeat-leak, r=oli-obk
Modify MIR building to drop repeat expressions with length zero Closes rust-lang#74836 . Previously, when a user wrote `[foo; 0]` we used to simply leak `foo`. The goal is to fix that. This PR changes MIR building to make `[foo; 0]` equivalent to `{ drop(foo); [] }` in all cases. Of course, this is a breaking change (see below). A crater run did not indicate any regressions though, and given that the previous behavior was almost definitely not what any user wanted, it seems unlikely that anyone was relying on this. Note that const generics are in general unaffected by this. Inserting the extra `drop` is only meaningful/necessary when `foo` is of a non-`Copy` type, and array repeat expressions with const generic repetition count must always be `Copy`. Besides the obvious change to behavior associated with the additional drop, there are three categories of examples where this also changes observable behavior. In all of these cases, the new behavior is consistent with what you would get by replacing `[foo; 0]` with `{ drop(foo); [] }`. As such, none of these give the user new powers to express more things. **No longer allowed in const (breaking)**: ```rust const _: [String; 0] = [String::new(); 0]; ``` This compiles on stable today. Because we now introduce the drop of `String`, this no longer compiles as `String` may not be dropped in a const context. **Reduced dataflow (non-breaking)**: ```rust let mut x: i32 = 0; let r = &x; let a = [r; 0]; x = 5; let _b = a; ``` Borrowck rejects this code on stable because it believes there is dataflow between `a` and `r`, and so the lifetime of `r` has to extend to the last statement. This change removes the dataflow and the above code is allowed to compile. **More const promotion (non-breaking)**: ```rust let _v: &'static [String; 0] = &[String::new(); 0]; ``` This does not compile today because `String` having drop glue keeps it from being const promoted (despite that drop glue never being executed). After this change, this is allowed to compile. ### Alternatives A previous attempt at this tried to reduce breakage by various tricks. This is still a possibility, but given that crater showed no regressions it seems unclear why we would want to introduce this complexity. Disallowing `[foo; 0]` completely is also an option, but obviously this is more of a breaking change. I do not know how often this is actually used though. r? `@oli-obk`
2 parents acb5c16 + 44410ef commit dbc18f6

File tree

5 files changed

+207
-5
lines changed

5 files changed

+207
-5
lines changed

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

+45-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5252
})
5353
}
5454
ExprKind::Repeat { value, count } => {
55-
let value_operand = unpack!(
56-
block =
57-
this.as_operand(block, scope, &this.thir[value], None, NeedsTemporary::No)
58-
);
59-
block.and(Rvalue::Repeat(value_operand, count))
55+
if Some(0) == count.try_eval_usize(this.tcx, this.param_env) {
56+
this.build_zero_repeat(block, value, scope, source_info)
57+
} else {
58+
let value_operand = unpack!(
59+
block = this.as_operand(
60+
block,
61+
scope,
62+
&this.thir[value],
63+
None,
64+
NeedsTemporary::No
65+
)
66+
);
67+
block.and(Rvalue::Repeat(value_operand, count))
68+
}
6069
}
6170
ExprKind::Binary { op, lhs, rhs } => {
6271
let lhs = unpack!(
@@ -515,6 +524,37 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
515524
}
516525
}
517526

527+
fn build_zero_repeat(
528+
&mut self,
529+
mut block: BasicBlock,
530+
value: ExprId,
531+
scope: Option<region::Scope>,
532+
outer_source_info: SourceInfo,
533+
) -> BlockAnd<Rvalue<'tcx>> {
534+
let this = self;
535+
let value = &this.thir[value];
536+
let elem_ty = value.ty;
537+
if let Some(Category::Constant) = Category::of(&value.kind) {
538+
// Repeating a const does nothing
539+
} else {
540+
// For a non-const, we may need to generate an appropriate `Drop`
541+
let value_operand =
542+
unpack!(block = this.as_operand(block, scope, value, None, NeedsTemporary::No));
543+
if let Operand::Move(to_drop) = value_operand {
544+
let success = this.cfg.start_new_block();
545+
this.cfg.terminate(
546+
block,
547+
outer_source_info,
548+
TerminatorKind::Drop { place: to_drop, target: success, unwind: None },
549+
);
550+
this.diverge_from(block);
551+
block = success;
552+
}
553+
this.record_operands_moved(&[value_operand]);
554+
}
555+
block.and(Rvalue::Aggregate(Box::new(AggregateKind::Array(elem_ty)), Vec::new()))
556+
}
557+
518558
fn limit_capture_mutability(
519559
&mut self,
520560
upvar_span: Span,

compiler/rustc_mir_build/src/build/scope.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10331033
self.cfg.block_data(start).terminator().kind,
10341034
TerminatorKind::Assert { .. }
10351035
| TerminatorKind::Call { .. }
1036+
| TerminatorKind::Drop { .. }
10361037
| TerminatorKind::DropAndReplace { .. }
10371038
| TerminatorKind::FalseUnwind { .. }
10381039
| TerminatorKind::InlineAsm { .. }

src/test/ui/drop/repeat-drop-2.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
fn borrowck_catch() {
2+
let foo = String::new();
3+
let _bar = foo;
4+
let _baz = [foo; 0]; //~ ERROR use of moved value: `foo` [E0382]
5+
}
6+
7+
const _: [String; 0] = [String::new(); 0];
8+
//~^ ERROR destructors cannot be evaluated at compile-time [E0493]
9+
10+
fn must_be_init() {
11+
let x: u8;
12+
let _ = [x; 0]; //~ ERROR: use of possibly-uninitialized variable: `x`
13+
}
14+
15+
fn main() {}

src/test/ui/drop/repeat-drop-2.stderr

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0382]: use of moved value: `foo`
2+
--> $DIR/repeat-drop-2.rs:4:17
3+
|
4+
LL | let foo = String::new();
5+
| --- move occurs because `foo` has type `String`, which does not implement the `Copy` trait
6+
LL | let _bar = foo;
7+
| --- value moved here
8+
LL | let _baz = [foo; 0];
9+
| ^^^ value used here after move
10+
11+
error[E0493]: destructors cannot be evaluated at compile-time
12+
--> $DIR/repeat-drop-2.rs:7:25
13+
|
14+
LL | const _: [String; 0] = [String::new(); 0];
15+
| -^^^^^^^^^^^^^----
16+
| ||
17+
| |constants cannot evaluate destructors
18+
| value is dropped here
19+
20+
error[E0381]: use of possibly-uninitialized variable: `x`
21+
--> $DIR/repeat-drop-2.rs:12:14
22+
|
23+
LL | let _ = [x; 0];
24+
| ^ use of possibly-uninitialized `x`
25+
26+
error: aborting due to 3 previous errors
27+
28+
Some errors have detailed explanations: E0381, E0382, E0493.
29+
For more information about an error, try `rustc --explain E0381`.

src/test/ui/drop/repeat-drop.rs

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// run-pass
2+
3+
static mut CHECK: usize = 0;
4+
5+
struct DropChecker(usize);
6+
7+
impl Drop for DropChecker {
8+
fn drop(&mut self) {
9+
unsafe {
10+
if CHECK != self.0 - 1 {
11+
panic!("Found {}, should have found {}", CHECK, self.0 - 1);
12+
}
13+
CHECK = self.0;
14+
}
15+
}
16+
}
17+
18+
macro_rules! check_drops {
19+
($l:literal) => {
20+
unsafe { assert_eq!(CHECK, $l) }
21+
};
22+
}
23+
24+
struct DropPanic;
25+
26+
impl Drop for DropPanic {
27+
fn drop(&mut self) {
28+
panic!()
29+
}
30+
}
31+
32+
fn value_zero() {
33+
unsafe { CHECK = 0 };
34+
let foo = DropChecker(1);
35+
let v: [DropChecker; 0] = [foo; 0];
36+
check_drops!(1);
37+
std::mem::drop(v);
38+
check_drops!(1);
39+
}
40+
41+
fn value_one() {
42+
unsafe { CHECK = 0 };
43+
let foo = DropChecker(1);
44+
let v: [DropChecker; 1] = [foo; 1];
45+
check_drops!(0);
46+
std::mem::drop(v);
47+
check_drops!(1);
48+
}
49+
50+
const DROP_CHECKER: DropChecker = DropChecker(1);
51+
52+
fn const_zero() {
53+
unsafe { CHECK = 0 };
54+
let v: [DropChecker; 0] = [DROP_CHECKER; 0];
55+
check_drops!(0);
56+
std::mem::drop(v);
57+
check_drops!(0);
58+
}
59+
60+
fn const_one() {
61+
unsafe { CHECK = 0 };
62+
let v: [DropChecker; 1] = [DROP_CHECKER; 1];
63+
check_drops!(0);
64+
std::mem::drop(v);
65+
check_drops!(1);
66+
}
67+
68+
fn const_generic_zero<const N: usize>() {
69+
unsafe { CHECK = 0 };
70+
let v: [DropChecker; N] = [DROP_CHECKER; N];
71+
check_drops!(0);
72+
std::mem::drop(v);
73+
check_drops!(0);
74+
}
75+
76+
fn const_generic_one<const N: usize>() {
77+
unsafe { CHECK = 0 };
78+
let v: [DropChecker; N] = [DROP_CHECKER; N];
79+
check_drops!(0);
80+
std::mem::drop(v);
81+
check_drops!(1);
82+
}
83+
84+
// Make sure that things are allowed to promote as expected
85+
86+
fn allow_promote() {
87+
unsafe { CHECK = 0 };
88+
let foo = DropChecker(1);
89+
let v: &'static [DropChecker; 0] = &[foo; 0];
90+
check_drops!(1);
91+
std::mem::drop(v);
92+
check_drops!(1);
93+
}
94+
95+
// Verify that unwinding in the drop causes the right things to drop in the right order
96+
fn on_unwind() {
97+
unsafe { CHECK = 0 };
98+
std::panic::catch_unwind(|| {
99+
let panic = DropPanic;
100+
let _local = DropChecker(2);
101+
let _v = (DropChecker(1), [panic; 0]);
102+
std::process::abort();
103+
})
104+
.unwrap_err();
105+
check_drops!(2);
106+
}
107+
108+
fn main() {
109+
value_zero();
110+
value_one();
111+
const_zero();
112+
const_one();
113+
const_generic_zero::<0>();
114+
const_generic_one::<1>();
115+
allow_promote();
116+
on_unwind();
117+
}

0 commit comments

Comments
 (0)