Skip to content

Commit db83fdc

Browse files
committed
unit rvalue: use constant () instead of tuple
1 parent c58c532 commit db83fdc

File tree

7 files changed

+20
-10
lines changed

7 files changed

+20
-10
lines changed

src/librustc_mir/transform/promote_consts.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,11 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
835835
if self.keep_original {
836836
rhs.clone()
837837
} else {
838-
let unit = Rvalue::Aggregate(box AggregateKind::Tuple, vec![]);
838+
let unit = Rvalue::Use(Operand::Constant(box Constant {
839+
span: statement.source_info.span,
840+
user_ty: None,
841+
literal: ty::Const::zero_sized(self.tcx, self.tcx.types.unit),
842+
}));
839843
mem::replace(rhs, unit)
840844
},
841845
statement.source_info,

src/librustc_mir_build/build/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
187187
if destination_ty.is_unit() {
188188
// We only want to assign an implicit `()` as the return value of the block if the
189189
// block does not diverge. (Otherwise, we may try to assign a unit to a `!`-type.)
190-
this.cfg.push_assign_unit(block, source_info, destination);
190+
this.cfg.push_assign_unit(block, source_info, destination, this.hir.tcx());
191191
}
192192
}
193193
// Finally, we pop all the let scopes before exiting out from the scope of block

src/librustc_mir_build/build/cfg.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use crate::build::CFG;
44
use rustc_middle::mir::*;
5+
use rustc_middle::ty::{self, TyCtxt};
56

67
impl<'tcx> CFG<'tcx> {
78
crate fn block_data(&self, blk: BasicBlock) -> &BasicBlockData<'tcx> {
@@ -58,12 +59,17 @@ impl<'tcx> CFG<'tcx> {
5859
block: BasicBlock,
5960
source_info: SourceInfo,
6061
place: Place<'tcx>,
62+
tcx: TyCtxt<'tcx>,
6163
) {
6264
self.push_assign(
6365
block,
6466
source_info,
6567
place,
66-
Rvalue::Aggregate(box AggregateKind::Tuple, vec![]),
68+
Rvalue::Use(Operand::Constant(box Constant {
69+
span: source_info.span,
70+
user_ty: None,
71+
literal: ty::Const::zero_sized(tcx, tcx.types.unit),
72+
})),
6773
);
6874
}
6975

src/librustc_mir_build/build/expr/as_rvalue.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
225225
}
226226
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
227227
block = unpack!(this.stmt_expr(block, expr, None));
228-
block.and(this.unit_rvalue())
228+
block.and(Rvalue::Use(Operand::Constant(box Constant {
229+
span: expr_span,
230+
user_ty: None,
231+
literal: ty::Const::zero_sized(this.hir.tcx(), this.hir.tcx().types.unit),
232+
})))
229233
}
230234
ExprKind::Yield { .. }
231235
| ExprKind::Literal { .. }

src/librustc_mir_build/build/expr/into.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
331331
| ExprKind::LlvmInlineAsm { .. }
332332
| ExprKind::Return { .. } => {
333333
unpack!(block = this.stmt_expr(block, expr, None));
334-
this.cfg.push_assign_unit(block, source_info, destination);
334+
this.cfg.push_assign_unit(block, source_info, destination, this.hir.tcx());
335335
block.unit()
336336
}
337337

src/librustc_mir_build/build/misc.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3232
Operand::Constant(constant)
3333
}
3434

35-
crate fn unit_rvalue(&mut self) -> Rvalue<'tcx> {
36-
Rvalue::Aggregate(box AggregateKind::Tuple, vec![])
37-
}
38-
3935
// Returns a zero literal operand for the appropriate type, works for
4036
// bool, char and integers.
4137
crate fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {

src/librustc_mir_build/build/scope.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
523523
unpack!(block = self.into(destination, block, value));
524524
self.block_context.pop();
525525
} else {
526-
self.cfg.push_assign_unit(block, source_info, destination)
526+
self.cfg.push_assign_unit(block, source_info, destination, self.hir.tcx())
527527
}
528528
} else {
529529
assert!(value.is_none(), "`return` and `break` should have a destination");

0 commit comments

Comments
 (0)