Skip to content

Support const blocks in const_continue #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: loop_match_attr
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ pub enum ExprKind<'tcx> {
/// A `#[const_continue] break` expression.
ConstContinue {
label: region::Scope,
value: ExprId,
did: DefId,
args: GenericArgsRef<'tcx>,
},
/// A `return` expression.
Return {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/thir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
}
}
Continue { label: _ } => {}
ConstContinue { value, label: _ } => visitor.visit_expr(&visitor.thir()[value]),
ConstContinue { label: _, did: _, args: _ } => {},
Return { value } => {
if let Some(value) = value {
visitor.visit_expr(&visitor.thir()[value])
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/builder/expr/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
ExprKind::Break { label, value } => {
this.break_scope(block, value, BreakableTarget::Break(label), source_info)
}
ExprKind::ConstContinue { label, value } => {
this.break_const_continuable_scope(block, value, label, source_info)
ExprKind::ConstContinue { label, did, args } => {
this.break_const_continuable_scope(block, did, args, expr.ty, label, source_info)
}
ExprKind::Return { value } => {
this.break_scope(block, value, BreakableTarget::Return, source_info)
Expand Down
37 changes: 8 additions & 29 deletions compiler/rustc_mir_build/src/builder/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ use std::mem;
use interpret::ErrorHandled;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::HirId;
use rustc_hir::def_id::DefId;
use rustc_index::{IndexSlice, IndexVec};
use rustc_middle::middle::region;
use rustc_middle::mir::{self, *};
use rustc_middle::thir::{AdtExpr, AdtExprBase, ArmId, ExprId, ExprKind, LintLevel};
use rustc_middle::ty::{Ty, TypeVisitableExt, ValTree};
use rustc_middle::ty::{GenericArgsRef, Ty, TypeVisitableExt, ValTree};
use rustc_middle::{bug, span_bug, ty};
use rustc_pattern_analysis::rustc::RustcPatCtxt;
use rustc_session::lint::Level;
Expand Down Expand Up @@ -854,39 +855,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
pub(crate) fn break_const_continuable_scope(
&mut self,
mut block: BasicBlock,
value: ExprId,
did: DefId,
args: GenericArgsRef<'tcx>,
ty: Ty<'tcx>,
scope: region::Scope,
source_info: SourceInfo,
) -> BlockAnd<()> {
let span = source_info.span;

// A break can only break out of a scope, so the value should be a scope.
let rustc_middle::thir::ExprKind::Scope { value, .. } = self.thir[value].kind else {
span_bug!(span, "break value must be a scope")
};

let constant = match &self.thir[value].kind {
ExprKind::Adt(box AdtExpr { variant_index, fields, base, .. }) => {
assert!(matches!(base, AdtExprBase::None));
assert!(fields.is_empty());
ConstOperand {
span: self.thir[value].span,
user_ty: None,
const_: Const::Ty(
self.thir[value].ty,
ty::Const::new_value(
self.tcx,
ValTree::from_branches(
self.tcx,
[ValTree::from_scalar_int(self.tcx, variant_index.as_u32().into())],
),
self.thir[value].ty,
),
),
}
}
_ => self.as_constant(&self.thir[value]),
};
let uneval = UnevaluatedConst::new(did, args);
let const_ = Const::Unevaluated(uneval, ty);
let constant = ConstOperand { user_ty: None, span, const_ };

let break_index = self
.scopes
Expand Down
14 changes: 13 additions & 1 deletion compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,12 +808,24 @@ impl<'tcx> ThirBuildCx<'tcx> {
self.tcx.dcx().emit_fatal(ConstContinueMissingValue { span })
};

let ty = self.typeck_results.node_type(anon_const.hir_id);
let did = anon_const.def_id.to_def_id();
let typeck_root_def_id = tcx.typeck_root_def_id(did);
let parent_args = tcx.erase_regions(GenericArgs::identity_for_item(
tcx,
typeck_root_def_id,
));
let args =
InlineConstArgs::new(tcx, InlineConstArgsParts { parent_args, ty })
.args;

ExprKind::ConstContinue {
label: region::Scope {
local_id: target_id.local_id,
data: region::ScopeData::Node,
},
value: self.mirror_expr(value),
did,
args,
}
}
Err(err) => bug!("invalid loop id for break: {}", err),
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_mir_build/src/thir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,11 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
print_indented!(self, format!("label: {:?}", label), depth_lvl + 1);
print_indented!(self, "}", depth_lvl);
}
ConstContinue { label, value } => {
ConstContinue { label, did, args } => {
print_indented!(self, "ConstContinue (", depth_lvl);
print_indented!(self, format!("label: {:?}", label), depth_lvl + 1);
print_indented!(self, "value:", depth_lvl + 1);
self.print_expr(*value, depth_lvl + 2);
print_indented!(self, format!("did: {:?}", did), depth_lvl + 1);
print_indented!(self, format!("args: {:?}", args), depth_lvl + 1);
print_indented!(self, ")", depth_lvl);
}
Return { value } => {
Expand Down
Loading