Skip to content

Commit 9a97790

Browse files
committed
Add an error variance, but don't use it yet
1 parent b745423 commit 9a97790

File tree

11 files changed

+55
-15
lines changed

11 files changed

+55
-15
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ impl<'me, 'bccx, 'tcx> NllTypeRelating<'me, 'bccx, 'tcx> {
106106
fn ambient_covariance(&self) -> bool {
107107
match self.ambient_variance {
108108
ty::Variance::Covariant | ty::Variance::Invariant => true,
109-
ty::Variance::Contravariant | ty::Variance::Bivariant => false,
109+
ty::Variance::Contravariant | ty::Variance::Bivariant | ty::Errvariant(_) => false,
110110
}
111111
}
112112

113113
fn ambient_contravariance(&self) -> bool {
114114
match self.ambient_variance {
115115
ty::Variance::Contravariant | ty::Variance::Invariant => true,
116-
ty::Variance::Covariant | ty::Variance::Bivariant => false,
116+
ty::Variance::Covariant | ty::Variance::Bivariant | ty::Errvariant(_) => false,
117117
}
118118
}
119119

@@ -526,6 +526,7 @@ impl<'bccx, 'tcx> TypeRelation<'tcx> for NllTypeRelating<'_, 'bccx, 'tcx> {
526526
})?;
527527
}
528528

529+
ty::Variance::Errvariant(guar) => self.type_checker.infcx.set_tainted_by_errors(guar),
529530
ty::Variance::Bivariant => {}
530531
}
531532

@@ -591,7 +592,7 @@ impl<'bccx, 'tcx> ObligationEmittingRelation<'tcx> for NllTypeRelating<'_, 'bccx
591592
b.into(),
592593
ty::AliasRelationDirection::Equate,
593594
),
594-
ty::Variance::Bivariant => {
595+
ty::Variance::Errvariant(_) | ty::Variance::Bivariant => {
595596
unreachable!("cannot defer an alias-relate goal with Bivariant variance (yet?)")
596597
}
597598
})]);

compiler/rustc_hir_analysis/src/variance/constraints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
174174
ty::Invariant => self.invariant,
175175
ty::Contravariant => self.contravariant,
176176
ty::Bivariant => self.bivariant,
177+
ty::Errvariant(_) => &*self.terms_cx.arena.alloc(ConstantTerm(v)),
177178
}
178179
}
179180

compiler/rustc_hir_analysis/src/variance/xform.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub fn glb(v1: ty::Variance, v2: ty::Variance) -> ty::Variance {
88
// - +
99
// o
1010
match (v1, v2) {
11+
(ty::Errvariant(guar), _) | (_, ty::Errvariant(guar)) => ty::Errvariant(guar),
1112
(ty::Invariant, _) | (_, ty::Invariant) => ty::Invariant,
1213

1314
(ty::Covariant, ty::Contravariant) => ty::Invariant,

compiler/rustc_infer/src/infer/at.rs

+4
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ impl<'a, 'tcx> At<'a, 'tcx> {
207207
ty::Variance::Covariant => self.sub(define_opaque_types, expected, actual),
208208
ty::Variance::Invariant => self.eq(define_opaque_types, expected, actual),
209209
ty::Variance::Contravariant => self.sup(define_opaque_types, expected, actual),
210+
ty::Variance::Errvariant(guar) => {
211+
self.infcx.set_tainted_by_errors(guar);
212+
Ok(InferOk { value: (), obligations: vec![] })
213+
}
210214

211215
// We could make this make sense but it's not readily
212216
// exposed and I don't feel like dealing with it. Note

compiler/rustc_infer/src/infer/relate/generalize.rs

+8
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ impl<'tcx> InferCtxt<'tcx> {
9191
ty::Variance::Contravariant => {
9292
(source_ty.into(), generalized_ty.into(), AliasRelationDirection::Subtype)
9393
}
94+
ty::Errvariant(guar) => {
95+
self.set_tainted_by_errors(guar);
96+
(
97+
Ty::new_error(self.tcx, guar).into(),
98+
generalized_ty.into(),
99+
AliasRelationDirection::Equate,
100+
)
101+
}
94102
ty::Variance::Bivariant => unreachable!("bivariant generalization"),
95103
};
96104

compiler/rustc_infer/src/infer/relate/glb.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ impl<'tcx> TypeRelation<'tcx> for Glb<'_, '_, 'tcx> {
4040
match variance {
4141
ty::Invariant => self.fields.equate(StructurallyRelateAliases::No).relate(a, b),
4242
ty::Covariant => self.relate(a, b),
43+
// TODO: report a new error kind
44+
ty::Errvariant(_) => Ok(a),
4345
// FIXME(#41044) -- not correct, need test
4446
ty::Bivariant => Ok(a),
4547
ty::Contravariant => self.fields.lub().relate(a, b),

compiler/rustc_infer/src/infer/relate/lub.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ impl<'tcx> TypeRelation<'tcx> for Lub<'_, '_, 'tcx> {
4040
match variance {
4141
ty::Invariant => self.fields.equate(StructurallyRelateAliases::No).relate(a, b),
4242
ty::Covariant => self.relate(a, b),
43+
// TODO: report a new kind of type error that preserves the `ErrorGuaranteed`?
44+
ty::Errvariant(_) => Ok(a),
4345
// FIXME(#41044) -- not correct, need test
4446
ty::Bivariant => Ok(a),
4547
ty::Contravariant => self.fields.glb().relate(a, b),

compiler/rustc_infer/src/infer/relate/type_relating.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'tcx> TypeRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
117117
ty::Invariant => {
118118
infcx.inner.borrow_mut().type_variables().equate(a_id, b_id);
119119
}
120-
ty::Bivariant => {
120+
ty::Errvariant(_) | ty::Bivariant => {
121121
unreachable!("Expected bivariance to be handled in relate_with_variance")
122122
}
123123
}
@@ -204,7 +204,7 @@ impl<'tcx> TypeRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
204204
.unwrap_region_constraints()
205205
.make_eqregion(origin, a, b);
206206
}
207-
ty::Bivariant => {
207+
ty::Errvariant(_) | ty::Bivariant => {
208208
unreachable!("Expected bivariance to be handled in relate_with_variance")
209209
}
210210
}
@@ -289,11 +289,15 @@ impl<'tcx> TypeRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
289289
self.relate(a, b)
290290
})?;
291291
}
292-
ty::Bivariant => {
292+
ty::Errvariant(_) | ty::Bivariant => {
293293
unreachable!("Expected bivariance to be handled in relate_with_variance")
294294
}
295295
}
296-
}
296+
ty::Errvariant(guar) => {
297+
infcx.set_tainted_by_errors(guar);
298+
return self.relate(a, b);
299+
}
300+
ty::Bivariant => {
297301

298302
Ok(a)
299303
}
@@ -341,9 +345,13 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
341345
b.into(),
342346
ty::AliasRelationDirection::Equate,
343347
),
344-
ty::Variance::Bivariant => {
348+
ty::Variance::Errvariant(_) | ty::Variance::Bivariant => {
345349
unreachable!("Expected bivariance to be handled in relate_with_variance")
346350
}
347351
})]);
348-
}
352+
ty::Variance::Errvariant(guar) => {
353+
self.fields.infcx.set_tainted_by_errors(guar);
354+
ty::PredicateKind::Ambiguous
355+
}
356+
ty::Variance::Bivariant => {
349357
}

