Skip to content

Commit 090d764

Browse files
committed
Remove the is_inline field from PatKind::ExpandedConstant
1 parent de57c05 commit 090d764

File tree

8 files changed

+32
-51
lines changed

8 files changed

+32
-51
lines changed

compiler/rustc_middle/src/thir.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -812,23 +812,17 @@ pub enum PatKind<'tcx> {
812812
},
813813

814814
/// Pattern obtained by converting a constant (inline or named) to its pattern
815-
/// representation using `const_to_pat`.
815+
/// representation using `const_to_pat`. This is used for unsafety checking.
816816
ExpandedConstant {
817-
/// [DefId] of the constant, we need this so that we have a
818-
/// reference that can be used by unsafety checking to visit nested
819-
/// unevaluated constants and for diagnostics. If the `DefId` doesn't
820-
/// correspond to a local crate, it points at the `const` item.
817+
/// [DefId] of the constant item.
821818
def_id: DefId,
822-
/// If `false`, then `def_id` points at a `const` item, otherwise it
823-
/// corresponds to a local inline const.
824-
is_inline: bool,
825-
/// If the inline constant is used in a range pattern, this subpattern
826-
/// represents the range (if both ends are inline constants, there will
827-
/// be multiple InlineConstant wrappers).
819+
/// The pattern that the constant lowered to.
828820
///
829-
/// Otherwise, the actual pattern that the constant lowered to. As with
830-
/// other constants, inline constants are matched structurally where
831-
/// possible.
821+
/// HACK: we need to keep the `DefId` of inline constants around for unsafety checking;
822+
/// therefore when a range pattern contains inline constants, we re-wrap the range pattern
823+
/// with the `ExpandedConstant` nodes that correspond to the range endpoints. Hence
824+
/// `subpattern` may actually be a range pattern, and `def_id` be the constant for one of
825+
/// its endpoints.
832826
subpattern: Box<Pat<'tcx>>,
833827
},
834828

