Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c86d99d

Browse files
committedNov 17, 2017
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 aabfed5 + 9877fa0 commit c86d99d

File tree

14 files changed

+232
-226
lines changed

14 files changed

+232
-226
lines changed
 

‎src/librustc/infer/error_reporting/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -762,16 +762,23 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
762762
}
763763
}
764764

765-
self.note_error_origin(diag, &cause);
766765
self.check_and_note_conflicting_crates(diag, terr, span);
767766
self.tcx.note_and_explain_type_err(diag, terr, span);
767+
768+
// It reads better to have the error origin as the final
769+
// thing.
770+
self.note_error_origin(diag, &cause);
768771
}
769772

770773
pub fn report_and_explain_type_error(&self,
771774
trace: TypeTrace<'tcx>,
772775
terr: &TypeError<'tcx>)
773776
-> DiagnosticBuilder<'tcx>
774777
{
778+
debug!("report_and_explain_type_error(trace={:?}, terr={:?})",
779+
trace,
780+
terr);
781+
775782
let span = trace.cause.span;
776783
let failure_str = trace.cause.as_failure_str();
777784
let mut diag = match trace.cause.code {

‎src/librustc/infer/glb.rs

+27-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,32 @@ 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+
// Subtle: use a fresh combine-fields here because we recover
81+
// from Err. Doing otherwise could propagate obligations out
82+
// through our `self.obligations` field.
83+
self.infcx()
84+
.combine_fields(self.fields.trace.clone(), self.fields.param_env)
85+
.higher_ranked_glb(a, b, self.a_is_expected)
86+
.is_err()
87+
});
88+
debug!("binders: was_error={:?}", was_error);
89+
90+
// When higher-ranked types are involved, computing the LUB is
91+
// very challenging, switch to invariance. This is obviously
92+
// overly conservative but works ok in practice.
93+
match self.relate_with_variance(ty::Variance::Invariant, a, b) {
94+
Ok(_) => Ok(a.clone()),
95+
Err(err) => {
96+
debug!("binders: error occurred, was_error={:?}", was_error);
97+
if !was_error {
98+
Err(TypeError::OldStyleLUB(Box::new(err)))
99+
} else {
100+
Err(err)
101+
}
102+
}
103+
}
78104
}
79105
}
80106

‎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_constraints::{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};
@@ -246,7 +247,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
246247
snapshot: &CombinedSnapshot,
247248
debruijn: ty::DebruijnIndex,
248249
new_vars: &[ty::RegionVid],
249-
a_map: &FxHashMap<ty::BoundRegion, ty::Region<'tcx>>,
250+
a_map: &BTreeMap<ty::BoundRegion, ty::Region<'tcx>>,
250251
r0: ty::Region<'tcx>)
251252
-> ty::Region<'tcx> {
252253
// Regions that pre-dated the LUB computation stay as they are.
@@ -342,7 +343,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
342343
snapshot: &CombinedSnapshot,
343344
debruijn: ty::DebruijnIndex,
344345
new_vars: &[ty::RegionVid],
345-
a_map: &FxHashMap<ty::BoundRegion, ty::Region<'tcx>>,
346+
a_map: &BTreeMap<ty::BoundRegion, ty::Region<'tcx>>,
346347
a_vars: &[ty::RegionVid],
347348
b_vars: &[ty::RegionVid],
348349
r0: ty::Region<'tcx>)
@@ -411,7 +412,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
411412

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

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

‎src/librustc/infer/lub.rs

