Skip to content

Commit 98d6f46

Browse files
committed
Auto merge of #45853 - nikomatsakis:chalk-simplify-hr-lub-glb, r=arielb1
Simplify higher-ranked LUB/GLB This is a better version of #44211. It still makes higher-ranked LUB/GLB into a hard equality test, however, it does try to identify that something changed and issue a notice to the user. I wroteup #45852 as a tracking issue for this change. Currently, this moves straight to a hard-error, on the basis that the crater run in #44211 saw no impact. It might be good to retest -- or perhaps to try for a warning period. Trying to do the latter in a precise way would be somewhat painful, but an imprecise way might suffice -- that is, we could issue warning *whenever* a LUB/GLB operation succeeds that will later fail, even if it doesn't ultimately impact the type check. I could experiment with this. ~~I am *mildly* wary about landing this independently of other code that moves to a universe-based system. In particular, I was nervous that this change would make coherence accepts new pairs of impls that will later be errors. I have the code for the universe-based approach available, I hope to open an PR and run some tests on its impact very shortly.~~ @arielb1 points out that I was being silly. r? @arielb1
2 parents 24840da + bb0e756 commit 98d6f46

File tree

13 files changed

+197
-226
lines changed

13 files changed

+197
-226
lines changed

src/librustc/infer/error_reporting/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -764,16 +764,24 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
764764
}
765765
}
766766

767-
self.note_error_origin(diag, &cause);
768767
self.check_and_note_conflicting_crates(diag, terr, span);
769768
self.tcx.note_and_explain_type_err(diag, terr, span);
769+
self.check_and_note_conflicting_crates(diag, terr, span);
770+
771+
// It reads better to have the error origin as the final
772+
// thing.
773+
self.note_error_origin(diag, &cause);
770774
}
771775

