Skip to content

Commit

Permalink
Rollup merge of rust-lang#79945 - jackh726:existential_trait_ref, r=n…
Browse files Browse the repository at this point in the history
…ikomatsakis

Move binder for dyn to each list item

This essentially changes `ty::Binder<&'tcx List<ExistentialTraitRef>>` to `&'tcx List<ty::Binder<ExistentialTraitRef>>`.

This is a first step in moving the `dyn Trait` representation closer to Chalk, which we've talked about in `````@rust-lang/wg-traits.`````

r? `````@nikomatsakis`````
  • Loading branch information
Dylan-DPC committed Dec 16, 2020
2 parents 864d804 + 01c2520 commit a674cb3
Show file tree
Hide file tree
Showing 25 changed files with 312 additions and 265 deletions.
24 changes: 13 additions & 11 deletions compiler/rustc_codegen_cranelift/src/value_and_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,17 +480,19 @@ impl<'tcx> CPlace<'tcx> {
// fn(&T) -> for<'l> fn(&'l T) is allowed
}
(&ty::Dynamic(from_traits, _), &ty::Dynamic(to_traits, _)) => {
let from_traits = fx
.tcx
.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), from_traits);
let to_traits = fx
.tcx
.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), to_traits);
assert_eq!(
from_traits, to_traits,
"Can't write trait object of incompatible traits {:?} to place with traits {:?}\n\n{:#?}",
from_traits, to_traits, fx,
);
for (from, to) in from_traits.iter().zip(to_traits) {
let from = fx
.tcx
.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), from);
let to = fx
.tcx
.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), to);
assert_eq!(
from, to,
"Can't write trait object of incompatible traits {:?} to place with traits {:?}\n\n{:#?}",
from_traits, to_traits, fx,
);
}
// dyn for<'r> Trait<'r> -> dyn Trait<'_> is allowed
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

