Skip to content

Commit 905f565

Browse files
Apply nits, uplift ExistentialPredicate too
1 parent 0a8f338 commit 905f565

File tree

9 files changed

+100
-82
lines changed

9 files changed

+100
-82
lines changed

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::def::{DefKind, Res};
77
use rustc_hir::def_id::DefId;
88
use rustc_lint_defs::builtin::UNUSED_ASSOCIATED_TYPE_BOUNDS;
99
use rustc_middle::ty::fold::BottomUpFolder;
10-
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
10+
use rustc_middle::ty::{self, ExistentialPredicateStableCmpExt as _, Ty, TyCtxt, TypeFoldable};
1111
use rustc_middle::ty::{DynKind, ToPredicate};
1212
use rustc_span::{ErrorGuaranteed, Span};
1313
use rustc_trait_selection::traits::error_reporting::report_object_safety_error;

Diff for: compiler/rustc_middle/src/ty/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::traits::solve;
2626
use crate::traits::solve::{
2727
ExternalConstraints, ExternalConstraintsData, PredefinedOpaques, PredefinedOpaquesData,
2828
};
29+
use crate::ty::predicate::ExistentialPredicateStableCmpExt as _;
2930
use crate::ty::{
3031
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Clauses, Const, ConstData,
3132
GenericParamDefKind, ImplPolarity, List, ListWithCachedTypeInfo, ParamConst, ParamTy, Pattern,

Diff for: compiler/rustc_middle/src/ty/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ pub use self::list::{List, ListWithCachedTypeInfo};
9696
pub use self::parameterized::ParameterizedOverTcx;
9797
pub use self::pattern::{Pattern, PatternKind};
9898
pub use self::predicate::{
99-
Clause, ClauseKind, CoercePredicate, ExistentialPredicate, ExistentialProjection,
100-
ExistentialTraitRef, NormalizesTo, OutlivesPredicate, PolyCoercePredicate,
101-
PolyExistentialPredicate, PolyExistentialProjection, PolyExistentialTraitRef,
102-
PolyProjectionPredicate, PolyRegionOutlivesPredicate, PolySubtypePredicate, PolyTraitPredicate,
103-
PolyTraitRef, PolyTypeOutlivesPredicate, Predicate, PredicateKind, ProjectionPredicate,
104-
RegionOutlivesPredicate, SubtypePredicate, ToPolyTraitRef, ToPredicate, TraitPredicate,
105-
TraitRef, TypeOutlivesPredicate,
99+
Clause, ClauseKind, CoercePredicate, ExistentialPredicate, ExistentialPredicateStableCmpExt,
100+
ExistentialProjection, ExistentialTraitRef, NormalizesTo, OutlivesPredicate,
101+
PolyCoercePredicate, PolyExistentialPredicate, PolyExistentialProjection,
102+
PolyExistentialTraitRef, PolyProjectionPredicate, PolyRegionOutlivesPredicate,
103+
PolySubtypePredicate, PolyTraitPredicate, PolyTraitRef, PolyTypeOutlivesPredicate, Predicate,
104+
PredicateKind, ProjectionPredicate, RegionOutlivesPredicate, SubtypePredicate, ToPolyTraitRef,
105+
ToPredicate, TraitPredicate, TraitRef, TypeOutlivesPredicate,
106106
};
107107
pub use self::region::{
108108
BoundRegion, BoundRegionKind, BoundRegionKind::*, EarlyParamRegion, LateParamRegion, Region,

Diff for: compiler/rustc_middle/src/ty/predicate.rs

+28-52
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
11
use rustc_data_structures::captures::Captures;
22
use rustc_data_structures::intern::Interned;
33
use rustc_hir::def_id::DefId;
4-
use rustc_macros::{HashStable, Lift, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
5-
use rustc_type_ir::ClauseKind as IrClauseKind;
6-
use rustc_type_ir::CoercePredicate as IrCoercePredicate;
7-
use rustc_type_ir::ExistentialProjection as IrExistentialProjection;
8-
use rustc_type_ir::ExistentialTraitRef as IrExistentialTraitRef;
9-
use rustc_type_ir::NormalizesTo as IrNormalizesTo;
10-
use rustc_type_ir::PredicateKind as IrPredicateKind;
11-
use rustc_type_ir::ProjectionPredicate as IrProjectionPredicate;
12-
use rustc_type_ir::SubtypePredicate as IrSubtypePredicate;
13-
use rustc_type_ir::TraitPredicate as IrTraitPredicate;
14-
use rustc_type_ir::TraitRef as IrTraitRef;
4+
use rustc_macros::{
5+
extension, HashStable, Lift, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable,
6+
};
7+
use rustc_type_ir as ir;
158
use std::cmp::Ordering;
169

1710
use crate::ty::{
18-
self, Binder, DebruijnIndex, DebugWithInfcx, EarlyBinder, PredicatePolarity, Term, Ty, TyCtxt,
19-
TypeFlags, WithCachedTypeInfo,
11+
self, Binder, DebruijnIndex, EarlyBinder, PredicatePolarity, Term, Ty, TyCtxt, TypeFlags,
12+
WithCachedTypeInfo,
2013
};
2114

22-
pub type TraitRef<'tcx> = IrTraitRef<TyCtxt<'tcx>>;
23-
pub type ProjectionPredicate<'tcx> = IrProjectionPredicate<TyCtxt<'tcx>>;
24-
pub type ExistentialTraitRef<'tcx> = IrExistentialTraitRef<TyCtxt<'tcx>>;
25-
pub type ExistentialProjection<'tcx> = IrExistentialProjection<TyCtxt<'tcx>>;
26-
pub type TraitPredicate<'tcx> = IrTraitPredicate<TyCtxt<'tcx>>;
27-
pub type ClauseKind<'tcx> = IrClauseKind<TyCtxt<'tcx>>;
28-
pub type PredicateKind<'tcx> = IrPredicateKind<TyCtxt<'tcx>>;
29-
pub type NormalizesTo<'tcx> = IrNormalizesTo<TyCtxt<'tcx>>;
30-
pub type CoercePredicate<'tcx> = IrCoercePredicate<TyCtxt<'tcx>>;
31-
pub type SubtypePredicate<'tcx> = IrSubtypePredicate<TyCtxt<'tcx>>;
15+
pub type TraitRef<'tcx> = ir::TraitRef<TyCtxt<'tcx>>;
16+
pub type ProjectionPredicate<'tcx> = ir::ProjectionPredicate<TyCtxt<'tcx>>;
17+
pub type ExistentialPredicate<'tcx> = ir::ExistentialPredicate<TyCtxt<'tcx>>;
18+
pub type ExistentialTraitRef<'tcx> = ir::ExistentialTraitRef<TyCtxt<'tcx>>;
19+
pub type ExistentialProjection<'tcx> = ir::ExistentialProjection<TyCtxt<'tcx>>;
20+
pub type TraitPredicate<'tcx> = ir::TraitPredicate<TyCtxt<'tcx>>;
21+
pub type ClauseKind<'tcx> = ir::ClauseKind<TyCtxt<'tcx>>;
22+
pub type PredicateKind<'tcx> = ir::PredicateKind<TyCtxt<'tcx>>;
23+
pub type NormalizesTo<'tcx> = ir::NormalizesTo<TyCtxt<'tcx>>;
24+
pub type CoercePredicate<'tcx> = ir::CoercePredicate<TyCtxt<'tcx>>;
25+
pub type SubtypePredicate<'tcx> = ir::SubtypePredicate<TyCtxt<'tcx>>;
3226

3327
/// A statement that can be proven by a trait solver. This includes things that may
3428
/// show up in where clauses, such as trait predicates and projection predicates,
@@ -207,43 +201,25 @@ impl<'tcx> Clause<'tcx> {
207201
}
208202
}
209203

210-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
211-
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
212-
pub enum ExistentialPredicate<'tcx> {
213-
/// E.g., `Iterator`.
214-
Trait(ExistentialTraitRef<'tcx>),
215-
/// E.g., `Iterator::Item = T`.
216-
Projection(ExistentialProjection<'tcx>),
217-
/// E.g., `Send`.
218-
AutoTrait(DefId),
219-
}
220-
221-
impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ExistentialPredicate<'tcx> {
222-
fn fmt<Infcx: rustc_type_ir::InferCtxtLike<Interner = TyCtxt<'tcx>>>(
223-
this: rustc_type_ir::WithInfcx<'_, Infcx, &Self>,
224-
f: &mut std::fmt::Formatter<'_>,
225-
) -> std::fmt::Result {
226-
std::fmt::Debug::fmt(&this.data, f)
227-
}
228-
}
229-
204+
#[extension(pub trait ExistentialPredicateStableCmpExt<'tcx>)]
230205
impl<'tcx> ExistentialPredicate<'tcx> {
231206
/// Compares via an ordering that will not change if modules are reordered or other changes are
232207
/// made to the tree. In particular, this ordering is preserved across incremental compilations.
233-
pub fn stable_cmp(&self, tcx: TyCtxt<'tcx>, other: &Self) -> Ordering {
234-
use self::ExistentialPredicate::*;
208+
fn stable_cmp(&self, tcx: TyCtxt<'tcx>, other: &Self) -> Ordering {
235209
match (*self, *other) {
236-
(Trait(_), Trait(_)) => Ordering::Equal,
237-
(Projection(ref a), Projection(ref b)) => {
210+
(ExistentialPredicate::Trait(_), ExistentialPredicate::Trait(_)) => Ordering::Equal,
211+
(ExistentialPredicate::Projection(ref a), ExistentialPredicate::Projection(ref b)) => {
238212
tcx.def_path_hash(a.def_id).cmp(&tcx.def_path_hash(b.def_id))
239213
}
240-
(AutoTrait(ref a), AutoTrait(ref b)) => {
214+
(ExistentialPredicate::AutoTrait(ref a), ExistentialPredicate::AutoTrait(ref b)) => {
241215
tcx.def_path_hash(*a).cmp(&tcx.def_path_hash(*b))
242216
}
243-
(Trait(_), _) => Ordering::Less,
244-
(Projection(_), Trait(_)) => Ordering::Greater,
245-
(Projection(_), _) => Ordering::Less,
246-
(AutoTrait(_), _) => Ordering::Greater,
217+
(ExistentialPredicate::Trait(_), _) => Ordering::Less,
218+
(ExistentialPredicate::Projection(_), ExistentialPredicate::Trait(_)) => {
219+
Ordering::Greater
220+
}
221+
(ExistentialPredicate::Projection(_), _) => Ordering::Less,
222+
(ExistentialPredicate::AutoTrait(_), _) => Ordering::Greater,
247223
}
248224
}
249225
}

Diff for: compiler/rustc_middle/src/ty/print/pretty.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -3088,6 +3088,16 @@ define_print! {
30883088
}
30893089
}
30903090

3091+
ty::ExistentialPredicate<'tcx> {
3092+
match *self {
3093+
ty::ExistentialPredicate::Trait(x) => p!(print(x)),
3094+
ty::ExistentialPredicate::Projection(x) => p!(print(x)),
3095+
ty::ExistentialPredicate::AutoTrait(def_id) => {
3096+
p!(print_def_path(def_id, &[]));
3097+
}
3098+
}
3099+
}
3100+
30913101
ty::ExistentialTraitRef<'tcx> {
30923102
// Use a type that can't appear in defaults of type parameters.
30933103
let dummy_self = Ty::new_fresh(cx.tcx(), 0);
@@ -3132,16 +3142,6 @@ define_print_and_forward_display! {
31323142
p!("{{", comma_sep(self.iter()), "}}")
31333143
}
31343144

3135-
ty::ExistentialPredicate<'tcx> {
3136-
match *self {
3137-
ty::ExistentialPredicate::Trait(x) => p!(print(x)),
3138-
ty::ExistentialPredicate::Projection(x) => p!(print(x)),
3139-
ty::ExistentialPredicate::AutoTrait(def_id) => {
3140-
p!(print_def_path(def_id, &[]));
3141-
}
3142-
}
3143-
}
3144-
31453145
ty::FnSig<'tcx> {
31463146
p!(write("{}", self.unsafety.prefix_str()));
31473147

Diff for: compiler/rustc_middle/src/ty/relate.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
//! subtyping, type equality, etc.
66
77
use crate::ty::error::{ExpectedFound, TypeError};
8-
use crate::ty::{self, Expr, ImplSubject, Term, TermKind, Ty, TyCtxt, TypeFoldable};
9-
use crate::ty::{GenericArg, GenericArgKind, GenericArgsRef};
8+
use crate::ty::{
9+
self, ExistentialPredicate, ExistentialPredicateStableCmpExt as _, Expr, GenericArg,
10+
GenericArgKind, GenericArgsRef, ImplSubject, Term, TermKind, Ty, TyCtxt, TypeFoldable,
11+
};
1012
use rustc_hir as hir;
1113
use rustc_hir::def::DefKind;
1214
use rustc_hir::def_id::DefId;
@@ -702,14 +704,21 @@ impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
702704
}
703705

