1
Fork 0

Store Option<Region> as value for RegionVid

This commit is contained in:
Jack Huey 2021-05-14 21:48:32 -04:00
parent e8c284ff28
commit 61157b341e
14 changed files with 172 additions and 84 deletions

View file

@ -304,6 +304,15 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
} }
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
let tcx = self.tcx();
let r = self
.infcx
.unwrap()
.inner
.borrow_mut()
.unwrap_region_constraints()
.opportunistic_resolve_region(tcx, r);
match *r { match *r {
ty::ReLateBound(index, ..) => { ty::ReLateBound(index, ..) => {
if index >= self.binder_index { if index >= self.binder_index {
@ -313,22 +322,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
} }
} }
ty::ReVar(vid) => { ty::ReVar(_) => self.canonicalize_region_mode.canonicalize_free_region(self, r),
let resolved_vid = self
.infcx
.unwrap()
.inner
.borrow_mut()
.unwrap_region_constraints()
.opportunistic_resolve_var(vid);
debug!(
"canonical: region var found with vid {:?}, \
opportunistically resolved to {:?}",
vid, r
);
let r = self.tcx.reuse_or_mk_region(r, ty::ReVar(resolved_vid));
self.canonicalize_region_mode.canonicalize_free_region(self, r)
}
ty::ReStatic ty::ReStatic
| ty::ReEarlyBound(..) | ty::ReEarlyBound(..)

View file

@ -11,9 +11,9 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_data_structures::undo_log::UndoLogs; use rustc_data_structures::undo_log::UndoLogs;
use rustc_data_structures::unify as ut; use rustc_data_structures::unify as ut;
use rustc_data_structures::unify::UnifyKey;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
use rustc_middle::infer::unify_key::{RegionVidKey, UnifiedRegion};
use rustc_middle::ty::ReStatic; use rustc_middle::ty::ReStatic;
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::ty::{ReLateBound, ReVar}; use rustc_middle::ty::{ReLateBound, ReVar};
@ -47,13 +47,13 @@ pub struct RegionConstraintStorage<'tcx> {
/// When we add a R1 == R2 constriant, we currently add (a) edges /// When we add a R1 == R2 constriant, we currently add (a) edges
/// R1 <= R2 and R2 <= R1 and (b) we unify the two regions in this /// R1 <= R2 and R2 <= R1 and (b) we unify the two regions in this
/// table. You can then call `opportunistic_resolve_var` early /// table. You can then call `opportunistic_resolve_region` early
/// which will map R1 and R2 to some common region (i.e., either /// which will map R1 and R2 to some common region (i.e., either
/// R1 or R2). This is important when fulfillment, dropck and other such /// R1 or R2). This is important when fulfillment, dropck and other such
/// code is iterating to a fixed point, because otherwise we sometimes /// code is iterating to a fixed point, because otherwise we sometimes
/// would wind up with a fresh stream of region variables that have been /// would wind up with a fresh stream of region variables that have been
/// equated but appear distinct. /// equated but appear distinct.
pub(super) unification_table: ut::UnificationTableStorage<ty::RegionVid>, pub(super) unification_table: ut::UnificationTableStorage<RegionVidKey<'tcx>>,
/// a flag set to true when we perform any unifications; this is used /// a flag set to true when we perform any unifications; this is used
/// to micro-optimize `take_and_reset_data` /// to micro-optimize `take_and_reset_data`
@ -406,8 +406,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
// `RegionConstraintData` contains the relationship here. // `RegionConstraintData` contains the relationship here.
if *any_unifications { if *any_unifications {
*any_unifications = false; *any_unifications = false;
self.unification_table() self.unification_table().reset_unifications(|_| UnifiedRegion(None));
.reset_unifications(|_| ());
} }
data data
@ -434,8 +433,8 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
) -> RegionVid { ) -> RegionVid {
let vid = self.var_infos.push(RegionVariableInfo { origin, universe }); let vid = self.var_infos.push(RegionVariableInfo { origin, universe });
let u_vid = self.unification_table().new_key(()); let u_vid = self.unification_table().new_key(UnifiedRegion(None));
assert_eq!(vid, u_vid); assert_eq!(vid, u_vid.vid);
self.undo_log.push(AddVar(vid)); self.undo_log.push(AddVar(vid));
debug!("created new region variable {:?} in {:?} with origin {:?}", vid, universe, origin); debug!("created new region variable {:?} in {:?} with origin {:?}", vid, universe, origin);
vid vid
@ -497,10 +496,18 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
self.make_subregion(origin.clone(), sub, sup); self.make_subregion(origin.clone(), sub, sup);
self.make_subregion(origin, sup, sub); self.make_subregion(origin, sup, sub);
if let (ty::ReVar(sub), ty::ReVar(sup)) = (*sub, *sup) { match (sub, sup) {
debug!("make_eqregion: uniying {:?} with {:?}", sub, sup); (&ty::ReVar(sub), &ty::ReVar(sup)) => {
self.unification_table().union(sub, sup); debug!("make_eqregion: unifying {:?} with {:?}", sub, sup);
self.any_unifications = true; self.unification_table().union(sub, sup);
self.any_unifications = true;
}
(&ty::ReVar(vid), value) | (value, &ty::ReVar(vid)) => {
debug!("make_eqregion: unifying {:?} with {:?}", vid, value);
self.unification_table().union_value(vid, UnifiedRegion(Some(value)));
self.any_unifications = true;
}
(_, _) => {}
} }
} }
} }
@ -616,8 +623,21 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
} }
} }
pub fn opportunistic_resolve_var(&mut self, rid: RegionVid) -> ty::RegionVid { pub fn opportunistic_resolve_region(
self.unification_table().find(rid) &mut self,
tcx: TyCtxt<'tcx>,
region: ty::Region<'tcx>,
) -> ty::Region<'tcx> {
match region {
ty::ReVar(rid) => {
let unified_region = self.unification_table().probe_value(*rid);
unified_region.0.unwrap_or_else(|| {
let root = self.unification_table().find(*rid).vid;
tcx.reuse_or_mk_region(region, ty::ReVar(root))
})
}
_ => region,
}
} }
fn combine_map(&mut self, t: CombineMapType) -> &mut CombineMap<'tcx> { fn combine_map(&mut self, t: CombineMapType) -> &mut CombineMap<'tcx> {
@ -672,8 +692,8 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
&self, &self,
value_count: usize, value_count: usize,
) -> (Range<RegionVid>, Vec<RegionVariableOrigin>) { ) -> (Range<RegionVid>, Vec<RegionVariableOrigin>) {
let range = RegionVid::from_index(value_count as u32) let range = RegionVid::from(value_count as u32)
..RegionVid::from_index(self.unification_table.len() as u32); ..RegionVid::from(self.unification_table.len() as u32);
( (
range.clone(), range.clone(),
(range.start.index()..range.end.index()) (range.start.index()..range.end.index())
@ -695,7 +715,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
} }
#[inline] #[inline]
fn unification_table(&mut self) -> super::UnificationTable<'_, 'tcx, ty::RegionVid> { fn unification_table(&mut self) -> super::UnificationTable<'_, 'tcx, RegionVidKey<'tcx>> {
ut::UnificationTable::with_log(&mut self.storage.unification_table, self.undo_log) ut::UnificationTable::with_log(&mut self.storage.unification_table, self.undo_log)
} }
} }

