@@ -284,6 +284,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
284284 ..
285285 } ,
286286 user_ty : pat_ascription_ty,
287+ variance : _,
287288 user_ty_span,
288289 } => {
289290 let place =
@@ -310,6 +311,20 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
310311 source_info : ty_source_info,
311312 kind : StatementKind :: AscribeUserType (
312313 place,
314+ // We always use invariant as the variance here. This is because the
315+ // variance field from the ascription refers to the variance to use
316+ // when applying the type to the value being matched, but this
317+ // ascription applies rather to the type of the binding. e.g., in this
318+ // example:
319+ //
320+ // ```
321+ // let x: T = <expr>
322+ // ```
323+ //
324+ // We are creating an ascription that defines the type of `x` to be
325+ // exactly `T` (i.e., with invariance). The variance field, in
326+ // contrast, is intended to be used to relate `T` to the type of
327+ // `<expr>`.
313328 ty:: Variance :: Invariant ,
314329 user_ty,
315330 ) ,
@@ -541,12 +556,20 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
541556 PatternKind :: Deref { ref subpattern } => {
542557 self . visit_bindings ( subpattern, pattern_user_ty. deref ( ) , f) ;
543558 }
544- PatternKind :: AscribeUserType { ref subpattern, ref user_ty, user_ty_span } => {
559+ PatternKind :: AscribeUserType {
560+ ref subpattern,
561+ ref user_ty,
562+ user_ty_span,
563+ variance : _,
564+ } => {
545565 // This corresponds to something like
546566 //
547567 // ```
548568 // let A::<'a>(_): A<'static> = ...;
549569 // ```
570+ //
571+ // Note that the variance doesn't apply here, as we are tracking the effect
572+ // of `user_ty` on any bindings contained with subpattern.
550573 let annotation = ( user_ty_span, user_ty. base ) ;
551574 let projection = UserTypeProjection {
552575 base : self . canonical_user_type_annotations . push ( annotation) ,
@@ -628,6 +651,7 @@ struct Ascription<'tcx> {
628651 span : Span ,
629652 source : Place < ' tcx > ,
630653 user_ty : PatternTypeProjection < ' tcx > ,
654+ variance : ty:: Variance ,
631655}
632656
633657#[ derive( Clone , Debug ) ]
@@ -1321,7 +1345,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
13211345 source_info,
13221346 kind : StatementKind :: AscribeUserType (
13231347 ascription. source . clone ( ) ,
1324- ty :: Variance :: Covariant ,
1348+ ascription . variance ,
13251349 user_ty,
13261350 ) ,
13271351 } ,
0 commit comments