@@ -13,12 +13,15 @@ use rustc_hir::def::{CtorOf, DefKind, Res};
1313use rustc_hir:: pat_util:: EnumerateAndAdjustIterator ;
1414use rustc_hir:: { self as hir, RangeEnd } ;
1515use rustc_index:: Idx ;
16+ use rustc_infer:: infer:: TyCtxtInferExt ;
1617use rustc_middle:: mir:: interpret:: LitToConstInput ;
1718use rustc_middle:: thir:: {
1819 Ascription , FieldPat , LocalVarId , Pat , PatKind , PatRange , PatRangeBoundary ,
1920} ;
2021use 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+ } ;
2225use rustc_middle:: { bug, span_bug} ;
2326use rustc_span:: def_id:: DefId ;
2427use 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