View file

@ -84,18 +84,12 @@ impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticRegionResolver<'a, 'tcx> {
} }
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
match *r { let tcx = self.tcx();
ty::ReVar(rid) => { self.infcx
let resolved = self .inner
.infcx .borrow_mut()
.inner .unwrap_region_constraints()
.borrow_mut() .opportunistic_resolve_region(tcx, r)
.unwrap_region_constraints()
.opportunistic_resolve_var(rid);
self.tcx().reuse_or_mk_region(r, ty::ReVar(resolved))
}
_ => r,
}
} }
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {

View file

@ -3,6 +3,7 @@ use std::marker::PhantomData;
use rustc_data_structures::snapshot_vec as sv; use rustc_data_structures::snapshot_vec as sv;
use rustc_data_structures::undo_log::{Rollback, UndoLogs}; use rustc_data_structures::undo_log::{Rollback, UndoLogs};
use rustc_data_structures::unify as ut; use rustc_data_structures::unify as ut;
use rustc_middle::infer::unify_key::RegionVidKey;
use rustc_middle::ty; use rustc_middle::ty;
use crate::{ use crate::{
@ -22,7 +23,7 @@ pub(crate) enum UndoLog<'tcx> {
IntUnificationTable(sv::UndoLog<ut::Delegate<ty::IntVid>>), IntUnificationTable(sv::UndoLog<ut::Delegate<ty::IntVid>>),
FloatUnificationTable(sv::UndoLog<ut::Delegate<ty::FloatVid>>), FloatUnificationTable(sv::UndoLog<ut::Delegate<ty::FloatVid>>),
RegionConstraintCollector(region_constraints::UndoLog<'tcx>), RegionConstraintCollector(region_constraints::UndoLog<'tcx>),
RegionUnificationTable(sv::UndoLog<ut::Delegate<ty::RegionVid>>), RegionUnificationTable(sv::UndoLog<ut::Delegate<RegionVidKey<'tcx>>>),
ProjectionCache(traits::UndoLog<'tcx>), ProjectionCache(traits::UndoLog<'tcx>),
PushRegionObligation, PushRegionObligation,
} }
@ -55,7 +56,7 @@ impl_from! {
ConstUnificationTable(sv::UndoLog<ut::Delegate<ty::ConstVid<'tcx>>>), ConstUnificationTable(sv::UndoLog<ut::Delegate<ty::ConstVid<'tcx>>>),
RegionUnificationTable(sv::UndoLog<ut::Delegate<ty::RegionVid>>), RegionUnificationTable(sv::UndoLog<ut::Delegate<RegionVidKey<'tcx>>>),
ProjectionCache(traits::UndoLog<'tcx>), ProjectionCache(traits::UndoLog<'tcx>),
} }

View file

@ -16,37 +16,45 @@ pub trait ToType {
} }
#[derive(PartialEq, Copy, Clone, Debug)] #[derive(PartialEq, Copy, Clone, Debug)]
pub struct RegionVidKey { pub struct UnifiedRegion<'tcx>(pub Option<ty::Region<'tcx>>);
/// The minimum region vid in the unification set. This is needed
/// to have a canonical name for a type to prevent infinite #[derive(PartialEq, Copy, Clone, Debug)]
/// recursion. pub struct RegionVidKey<'tcx> {
pub min_vid: ty::RegionVid, pub vid: ty::RegionVid,
pub phantom: PhantomData<UnifiedRegion<'tcx>>,
} }
impl UnifyValue for RegionVidKey { impl<'tcx> From<ty::RegionVid> for RegionVidKey<'tcx> {
fn from(vid: ty::RegionVid) -> Self {
RegionVidKey { vid, phantom: PhantomData }
}
}
impl<'tcx> UnifyKey for RegionVidKey<'tcx> {
type Value = UnifiedRegion<'tcx>;
fn index(&self) -> u32 {
self.vid.as_u32()
}
fn from_index(i: u32) -> Self {
RegionVidKey::from(ty::RegionVid::from_u32(i))
}
fn tag() -> &'static str {
"RegionVidKey"
}
}
impl<'tcx> UnifyValue for UnifiedRegion<'tcx> {
type Error = NoError; type Error = NoError;
fn unify_values(value1: &Self, value2: &Self) -> Result<Self, NoError> { fn unify_values(value1: &Self, value2: &Self) -> Result<Self, NoError> {
let min_vid = if value1.min_vid.index() < value2.min_vid.index() { Ok(match (value1.0, value2.0) {
value1.min_vid (Some(_), Some(_)) => *value1,
} else {
value2.min_vid
};
Ok(RegionVidKey { min_vid }) (Some(_), _) => *value1,
} (_, Some(_)) => *value2,
}
impl UnifyKey for ty::RegionVid { (None, None) => *value1,
type Value = (); })
fn index(&self) -> u32 {
u32::from(*self)
}
fn from_index(i: u32) -> ty::RegionVid {
ty::RegionVid::from(i)
}
fn tag() -> &'static str {
"RegionVid"
} }
} }

