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

unit rvalue, use constant () instead of tuple #70891

Merged
merged 3 commits into from
Apr 15, 2020
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
6 changes: 5 additions & 1 deletion src/librustc_mir/transform/promote_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,11 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
if self.keep_original {
rhs.clone()
} else {
let unit = Rvalue::Aggregate(box AggregateKind::Tuple, vec![]);
let unit = Rvalue::Use(Operand::Constant(box Constant {
span: statement.source_info.span,
user_ty: None,
literal: ty::Const::zero_sized(self.tcx, self.tcx.types.unit),
}));
mem::replace(rhs, unit)
},
statement.source_info,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir_build/build/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
if destination_ty.is_unit() {
// We only want to assign an implicit `()` as the return value of the block if the
// block does not diverge. (Otherwise, we may try to assign a unit to a `!`-type.)
this.cfg.push_assign_unit(block, source_info, destination);
this.cfg.push_assign_unit(block, source_info, destination, this.hir.tcx());
}
}
// Finally, we pop all the let scopes before exiting out from the scope of block
Expand Down
8 changes: 7 additions & 1 deletion src/librustc_mir_build/build/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::build::CFG;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt};

impl<'tcx> CFG<'tcx> {
crate fn block_data(&self, blk: BasicBlock) -> &BasicBlockData<'tcx> {
Expand Down Expand Up @@ -58,12 +59,17 @@ impl<'tcx> CFG<'tcx> {
block: BasicBlock,
source_info: SourceInfo,
place: Place<'tcx>,
tcx: TyCtxt<'tcx>,
) {
self.push_assign(
block,
source_info,
place,
Rvalue::Aggregate(box AggregateKind::Tuple, vec![]),
Rvalue::Use(Operand::Constant(box Constant {
span: source_info.span,
user_ty: None,
literal: ty::Const::zero_sized(tcx, tcx.types.unit),
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, I'm coincidentally adding tcx.consts.unit to #70970.

})),
);
}

Expand Down
6 changes: 5 additions & 1 deletion src/librustc_mir_build/build/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
block = unpack!(this.stmt_expr(block, expr, None));
block.and(this.unit_rvalue())
block.and(Rvalue::Use(Operand::Constant(box Constant {
span: expr_span,
user_ty: None,
literal: ty::Const::zero_sized(this.hir.tcx(), this.hir.tcx().types.unit),
})))
}
ExprKind::Yield { .. }
| ExprKind::Literal { .. }
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir_build/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
| ExprKind::LlvmInlineAsm { .. }
| ExprKind::Return { .. } => {
unpack!(block = this.stmt_expr(block, expr, None));
this.cfg.push_assign_unit(block, source_info, destination);
this.cfg.push_assign_unit(block, source_info, destination, this.hir.tcx());
block.unit()
}

Expand Down
4 changes: 0 additions & 4 deletions src/librustc_mir_build/build/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Operand::Constant(constant)
}

crate fn unit_rvalue(&mut self) -> Rvalue<'tcx> {
Rvalue::Aggregate(box AggregateKind::Tuple, vec![])
}