704706
let v = iter::zip(a_v, b_v).map(|(ep_a, ep_b)| {
705-
use crate::ty::ExistentialPredicate::*;
706707
match (ep_a.skip_binder(), ep_b.skip_binder()) {
707-
(Trait(a), Trait(b)) => Ok(ep_a
708-
.rebind(Trait(relation.relate(ep_a.rebind(a), ep_b.rebind(b))?.skip_binder()))),
709-
(Projection(a), Projection(b)) => Ok(ep_a.rebind(Projection(
710-
relation.relate(ep_a.rebind(a), ep_b.rebind(b))?.skip_binder(),
711-
))),
712-
(AutoTrait(a), AutoTrait(b)) if a == b => Ok(ep_a.rebind(AutoTrait(a))),
708+
(ExistentialPredicate::Trait(a), ExistentialPredicate::Trait(b)) => Ok(ep_a
709+
.rebind(ExistentialPredicate::Trait(
710+
relation.relate(ep_a.rebind(a), ep_b.rebind(b))?.skip_binder(),
711+
))),
712+
(ExistentialPredicate::Projection(a), ExistentialPredicate::Projection(b)) => {
713+
Ok(ep_a.rebind(ExistentialPredicate::Projection(
714+
relation.relate(ep_a.rebind(a), ep_b.rebind(b))?.skip_binder(),
715+
)))
716+
}
717+
(ExistentialPredicate::AutoTrait(a), ExistentialPredicate::AutoTrait(b))
718+
if a == b =>
719+
{
720+
Ok(ep_a.rebind(ExistentialPredicate::AutoTrait(a)))
721+
}
713722
_ => Err(TypeError::ExistentialMismatch(expected_found(a, b))),
714723
}
715724
});