+27-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,32 @@ 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+
// Subtle: use a fresh combine-fields here because we recover
81+
// from Err. Doing otherwise could propagate obligations out
82+
// through our `self.obligations` field.
83+
self.infcx()
84+
.combine_fields(self.fields.trace.clone(), self.fields.param_env)
85+
.higher_ranked_lub(a, b, self.a_is_expected)
86+
.is_err()
87+
});
88+
debug!("binders: was_error={:?}", was_error);
89+
90+
// When higher-ranked types are involved, computing the LUB is
91+
// very challenging, switch to invariance. This is obviously
92+
// overly conservative but works ok in practice.
93+
match self.relate_with_variance(ty::Variance::Invariant, a, b) {
94+
Ok(_) => Ok(a.clone()),
95+
Err(err) => {
96+
debug!("binders: error occurred, was_error={:?}", was_error);
97+
if !was_error {
98+
Err(TypeError::OldStyleLUB(Box::new(err)))
99+
} else {
100+
Err(err)
101+
}
102+
}
103+
}
78104
}
79105
}
80106

‎src/librustc/infer/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use ty::relate::RelateResult;
3131
use traits::{self, ObligationCause, PredicateObligations, Reveal};
3232
use rustc_data_structures::unify::{self, UnificationTable};
3333
use std::cell::{Cell, RefCell, Ref, RefMut};
34+
use std::collections::BTreeMap;
3435
use std::fmt;
3536
use syntax::ast;
3637
use errors::DiagnosticBuilder;
@@ -184,7 +185,7 @@ pub struct InferCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
184185

185186
/// A map returned by `skolemize_late_bound_regions()` indicating the skolemized
186187
/// region that each late-bound region was replaced with.
187-
pub type SkolemizationMap<'tcx> = FxHashMap<ty::BoundRegion, ty::Region<'tcx>>;
188+
pub type SkolemizationMap<'tcx> = BTreeMap<ty::BoundRegion, ty::Region<'tcx>>;
188189

189190
/// See `error_reporting` module for more details
190191
#[derive(Clone, Debug)]
@@ -1384,7 +1385,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
13841385
span: Span,
13851386
lbrct: LateBoundRegionConversionTime,
13861387
value: &ty::Binder<T>)
1387-
-> (T, FxHashMap<ty::BoundRegion, ty::Region<'tcx>>)
1388+
-> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
13881389
where T : TypeFoldable<'tcx>
13891390
{
13901391
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 |

‎src/librustc_driver/test.rs

-212
Original file line numberDiff line numberDiff line change
@@ -353,28 +353,10 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
353353
self.infcx.tcx.mk_imm_ref(r, self.tcx().types.isize)
354354
}
355355

356-
pub fn t_rptr_static(&self) -> Ty<'tcx> {
357-
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.types.re_static,
358-
self.tcx().types.isize)
359-
}
360-
361-
pub fn t_rptr_empty(&self) -> Ty<'tcx> {
362-
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.types.re_empty,
363-
self.tcx().types.isize)
364-
}
365-
366356
pub fn sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> InferResult<'tcx, ()> {
367357
self.infcx.at(&ObligationCause::dummy(), self.param_env).sub(t1, t2)
368358
}
369359

370-
pub fn lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
371-
self.infcx.at(&ObligationCause::dummy(), self.param_env).lub(t1, t2)
372-
}
373-
374-
pub fn glb(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
375-
self.infcx.at(&ObligationCause::dummy(), self.param_env).glb(t1, t2)
376-
}
377-
378360
/// Checks that `t1 <: t2` is true (this may register additional
379361
/// region checks).
380362
pub fn check_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
@@ -399,37 +381,6 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
399381
}
400382
}
401383
}
402-
403-
/// Checks that `LUB(t1,t2) == t_lub`
404-
pub fn check_lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_lub: Ty<'tcx>) {
405-
match self.lub(t1, t2) {
406-
Ok(InferOk { obligations, value: t }) => {
407-
// None of these tests should require nested obligations:
408-
assert!(obligations.is_empty());
409-
410-
self.assert_eq(t, t_lub);
411-
}
412-
Err(ref e) => panic!("unexpected error in LUB: {}", e),
413-
}
414-
}
415-
416-
/// Checks that `GLB(t1,t2) == t_glb`
417-
pub fn check_glb(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_glb: Ty<'tcx>) {
418-
debug!("check_glb(t1={}, t2={}, t_glb={})", t1, t2, t_glb);
419-
match self.glb(t1, t2) {
420-
Err(e) => panic!("unexpected error computing LUB: {:?}", e),
421-
Ok(InferOk { obligations, value: t }) => {
422-
// None of these tests should require nested obligations:
423-
assert!(obligations.is_empty());
424-
425-
self.assert_eq(t, t_glb);
426-
427-
// sanity check for good measure:
428-
self.assert_subtype(t, t1);
429-
self.assert_subtype(t, t2);
430-
}
431-
}
432-
}
433384
}
434385