// Returns a zero literal operand for the appropriate type, works for
// bool, char and integers.
crate fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir_build/build/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
unpack!(block = self.into(destination, block, value));
self.block_context.pop();
} else {
self.cfg.push_assign_unit(block, source_info, destination)
self.cfg.push_assign_unit(block, source_info, destination, self.hir.tcx())
}
} else {
assert!(value.is_none(), "`return` and `break` should have a destination");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,13 @@ fn address_of_reborrow() -> () {
StorageDead(_48); // bb0[157]: scope 13 at $DIR/address-of.rs:36:25: 36:26
FakeRead(ForLet, _47); // bb0[158]: scope 13 at $DIR/address-of.rs:36:9: 36:10
AscribeUserType(_47, o, UserTypeProjection { base: UserType(29), projs: [] }); // bb0[159]: scope 13 at $DIR/address-of.rs:36:12: 36:22
_0 = (); // bb0[160]: scope 0 at $DIR/address-of.rs:3:26: 37:2
_0 = const (); // bb0[160]: scope 0 at $DIR/address-of.rs:3:26: 37:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/address-of.rs:3:26: 37:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
Comment on lines -301 to +307
Copy link
Member

Choose a reason for hiding this comment

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

@rust-lang/wg-mir-opt Should we prioritize removing these verbose comments? I'm not sure they add much value.
(There's the Span but that only makes sense when it differs the statement Span, which it doesn't here)

StorageDead(_47); // bb0[161]: scope 13 at $DIR/address-of.rs:37:1: 37:2
StorageDead(_45); // bb0[162]: scope 12 at $DIR/address-of.rs:37:1: 37:2
StorageDead(_44); // bb0[163]: scope 11 at $DIR/address-of.rs:37:1: 37:2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ fn borrow_and_cast(_1: i32) -> () {
_6 = &raw mut (*_7); // bb0[15]: scope 2 at $DIR/address-of.rs:44:13: 44:19
FakeRead(ForLet, _6); // bb0[16]: scope 2 at $DIR/address-of.rs:44:9: 44:10
StorageDead(_7); // bb0[17]: scope 2 at $DIR/address-of.rs:44:31: 44:32
_0 = (); // bb0[18]: scope 0 at $DIR/address-of.rs:41:32: 45:2
_0 = const (); // bb0[18]: scope 0 at $DIR/address-of.rs:41:32: 45:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/address-of.rs:41:32: 45:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
StorageDead(_6); // bb0[19]: scope 2 at $DIR/address-of.rs:45:1: 45:2
StorageDead(_4); // bb0[20]: scope 1 at $DIR/address-of.rs:45:1: 45:2
StorageDead(_2); // bb0[21]: scope 0 at $DIR/address-of.rs:45:1: 45:2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ fn main() -> () {
_1[_7] = move _5; // bb2[0]: scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:29
StorageDead(_5); // bb2[1]: scope 3 at $DIR/array-index-is-temporary.rs:16:28: 16:29
StorageDead(_7); // bb2[2]: scope 3 at $DIR/array-index-is-temporary.rs:16:29: 16:30
_0 = (); // bb2[3]: scope 0 at $DIR/array-index-is-temporary.rs:12:11: 17:2
_0 = const (); // bb2[3]: scope 0 at $DIR/array-index-is-temporary.rs:12:11: 17:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/array-index-is-temporary.rs:12:11: 17:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
StorageDead(_3); // bb2[4]: scope 2 at $DIR/array-index-is-temporary.rs:17:1: 17:2
StorageDead(_2); // bb2[5]: scope 1 at $DIR/array-index-is-temporary.rs:17:1: 17:2
StorageDead(_1); // bb2[6]: scope 0 at $DIR/array-index-is-temporary.rs:17:1: 17:2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ fn main() -> () {
_1[_7] = move _5; // bb2[0]: scope 3 at $DIR/array-index-is-temporary.rs:16:5: 16:29
StorageDead(_5); // bb2[1]: scope 3 at $DIR/array-index-is-temporary.rs:16:28: 16:29
StorageDead(_7); // bb2[2]: scope 3 at $DIR/array-index-is-temporary.rs:16:29: 16:30
_0 = (); // bb2[3]: scope 0 at $DIR/array-index-is-temporary.rs:12:11: 17:2
_0 = const (); // bb2[3]: scope 0 at $DIR/array-index-is-temporary.rs:12:11: 17:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/array-index-is-temporary.rs:12:11: 17:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
StorageDead(_3); // bb2[4]: scope 2 at $DIR/array-index-is-temporary.rs:17:1: 17:2
StorageDead(_2); // bb2[5]: scope 1 at $DIR/array-index-is-temporary.rs:17:1: 17:2
StorageDead(_1); // bb2[6]: scope 0 at $DIR/array-index-is-temporary.rs:17:1: 17:2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ fn main() -> () {

bb6: {
StorageDead(_6); // bb6[0]: scope 4 at $DIR/basic_assignment.rs:23:19: 23:20
_0 = (); // bb6[1]: scope 0 at $DIR/basic_assignment.rs:10:11: 24:2
_0 = const (); // bb6[1]: scope 0 at $DIR/basic_assignment.rs:10:11: 24:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/basic_assignment.rs:10:11: 24:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
drop(_5) -> [return: bb7, unwind: bb3]; // bb6[2]: scope 3 at $DIR/basic_assignment.rs:24:1: 24:2
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ fn main() -> () {
bb5: {
StorageDead(_4); // bb5[0]: scope 1 at $DIR/box_expr.rs:8:11: 8:12
StorageDead(_3); // bb5[1]: scope 1 at $DIR/box_expr.rs:8:12: 8:13
_0 = (); // bb5[2]: scope 0 at $DIR/box_expr.rs:6:11: 9:2
_0 = const (); // bb5[2]: scope 0 at $DIR/box_expr.rs:6:11: 9:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/box_expr.rs:6:11: 9:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
drop(_1) -> bb8; // bb5[3]: scope 0 at $DIR/box_expr.rs:9:1: 9:2
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ fn main() -> () {
// mir::Constant
// + span: $DIR/byte_slice.rs:6:19: 6:23
// + literal: Const { ty: u8, val: Value(Scalar(0x78)) }
_0 = (); // bb0[4]: scope 0 at $DIR/byte_slice.rs:4:11: 7:2
_0 = const (); // bb0[4]: scope 0 at $DIR/byte_slice.rs:4:11: 7:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/byte_slice.rs:4:11: 7:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
StorageDead(_2); // bb0[5]: scope 1 at $DIR/byte_slice.rs:7:1: 7:2
StorageDead(_1); // bb0[6]: scope 0 at $DIR/byte_slice.rs:7:1: 7:2
return; // bb0[7]: scope 0 at $DIR/byte_slice.rs:7:2: 7:2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ fn main() -> () {
_1 = (*_2); // bb0[3]: scope 0 at $DIR/const_allocation.rs:8:5: 8:8
StorageDead(_2); // bb0[4]: scope 0 at $DIR/const_allocation.rs:8:8: 8:9
StorageDead(_1); // bb0[5]: scope 0 at $DIR/const_allocation.rs:8:8: 8:9
_0 = (); // bb0[6]: scope 0 at $DIR/const_allocation.rs:7:11: 9:2
_0 = const (); // bb0[6]: scope 0 at $DIR/const_allocation.rs:7:11: 9:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/const_allocation.rs:7:11: 9:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
return; // bb0[7]: scope 0 at $DIR/const_allocation.rs:9:2: 9:2
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ fn main() -> () {
_1 = (*_2); // bb0[3]: scope 0 at $DIR/const_allocation.rs:8:5: 8:8
StorageDead(_2); // bb0[4]: scope 0 at $DIR/const_allocation.rs:8:8: 8:9
StorageDead(_1); // bb0[5]: scope 0 at $DIR/const_allocation.rs:8:8: 8:9
_0 = (); // bb0[6]: scope 0 at $DIR/const_allocation.rs:7:11: 9:2
_0 = const (); // bb0[6]: scope 0 at $DIR/const_allocation.rs:7:11: 9:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/const_allocation.rs:7:11: 9:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
return; // bb0[7]: scope 0 at $DIR/const_allocation.rs:9:2: 9:2
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ fn main() -> () {
_1 = (*_2); // bb0[3]: scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
StorageDead(_2); // bb0[4]: scope 0 at $DIR/const_allocation2.rs:5:8: 5:9
StorageDead(_1); // bb0[5]: scope 0 at $DIR/const_allocation2.rs:5:8: 5:9
_0 = (); // bb0[6]: scope 0 at $DIR/const_allocation2.rs:4:11: 6:2
_0 = const (); // bb0[6]: scope 0 at $DIR/const_allocation2.rs:4:11: 6:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/const_allocation2.rs:4:11: 6:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
return; // bb0[7]: scope 0 at $DIR/const_allocation2.rs:6:2: 6:2
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ fn main() -> () {
_1 = (*_2); // bb0[3]: scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
StorageDead(_2); // bb0[4]: scope 0 at $DIR/const_allocation2.rs:5:8: 5:9
StorageDead(_1); // bb0[5]: scope 0 at $DIR/const_allocation2.rs:5:8: 5:9
_0 = (); // bb0[6]: scope 0 at $DIR/const_allocation2.rs:4:11: 6:2
_0 = const (); // bb0[6]: scope 0 at $DIR/const_allocation2.rs:4:11: 6:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/const_allocation2.rs:4:11: 6:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
return; // bb0[7]: scope 0 at $DIR/const_allocation2.rs:6:2: 6:2
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ fn main() -> () {
_1 = (*_2); // bb0[3]: scope 0 at $DIR/const_allocation3.rs:5:5: 5:8
StorageDead(_2); // bb0[4]: scope 0 at $DIR/const_allocation3.rs:5:8: 5:9
StorageDead(_1); // bb0[5]: scope 0 at $DIR/const_allocation3.rs:5:8: 5:9
_0 = (); // bb0[6]: scope 0 at $DIR/const_allocation3.rs:4:11: 6:2
_0 = const (); // bb0[6]: scope 0 at $DIR/const_allocation3.rs:4:11: 6:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/const_allocation3.rs:4:11: 6:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
return; // bb0[7]: scope 0 at $DIR/const_allocation3.rs:6:2: 6:2
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ fn main() -> () {
_1 = (*_2); // bb0[3]: scope 0 at $DIR/const_allocation3.rs:5:5: 5:8
StorageDead(_2); // bb0[4]: scope 0 at $DIR/const_allocation3.rs:5:8: 5:9
StorageDead(_1); // bb0[5]: scope 0 at $DIR/const_allocation3.rs:5:8: 5:9
_0 = (); // bb0[6]: scope 0 at $DIR/const_allocation3.rs:4:11: 6:2
_0 = const (); // bb0[6]: scope 0 at $DIR/const_allocation3.rs:4:11: 6:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/const_allocation3.rs:4:11: 6:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
return; // bb0[7]: scope 0 at $DIR/const_allocation3.rs:6:2: 6:2
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@
+ // + literal: Const { ty: i32, val: Value(Scalar(0x00000001)) }
StorageDead(_2); // bb0[6]: scope 0 at $DIR/aggregate.rs:5:27: 5:28
StorageDead(_3); // bb0[7]: scope 0 at $DIR/aggregate.rs:5:28: 5:29
_0 = (); // bb0[8]: scope 0 at $DIR/aggregate.rs:4:11: 6:2
_0 = const (); // bb0[8]: scope 0 at $DIR/aggregate.rs:4:11: 6:2
// ty::Const
// + ty: ()
// + val: Value(Scalar(<ZST>))
// mir::Constant
// + span: $DIR/aggregate.rs:4:11: 6:2
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
StorageDead(_1); // bb0[9]: scope 0 at $DIR/aggregate.rs:6:1: 6:2
return; // bb0[10]: scope 0 at $DIR/aggregate.rs:6:2: 6:2
}
Expand Down
Loading