@@ -13,12 +13,15 @@ use rustc_hir::def::{CtorOf, DefKind, Res};
13
13
use rustc_hir:: pat_util:: EnumerateAndAdjustIterator ;
14
14
use rustc_hir:: { self as hir, LangItem , RangeEnd } ;
15
15
use rustc_index:: Idx ;
16
+ use rustc_infer:: infer:: TyCtxtInferExt ;
16
17
use rustc_middle:: mir:: interpret:: LitToConstInput ;
17
18
use rustc_middle:: thir:: {
18
19
Ascription , FieldPat , LocalVarId , Pat , PatKind , PatRange , PatRangeBoundary ,
19
20
} ;
20
21
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
+ } ;
22
25
use rustc_middle:: { bug, span_bug} ;
23
26
use rustc_span:: def_id:: DefId ;
24
27
use rustc_span:: { ErrorGuaranteed , Span } ;
@@ -605,7 +608,36 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
605
608
606
609
let ct = ty:: UnevaluatedConst { def : def_id. to_def_id ( ) , args } ;
607
610
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
+ }
609
641
}
610
642
611
643
/// Lowers the kinds of "expression" that can appear in a HIR pattern:
0 commit comments