Skip to content

Commit 7ff01ec

Browse files
committed
[WIP] Use anon consts for #[const_continue]
1 parent 70b2f11 commit 7ff01ec

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
@@ -86,11 +86,12 @@ use std::mem;
8686
use interpret::ErrorHandled;
8787
use rustc_data_structures::fx::FxHashMap;
8888
use rustc_hir::HirId;
89+
use rustc_hir::def_id::DefId;
8990
use rustc_index::{IndexSlice, IndexVec};
9091
use rustc_middle::middle::region;
9192
use rustc_middle::mir::{self, *};
9293
use rustc_middle::thir::{AdtExpr, AdtExprBase, ArmId, ExprId, ExprKind, LintLevel};
93-
use rustc_middle::ty::{Ty, TypeVisitableExt, ValTree};
94+
use rustc_middle::ty::{GenericArgsRef, Ty, TypeVisitableExt, ValTree};
9495
use rustc_middle::{bug, span_bug, ty};
9596
use rustc_pattern_analysis::rustc::RustcPatCtxt;
9697
use rustc_session::lint::Level;
@@ -854,39 +855,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
854855
pub(crate) fn break_const_continuable_scope(
855856
&mut self,
856857
mut block: BasicBlock,
857-
value: ExprId,
858+
did: DefId,
859+
args: GenericArgsRef<'tcx>,
860+
ty: Ty<'tcx>,
858861
scope: region::Scope,
859862
source_info: SourceInfo,
860863
) -> BlockAnd<()> {
861864
let span = source_info.span;
862865

863-
// A break can only break out of a scope, so the value should be a scope.
864-
let rustc_middle::thir::ExprKind::Scope { value, .. } = self.thir[value].kind else {
865-
span_bug!(span, "break value must be a scope")
866-
};
867-
868-
let constant = match &self.thir[value].kind {
869-
ExprKind::Adt(box AdtExpr { variant_index, fields, base, .. }) => {
870-
assert!(matches!(base, AdtExprBase::None));
871-
assert!(fields.is_empty());
872-
ConstOperand {
873-
span: self.thir[value].span,
874-
user_ty: None,
875-
const_: Const::Ty(
876-
self.thir[value].ty,
877-
ty::Const::new_value(
878-
self.tcx,
879-
ValTree::from_branches(
880-
self.tcx,
881-
[ValTree::from_scalar_int(self.tcx, variant_index.as_u32().into())],
882-
),
883-
self.thir[value].ty,
884-
),
885-
),
886-
}
887-
}
888-
_ => self.as_constant(&self.thir[value]),
889-
};
866+
let uneval = UnevaluatedConst::new(did, args);
867+
let const_ = Const::Unevaluated(uneval, ty);
868+
let constant = ConstOperand { user_ty: None, span, const_ };
890869

891870
let break_index = self
892871
.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)