Skip to content

Commit de57c05

Browse files
committed
Let const_to_pat handle the ExpandedConstant wrapping
1 parent f5c5102 commit de57c05

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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;
89
use rustc_index::Idx;
910
use rustc_infer::infer::TyCtxtInferExt;
1011
use rustc_infer::traits::Obligation;
@@ -182,7 +183,14 @@ impl<'tcx> ConstToPat<'tcx> {
182183
}
183184
}
184185

185-
inlined_const_as_pat
186+
// Wrap the pattern in a marker node to indicate that it is the result of lowering a
187+
// 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+
};
193+
Box::new(Pat { kind, ty, span: self.span })
186194
}
187195

188196
fn field_pats(

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -567,15 +567,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
567567
// Lower the named constant to a THIR pattern.
568568
let args = self.typeck_results.node_args(id);
569569
let c = ty::Const::new_unevaluated(self.tcx, ty::UnevaluatedConst { def: def_id, args });
570-
let subpattern = self.const_to_pat(c, ty, id, span);
571-
572-
// Wrap the pattern in a marker node to indicate that it is the result
573-
// of lowering a named constant. This marker is used for improved
574-
// diagnostics in some situations, but has no effect at runtime.
575-
let mut pattern = {
576-
let kind = PatKind::ExpandedConstant { subpattern, def_id, is_inline: false };
577-
Box::new(Pat { span, ty, kind })
578-
};
570+
let mut pattern = self.const_to_pat(c, ty, id, span);
579571

580572
// If this is an associated constant with an explicit user-written
581573
// type, add an ascription node (e.g. `<Foo<'a> as MyTrait>::CONST`).
@@ -619,11 +611,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
619611
debug_assert!(!args.has_free_regions());
620612

621613
let ct = ty::UnevaluatedConst { def: def_id.to_def_id(), args };
622-
let subpattern = self.const_to_pat(ty::Const::new_unevaluated(self.tcx, ct), ty, id, span);
623-
624-
// Wrap the pattern in a marker node to indicate that it is the result
625-
// of lowering an inline const block.
626-
PatKind::ExpandedConstant { subpattern, def_id: def_id.to_def_id(), is_inline: true }
614+
let c = ty::Const::new_unevaluated(self.tcx, ct);
615+
self.const_to_pat(c, ty, id, span).kind
627616
}
628617

629618
/// Lowers the kinds of "expression" that can appear in a HIR pattern:

0 commit comments

Comments
 (0)