Skip to content

Commit 4571be3

Browse files
authored
Rollup merge of #113093 - WaffleLapkin:become_unuwuable_in_thir, r=Nilstrieb
`thir`: Add `Become` expression kind This PR is pretty small and just adds `thir::ExprKind::Become`. I didn't include the checks that will be done on thir, since they are much more complicated and can be done in parallel with with MIR (or, well, at least I believe they can). r? `@Nilstrieb`
2 parents 1dc29bb + c60fb12 commit 4571be3

File tree

11 files changed

+32
-9
lines changed

11 files changed

+32
-9
lines changed

compiler/rustc_middle/src/thir.rs

+4
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ pub enum ExprKind<'tcx> {
410410
Return {
411411
value: Option<ExprId>,
412412
},
413+
/// A `become` expression.
414+
Become {
415+
value: ExprId,
416+
},
413417
/// An inline `const` block, e.g. `const {}`.
414418
ConstBlock {
415419
did: DefId,

compiler/rustc_middle/src/thir/visit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
100100
visitor.visit_expr(&visitor.thir()[value])
101101
}
102102
}
103+
Become { value } => visitor.visit_expr(&visitor.thir()[value]),
103104
ConstBlock { did: _, substs: _ } => {}
104105
Repeat { value, count: _ } => {
105106
visitor.visit_expr(&visitor.thir()[value]);

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

+1
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
549549
| ExprKind::Break { .. }
550550
| ExprKind::Continue { .. }
551551
| ExprKind::Return { .. }
552+
| ExprKind::Become { .. }
552553
| ExprKind::Literal { .. }
553554
| ExprKind::NamedConst { .. }
554555
| ExprKind::NonHirLiteral { .. }

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

+1
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
532532
| ExprKind::Break { .. }
533533
| ExprKind::Continue { .. }
534534
| ExprKind::Return { .. }
535+
| ExprKind::Become { .. }
535536
| ExprKind::InlineAsm { .. }
536537
| ExprKind::PlaceTypeAscription { .. }
537538
| ExprKind::ValueTypeAscription { .. } => {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ impl Category {
8282
| ExprKind::Block { .. }
8383
| ExprKind::Break { .. }
8484
| ExprKind::Continue { .. }
85-
| ExprKind::Return { .. } =>
85+
| ExprKind::Return { .. }
86+
| ExprKind::Become { .. } =>
8687
// FIXME(#27840) these probably want their own
8788
// category, like "nonterminating"
8889
{

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
493493
block.unit()
494494
}
495495

496-
ExprKind::Continue { .. } | ExprKind::Break { .. } | ExprKind::Return { .. } => {
496+
ExprKind::Continue { .. }
497+
| ExprKind::Break { .. }
498+
| ExprKind::Return { .. }
499+
| ExprKind::Become { .. } => {
497500
unpack!(block = this.stmt_expr(block, expr, None));
498501
// No assign, as these have type `!`.
499502
block.unit()

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

+7
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9999
BreakableTarget::Return,
100100
source_info,
101101
),
102+
// FIXME(explicit_tail_calls): properly lower tail calls here
103+
ExprKind::Become { value } => this.break_scope(
104+
block,
105+
Some(&this.thir[value]),
106+
BreakableTarget::Return,
107+
source_info,
108+
),
102109
_ => {
103110
assert!(
104111
statement_scope.is_some(),

compiler/rustc_mir_build/src/check_unsafety.rs

+1
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
316316
| ExprKind::Closure { .. }
317317
| ExprKind::Continue { .. }
318318
| ExprKind::Return { .. }
319+
| ExprKind::Become { .. }
319320
| ExprKind::Yield { .. }
320321
| ExprKind::Loop { .. }
321322
| ExprKind::Let { .. }

compiler/rustc_mir_build/src/thir/cx/expr.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -694,12 +694,8 @@ impl<'tcx> Cx<'tcx> {
694694

695695
ExprKind::Repeat { value: self.mirror_expr(v), count: *count }
696696
}
697-
hir::ExprKind::Ret(ref v) => ExprKind::Return { value: v.map(|v| self.mirror_expr(v)) },
698-
hir::ExprKind::Become(call) => {
699-
// FIXME(explicit_tail_calls): use `ExprKind::Become` once we implemented it
700-
// Temporary transform `become` into a `return`, so we can write tests for code before this stage
701-
ExprKind::Return { value: Some(self.mirror_expr(call)) }
702-
}
697+
hir::ExprKind::Ret(v) => ExprKind::Return { value: v.map(|v| self.mirror_expr(v)) },
698+
hir::ExprKind::Become(call) => ExprKind::Become { value: self.mirror_expr(call) },
703699
hir::ExprKind::Break(dest, ref value) => match dest.target_id {
704700
Ok(target_id) => ExprKind::Break {
705701
label: region::Scope { id: target_id.local_id, data: region::ScopeData::Node },

compiler/rustc_mir_build/src/thir/print.rs

+6
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,12 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
421421

422422
print_indented!(self, "}", depth_lvl);
423423
}
424+
Become { value } => {
425+
print_indented!(self, "Become {", depth_lvl);
426+
print_indented!(self, "value:", depth_lvl + 1);
427+
self.print_expr(*value, depth_lvl + 2);
428+
print_indented!(self, "}", depth_lvl);
429+
}
424430
ConstBlock { did, substs } => {
425431
print_indented!(self, "ConstBlock {", depth_lvl);
426432
print_indented!(self, format!("did: {:?}", did), depth_lvl + 1);

compiler/rustc_ty_utils/src/consts.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ fn recurse_build<'tcx>(
241241
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
242242
error(GenericConstantTooComplexSub::AssignNotSupported(node.span))?
243243
}
244-
ExprKind::Closure { .. } | ExprKind::Return { .. } => {
244+
// FIXME(explicit_tail_calls): maybe get `become` a new error
245+
ExprKind::Closure { .. } | ExprKind::Return { .. } | ExprKind::Become { .. } => {
245246
error(GenericConstantTooComplexSub::ClosureAndReturnNotSupported(node.span))?
246247
}
247248
// let expressions imply control flow
@@ -337,6 +338,7 @@ impl<'a, 'tcx> IsThirPolymorphic<'a, 'tcx> {
337338
| thir::ExprKind::Break { .. }
338339
| thir::ExprKind::Continue { .. }
339340
| thir::ExprKind::Return { .. }
341+
| thir::ExprKind::Become { .. }
340342
| thir::ExprKind::Array { .. }
341343
| thir::ExprKind::Tuple { .. }
342344
| thir::ExprKind::Adt(_)

0 commit comments

Comments
 (0)