Skip to content

Modify MIR building to drop repeat expressions with length zero #95935

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

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 17 additions & 0 deletions compiler/rustc_mir_build/src/build/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
ExprKind::Repeat { value, count } => {
let value_operand =
unpack!(block = this.as_operand(block, scope, &this.thir[value], None));
if let Some(0) = count.try_eval_usize(this.tcx, this.param_env) {
if let Operand::Move(place) = value_operand {
let success = this.cfg.start_new_block();
this.cfg.terminate(
block,
source_info,
TerminatorKind::Drop { place, target: success, unwind: None },
);
this.diverge_from(block);
block = success;
return block.and(Rvalue::Use(Operand::Constant(Box::new(Constant {
span: expr_span,
user_ty: None,
literal: ty::Const::zero_sized(this.tcx, expr.ty).into(),
}))));
}
}
block.and(Rvalue::Repeat(value_operand, count))
}
ExprKind::Binary { op, lhs, rhs } => {
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/drop/repeat-drop-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let foo = String::new();
let _bar = foo;
let _baz = [foo; 0]; //~ ERROR use of moved value: `foo` [E0382]
}
13 changes: 13 additions & 0 deletions src/test/ui/drop/repeat-drop-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0382]: use of moved value: `foo`
--> $DIR/repeat-drop-2.rs:4:17
|
LL | let foo = String::new();
| --- move occurs because `foo` has type `String`, which does not implement the `Copy` trait
LL | let _bar = foo;
| --- value moved here
LL | let _baz = [foo; 0];
| ^^^ value used here after move

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
36 changes: 36 additions & 0 deletions src/test/ui/drop/repeat-drop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// run-pass

static mut COUNT: usize = 0;

struct Foo;

impl Drop for Foo {
fn drop(&mut self) {
unsafe {
COUNT += 1;
}
}
}

fn a() {
let foo = Foo;
let v: [Foo; 0] = [foo; 0];
unsafe { assert_eq!(COUNT, 1) }
std::mem::drop(v);
}

fn b() {
let foo = Foo;
let v: [Foo; 1] = [foo; 1];
unsafe { assert_eq!(COUNT, 0) }
std::mem::drop(v);
unsafe { assert_eq!(COUNT, 1) }
}

fn main() {
a();
unsafe {
COUNT = 0;
}
b();
}