435386
#[test]
@@ -508,169 +459,6 @@ fn sub_free_bound_false_infer() {
508459
})
509460
}
510461

511-
#[test]
512-
fn lub_free_bound_infer() {
513-
//! Test result of:
514-
//!
515-
//! LUB(fn(_#1), for<'b> fn(&'b isize))
516-
//!
517-
//! This should yield `fn(&'_ isize)`. We check
518-
//! that it yields `fn(&'x isize)` for some free `'x`,
519-
//! anyhow.
520-
521-
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
522-
env.create_simple_region_hierarchy();
523-
let t_infer1 = env.infcx.next_ty_var(TypeVariableOrigin::MiscVariable(DUMMY_SP));
524-
let t_rptr_bound1 = env.t_rptr_late_bound(1);
525-
let t_rptr_free1 = env.t_rptr_free(1);
526-
env.check_lub(env.t_fn(&[t_infer1], env.tcx().types.isize),
527-
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
528-
env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
529-
});
530-
}
531-
532-
#[test]
533-
fn lub_bound_bound() {
534-
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
535-
let t_rptr_bound1 = env.t_rptr_late_bound(1);
536-
let t_rptr_bound2 = env.t_rptr_late_bound(2);
537-
env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
538-
env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
539-
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
540-
})
541-
}
542-
543-
#[test]
544-
fn lub_bound_free() {
545-
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
546-
env.create_simple_region_hierarchy();
547-
let t_rptr_bound1 = env.t_rptr_late_bound(1);
548-
let t_rptr_free1 = env.t_rptr_free(1);
549-
env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
550-
env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
551-
env.t_fn(&[t_rptr_free1], env.tcx().types.isize));
552-
})
553-
}
554-
555-
#[test]
556-
fn lub_bound_static() {
557-
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
558-
let t_rptr_bound1 = env.t_rptr_late_bound(1);
559-
let t_rptr_static = env.t_rptr_static();
560-
env.check_lub(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
561-
env.t_fn(&[t_rptr_static], env.tcx().types.isize),
562-
env.t_fn(&[t_rptr_static], env.tcx().types.isize));
563-
})
564-
}
565-
566-
#[test]
567-
fn lub_bound_bound_inverse_order() {
568-
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
569-
let t_rptr_bound1 = env.t_rptr_late_bound(1);
570-
let t_rptr_bound2 = env.t_rptr_late_bound(2);
571-
env.check_lub(env.t_fn(&[t_rptr_bound1, t_rptr_bound2], t_rptr_bound1),
572-
env.t_fn(&[t_rptr_bound2, t_rptr_bound1], t_rptr_bound1),
573-
env.t_fn(&[t_rptr_bound1, t_rptr_bound1], t_rptr_bound1));
574-
})
575-
}
576-
577-
#[test]
578-
fn lub_free_free() {
579-
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
580-
env.create_simple_region_hierarchy();
581-
let t_rptr_free1 = env.t_rptr_free(1);
582-
let t_rptr_free2 = env.t_rptr_free(2);
583-
let t_rptr_static = env.t_rptr_static();
584-
env.check_lub(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
585-
env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
586-
env.t_fn(&[t_rptr_static], env.tcx().types.isize));
587-
})
588-
}
589-
590-
#[test]
591-
fn lub_returning_scope() {
592-
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
593-
env.create_simple_region_hierarchy();
594-
let t_rptr_scope10 = env.t_rptr_scope(10);
595-
let t_rptr_scope11 = env.t_rptr_scope(11);
596-
let t_rptr_empty = env.t_rptr_empty();
597-
env.check_lub(env.t_fn(&[t_rptr_scope10], env.tcx().types.isize),
598-
env.t_fn(&[t_rptr_scope11], env.tcx().types.isize),
599-
env.t_fn(&[t_rptr_empty], env.tcx().types.isize));
600-
});
601-
}
602-
603-
#[test]
604-
fn glb_free_free_with_common_scope() {
605-
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
606-
env.create_simple_region_hierarchy();
607-
let t_rptr_free1 = env.t_rptr_free(1);
608-
let t_rptr_free2 = env.t_rptr_free(2);
609-
let t_rptr_scope = env.t_rptr_scope(1);
610-
env.check_glb(env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
611-
env.t_fn(&[t_rptr_free2], env.tcx().types.isize),
612-
env.t_fn(&[t_rptr_scope], env.tcx().types.isize));
613-
})
614-
}
615-
616-
#[test]
617-
fn glb_bound_bound() {
618-
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
619-
let t_rptr_bound1 = env.t_rptr_late_bound(1);
620-
let t_rptr_bound2 = env.t_rptr_late_bound(2);
621-
env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
622-
env.t_fn(&[t_rptr_bound2], env.tcx().types.isize),
623-
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
624-
})
625-
}
626-
627-
#[test]
628-
fn glb_bound_free() {
629-
test_env(EMPTY_SOURCE_STR, errors(&[]), |mut env| {
630-
env.create_simple_region_hierarchy();
631-
let t_rptr_bound1 = env.t_rptr_late_bound(1);
632-
let t_rptr_free1 = env.t_rptr_free(1);
633-
env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
634-
env.t_fn(&[t_rptr_free1], env.tcx().types.isize),
635-
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
636-
})
637-
}
638-
639-
#[test]
640-
fn glb_bound_free_infer() {
641-
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
642-
let t_rptr_bound1 = env.t_rptr_late_bound(1);
643-
let t_infer1 = env.infcx.next_ty_var(TypeVariableOrigin::MiscVariable(DUMMY_SP));
644-
645-
// compute GLB(fn(_) -> isize, for<'b> fn(&'b isize) -> isize),
646-
// which should yield for<'b> fn(&'b isize) -> isize
647-
env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
648-
env.t_fn(&[t_infer1], env.tcx().types.isize),
649-
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
650-
651-
// as a side-effect, computing GLB should unify `_` with
652-
// `&'_ isize`
653-
let t_resolve1 = env.infcx.shallow_resolve(t_infer1);
654-
match t_resolve1.sty {
655-
ty::TyRef(..) => {}
656-
_ => {
657-
panic!("t_resolve1={:?}", t_resolve1);
658-
}
659-
}
660-
})
661-
}
662-
663-
#[test]
664-
fn glb_bound_static() {
665-
test_env(EMPTY_SOURCE_STR, errors(&[]), |env| {
666-
let t_rptr_bound1 = env.t_rptr_late_bound(1);
667-
let t_rptr_static = env.t_rptr_static();
668-
env.check_glb(env.t_fn(&[t_rptr_bound1], env.tcx().types.isize),
669-
env.t_fn(&[t_rptr_static], env.tcx().types.isize),
670-
env.t_fn(&[t_rptr_bound1], env.tcx().types.isize));
671-
})
672-
}
673-
674462
/// Test substituting a bound region into a function, which introduces another level of binding.
675463
/// This requires adjusting the Debruijn index.
676464
#[test]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test for a specific corner case: when we compute the LUB of two fn
12+
// types and their parameters have unbound variables. In that case, we
13+
// wind up relating those two variables. This was causing an ICE in an
14+
// in-progress PR.
15+
16+
fn main() {
17+
let a_f: fn(_) = |_| ();
18+
let b_f: fn(_) = |_| ();
19+
let c_f = match 22 {
20+
0 => a_f,
21+
_ => b_f,
22+
};
23+
c_f(4);
24+
}

