Skip to content

Commit be012c7

Browse files
committed
change how ascribe user ty deals with variance
1 parent 7edcec0 commit be012c7

File tree

12 files changed

+41
-67
lines changed

12 files changed

+41
-67
lines changed

compiler/rustc_borrowck/src/constraint_generation.rs

-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ impl<'cg, 'cx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'tcx> {
151151
fn visit_ascribe_user_ty(
152152
&mut self,
153153
_place: &Place<'tcx>,
154-
_variance: &ty::Variance,
155154
_user_ty: &UserTypeProjection,
156155
_location: Location,
157156
) {

compiler/rustc_borrowck/src/type_check/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
365365
if let Some(annotation_index) = constant.user_ty {
366366
if let Err(terr) = self.cx.relate_type_and_user_type(
367367
constant.literal.ty(),
368-
ty::Variance::Invariant,
369368
&UserTypeProjection { base: annotation_index, projs: vec![] },
370369
location.to_locations(),
371370
ConstraintCategory::Boring,
@@ -425,6 +424,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
425424
ConstraintCategory::Boring,
426425
self.cx.param_env.and(type_op::ascribe_user_type::AscribeUserType::new(
427426
constant.literal.ty(),
427+
ty::Variance::Invariant,
428428
uv.def.did,
429429
UserSubsts { substs: uv.substs, user_self_ty: None },
430430
)),
@@ -492,7 +492,6 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
492492

493493
if let Err(terr) = self.cx.relate_type_and_user_type(
494494
ty,
495-
ty::Variance::Invariant,
496495
user_ty,
497496
Locations::All(*span),
498497
ConstraintCategory::TypeAnnotation,
@@ -1108,6 +1107,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11081107
ConstraintCategory::BoringNoLocation,
11091108
self.param_env.and(type_op::ascribe_user_type::AscribeUserType::new(
11101109
inferred_ty,
1110+
variance,
11111111
def_id,
11121112
user_substs,
11131113
)),
@@ -1178,7 +1178,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11781178
fn relate_type_and_user_type(
11791179
&mut self,
11801180
a: Ty<'tcx>,
1181-
v: ty::Variance,
11821181
user_ty: &UserTypeProjection,
11831182
locations: Locations,
11841183
category: ConstraintCategory,
@@ -1206,7 +1205,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
12061205
);
12071206

12081207
let ty = curr_projected_ty.ty;
1209-
self.relate_types(ty, v.xform(ty::Variance::Contravariant), a, locations, category)?;
1208+
self.relate_types(ty, ty::Variance::Invariant, a, locations, category)?;
12101209

12111210
Ok(())
12121211
}
@@ -1401,7 +1400,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
14011400
if let Some(annotation_index) = self.rvalue_user_ty(rv) {
14021401
if let Err(terr) = self.relate_type_and_user_type(
14031402
rv_ty,
1404-
ty::Variance::Invariant,
14051403
&UserTypeProjection { base: annotation_index, projs: vec![] },
14061404
location.to_locations(),
14071405
ConstraintCategory::Boring,
@@ -1453,11 +1451,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
14531451
);
14541452
};
14551453
}
1456-
StatementKind::AscribeUserType(box (ref place, ref projection), variance) => {
1454+
StatementKind::AscribeUserType(box (ref place, ref projection)) => {
14571455
let place_ty = place.ty(body, tcx).ty;
14581456
if let Err(terr) = self.relate_type_and_user_type(
14591457
place_ty,
1460-
variance,
14611458
projection,
14621459
Locations::All(stmt.source_info.span),
14631460
ConstraintCategory::TypeAnnotation,

compiler/rustc_middle/src/mir/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ pub enum StatementKind<'tcx> {
15811581
/// - `Contravariant` -- requires that `T_y :> T`
15821582
/// - `Invariant` -- requires that `T_y == T`
15831583
/// - `Bivariant` -- no effect
1584-
AscribeUserType(Box<(Place<'tcx>, UserTypeProjection)>, ty::Variance),
1584+
AscribeUserType(Box<(Place<'tcx>, UserTypeProjection)>),
15851585

15861586
/// Marks the start of a "coverage region", injected with '-Cinstrument-coverage'. A
15871587
/// `Coverage` statement carries metadata about the coverage region, used to inject a coverage
@@ -1705,8 +1705,8 @@ impl Debug for Statement<'_> {
17051705
SetDiscriminant { ref place, variant_index } => {
17061706
write!(fmt, "discriminant({:?}) = {:?}", place, variant_index)
17071707
}
1708-
AscribeUserType(box (ref place, ref c_ty), ref variance) => {
1709-
write!(fmt, "AscribeUserType({:?}, {:?}, {:?})", place, variance, c_ty)
1708+
AscribeUserType(box (ref place, ref c_ty)) => {
1709+
write!(fmt, "AscribeUserType({:?}, {:?})", place, c_ty)
17101710
}
17111711
Coverage(box self::Coverage { ref kind, code_region: Some(ref rgn) }) => {
17121712
write!(fmt, "Coverage::{:?} for {:?}", kind, rgn)

compiler/rustc_middle/src/mir/visit.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,9 @@ macro_rules! make_mir_visitor {
128128

129129
fn visit_ascribe_user_ty(&mut self,
130130
place: & $($mutability)? Place<'tcx>,
131-
variance: & $($mutability)? ty::Variance,
132131
user_ty: & $($mutability)? UserTypeProjection,
133132
location: Location) {
134-
self.super_ascribe_user_ty(place, variance, user_ty, location);
133+
self.super_ascribe_user_ty(place, user_ty, location);
135134
}
136135

137136
fn visit_coverage(&mut self,
@@ -413,9 +412,8 @@ macro_rules! make_mir_visitor {
413412
}
414413
StatementKind::AscribeUserType(
415414
box(ref $($mutability)? place, ref $($mutability)? user_ty),
416-
variance
417415
) => {
418-
self.visit_ascribe_user_ty(place, variance, user_ty, location);
416+
self.visit_ascribe_user_ty(place, user_ty, location);
419417
}
420418
StatementKind::Coverage(coverage) => {
421419
self.visit_coverage(
@@ -775,7 +773,6 @@ macro_rules! make_mir_visitor {
775773

776774
fn super_ascribe_user_ty(&mut self,
777775
place: & $($mutability)? Place<'tcx>,
778-
_variance: & $($mutability)? ty::Variance,
779776
user_ty: & $($mutability)? UserTypeProjection,
780777
location: Location) {
781778
self.visit_place(

compiler/rustc_middle/src/traits/query.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,26 @@ use std::mem;
2020
pub mod type_op {
2121
use crate::ty::fold::TypeFoldable;
2222
use crate::ty::subst::UserSubsts;
23-
use crate::ty::{Predicate, Ty};
23+
use crate::ty::{self, Predicate, Ty};
2424
use rustc_hir::def_id::DefId;
2525
use std::fmt;
2626

2727
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, HashStable, TypeFoldable, Lift)]
2828
pub struct AscribeUserType<'tcx> {
2929
pub mir_ty: Ty<'tcx>,
30+
pub variance: ty::Variance,
3031
pub def_id: DefId,
3132
pub user_substs: UserSubsts<'tcx>,
3233
}
3334

3435
impl<'tcx> AscribeUserType<'tcx> {
35-
pub fn new(mir_ty: Ty<'tcx>, def_id: DefId, user_substs: UserSubsts<'tcx>) -> Self {
36-
Self { mir_ty, def_id, user_substs }
36+
pub fn new(
37+
mir_ty: Ty<'tcx>,
38+
variance: ty::Variance,
39+
def_id: DefId,
40+
user_substs: UserSubsts<'tcx>,
41+
) -> Self {
42+
Self { mir_ty, variance, def_id, user_substs }
3743
}
3844
}
3945

compiler/rustc_mir_build/src/build/expr/as_place.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
501501
block,
502502
Statement {
503503
source_info,
504-
kind: StatementKind::AscribeUserType(
505-
Box::new((
506-
place,
507-
UserTypeProjection { base: annotation_index, projs: vec![] },
508-
)),
509-
Variance::Invariant,
510-
),
504+
kind: StatementKind::AscribeUserType(Box::new((
505+
place,
506+
UserTypeProjection { base: annotation_index, projs: vec![] },
507+
))),
511508
},
512509
);
513510
}
@@ -529,13 +526,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
529526
block,
530527
Statement {
531528
source_info,
532-
kind: StatementKind::AscribeUserType(
533-
Box::new((
534-
Place::from(temp),
535-
UserTypeProjection { base: annotation_index, projs: vec![] },
536-
)),
537-
Variance::Invariant,
538-
),
529+
kind: StatementKind::AscribeUserType(Box::new((
530+
Place::from(temp),
531+
UserTypeProjection { base: annotation_index, projs: vec![] },
532+
))),
539533
},
540534
);
541535
}

compiler/rustc_mir_build/src/build/matches/mod.rs

+2-22
Original file line numberDiff line numberDiff line change
@@ -546,24 +546,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
546546
block,
547547
Statement {
548548
source_info: ty_source_info,
549-
kind: StatementKind::AscribeUserType(
550-
Box::new((place, user_ty)),
551-
// We always use invariant as the variance here. This is because the
552-
// variance field from the ascription refers to the variance to use
553-
// when applying the type to the value being matched, but this
554-
// ascription applies rather to the type of the binding. e.g., in this
555-
// example:
556-
//
557-
// ```
558-
// let x: T = <expr>
559-
// ```
560-
//
561-
// We are creating an ascription that defines the type of `x` to be
562-
// exactly `T` (i.e., with invariance). The variance field, in
563-
// contrast, is intended to be used to relate `T` to the type of
564-
// `<expr>`.
565-
ty::Variance::Invariant,
566-
),
549+
kind: StatementKind::AscribeUserType(Box::new((place, user_ty))),
567550
},
568551
);
569552

@@ -2086,10 +2069,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
20862069
block,
20872070
Statement {
20882071
source_info,
2089-
kind: StatementKind::AscribeUserType(
2090-
Box::new((ascription.source, user_ty)),
2091-
ascription.variance,
2092-
),
2072+
kind: StatementKind::AscribeUserType(Box::new((ascription.source, user_ty))),
20932073
},
20942074
);
20952075
}

compiler/rustc_mir_transform/src/coverage/spans.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ pub(super) fn filtered_statement_span(statement: &Statement<'_>) -> Option<Span>
833833
| StatementKind::Assign(_)
834834
| StatementKind::SetDiscriminant { .. }
835835
| StatementKind::Retag(_, _)
836-
| StatementKind::AscribeUserType(_, _) => {
836+
| StatementKind::AscribeUserType(_) => {
837837
Some(statement.source_info.span)
838838
}
839839
}

compiler/rustc_mir_transform/src/separate_const_switch.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ fn is_likely_const<'tcx>(mut tracked_place: Place<'tcx>, block: &BasicBlockData<
244244
StatementKind::FakeRead(_)
245245
| StatementKind::StorageLive(_)
246246
| StatementKind::Retag(_, _)
247-
| StatementKind::AscribeUserType(_, _)
247+
| StatementKind::AscribeUserType(_)
248248
| StatementKind::Coverage(_)
249249
| StatementKind::StorageDead(_)
250250
| StatementKind::CopyNonOverlapping(_)
@@ -311,7 +311,7 @@ fn find_determining_place<'tcx>(
311311
| StatementKind::StorageLive(_)
312312
| StatementKind::StorageDead(_)
313313
| StatementKind::Retag(_, _)
314-
| StatementKind::AscribeUserType(_, _)
314+
| StatementKind::AscribeUserType(_)
315315
| StatementKind::Coverage(_)
316316
| StatementKind::CopyNonOverlapping(_)
317317
| StatementKind::Nop => {}

compiler/rustc_traits/src/type_op.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ pub fn type_op_ascribe_user_type_with_span<'a, 'tcx: 'a>(
5353
key: ParamEnvAnd<'tcx, AscribeUserType<'tcx>>,
5454
span: Option<Span>,
5555
) -> Result<(), NoSolution> {
56-
let (param_env, AscribeUserType { mir_ty, def_id, user_substs }) = key.into_parts();
56+
let (param_env, AscribeUserType { mir_ty, variance, def_id, user_substs }) = key.into_parts();
5757
debug!(
5858
"type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}",
5959
mir_ty, def_id, user_substs
6060
);
6161

6262
let mut cx = AscribeUserTypeCx { infcx, param_env, fulfill_cx };
63-
cx.relate_mir_and_user_ty(mir_ty, def_id, user_substs, span)?;
63+
cx.relate_mir_and_user_ty(mir_ty, variance, def_id, user_substs, span)?;
6464
Ok(())
6565
}
6666

@@ -121,6 +121,7 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
121121
fn relate_mir_and_user_ty(
122122
&mut self,
123123
mir_ty: Ty<'tcx>,
124+
variance: ty::Variance,
124125
def_id: DefId,
125126
user_substs: UserSubsts<'tcx>,
126127
span: Option<Span>,
@@ -133,7 +134,7 @@ impl<'me, 'tcx> AscribeUserTypeCx<'me, 'tcx> {
133134
debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
134135
let ty = self.normalize(ty);
135136

136-
self.relate(mir_ty, Variance::Invariant, ty)?;
137+
self.relate(mir_ty, variance, ty)?;
137138

138139
// Prove the predicates coming along with `def_id`.
139140
//

compiler/rustc_type_ir/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ impl UnifyKey for FloatVid {
447447
}
448448
}
449449

450-
#[derive(Copy, Clone, PartialEq, Decodable, Encodable, Hash)]
450+
#[derive(Copy, Clone, Decodable, Encodable, Hash, PartialEq, Eq)]
451451
pub enum Variance {
452452
Covariant, // T<A> <: T<B> iff A <: B -- e.g., function return type
453453
Invariant, // T<A> <: T<B> iff B == A -- e.g., type of mutable cell

src/test/ui/hr-subtype/placeholder-pattern-fail.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/placeholder-pattern-fail.rs:8:47
2+
--> $DIR/placeholder-pattern-fail.rs:9:47
33
|
44
LL | let _: for<'a, 'b> fn(Inv<'a>, Inv<'b>) = sub;
55
| ^^^ one type is more general than the other
@@ -8,30 +8,30 @@ LL | let _: for<'a, 'b> fn(Inv<'a>, Inv<'b>) = sub;
88
found fn pointer `for<'a> fn(Inv<'a>, Inv<'a>)`
99

1010
error[E0308]: mismatched types
11-
--> $DIR/placeholder-pattern-fail.rs:13:31
11+
--> $DIR/placeholder-pattern-fail.rs:14:31
1212
|
1313
LL | let _x: (&'static i32,) = x;
1414
| ^ lifetime mismatch
1515
|
1616
= note: expected tuple `(&'static i32,)`
1717
found tuple `(&'c i32,)`
1818
note: the lifetime `'c` as defined here...
19-
--> $DIR/placeholder-pattern-fail.rs:12:12
19+
--> $DIR/placeholder-pattern-fail.rs:13:12
2020
|
2121
LL | fn simple1<'c>(x: (&'c i32,)) {
2222
| ^^
2323
= note: ...does not necessarily outlive the static lifetime
2424

2525
error[E0308]: mismatched types
26-
--> $DIR/placeholder-pattern-fail.rs:18:30
26+
--> $DIR/placeholder-pattern-fail.rs:19:30
2727
|
2828
LL | let _: (&'static i32,) = x;
2929
| ^ lifetime mismatch
3030
|
3131
= note: expected tuple `(&'static i32,)`
3232
found tuple `(&'c i32,)`
3333
note: the lifetime `'c` as defined here...
34-
--> $DIR/placeholder-pattern-fail.rs:17:12
34+
--> $DIR/placeholder-pattern-fail.rs:18:12
3535
|
3636
LL | fn simple2<'c>(x: (&'c i32,)) {
3737
| ^^

0 commit comments

Comments
 (0)