Diff for: compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use rustc_hir::LangItem;
88
use rustc_middle::bug;
99
use rustc_middle::ty::fold::{TypeFolder, TypeSuperFoldable};
1010
use rustc_middle::ty::{
11-
self, Instance, IntTy, List, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, UintTy,
11+
self, ExistentialPredicateStableCmpExt as _, Instance, IntTy, List, Ty, TyCtxt, TypeFoldable,
12+
TypeVisitableExt, UintTy,
1213
};
1314
use rustc_span::sym;
1415
use rustc_trait_selection::traits;

Diff for: compiler/rustc_trait_selection/src/traits/object_safety.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use rustc_hir as hir;
1818
use rustc_hir::def_id::DefId;
1919
use rustc_middle::query::Providers;
2020
use rustc_middle::ty::{
21-
self, EarlyBinder, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor,
21+
self, EarlyBinder, ExistentialPredicateStableCmpExt as _, Ty, TyCtxt, TypeSuperVisitable,
22+
TypeVisitable, TypeVisitor,
2223
};
2324
use rustc_middle::ty::{GenericArg, GenericArgs};
2425
use rustc_middle::ty::{ToPredicate, TypeVisitableExt};

Diff for: compiler/rustc_type_ir/src/predicate.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Gen
55

66
use crate::inherent::*;
77
use crate::visit::TypeVisitableExt as _;
8-
use crate::Interner;
8+
use crate::{DebugWithInfcx, Interner};
99

