Skip to content

Commit 961c746

Browse files
committed
Add the inline const type annotation in pattern lowering
1 parent 50e10b3 commit 961c746

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

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

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

3-
use rustc_hir::def::DefKind;
43
use rustc_middle::mir::*;
54
use rustc_middle::thir::*;
65
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
@@ -202,35 +201,8 @@ impl<'tcx> MatchPairTree<'tcx> {
202201
None
203202
}
204203

205-
PatKind::ExpandedConstant { subpattern: ref pattern, def_id } => {
204+
PatKind::ExpandedConstant { subpattern: ref pattern, .. } => {
206205
MatchPairTree::for_pattern(place_builder, pattern, cx, &mut subpairs, extra_data);
207-
208-
// Apply a type ascription for the inline constant to the value at `match_pair.place`
209-
if let Some(source) = place
210-
&& matches!(cx.tcx.def_kind(def_id), DefKind::InlineConst)
211-
{
212-
let span = pattern.span;
213-
let parent_id = cx.tcx.typeck_root_def_id(cx.def_id.to_def_id());
214-
let args = ty::InlineConstArgs::new(
215-
cx.tcx,
216-
ty::InlineConstArgsParts {
217-
parent_args: ty::GenericArgs::identity_for_item(cx.tcx, parent_id),
218-
ty: cx.infcx.next_ty_var(span),
219-
},
220-
)
221-
.args;
222-
let user_ty = cx.infcx.canonicalize_user_type_annotation(ty::UserType::new(
223-
ty::UserTypeKind::TypeOf(def_id, ty::UserArgs { args, user_self_ty: None }),
224-
));
225-
let annotation = ty::CanonicalUserTypeAnnotation {
226-
inferred_ty: pattern.ty,
227-
span,
228-
user_ty: Box::new(user_ty),
229-
};
230-
let variance = ty::Contravariant;
231-
extra_data.ascriptions.push(super::Ascription { annotation, source, variance });
232-
}
233-
234206
None
235207
}
236208

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

+34-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ use rustc_hir::def::{CtorOf, DefKind, Res};
1313
use rustc_hir::pat_util::EnumerateAndAdjustIterator;
1414
use rustc_hir::{self as hir, LangItem, RangeEnd};
1515
use rustc_index::Idx;
16+
use rustc_infer::infer::TyCtxtInferExt;
1617
use rustc_middle::mir::interpret::LitToConstInput;
1718
use rustc_middle::thir::{
1819
Ascription, FieldPat, LocalVarId, Pat, PatKind, PatRange, PatRangeBoundary,
1920
};
2021
use rustc_middle::ty::layout::IntegerExt;
21-
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt, TypeVisitableExt};
22+
use rustc_middle::ty::{
23+
self, CanonicalUserTypeAnnotation, Ty, TyCtxt, TypeVisitableExt, TypingMode,
24+
};
2225
use rustc_middle::{bug, span_bug};
2326
use rustc_span::def_id::DefId;
2427
use rustc_span::{ErrorGuaranteed, Span};
@@ -605,7 +608,36 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
605608

606609
let ct = ty::UnevaluatedConst { def: def_id.to_def_id(), args };
607610
let c = ty::Const::new_unevaluated(self.tcx, ct);
608-
self.const_to_pat(c, ty, id, span).kind
611+
let pattern = self.const_to_pat(c, ty, id, span);
612+
613+
// Apply a type ascription for the inline constant.
614+
// FIXME: reusing the `args` above causes an ICE
615+
let annotation = {
616+
let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
617+
let args = ty::InlineConstArgs::new(
618+
tcx,
619+
ty::InlineConstArgsParts {
620+
parent_args: ty::GenericArgs::identity_for_item(tcx, typeck_root_def_id),
621+
ty: infcx.next_ty_var(span),
622+
},
623+
)
624+
.args;
625+
infcx.canonicalize_user_type_annotation(ty::UserType::new(ty::UserTypeKind::TypeOf(
626+
def_id.to_def_id(),
627+
ty::UserArgs { args, user_self_ty: None },
628+
)))
629+
};
630+
let annotation =
631+
CanonicalUserTypeAnnotation { user_ty: Box::new(annotation), span, inferred_ty: ty };
632+
PatKind::AscribeUserType {
633+
subpattern: pattern,
634+
ascription: Ascription {
635+
annotation,
636+
// Note that we use `Contravariant` here. See the `variance` field documentation
637+
// for details.
638+
variance: ty::Contravariant,
639+
},
640+
}
609641
}
610642

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

0 commit comments

Comments
 (0)