@@ -284,6 +284,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
284
284
..
285
285
} ,
286
286
user_ty : pat_ascription_ty,
287
+ variance : _,
287
288
user_ty_span,
288
289
} => {
289
290
let place =
@@ -310,6 +311,20 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
310
311
source_info : ty_source_info,
311
312
kind : StatementKind :: AscribeUserType (
312
313
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>`.
313
328
ty:: Variance :: Invariant ,
314
329
user_ty,
315
330
) ,
@@ -541,12 +556,20 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
541
556
PatternKind :: Deref { ref subpattern } => {
542
557
self . visit_bindings ( subpattern, pattern_user_ty. deref ( ) , f) ;
543
558
}
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
+ } => {
545
565
// This corresponds to something like
546
566
//
547
567
// ```
548
568
// let A::<'a>(_): A<'static> = ...;
549
569
// ```
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.
550
573
let annotation = ( user_ty_span, user_ty. base ) ;
551
574
let projection = UserTypeProjection {
552
575
base : self . canonical_user_type_annotations . push ( annotation) ,
@@ -628,6 +651,7 @@ struct Ascription<'tcx> {
628
651
span : Span ,
629
652
source : Place < ' tcx > ,
630
653
user_ty : PatternTypeProjection < ' tcx > ,
654
+ variance : ty:: Variance ,
631
655
}
632
656
633
657
#[ derive( Clone , Debug ) ]
@@ -1321,7 +1345,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
1321
1345
source_info,
1322
1346
kind : StatementKind :: AscribeUserType (
1323
1347
ascription. source . clone ( ) ,
1324
- ty :: Variance :: Covariant ,
1348
+ ascription . variance ,
1325
1349
user_ty,
1326
1350
) ,
1327
1351
} ,
0 commit comments