Skip to content

Commit b47c983

Browse files
committed
[WIP] Use anon consts for #[const_continue]
1 parent 76629d1 commit b47c983

File tree

6 files changed

+29
-37
lines changed

6 files changed

+29
-37
lines changed

compiler/rustc_middle/src/thir.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,8 @@ pub enum ExprKind<'tcx> {
462462
/// A `#[const_continue] break` expression.
463463
ConstContinue {
464464
label: region::Scope,
465-
value: ExprId,
465+
did: DefId,
466+
args: GenericArgsRef<'tcx>,
466467
},
467468
/// A `return` expression.
468469
Return {

compiler/rustc_middle/src/thir/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
110110
}
111111
}
112112
Continue { label: _ } => {}
113-
ConstContinue { value, label: _ } => visitor.visit_expr(&visitor.thir()[value]),
113+
ConstContinue { label: _, did: _, args: _ } => {},
114114
Return { value } => {
115115
if let Some(value) = value {
116116
visitor.visit_expr(&visitor.thir()[value])

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9292
ExprKind::Break { label, value } => {
9393
this.break_scope(block, value, BreakableTarget::Break(label), source_info)
9494
}
95-
ExprKind::ConstContinue { label, value } => {
96-
this.break_const_continuable_scope(block, value, label, source_info)
95+
ExprKind::ConstContinue { label, did, args } => {
96+
this.break_const_continuable_scope(block, did, args, expr.ty, label, source_info)
9797
}
9898
ExprKind::Return { value } => {
9999
this.break_scope(block, value, BreakableTarget::Return, source_info)

compiler/rustc_mir_build/src/builder/scope.rs

+8-29
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ use std::mem;
8585

8686
use rustc_data_structures::fx::FxHashMap;
8787
use rustc_hir::HirId;
88+
use rustc_hir::def_id::DefId;
8889
use rustc_index::{IndexSlice, IndexVec};
8990
use rustc_middle::middle::region;
9091
use rustc_middle::mir::*;
9192
use rustc_middle::thir::{AdtExpr, AdtExprBase, ArmId, ExprId, ExprKind, LintLevel};
92-
use rustc_middle::ty::ValTree;
93+
use rustc_middle::ty::{GenericArgsRef, Ty, ValTree};
9394
use rustc_middle::{bug, span_bug, ty};
9495
use rustc_pattern_analysis::rustc::RustcPatCtxt;
9596
use rustc_session::lint::Level;
@@ -814,39 +815,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
814815
pub(crate) fn break_const_continuable_scope(
815816
&mut self,
816817
mut block: BasicBlock,
817-
value: ExprId,
818+
did: DefId,
819+
args: GenericArgsRef<'tcx>,
820+
ty: Ty<'tcx>,
818821
scope: region::Scope,
819822
source_info: SourceInfo,
820823
) -> BlockAnd<()> {
821824
let span = source_info.span;
822825

823-
// A break can only break out of a scope, so the value should be a scope.
824-
let rustc_middle::thir::ExprKind::Scope { value, .. } = self.thir[value].kind else {
825-
span_bug!(span, "break value must be a scope")
826-
};
827-
828-
let constant = match &self.thir[value].kind {
829-
ExprKind::Adt(box AdtExpr { variant_index, fields, base, .. }) => {
830-
assert!(matches!(base, AdtExprBase::None));
831-
assert!(fields.is_empty());
832-
ConstOperand {
833-
span: self.thir[value].span,
834-
user_ty: None,
835-
const_: Const::Ty(
836-
self.thir[value].ty,
837-
ty::Const::new_value(
838-
self.tcx,
839-
ValTree::from_branches(
840-
self.tcx,
841-
[ValTree::from_scalar_int(self.tcx, variant_index.as_u32().into())],
842-
),
843-
self.thir[value].ty,
844-
),
845-
),
846-
}
847-
}
848-
_ => self.as_constant(&self.thir[value]),
849-
};
826+
let uneval = UnevaluatedConst::new(did, args);
827+
let const_ = Const::Unevaluated(uneval, ty);
828+
let constant = ConstOperand { user_ty: None, span, const_ };
850829

851830
let break_index = self
852831
.scopes

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -808,12 +808,24 @@ impl<'tcx> ThirBuildCx<'tcx> {
808808
self.tcx.dcx().emit_fatal(ConstContinueMissingValue { span })
809809
};
810810

811+
let ty = self.typeck_results.node_type(anon_const.hir_id);
812+
let did = anon_const.def_id.to_def_id();
813+
let typeck_root_def_id = tcx.typeck_root_def_id(did);
814+
let parent_args = tcx.erase_regions(GenericArgs::identity_for_item(
815+
tcx,
816+
typeck_root_def_id,
817+
));
818+
let args =
819+
InlineConstArgs::new(tcx, InlineConstArgsParts { parent_args, ty })
820+
.args;
821+
811822
ExprKind::ConstContinue {
812823
label: region::Scope {
813824
local_id: target_id.local_id,
814825
data: region::ScopeData::Node,
815826
},
816-
value: self.mirror_expr(value),
827+
did,
828+
args,
817829
}
818830
}
819831
Err(err) => bug!("invalid loop id for break: {}", err),

compiler/rustc_mir_build/src/thir/print.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,11 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
430430
print_indented!(self, format!("label: {:?}", label), depth_lvl + 1);
431431
print_indented!(self, "}", depth_lvl);
432432
}
433-
ConstContinue { label, value } => {
433+
ConstContinue { label, did, args } => {
434434
print_indented!(self, "ConstContinue (", depth_lvl);
435435
print_indented!(self, format!("label: {:?}", label), depth_lvl + 1);
436-
print_indented!(self, "value:", depth_lvl + 1);
437-
self.print_expr(*value, depth_lvl + 2);
436+
print_indented!(self, format!("did: {:?}", did), depth_lvl + 1);
437+
print_indented!(self, format!("args: {:?}", args), depth_lvl + 1);
438438
print_indented!(self, ")", depth_lvl);
439439
}
440440
Return { value } => {

0 commit comments

Comments
 (0)