unit rvalue: use constant () instead of tuple

This commit is contained in:
Bastian Kauschke 2020-04-09 12:24:53 +02:00
parent c58c532744
commit db83fdc46c
7 changed files with 20 additions and 10 deletions

View File

@ -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,

View File

@ -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

View File

@ -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> {
@ -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),
})),
);
}

View File

@ -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 { .. }

View File

@ -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()
}

View File

@ -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> {

View File

@ -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");