Skip to content

Commit 2d8ed99

Browse files
committed
Address review comments
Clean up code and add comments. Use InlineConstant to wrap range patterns.
1 parent 98b4c1e commit 2d8ed99

File tree

5 files changed

+43
-38
lines changed

5 files changed

+43
-38
lines changed

compiler/rustc_middle/src/thir.rs

+10
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,18 @@ pub enum PatKind<'tcx> {
749749
value: mir::Const<'tcx>,
750750
},
751751

752+
/// Pattern lowered from an inline constant
752753
InlineConstant {
754+
/// Unevaluated version of the constant, we need this for:
755+
/// 1. Having a reference that can be used by unsafety checking to visit nested
756+
/// unevaluated constants.
757+
/// 2. During THIR building we turn this back to a [Self::Constant] in range patterns.
753758
value: mir::UnevaluatedConst<'tcx>,
759+
/// If the inline constant is used in a range pattern, this subpattern represents the range
760+
/// (if both ends are inline constants, there will be multiple InlineConstant wrappers).
761+
///
762+
/// Otherwise, the actual pattern that the constant lowered to. As with other constants, inline constants
763+
/// are matched structurally where possible.
754764
subpattern: Box<Pat<'tcx>>,
755765
},
756766

compiler/rustc_mir_build/src/build/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ fn mir_build<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Body<'tcx> {
6767
thir::BodyTy::Const(ty) => construct_const(tcx, def, thir, expr, ty),
6868
};
6969

70-
tcx.ensure().check_match(def);
7170
// this must run before MIR dump, because
7271
// "not all control paths return a value" is reported here.
7372
//

compiler/rustc_mir_build/src/check_unsafety.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::errors::*;
33
use rustc_middle::thir::visit::{self, Visitor};
44