compiler/rustc_mir_build/src/builder/custom/parse/instruction.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
145145
let arm = &self.thir[*arm];
146146
let value = match arm.pattern.kind {
147147
PatKind::Constant { value } => value,
148-
PatKind::ExpandedConstant { ref subpattern, def_id: _, is_inline: false }
148+
PatKind::ExpandedConstant { ref subpattern, def_id: _ }
149149
if let PatKind::Constant { value } = subpattern.kind =>
150150
{
151151
value

compiler/rustc_mir_build/src/builder/matches/match_pair.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::sync::Arc;
22

3+
use rustc_hir::def::DefKind;
34
use rustc_middle::mir::*;
45
use rustc_middle::thir::*;
56
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
@@ -201,15 +202,13 @@ impl<'tcx> MatchPairTree<'tcx> {
201202
None
202203
}
203204

204-
PatKind::ExpandedConstant { subpattern: ref pattern, def_id: _, is_inline: false } => {
205-
MatchPairTree::for_pattern(place_builder, pattern, cx, &mut subpairs, extra_data);
206-
None
207-
}
208-
PatKind::ExpandedConstant { subpattern: ref pattern, def_id, is_inline: true } => {
205+
PatKind::ExpandedConstant { subpattern: ref pattern, def_id } => {
209206
MatchPairTree::for_pattern(place_builder, pattern, cx, &mut subpairs, extra_data);
210207

211208
// Apply a type ascription for the inline constant to the value at `match_pair.place`
212-
if let Some(source) = place {
209+
if let Some(source) = place
210+
&& matches!(cx.tcx.def_kind(def_id), DefKind::InlineConst)
211+
{
213212
let span = pattern.span;
214213
let parent_id = cx.tcx.typeck_root_def_id(cx.def_id.to_def_id());
215214
let args = ty::InlineConstArgs::new(

compiler/rustc_mir_build/src/check_unsafety.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,9 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
403403
visit::walk_pat(self, pat);
404404
self.inside_adt = old_inside_adt;
405405
}
406-
PatKind::ExpandedConstant { def_id, is_inline, .. } => {
406+
PatKind::ExpandedConstant { def_id, .. } => {
407407
if let Some(def) = def_id.as_local()
408-
&& *is_inline
408+
&& matches!(self.tcx.def_kind(def_id), DefKind::InlineConst)
409409
{
410410
self.visit_inner_body(def);
411411
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
676676
unpeeled_pat = subpattern;
677677
}
678678

679-
if let PatKind::ExpandedConstant { def_id, is_inline: false, .. } = unpeeled_pat.kind
679+
if let PatKind::ExpandedConstant { def_id, .. } = unpeeled_pat.kind
680680
&& let DefKind::Const = self.tcx.def_kind(def_id)
681681
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span)
682682
// We filter out paths with multiple path::segments.
@@ -1296,7 +1296,8 @@ fn report_non_exhaustive_match<'p, 'tcx>(
12961296

12971297
for &arm in arms {
12981298
let arm = &thir.arms[arm];
1299-
if let PatKind::ExpandedConstant { def_id, is_inline: false, .. } = arm.pattern.kind
1299+
if let PatKind::ExpandedConstant { def_id, .. } = arm.pattern.kind
1300+
&& !matches!(cx.tcx.def_kind(def_id), DefKind::InlineConst)
13001301
&& let Ok(snippet) = cx.tcx.sess.source_map().span_to_snippet(arm.pattern.span)
13011302
// We filter out paths with multiple path::segments.
13021303
&& snippet.chars().all(|c| c.is_alphanumeric() || c == '_')

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_apfloat::Float;
55
use rustc_data_structures::fx::FxHashSet;
66
use rustc_errors::Diag;
77
use rustc_hir as hir;
8-
use rustc_hir::def::DefKind;
98
use rustc_index::Idx;
109
use rustc_infer::infer::TyCtxtInferExt;
1110
use rustc_infer::traits::Obligation;
@@ -185,11 +184,7 @@ impl<'tcx> ConstToPat<'tcx> {
185184

186185
// Wrap the pattern in a marker node to indicate that it is the result of lowering a
187186
// constant. This is used for diagnostics, and for unsafety checking of inline const blocks.
188-
let kind = PatKind::ExpandedConstant {
189-
subpattern: inlined_const_as_pat,
190-
def_id: uv.def,
191-
is_inline: matches!(self.tcx.def_kind(uv.def), DefKind::InlineConst),
192-
};
187+
let kind = PatKind::ExpandedConstant { subpattern: inlined_const_as_pat, def_id: uv.def };
193188
Box::new(Pat { kind, ty, span: self.span })
194189
}
195190

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

+11-18
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_middle::thir::{
2020
use rustc_middle::ty::layout::IntegerExt;
2121
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt, TypeVisitableExt};
2222
use rustc_middle::{bug, span_bug};
23-
use rustc_span::def_id::LocalDefId;
23+
use rustc_span::def_id::DefId;
2424
use rustc_span::{ErrorGuaranteed, Span};
2525
use tracing::{debug, instrument};
2626

@@ -124,7 +124,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
124124
expr: Option<&'tcx hir::PatExpr<'tcx>>,
125125
// Out-parameters collecting extra data to be reapplied by the caller
126126
ascriptions: &mut Vec<Ascription<'tcx>>,
127-
inline_consts: &mut Vec<LocalDefId>,
127+
expanded_consts: &mut Vec<DefId>,
128128
) -> Result<Option<PatRangeBoundary<'tcx>>, ErrorGuaranteed> {
129129
let Some(expr) = expr else { return Ok(None) };
130130

@@ -139,10 +139,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
139139
ascriptions.push(ascription);
140140
kind = subpattern.kind;
141141
}
142-
PatKind::ExpandedConstant { is_inline, def_id, subpattern } => {
143-
if is_inline {
144-
inline_consts.extend(def_id.as_local());
145-
}
142+
PatKind::ExpandedConstant { def_id, subpattern } => {
143+
expanded_consts.push(def_id);
146144
kind = subpattern.kind;
147145
}
148146
_ => break,
@@ -221,10 +219,10 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
221219

222220
// Collect extra data while lowering the endpoints, to be reapplied later.
223221
let mut ascriptions = vec![];
224-
let mut inline_consts = vec![];
222+
let mut expanded_consts = vec![];
225223

226224
let mut lower_endpoint =
227-
|expr| self.lower_pattern_range_endpoint(expr, &mut ascriptions, &mut inline_consts);
225+
|expr| self.lower_pattern_range_endpoint(expr, &mut ascriptions, &mut expanded_consts);
228226

229227
let lo = lower_endpoint(lo_expr)?.unwrap_or(PatRangeBoundary::NegInfinity);
230228
let hi = lower_endpoint(hi_expr)?.unwrap_or(PatRangeBoundary::PosInfinity);
@@ -269,17 +267,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
269267
// `Foo::<'a>::A..=Foo::B`), we need to put the ascriptions for the associated
270268
// constants somewhere. Have them on the range pattern.
271269
for ascription in ascriptions {
272-
kind = PatKind::AscribeUserType {
273-
ascription,
274-
subpattern: Box::new(Pat { span, ty, kind }),
275-
};
270+
let subpattern = Box::new(Pat { span, ty, kind });
271+
kind = PatKind::AscribeUserType { ascription, subpattern };
276272
}
277-
for def in inline_consts {
278-
kind = PatKind::ExpandedConstant {
279-
def_id: def.to_def_id(),
280-
is_inline: true,
281-
subpattern: Box::new(Pat { span, ty, kind }),
282-
};
273+
for def_id in expanded_consts {
274+
let subpattern = Box::new(Pat { span, ty, kind });
275+
kind = PatKind::ExpandedConstant { def_id, subpattern };
283276
}
284277
Ok(kind)
285278
}

compiler/rustc_mir_build/src/thir/print.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -740,10 +740,9 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
740740
print_indented!(self, format!("value: {:?}", value), depth_lvl + 2);
741741
print_indented!(self, "}", depth_lvl + 1);
742742
}
743-
PatKind::ExpandedConstant { def_id, is_inline, subpattern } => {
743+
PatKind::ExpandedConstant { def_id, subpattern } => {
744744
print_indented!(self, "ExpandedConstant {", depth_lvl + 1);
745745
print_indented!(self, format!("def_id: {def_id:?}"), depth_lvl + 2);
746-
print_indented!(self, format!("is_inline: {is_inline:?}"), depth_lvl + 2);
747746
print_indented!(self, "subpattern:", depth_lvl + 2);
748747
self.print_pat(subpattern, depth_lvl + 2);
749748
print_indented!(self, "}", depth_lvl + 1);

0 commit comments

Comments
 (0)