fn print_dyn_existential(
self,
_predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
_predicates: &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>>,
) -> Result<Self::DynExistential, Self::Error> {
Err(NonTrivialPath)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ impl<'tcx> LateContext<'tcx> {

fn print_dyn_existential(
self,
_predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
_predicates: &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>>,
) -> Result<Self::DynExistential, Self::Error> {
Ok(())
}
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,10 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
}
ty::Dynamic(binder, _) => {
let mut has_emitted = false;
for predicate in binder.skip_binder().iter() {
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate {
for predicate in binder.iter() {
if let ty::ExistentialPredicate::Trait(ref trait_ref) =
predicate.skip_binder()
{
let def_id = trait_ref.def_id;
let descr_post =
&format!(" trait object{}{}", plural_suffix, descr_post,);
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_middle/src/ty/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,14 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<Ty<'tcx>> {
}
}

impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<ty::ExistentialPredicate<'tcx>> {
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D>
for ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>>
{
fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
let len = decoder.read_usize()?;
Ok(decoder.tcx().mk_existential_predicates((0..len).map(|_| Decodable::decode(decoder)))?)
Ok(decoder
.tcx()
.mk_poly_existential_predicates((0..len).map(|_| Decodable::decode(decoder)))?)
}
}

Expand Down Expand Up @@ -373,7 +377,7 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::N
impl_decodable_via_ref! {
&'tcx ty::TypeckResults<'tcx>,
&'tcx ty::List<Ty<'tcx>>,
&'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
&'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>>,
&'tcx Allocation,
&'tcx mir::Body<'tcx>,
&'tcx mir::UnsafetyCheckResult,
Expand Down
34 changes: 21 additions & 13 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub struct CtxtInterners<'tcx> {
substs: InternedSet<'tcx, InternalSubsts<'tcx>>,
canonical_var_infos: InternedSet<'tcx, List<CanonicalVarInfo<'tcx>>>,
region: InternedSet<'tcx, RegionKind>,
existential_predicates: InternedSet<'tcx, List<ExistentialPredicate<'tcx>>>,
poly_existential_predicates: InternedSet<'tcx, List<ty::Binder<ExistentialPredicate<'tcx>>>>,
predicate: InternedSet<'tcx, PredicateInner<'tcx>>,
predicates: InternedSet<'tcx, List<Predicate<'tcx>>>,
projs: InternedSet<'tcx, List<ProjectionKind>>,
Expand All @@ -103,7 +103,7 @@ impl<'tcx> CtxtInterners<'tcx> {
type_list: Default::default(),
substs: Default::default(),
region: Default::default(),
existential_predicates: Default::default(),
poly_existential_predicates: Default::default(),
canonical_var_infos: Default::default(),
predicate: Default::default(),
predicates: Default::default(),
Expand Down Expand Up @@ -1623,7 +1623,7 @@ nop_lift! {const_; &'a Const<'a> => &'tcx Const<'tcx>}
nop_lift! {predicate; &'a PredicateInner<'a> => &'tcx PredicateInner<'tcx>}

nop_list_lift! {type_list; Ty<'a> => Ty<'tcx>}
nop_list_lift! {existential_predicates; ExistentialPredicate<'a> => ExistentialPredicate<'tcx>}
nop_list_lift! {poly_existential_predicates; ty::Binder<ExistentialPredicate<'a>> => ty::Binder<ExistentialPredicate<'tcx>>}
nop_list_lift! {predicates; Predicate<'a> => Predicate<'tcx>}
nop_list_lift! {canonical_var_infos; CanonicalVarInfo<'a> => CanonicalVarInfo<'tcx>}
nop_list_lift! {projs; ProjectionKind => ProjectionKind}
Expand Down Expand Up @@ -2064,7 +2064,8 @@ slice_interners!(
type_list: _intern_type_list(Ty<'tcx>),
substs: _intern_substs(GenericArg<'tcx>),
canonical_var_infos: _intern_canonical_var_infos(CanonicalVarInfo<'tcx>),
existential_predicates: _intern_existential_predicates(ExistentialPredicate<'tcx>),
poly_existential_predicates:
_intern_poly_existential_predicates(ty::Binder<ExistentialPredicate<'tcx>>),
predicates: _intern_predicates(Predicate<'tcx>),
projs: _intern_projs(ProjectionKind),
place_elems: _intern_place_elems(PlaceElem<'tcx>),
Expand Down Expand Up @@ -2295,7 +2296,7 @@ impl<'tcx> TyCtxt<'tcx> {
#[inline]
pub fn mk_dynamic(
self,
obj: ty::Binder<&'tcx List<ExistentialPredicate<'tcx>>>,
obj: &'tcx List<ty::Binder<ExistentialPredicate<'tcx>>>,
reg: ty::Region<'tcx>,
) -> Ty<'tcx> {
self.mk_ty(Dynamic(obj, reg))
Expand Down Expand Up @@ -2425,13 +2426,17 @@ impl<'tcx> TyCtxt<'tcx> {
Place { local: place.local, projection: self.intern_place_elems(&projection) }
}

pub fn intern_existential_predicates(
pub fn intern_poly_existential_predicates(
self,
eps: &[ExistentialPredicate<'tcx>],
) -> &'tcx List<ExistentialPredicate<'tcx>> {
eps: &[ty::Binder<ExistentialPredicate<'tcx>>],
) -> &'tcx List<ty::Binder<ExistentialPredicate<'tcx>>> {
assert!(!eps.is_empty());
assert!(eps.array_windows().all(|[a, b]| a.stable_cmp(self, b) != Ordering::Greater));
self._intern_existential_predicates(eps)
assert!(
eps.array_windows()
.all(|[a, b]| a.skip_binder().stable_cmp(self, &b.skip_binder())
!= Ordering::Greater)
);
self._intern_poly_existential_predicates(eps)
}

pub fn intern_predicates(self, preds: &[Predicate<'tcx>]) -> &'tcx List<Predicate<'tcx>> {
Expand Down Expand Up @@ -2488,13 +2493,16 @@ impl<'tcx> TyCtxt<'tcx> {
})
}

pub fn mk_existential_predicates<
I: InternAs<[ExistentialPredicate<'tcx>], &'tcx List<ExistentialPredicate<'tcx>>>,
pub fn mk_poly_existential_predicates<
I: InternAs<
[ty::Binder<ExistentialPredicate<'tcx>>],
&'tcx List<ty::Binder<ExistentialPredicate<'tcx>>>,
>,
>(
self,
iter: I,
) -> I::Output {
iter.intern_with(|xs| self.intern_existential_predicates(xs))
iter.intern_with(|xs| self.intern_poly_existential_predicates(xs))
}

pub fn mk_predicates<I: InternAs<[Predicate<'tcx>], &'tcx List<Predicate<'tcx>>>>(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub enum TypeError<'tcx> {
CyclicTy(Ty<'tcx>),
CyclicConst(&'tcx ty::Const<'tcx>),
ProjectionMismatched(ExpectedFound<DefId>),
ExistentialMismatch(ExpectedFound<&'tcx ty::List<ty::ExistentialPredicate<'tcx>>>),
ExistentialMismatch(ExpectedFound<&'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>>>),
ObjectUnsafeCoercion(DefId),
ConstMismatch(ExpectedFound<&'tcx ty::Const<'tcx>>),

Expand Down
20 changes: 8 additions & 12 deletions compiler/rustc_middle/src/ty/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,15 @@ impl FlagComputation {
}

&ty::Dynamic(obj, r) => {
self.bound_computation(obj, |computation, obj| {
for predicate in obj.iter() {
match predicate {
ty::ExistentialPredicate::Trait(tr) => {
computation.add_substs(tr.substs)
}
ty::ExistentialPredicate::Projection(p) => {
computation.add_existential_projection(&p);
}
ty::ExistentialPredicate::AutoTrait(_) => {}
for predicate in obj.iter() {
self.bound_computation(predicate, |computation, predicate| match predicate {
ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),
ty::ExistentialPredicate::Projection(p) => {
computation.add_existential_projection(&p);
}
}
});
ty::ExistentialPredicate::AutoTrait(_) => {}
});
}

self.add_region(r);
}
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_middle/src/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub trait Printer<'tcx>: Sized {

fn print_dyn_existential(
self,
predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
predicates: &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>>,
) -> Result<Self::DynExistential, Self::Error>;

fn print_const(self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error>;
Expand Down Expand Up @@ -343,7 +343,9 @@ impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for Ty<'tcx> {
}
}

impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
impl<'tcx, P: Printer<'tcx>> Print<'tcx, P>
for &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>>
{
type Output = P::DynExistential;
type Error = P::Error;
fn print(&self, cx: P) -> Result<Self::Output, Self::Error> {
Expand Down
Loading

0 comments on commit a674cb3

Please sign in to comment.