772776
pub fn report_and_explain_type_error(&self,
773777
trace: TypeTrace<'tcx>,
774778
terr: &TypeError<'tcx>)
775779
-> DiagnosticBuilder<'tcx>
776780
{
781+
debug!("report_and_explain_type_error(trace={:?}, terr={:?})",
782+
trace,
783+
terr);
784+
777785
let span = trace.cause.span;
778786
let failure_str = trace.cause.as_failure_str();
779787
let mut diag = match trace.cause.code {

src/librustc/infer/glb.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use super::Subtype;
1515

1616
use traits::ObligationCause;
1717
use ty::{self, Ty, TyCtxt};
18+
use ty::error::TypeError;
1819
use ty::relate::{Relate, RelateResult, TypeRelation};
1920

2021
/// "Greatest lower bound" (common subtype)
@@ -74,7 +75,26 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
7475
-> RelateResult<'tcx, ty::Binder<T>>
7576
where T: Relate<'tcx>
7677
{
77-
self.fields.higher_ranked_glb(a, b, self.a_is_expected)
78+
debug!("binders(a={:?}, b={:?})", a, b);
79+
let was_error = self.infcx().probe(|_snapshot| {
80+
self.fields.higher_ranked_glb(a, b, self.a_is_expected).is_err()
81+
});
82+
debug!("binders: was_error={:?}", was_error);
83+
84+
// When higher-ranked types are involved, computing the LUB is
85+
// very challenging, switch to invariance. This is obviously
86+
// overly conservative but works ok in practice.
87+
match self.relate_with_variance(ty::Variance::Invariant, a, b) {
88+
Ok(_) => Ok(a.clone()),
89+
Err(err) => {
90+
debug!("binders: error occurred, was_error={:?}", was_error);
91+
if !was_error {
92+
Err(TypeError::OldStyleLUB(Box::new(err)))
93+
} else {
94+
Err(err)
95+
}
96+
}
97+
}
7898
}
7999
}
80100

src/librustc/infer/higher_ranked/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use super::{CombinedSnapshot,
1919
use super::combine::CombineFields;
2020
use super::region_inference::{TaintDirections};
2121

22+
use std::collections::BTreeMap;
2223
use ty::{self, TyCtxt, Binder, TypeFoldable};
2324
use ty::error::TypeError;
2425
use ty::relate::{Relate, RelateResult, TypeRelation};
@@ -245,7 +246,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
245246
snapshot: &CombinedSnapshot,
246247
debruijn: ty::DebruijnIndex,
247248
new_vars: &[ty::RegionVid],
248-
a_map: &FxHashMap<ty::BoundRegion, ty::Region<'tcx>>,
249+
a_map: &BTreeMap<ty::BoundRegion, ty::Region<'tcx>>,
249250
r0: ty::Region<'tcx>)
250251
-> ty::Region<'tcx> {
251252
// Regions that pre-dated the LUB computation stay as they are.
@@ -341,7 +342,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
341342
snapshot: &CombinedSnapshot,
342343
debruijn: ty::DebruijnIndex,
343344
new_vars: &[ty::RegionVid],
344-
a_map: &FxHashMap<ty::BoundRegion, ty::Region<'tcx>>,
345+
a_map: &BTreeMap<ty::BoundRegion, ty::Region<'tcx>>,
345346
a_vars: &[ty::RegionVid],
346347
b_vars: &[ty::RegionVid],
347348
r0: ty::Region<'tcx>)
@@ -410,7 +411,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
410411

411412
fn rev_lookup<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
412413
span: Span,
413-
a_map: &FxHashMap<ty::BoundRegion, ty::Region<'tcx>>,
414+
a_map: &BTreeMap<ty::BoundRegion, ty::Region<'tcx>>,
414415
r: ty::Region<'tcx>) -> ty::Region<'tcx>
415416
{
416417
for (a_br, a_r) in a_map {
@@ -433,7 +434,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
433434
}
434435

435436
fn var_ids<'a, 'gcx, 'tcx>(fields: &CombineFields<'a, 'gcx, 'tcx>,
436-
map: &FxHashMap<ty::BoundRegion, ty::Region<'tcx>>)
437+
map: &BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
437438
-> Vec<ty::RegionVid> {
438439
map.iter()
439440
.map(|(_, &r)| match *r {

src/librustc/infer/lub.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use super::Subtype;
1515

1616
use traits::ObligationCause;
1717
use ty::{self, Ty, TyCtxt};
18+
use ty::error::TypeError;
1819
use ty::relate::{Relate, RelateResult, TypeRelation};
1920

2021
/// "Least upper bound" (common supertype)
@@ -74,7 +75,26 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
7475
-> RelateResult<'tcx, ty::Binder<T>>
7576
where T: Relate<'tcx>
7677
{
77-
self.fields.higher_ranked_lub(a, b, self.a_is_expected)
78+
debug!("binders(a={:?}, b={:?})", a, b);
79+
let was_error = self.infcx().probe(|_snapshot| {
80+
self.fields.higher_ranked_lub(a, b, self.a_is_expected).is_err()
81+
});
82+
debug!("binders: was_error={:?}", was_error);
83+
84+
// When higher-ranked types are involved, computing the LUB is
85+
// very challenging, switch to invariance. This is obviously
86+
// overly conservative but works ok in practice.
87+
match self.relate_with_variance(ty::Variance::Invariant, a, b) {
88+
Ok(_) => Ok(a.clone()),
89+
Err(err) => {
90+
debug!("binders: error occurred, was_error={:?}", was_error);
91+
if !was_error {
92+
Err(TypeError::OldStyleLUB(Box::new(err)))
93+
} else {
94+
Err(err)
95+
}
96+
}
97+
}
7898
}
7999
}
80100

src/librustc/infer/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use ty::relate::RelateResult;
3232
use traits::{self, ObligationCause, PredicateObligations, Reveal};
3333
use rustc_data_structures::unify::{self, UnificationTable};
3434
use std::cell::{Cell, RefCell, Ref};
35+
use std::collections::BTreeMap;
3536
use std::fmt;
3637
use syntax::ast;
3738
use errors::DiagnosticBuilder;
@@ -139,7 +140,7 @@ pub struct InferCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
139140

140141
/// A map returned by `skolemize_late_bound_regions()` indicating the skolemized
141142
/// region that each late-bound region was replaced with.
142-
pub type SkolemizationMap<'tcx> = FxHashMap<ty::BoundRegion, ty::Region<'tcx>>;
143+
pub type SkolemizationMap<'tcx> = BTreeMap<ty::BoundRegion, ty::Region<'tcx>>;
143144

144145
/// See `error_reporting` module for more details
145146
#[derive(Clone, Debug)]
@@ -1260,7 +1261,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
12601261
span: Span,
12611262
lbrct: LateBoundRegionConversionTime,
12621263
value: &ty::Binder<T>)
1263-
-> (T, FxHashMap<ty::BoundRegion, ty::Region<'tcx>>)
1264+
-> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
12641265
where T : TypeFoldable<'tcx>
12651266
{
12661267
self.tcx.replace_late_bound_regions(

src/librustc/ty/error.rs

+11
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ pub enum TypeError<'tcx> {
5454
ProjectionBoundsLength(ExpectedFound<usize>),
5555
TyParamDefaultMismatch(ExpectedFound<type_variable::Default<'tcx>>),
5656
ExistentialMismatch(ExpectedFound<&'tcx ty::Slice<ty::ExistentialPredicate<'tcx>>>),
57+
58+
OldStyleLUB(Box<TypeError<'tcx>>),
5759
}
5860

5961
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug, Copy)]
@@ -170,6 +172,9 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
170172
report_maybe_different(f, format!("trait `{}`", values.expected),
171173
format!("trait `{}`", values.found))
172174
}
175+
OldStyleLUB(ref err) => {
176+
write!(f, "{}", err)
177+
}
173178
}
174179
}
175180
}
@@ -293,6 +298,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
293298
db.span_note(found.origin_span,
294299
"...that also applies to the same type variable here");
295300
}
301+
OldStyleLUB(err) => {
302+
db.note("this was previously accepted by the compiler but has been phased out");
303+
db.note("for more information, see https://github.com/rust-lang/rust/issues/45852");
304+
305+
self.note_and_explain_type_err(db, &err, sp);
306+
}
296307
_ => {}
297308
}
298309
}