55
use rustc_hir as hir;
6-
use rustc_middle::mir::{BorrowKind, Const};
6+
use rustc_middle::mir::BorrowKind;
77
use rustc_middle::thir::*;
88
use rustc_middle::ty::print::with_no_trimmed_paths;
99
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
@@ -124,7 +124,8 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
124124
/// Handle closures/generators/inline-consts, which is unsafecked with their parent body.
125125
fn visit_inner_body(&mut self, def: LocalDefId) {
126126
if let Ok((inner_thir, expr)) = self.tcx.thir_body(def) {
127-
let _ = self.tcx.ensure_with_value().mir_built(def);
127+
// Runs all other queries that depend on THIR.
128+
self.tcx.ensure_with_value().mir_built(def);
128129
let inner_thir = &inner_thir.steal();
129130
let hir_context = self.tcx.hir().local_def_id_to_hir_id(def);
130131
let mut inner_visitor = UnsafetyVisitor { thir: inner_thir, hir_context, ..*self };
@@ -278,20 +279,6 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
278279
visit::walk_pat(self, pat);
279280
self.inside_adt = old_inside_adt;
280281
}
281-
PatKind::Range(range) => {
282-
if let Const::Unevaluated(c, _) = range.lo {
283-
if let hir::def::DefKind::InlineConst = self.tcx.def_kind(c.def) {
284-
let def_id = c.def.expect_local();
285-
self.visit_inner_body(def_id);
286-
}
287-
}
288-
if let Const::Unevaluated(c, _) = range.hi {
289-
if let hir::def::DefKind::InlineConst = self.tcx.def_kind(c.def) {
290-
let def_id = c.def.expect_local();
291-
self.visit_inner_body(def_id);
292-
}
293-
}
294-
}
295282
PatKind::InlineConstant { value, .. } => {
296283
let def_id = value.def.expect_local();
297284
self.visit_inner_body(def_id);
@@ -804,7 +791,8 @@ pub fn thir_check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) {
804791
}
805792

806793
let Ok((thir, expr)) = tcx.thir_body(def) else { return };
807-
let _ = tcx.ensure_with_value().mir_built(def);
794+
// Runs all other queries that depend on THIR.
795+
tcx.ensure_with_value().mir_built(def);
808796
let thir = &thir.steal();
809797
// If `thir` is empty, a type error occurred, skip this body.
810798
if thir.exprs.is_empty() {

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+26-18
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ use rustc_index::Idx;
2020
use rustc_middle::mir::interpret::{
2121
ErrorHandled, GlobalId, LitToConstError, LitToConstInput, Scalar,
2222
};
23-
use rustc_middle::mir::{self, BorrowKind, Const, Mutability, UserTypeProjection};
23+
use rustc_middle::mir::{
24+
self, BorrowKind, Const, Mutability, UnevaluatedConst, UserTypeProjection,
25+
};
2426
use rustc_middle::thir::{Ascription, BindingMode, FieldPat, LocalVarId, Pat, PatKind, PatRange};
2527
use rustc_middle::ty::layout::IntegerExt;
2628
use rustc_middle::ty::{
@@ -88,19 +90,21 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
8890
fn lower_pattern_range_endpoint(
8991
&mut self,
9092
expr: Option<&'tcx hir::Expr<'tcx>>,
91-
) -> Result<(Option<mir::Const<'tcx>>, Option<Ascription<'tcx>>), ErrorGuaranteed> {
93+
) -> Result<
94+
(Option<mir::Const<'tcx>>, Option<Ascription<'tcx>>, Option<UnevaluatedConst<'tcx>>),
95+
ErrorGuaranteed,
96+
> {
9297
match expr {
93-
None => Ok((None, None)),
98+
None => Ok((None, None, None)),
9499
Some(expr) => {
95-
let (kind, ascr) = match self.lower_lit(expr) {
96-
PatKind::InlineConstant { subpattern, value } => (
97-
PatKind::Constant { value: Const::Unevaluated(value, subpattern.ty) },
98-
None,
99-
),
100+
let (kind, ascr, inline_const) = match self.lower_lit(expr) {
101+
PatKind::InlineConstant { subpattern, value } => {
102+
(subpattern.kind, None, Some(value))
103+
}
100104
PatKind::AscribeUserType { ascription, subpattern: box Pat { kind, .. } } => {
101-
(kind, Some(ascription))
105+
(kind, Some(ascription), None)
102106
}
103-
kind => (kind, None),
107+
kind => (kind, None, None),
104108
};
105109
let value = if let PatKind::Constant { value } = kind {
106110
value
@@ -110,7 +114,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
110114
);
111115
return Err(self.tcx.sess.delay_span_bug(expr.span, msg));
112116
};
113-
Ok((Some(value), ascr))
117+
Ok((Some(value), ascr, inline_const))
114118
}
115119
}
116120
}
@@ -181,8 +185,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
181185
return Err(self.tcx.sess.delay_span_bug(span, msg));
182186
}
183187

184-
let (lo, lo_ascr) = self.lower_pattern_range_endpoint(lo_expr)?;
185-
let (hi, hi_ascr) = self.lower_pattern_range_endpoint(hi_expr)?;
188+
let (lo, lo_ascr, lo_inline) = self.lower_pattern_range_endpoint(lo_expr)?;
189+
let (hi, hi_ascr, hi_inline) = self.lower_pattern_range_endpoint(hi_expr)?;
186190

187191
let lo = lo.unwrap_or_else(|| {
188192
// Unwrap is ok because the type is known to be numeric.
@@ -241,6 +245,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
241245
};
242246
}
243247
}
248+
for inline_const in [lo_inline, hi_inline] {
249+
if let Some(value) = inline_const {
250+
kind =
251+
PatKind::InlineConstant { value, subpattern: Box::new(Pat { span, ty, kind }) };
252+
}
253+
}
244254
Ok(kind)
245255
}
246256

@@ -606,11 +616,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
606616
// const eval path below.
607617
// FIXME: investigate the performance impact of removing this.
608618
let lit_input = match expr.kind {
609-
hir::ExprKind::Lit(ref lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: false }),
610-
hir::ExprKind::Unary(hir::UnOp::Neg, ref expr) => match expr.kind {
611-
hir::ExprKind::Lit(ref lit) => {
612-
Some(LitToConstInput { lit: &lit.node, ty, neg: true })
613-
}
619+
hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: false }),
620+
hir::ExprKind::Unary(hir::UnOp::Neg, expr) => match expr.kind {
621+
hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: true }),
614622
_ => None,
615623
},
616624
_ => None,

compiler/rustc_mir_build/src/thir/print.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
692692
}
693693
PatKind::Deref { subpattern } => {
694694
print_indented!(self, "Deref { ", depth_lvl + 1);
695-
print_indented!(self, "subpattern: ", depth_lvl + 2);
695+
print_indented!(self, "subpattern:", depth_lvl + 2);
696696
self.print_pat(subpattern, depth_lvl + 2);
697697
print_indented!(self, "}", depth_lvl + 1);
698698
}
@@ -704,7 +704,7 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
704704
PatKind::InlineConstant { value, subpattern } => {
705705
print_indented!(self, "InlineConstant {", depth_lvl + 1);
706706
print_indented!(self, format!("value: {:?}", value), depth_lvl + 2);
707-
print_indented!(self, "subpattern: ", depth_lvl + 2);
707+
print_indented!(self, "subpattern:", depth_lvl + 2);
708708
self.print_pat(subpattern, depth_lvl + 2);
709709
print_indented!(self, "}", depth_lvl + 1);
710710
}

0 commit comments

Comments
 (0)