‎src/test/ui/lub-glb/old-lub-glb-hr.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that we give a note when the old LUB/GLB algorithm would have
12+
// succeeded but the new code (which is stricter) gives an error.
13+
14+
fn foo(
15+
x: fn(&u8, &u8),
16+
y: for<'a> fn(&'a u8, &'a u8),
17+
) {
18+
let z = match 22 {
19+
0 => x,
20+
_ => y,
21+
};
22+
}
23+
24+
fn bar(
25+
x: fn(&u8, &u8),
26+
y: for<'a> fn(&'a u8, &'a u8),
27+
) {
28+
let z = match 22 {
29+
// No error with an explicit cast:
30+
0 => x as for<'a> fn(&'a u8, &'a u8),
31+
_ => y,
32+
};
33+
}
34+
35+
fn main() {
36+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0308]: match arms have incompatible types
2+
--> $DIR/old-lub-glb-hr.rs:18:13
3+
|
4+
18 | let z = match 22 {
5+
| _____________^
6+
19 | | 0 => x,
7+
20 | | _ => y,
8+
21 | | };
9+
| |_____^ expected bound lifetime parameter, found concrete lifetime
10+
|
11+
= note: expected type `for<'r, 's> fn(&'r u8, &'s u8)`
12+
found type `for<'a> fn(&'a u8, &'a u8)`
13+
= note: this was previously accepted by the compiler but has been phased out
14+
= note: for more information, see https://github.com/rust-lang/rust/issues/45852
15+
note: match arm with an incompatible type
16+
--> $DIR/old-lub-glb-hr.rs:20:14
17+
|
18+
20 | _ => y,
19+
| ^
20+
21+
error: aborting due to previous error
22+
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that we give a note when the old LUB/GLB algorithm would have
12+
// succeeded but the new code (which is stricter) gives an error.
13+
14+
trait Foo<T, U> { }
15+
16+
fn foo(
17+
x: &for<'a, 'b> Foo<&'a u8, &'b u8>,
18+
y: &for<'a> Foo<&'a u8, &'a u8>,
19+
) {
20+
let z = match 22 {
21+
0 => x,
22+
_ => y,
23+
};
24+
}
25+
26+
fn bar(
27+
x: &for<'a, 'b> Foo<&'a u8, &'b u8>,
28+
y: &for<'a> Foo<&'a u8, &'a u8>,
29+
) {
30+
// Accepted with explicit case:
31+
let z = match 22 {
32+
0 => x as &for<'a> Foo<&'a u8, &'a u8>,
33+
_ => y,
34+
};
35+
}
36+
37+
fn main() {
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0308]: match arms have incompatible types
2+
--> $DIR/old-lub-glb-object.rs:20:13
3+
|
4+
20 | let z = match 22 {
5+
| _____________^
6+
21 | | 0 => x,
7+
22 | | _ => y,
8+
23 | | };
9+
| |_____^ expected bound lifetime parameter 'a, found concrete lifetime
10+
|
11+
= note: expected type `&for<'a, 'b> Foo<&'a u8, &'b u8>`
12+
found type `&for<'a> Foo<&'a u8, &'a u8>`
13+
= note: this was previously accepted by the compiler but has been phased out
14+
= note: for more information, see https://github.com/rust-lang/rust/issues/45852
15+
note: match arm with an incompatible type
16+
--> $DIR/old-lub-glb-object.rs:22:14
17+
|
18+
22 | _ => y,
19+
| ^
20+
21+
error: aborting due to previous error
22+

0 commit comments

Comments
 (0)
Please sign in to comment.