src/librustc/ty/fold.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ use middle::const_val::ConstVal;
4343
use ty::{self, Binder, Ty, TyCtxt, TypeFlags};
4444

4545
use std::fmt;
46-
use util::nodemap::{FxHashMap, FxHashSet};
46+
use std::collections::BTreeMap;
47+
use util::nodemap::FxHashSet;
4748

4849
/// The TypeFoldable trait is implemented for every type that can be folded.
4950
/// Basically, every type that has a corresponding method in TypeFolder.
@@ -324,14 +325,14 @@ struct RegionReplacer<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
324325
tcx: TyCtxt<'a, 'gcx, 'tcx>,
325326
current_depth: u32,
326327
fld_r: &'a mut (FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a),
327-
map: FxHashMap<ty::BoundRegion, ty::Region<'tcx>>
328+
map: BTreeMap<ty::BoundRegion, ty::Region<'tcx>>
328329
}
329330

330331
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
331332
pub fn replace_late_bound_regions<T,F>(self,
332333
value: &Binder<T>,
333334
mut f: F)
334-
-> (T, FxHashMap<ty::BoundRegion, ty::Region<'tcx>>)
335+
-> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
335336
where F : FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
336337
T : TypeFoldable<'tcx>,
337338
{
@@ -438,7 +439,7 @@ impl<'a, 'gcx, 'tcx> RegionReplacer<'a, 'gcx, 'tcx> {
438439
tcx,
439440
current_depth: 1,
440441
fld_r,
441-
map: FxHashMap()
442+
map: BTreeMap::default()
442443
}
443444
}
444445
}

src/librustc/ty/structural_impls.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@ impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
428428
TyParamDefaultMismatch(ref x) => {
429429
return tcx.lift(x).map(TyParamDefaultMismatch)
430430
}
431-
ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch)
431+
ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch),
432+
OldStyleLUB(ref x) => return tcx.lift(x).map(OldStyleLUB),
432433
})
433434
}
434435
}
@@ -1174,6 +1175,7 @@ impl<'tcx> TypeFoldable<'tcx> for ty::error::TypeError<'tcx> {
11741175
Sorts(x) => Sorts(x.fold_with(folder)),
11751176
TyParamDefaultMismatch(ref x) => TyParamDefaultMismatch(x.fold_with(folder)),
11761177
ExistentialMismatch(x) => ExistentialMismatch(x.fold_with(folder)),
1178+
OldStyleLUB(ref x) => OldStyleLUB(x.fold_with(folder)),
11771179
}
11781180
}
11791181

@@ -1191,6 +1193,7 @@ impl<'tcx> TypeFoldable<'tcx> for ty::error::TypeError<'tcx> {
11911193
b.visit_with(visitor)
11921194
},
11931195
Sorts(x) => x.visit_with(visitor),
1196+
OldStyleLUB(ref x) => x.visit_with(visitor),
11941197
TyParamDefaultMismatch(ref x) => x.visit_with(visitor),
11951198
ExistentialMismatch(x) => x.visit_with(visitor),
11961199
Mismatch |

0 commit comments

Comments
 (0)