compiler/rustc_smir/src/rustc_smir/convert/ty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,7 @@ impl<'tcx> Stable<'tcx> for ty::Variance {
870870
ty::Variance::Contravariant => stable_mir::mir::Variance::Contravariant,
871871
ty::Variance::Covariant => stable_mir::mir::Variance::Covariant,
872872
ty::Variance::Invariant => stable_mir::mir::Variance::Invariant,
873+
ty::Variance::Errvariant(_) => stable_mir::mir::Variance::Bivariant,
873874
}
874875
}
875876
}

compiler/rustc_span/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
extern crate self as rustc_span;
4040

4141
use rustc_data_structures::{outline, AtomicRef};
42-
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
42+
use rustc_macros::{Decodable, Encodable, HashStable_Generic, HashStable_NoContext};
4343
use rustc_serialize::opaque::{FileEncoder, MemDecoder};
4444
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
4545
use tracing::debug;
@@ -2573,7 +2573,7 @@ where
25732573
/// The `()` field is necessary: it is non-`pub`, which means values of this
25742574
/// type cannot be constructed outside of this crate.
25752575
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
2576-
#[derive(HashStable_Generic)]
2576+
#[derive(HashStable_NoContext)]
25772577
pub struct ErrorGuaranteed(());
25782578

25792579
impl ErrorGuaranteed {

compiler/rustc_type_ir/src/lib.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_data_structures::sso::SsoHashSet;
1414
use rustc_data_structures::sync::Lrc;
1515
#[cfg(feature = "nightly")]
1616
use rustc_macros::{Decodable, Encodable, HashStable_NoContext};
17+
use rustc_span::ErrorGuaranteed;
1718
#[cfg(not(feature = "nightly"))]
1819
use std::collections::HashSet as SsoHashSet;
1920
use std::fmt;
@@ -200,10 +201,16 @@ pub fn debug_bound_var<T: std::fmt::Write>(
200201
#[cfg_attr(feature = "nightly", derive(Decodable, Encodable, Hash, HashStable_NoContext))]
201202
#[cfg_attr(feature = "nightly", rustc_pass_by_value)]
202203
pub enum Variance {
203-
Covariant, // T<A> <: T<B> iff A <: B -- e.g., function return type
204-
Invariant, // T<A> <: T<B> iff B == A -- e.g., type of mutable cell
205-
Contravariant, // T<A> <: T<B> iff B <: A -- e.g., function param type
206-
Bivariant, // T<A> <: T<B> -- e.g., unused type parameter
204+
/// T<A> <: T<B> iff A <: B -- e.g., function return type
205+
Covariant,
206+
/// T<A> <: T<B> iff B == A -- e.g., type of mutable cell
207+
Invariant,
208+
/// T<A> <: T<B> iff B <: A -- e.g., function param type
209+
Contravariant,
210+
/// T<A> <: T<B> -- e.g., unused type parameter
211+
Bivariant,
212+
/// Oddly named since these are all glob exported in various places in the compiler
213+
Errvariant(ErrorGuaranteed),
207214
}
208215

209216
impl Variance {
@@ -245,6 +252,10 @@ impl Variance {
245252
/// Combining Definition- and Use-Site Variance" published in PLDI'11.
246253
pub fn xform(self, v: Variance) -> Variance {
247254
match (self, v) {
255+
(Variance::Errvariant(guar), _) | (_, Variance::Errvariant(guar)) => {
256+
Variance::Errvariant(guar)
257+
}
258+
248259
// Figure 1, column 1.
249260
(Variance::Covariant, Variance::Covariant) => Variance::Covariant,
250261
(Variance::Covariant, Variance::Contravariant) => Variance::Contravariant,
@@ -273,6 +284,7 @@ impl fmt::Debug for Variance {
273284
Variance::Contravariant => "-",
274285
Variance::Invariant => "o",
275286
Variance::Bivariant => "*",
287+
Variance::Errvariant(_) => "{error}",
276288
})
277289
}
278290
}

0 commit comments

Comments
 (0)