View file

@ -102,6 +102,8 @@ pub fn call_tuple_two() {
tuple_two::<Tuple>(); tuple_two::<Tuple>();
//~^ ERROR implementation of `TheTrait` is not general enough //~^ ERROR implementation of `TheTrait` is not general enough
//~| ERROR implementation of `TheTrait` is not general enough //~| ERROR implementation of `TheTrait` is not general enough
//~| ERROR mismatched types
//~| ERROR mismatched types
} }
pub fn call_tuple_three() { pub fn call_tuple_three() {

View file

@ -46,6 +46,34 @@ LL | tuple_one::<Tuple>();
= note: `Tuple` must implement `TheTrait<(&'0 isize, &'1 isize)>`, for any two lifetimes `'0` and `'1`... = note: `Tuple` must implement `TheTrait<(&'0 isize, &'1 isize)>`, for any two lifetimes `'0` and `'1`...
= note: ...but it actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2` = note: ...but it actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
error[E0308]: mismatched types
--> $DIR/associated-types-eq-hr.rs:102:5
|
LL | tuple_two::<Tuple>();
| ^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected reference `&'x isize`
found reference `&'y isize`
note: the lifetime requirement is introduced here
--> $DIR/associated-types-eq-hr.rs:66:53
|
LL | T: for<'x, 'y> TheTrait<(&'x isize, &'y isize), A = &'y isize>,
| ^^^^^^^^^^^^^
error[E0308]: mismatched types
--> $DIR/associated-types-eq-hr.rs:102:5
|
LL | tuple_two::<Tuple>();
| ^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected reference `&'x isize`
found reference `&'y isize`
note: the lifetime requirement is introduced here
--> $DIR/associated-types-eq-hr.rs:66:53
|
LL | T: for<'x, 'y> TheTrait<(&'x isize, &'y isize), A = &'y isize>,
| ^^^^^^^^^^^^^
error: implementation of `TheTrait` is not general enough error: implementation of `TheTrait` is not general enough
--> $DIR/associated-types-eq-hr.rs:102:5 --> $DIR/associated-types-eq-hr.rs:102:5
| |
@ -65,7 +93,7 @@ LL | tuple_two::<Tuple>();
= note: ...but it actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2` = note: ...but it actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
error: implementation of `TheTrait` is not general enough error: implementation of `TheTrait` is not general enough
--> $DIR/associated-types-eq-hr.rs:112:5 --> $DIR/associated-types-eq-hr.rs:114:5
| |
LL | tuple_four::<Tuple>(); LL | tuple_four::<Tuple>();
| ^^^^^^^^^^^^^^^^^^^ implementation of `TheTrait` is not general enough | ^^^^^^^^^^^^^^^^^^^ implementation of `TheTrait` is not general enough
@ -73,6 +101,7 @@ LL | tuple_four::<Tuple>();
= note: `Tuple` must implement `TheTrait<(&'0 isize, &'1 isize)>`, for any two lifetimes `'0` and `'1`... = note: `Tuple` must implement `TheTrait<(&'0 isize, &'1 isize)>`, for any two lifetimes `'0` and `'1`...
= note: ...but it actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2` = note: ...but it actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
error: aborting due to 7 previous errors error: aborting due to 9 previous errors
For more information about this error, try `rustc --explain E0271`. Some errors have detailed explanations: E0271, E0308.
For more information about an error, try `rustc --explain E0271`.

View file

@ -9,7 +9,7 @@ LL | let v = Unit2.m(
= help: consider constraining the associated type `<_ as Ty<'_>>::V` to `Unit4` = help: consider constraining the associated type `<_ as Ty<'_>>::V` to `Unit4`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
error[E0271]: type mismatch resolving `<[closure@$DIR/issue-62203-hrtb-ice.rs:42:17: 42:39] as FnOnce<((&u8,),)>>::Output == Unit3` error[E0271]: type mismatch resolving `<[closure@$DIR/issue-62203-hrtb-ice.rs:42:17: 42:39] as FnOnce<((&'r u8,),)>>::Output == Unit3`
--> $DIR/issue-62203-hrtb-ice.rs:38:19 --> $DIR/issue-62203-hrtb-ice.rs:38:19
| |
LL | let v = Unit2.m( LL | let v = Unit2.m(

View file

@ -23,6 +23,10 @@ static SOME_STRUCT: &SomeStruct = &SomeStruct {
bar: &Bar { bools: &[true, true] }, bar: &Bar { bools: &[true, true] },
f: &id, f: &id,
//~^ ERROR implementation of `FnOnce` is not general enough //~^ ERROR implementation of `FnOnce` is not general enough
//~^^ mismatched types
//~^^^ mismatched types
//~^^^^ mismatched types
//~^^^^^ mismatched types
}; };
// very simple test for a 'static static with default lifetime // very simple test for a 'static static with default lifetime

View file

@ -1,3 +1,39 @@
error[E0308]: mismatched types
--> $DIR/rfc1623.rs:24:8
|
LL | f: &id,
| ^^^ one type is more general than the other
|
= note: expected reference `&'a Foo<'b>`
found reference `&'a Foo<'b>`
error[E0308]: mismatched types
--> $DIR/rfc1623.rs:24:8
|
LL | f: &id,
| ^^^ one type is more general than the other
|
= note: expected reference `&'a Foo<'b>`
found reference `&'a Foo<'b>`
error[E0308]: mismatched types
--> $DIR/rfc1623.rs:24:8
|
LL | f: &id,
| ^^^ one type is more general than the other
|
= note: expected reference `&'a Foo<'b>`
found reference `&'a Foo<'b>`
error[E0308]: mismatched types
--> $DIR/rfc1623.rs:24:8
|
LL | f: &id,
| ^^^ one type is more general than the other
|
= note: expected reference `&'a Foo<'b>`
found reference `&'a Foo<'b>`
error: implementation of `FnOnce` is not general enough error: implementation of `FnOnce` is not general enough
--> $DIR/rfc1623.rs:24:8 --> $DIR/rfc1623.rs:24:8
| |
@ -7,5 +43,6 @@ LL | f: &id,
= note: `fn(&'2 Foo<'_>) -> &'2 Foo<'_> {id::<&'2 Foo<'_>>}` must implement `FnOnce<(&'1 Foo<'b>,)>`, for any lifetime `'1`... = note: `fn(&'2 Foo<'_>) -> &'2 Foo<'_> {id::<&'2 Foo<'_>>}` must implement `FnOnce<(&'1 Foo<'b>,)>`, for any lifetime `'1`...
= note: ...but it actually implements `FnOnce<(&'2 Foo<'_>,)>`, for some specific lifetime `'2` = note: ...but it actually implements `FnOnce<(&'2 Foo<'_>,)>`, for some specific lifetime `'2`
error: aborting due to previous error error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0308`.

View file

@ -26,6 +26,4 @@ fn main() {
// Should only be a few notes. // Should only be a few notes.
is_send::<X<C<'static>>>(); is_send::<X<C<'static>>>();
//~^ ERROR overflow evaluating //~^ ERROR overflow evaluating
//~^^ 2 redundant
//~^^^ required because of
} }

View file

@ -1,4 +1,4 @@
error[E0275]: overflow evaluating the requirement `Box<X<C<'_>>>: NotAuto` error[E0275]: overflow evaluating the requirement `Box<X<C<'static>>>: NotAuto`
--> $DIR/lifetime.rs:27:5 --> $DIR/lifetime.rs:27:5
| |
LL | fn is_send<S: NotAuto>() {} LL | fn is_send<S: NotAuto>() {}
@ -7,13 +7,11 @@ LL | fn is_send<S: NotAuto>() {}
LL | is_send::<X<C<'static>>>(); LL | is_send::<X<C<'static>>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^
| |
note: required because of the requirements on the impl of `NotAuto` for `X<C<'_>>` note: required because of the requirements on the impl of `NotAuto` for `X<C<'static>>`
--> $DIR/lifetime.rs:19:12 --> $DIR/lifetime.rs:19:12
| |
LL | impl<T: Y> NotAuto for X<T> where T::P: NotAuto {} LL | impl<T: Y> NotAuto for X<T> where T::P: NotAuto {}
| ^^^^^^^ ^^^^ | ^^^^^^^ ^^^^
= note: 2 redundant requirements hidden
= note: required because of the requirements on the impl of `NotAuto` for `X<C<'static>>`
error: aborting due to previous error error: aborting due to previous error

View file

@ -9,7 +9,7 @@ trait Bug {
impl Bug for &() { impl Bug for &() {
type Item = impl Bug; //~ ERROR `impl Trait` in type aliases is unstable type Item = impl Bug; //~ ERROR `impl Trait` in type aliases is unstable
//~^ ERROR the trait bound `(): Bug` is not satisfied //~^ ERROR the trait bound `(): Bug` is not satisfied
//~^^ ERROR could not find defining uses //~^^ ERROR the trait bound
const FUN: fn() -> Self::Item = || (); const FUN: fn() -> Self::Item = || ();
//~^ ERROR type alias impl trait is not permitted here //~^ ERROR type alias impl trait is not permitted here

View file

@ -25,11 +25,14 @@ LL | type Item = impl Bug;
= help: the following implementations were found: = help: the following implementations were found:
<&() as Bug> <&() as Bug>
error: could not find defining uses error[E0277]: the trait bound `(): Bug` is not satisfied
--> $DIR/issue-60371.rs:10:17 --> $DIR/issue-60371.rs:10:17
| |
LL | type Item = impl Bug; LL | type Item = impl Bug;
| ^^^^^^^^ | ^^^^^^^^ the trait `Bug` is not implemented for `()`
|
= help: the following implementations were found:
<&() as Bug>
error: aborting due to 4 previous errors error: aborting due to 4 previous errors