1010
/// A complete reference to a trait. These take numerous guises in syntax,
1111
/// but perhaps the most recognizable form is in a where-clause:
@@ -146,6 +146,36 @@ impl fmt::Display for PredicatePolarity {
146146
}
147147
}
148148

149+
#[derive(derivative::Derivative)]
150+
#[derivative(
151+
Clone(bound = ""),
152+
Copy(bound = ""),
153+
Hash(bound = ""),
154+
PartialEq(bound = ""),
155+
Eq(bound = ""),
156+
Debug(bound = "")
157+
)]
158+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
159+
#[cfg_attr(feature = "nightly", derive(TyDecodable, TyEncodable, HashStable_NoContext))]
160+
pub enum ExistentialPredicate<I: Interner> {
161+
/// E.g., `Iterator`.
162+
Trait(ExistentialTraitRef<I>),
163+
/// E.g., `Iterator::Item = T`.
164+
Projection(ExistentialProjection<I>),
165+
/// E.g., `Send`.
166+
AutoTrait(I::DefId),
167+
}
168+
169+
// FIXME: Implement this the right way after
170+
impl<I: Interner> DebugWithInfcx<I> for ExistentialPredicate<I> {
171+
fn fmt<Infcx: rustc_type_ir::InferCtxtLike<Interner = I>>(
172+
this: rustc_type_ir::WithInfcx<'_, Infcx, &Self>,
173+
f: &mut fmt::Formatter<'_>,
174+
) -> fmt::Result {
175+
fmt::Debug::fmt(&this.data, f)
176+
}
177+
}
178+
149179
/// An existential reference to a trait, where `Self` is erased.
150180
/// For example, the trait object `Trait<'a, 'b, X, Y>` is:
151181
/// ```ignore (illustrative)

0 commit comments

Comments
 (0)