Skip to content

Commit 561ad7e

Browse files
committed
Derive traversable impls for more type lib kinds
1 parent da6f90a commit 561ad7e

File tree

2 files changed

+10
-194
lines changed

2 files changed

+10
-194
lines changed

compiler/rustc_type_ir/src/canonical.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
use std::fmt;
22
use std::hash::Hash;
3-
use std::ops::ControlFlow;
43

54
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
65
use rustc_serialize::{Decodable, Encodable};
76

8-
use crate::fold::{FallibleTypeFolder, TypeFoldable};
9-
use crate::visit::{TypeVisitable, TypeVisitor};
107
use crate::TyDecoder;
118
use crate::{HashStableContext, Interner, TyEncoder, UniverseIndex};
129

1310
/// A "canonicalized" type `V` is one where all free inference
1411
/// variables have been rewritten to "canonical vars". These are
1512
/// numbered starting from 0 in order of first appearance.
16-
#[derive(derivative::Derivative)]
13+
#[derive(derivative::Derivative, TypeFoldable, TypeVisitable)]
1714
#[derivative(Clone(bound = "V: Clone"), Hash(bound = "V: Hash"))]
1815
pub struct Canonical<I: Interner, V> {
1916
pub value: V,
@@ -104,30 +101,6 @@ impl<I: Interner, V: fmt::Debug> fmt::Debug for Canonical<I, V> {
104101

105102
impl<I: Interner, V: Copy> Copy for Canonical<I, V> where I::CanonicalVars: Copy {}
106103

107-
impl<I: Interner, V: TypeFoldable<I>> TypeFoldable<I> for Canonical<I, V>
108-
where
109-
I::CanonicalVars: TypeFoldable<I>,
110-
{
111-
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
112-
Ok(Canonical {
113-
value: noop_traversal_if_boring!({ self.value }.try_fold_with(folder))?,
114-
max_universe: noop_traversal_if_boring!({ self.max_universe }.try_fold_with(folder))?,
115-
variables: noop_traversal_if_boring!({ self.variables }.try_fold_with(folder))?,
116-
})
117-
}
118-
}
119-
120-
impl<I: Interner, V: TypeVisitable<I>> TypeVisitable<I> for Canonical<I, V>
121-
where
122-
I::CanonicalVars: TypeVisitable<I>,
123-
{
124-
fn visit_with<F: TypeVisitor<I>>(&self, visitor: &mut F) -> ControlFlow<F::BreakTy> {
125-
noop_traversal_if_boring!({ &self.value }.visit_with(visitor))?;
126-
noop_traversal_if_boring!({ &self.max_universe }.visit_with(visitor))?;
127-
noop_traversal_if_boring!({ &self.variables }.visit_with(visitor))
128-
}
129-
}
130-
131104
impl<I: Interner, E: TyEncoder<I = I>, V: Encodable<E>> Encodable<E> for Canonical<I, V>
132105
where
133106
I::CanonicalVars: Encodable<E>,

compiler/rustc_type_ir/src/predicate_kind.rs

Lines changed: 9 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
22
use rustc_serialize::Decoder;
33
use rustc_serialize::{Decodable, Encodable};
44
use std::fmt;
5-
use std::ops::ControlFlow;
65

7-
use crate::fold::{FallibleTypeFolder, TypeFoldable};
8-
use crate::visit::{TypeVisitable, TypeVisitor};
9-
use crate::{HashStableContext, Interner, BoringTraversable};
6+
use crate::{HashStableContext, Interner};
107
use crate::{TyDecoder, TyEncoder};
118

129
/// A clause is something that can appear in where bounds or be inferred
1310
/// by implied bounds.
14-
#[derive(derivative::Derivative)]
11+
#[derive(derivative::Derivative, TypeFoldable, TypeVisitable)]
1512
#[derivative(Clone(bound = ""), Hash(bound = ""))]
1613
pub enum ClauseKind<I: Interner> {
1714
/// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be
@@ -108,80 +105,6 @@ where
108105
}
109106
}
110107

111-
impl<I: Interner> TypeFoldable<I> for ClauseKind<I>
112-
where
113-
I::Ty: TypeFoldable<I>,
114-
I::Const: TypeFoldable<I>,
115-
I::GenericArg: TypeFoldable<I>,
116-
I::TraitPredicate: TypeFoldable<I>,
117-
I::ProjectionPredicate: TypeFoldable<I>,
118-
I::TypeOutlivesPredicate: TypeFoldable<I>,
119-
I::RegionOutlivesPredicate: TypeFoldable<I>,
120-
{
121-
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
122-
Ok(match self {
123-
ClauseKind::Trait(p) => {
124-
ClauseKind::Trait(noop_traversal_if_boring!(p.try_fold_with(folder))?)
125-
}
126-
ClauseKind::RegionOutlives(p) => {
127-
ClauseKind::RegionOutlives(noop_traversal_if_boring!(p.try_fold_with(folder))?)
128-
}
129-
ClauseKind::TypeOutlives(p) => {
130-
ClauseKind::TypeOutlives(noop_traversal_if_boring!(p.try_fold_with(folder))?)
131-
}
132-
ClauseKind::Projection(p) => {
133-
ClauseKind::Projection(noop_traversal_if_boring!(p.try_fold_with(folder))?)
134-
}
135-
ClauseKind::ConstArgHasType(c, t) => ClauseKind::ConstArgHasType(
136-
noop_traversal_if_boring!(c.try_fold_with(folder))?,
137-
noop_traversal_if_boring!(t.try_fold_with(folder))?,
138-
),
139-
ClauseKind::WellFormed(p) => {
140-
ClauseKind::WellFormed(noop_traversal_if_boring!(p.try_fold_with(folder))?)
141-
}
142-
ClauseKind::ConstEvaluatable(p) => {
143-
ClauseKind::ConstEvaluatable(noop_traversal_if_boring!(p.try_fold_with(folder))?)
144-
}
145-
})
146-
}
147-
}
148-
149-
impl<I: Interner> TypeVisitable<I> for ClauseKind<I>
150-
where
151-
I::Ty: TypeVisitable<I>,
152-
I::Const: TypeVisitable<I>,
153-
I::GenericArg: TypeVisitable<I>,
154-
I::TraitPredicate: TypeVisitable<I>,
155-
I::ProjectionPredicate: TypeVisitable<I>,
156-
I::TypeOutlivesPredicate: TypeVisitable<I>,
157-
I::RegionOutlivesPredicate: TypeVisitable<I>,
158-
{
159-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
160-
match self {
161-
ClauseKind::Trait(p) => noop_traversal_if_boring!(p.visit_with(visitor)),
162-
ClauseKind::RegionOutlives(p) => {
163-
noop_traversal_if_boring!(p.visit_with(visitor))
164-
}
165-
ClauseKind::TypeOutlives(p) => {
166-
noop_traversal_if_boring!(p.visit_with(visitor))
167-
}
168-
ClauseKind::Projection(p) => {
169-
noop_traversal_if_boring!(p.visit_with(visitor))
170-
}
171-
ClauseKind::ConstArgHasType(c, t) => {
172-
noop_traversal_if_boring!(c.visit_with(visitor))?;
173-
noop_traversal_if_boring!(t.visit_with(visitor))
174-
}
175-
ClauseKind::WellFormed(p) => {
176-
noop_traversal_if_boring!(p.visit_with(visitor))
177-
}
178-
ClauseKind::ConstEvaluatable(p) => {
179-
noop_traversal_if_boring!(p.visit_with(visitor))
180-
}
181-
}
182-
}
183-
}
184-
185108
impl<I: Interner, D: TyDecoder<I = I>> Decodable<D> for ClauseKind<I>
186109
where
187110
I::Ty: Decodable<D>,
@@ -239,19 +162,23 @@ where
239162
}
240163
}
241164

242-
#[derive(derivative::Derivative)]
165+
#[derive(derivative::Derivative, TypeFoldable, TypeVisitable)]
243166
#[derivative(Clone(bound = ""), Hash(bound = ""))]
244167
pub enum PredicateKind<I: Interner> {
245168
/// Prove a clause
246169
Clause(ClauseKind<I>),
247170

248171
/// Trait must be object-safe.
249-
ObjectSafe(I::DefId),
172+
ObjectSafe(#[skip_traversal(because_boring)] I::DefId),
250173

251174
/// No direct syntax. May be thought of as `where T: FnFoo<...>`
252175
/// for some generic args `...` and `T` being a closure type.
253176
/// Satisfied (or refuted) once we know the closure's kind.
254-
ClosureKind(I::DefId, I::GenericArgs, I::ClosureKind),
177+
ClosureKind(
178+
#[skip_traversal(because_boring)] I::DefId,
179+
I::GenericArgs,
180+
#[skip_traversal(because_boring)] I::ClosureKind,
181+
),
255182

256183
/// `T1 <: T2`
257184
///
@@ -368,90 +295,6 @@ where
368295
}
369296
}
370297

371-
impl<I: Interner> TypeFoldable<I> for PredicateKind<I>
372-
where
373-
I::DefId: BoringTraversable,
374-
I::Const: TypeFoldable<I>,
375-
I::GenericArgs: TypeFoldable<I>,
376-
I::Term: TypeFoldable<I>,
377-
I::CoercePredicate: TypeFoldable<I>,
378-
I::SubtypePredicate: TypeFoldable<I>,
379-
I::ClosureKind: BoringTraversable,
380-
ClauseKind<I>: TypeFoldable<I>,
381-
{
382-
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
383-
Ok(match self {
384-
PredicateKind::Clause(c) => {
385-
PredicateKind::Clause(noop_traversal_if_boring!(c.try_fold_with(folder))?)
386-
}
387-
PredicateKind::ObjectSafe(d) => {
388-
PredicateKind::ObjectSafe(noop_traversal_if_boring!(d.try_fold_with(folder))?)
389-
}
390-
PredicateKind::ClosureKind(d, g, k) => PredicateKind::ClosureKind(
391-
noop_traversal_if_boring!(d.try_fold_with(folder))?,
392-
noop_traversal_if_boring!(g.try_fold_with(folder))?,
393-
noop_traversal_if_boring!(k.try_fold_with(folder))?,
394-
),
395-
PredicateKind::Subtype(s) => {
396-
PredicateKind::Subtype(noop_traversal_if_boring!(s.try_fold_with(folder))?)
397-
}
398-
PredicateKind::Coerce(s) => {
399-
PredicateKind::Coerce(noop_traversal_if_boring!(s.try_fold_with(folder))?)
400-
}
401-
PredicateKind::ConstEquate(a, b) => PredicateKind::ConstEquate(
402-
noop_traversal_if_boring!(a.try_fold_with(folder))?,
403-
noop_traversal_if_boring!(b.try_fold_with(folder))?,
404-
),
405-
PredicateKind::Ambiguous => PredicateKind::Ambiguous,
406-
PredicateKind::AliasRelate(a, b, d) => PredicateKind::AliasRelate(
407-
noop_traversal_if_boring!(a.try_fold_with(folder))?,
408-
noop_traversal_if_boring!(b.try_fold_with(folder))?,
409-
noop_traversal_if_boring!(d.try_fold_with(folder))?,
410-
),
411-
})
412-
}
413-
}
414-
415-
impl<I: Interner> TypeVisitable<I> for PredicateKind<I>
416-
where
417-
I::DefId: BoringTraversable,
418-
I::Const: TypeVisitable<I>,
419-
I::GenericArgs: TypeVisitable<I>,
420-
I::Term: TypeVisitable<I>,
421-
I::CoercePredicate: TypeVisitable<I>,
422-
I::SubtypePredicate: TypeVisitable<I>,
423-
I::ClosureKind: BoringTraversable,
424-
ClauseKind<I>: TypeVisitable<I>,
425-
{
426-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
427-
match self {
428-
PredicateKind::Clause(p) => noop_traversal_if_boring!(p.visit_with(visitor)),
429-
PredicateKind::ObjectSafe(d) => {
430-
noop_traversal_if_boring!(d.visit_with(visitor))
431-
}
432-
PredicateKind::ClosureKind(d, g, k) => {
433-
noop_traversal_if_boring!(d.visit_with(visitor))?;
434-
noop_traversal_if_boring!(g.visit_with(visitor))?;
435-
noop_traversal_if_boring!(k.visit_with(visitor))
436-
}
437-
PredicateKind::Subtype(s) => {
438-
noop_traversal_if_boring!(s.visit_with(visitor))
439-
}
440-
PredicateKind::Coerce(s) => noop_traversal_if_boring!(s.visit_with(visitor)),
441-
PredicateKind::ConstEquate(a, b) => {
442-
noop_traversal_if_boring!(a.visit_with(visitor))?;
443-
noop_traversal_if_boring!(b.visit_with(visitor))
444-
}
445-
PredicateKind::Ambiguous => ControlFlow::Continue(()),
446-
PredicateKind::AliasRelate(a, b, d) => {
447-
noop_traversal_if_boring!(a.visit_with(visitor))?;
448-
noop_traversal_if_boring!(b.visit_with(visitor))?;
449-
noop_traversal_if_boring!(d.visit_with(visitor))
450-
}
451-
}
452-
}
453-
}
454-
455298
impl<I: Interner, D: TyDecoder<I = I>> Decodable<D> for PredicateKind<I>
456299
where
457300
I::DefId: Decodable<D>,

0 commit comments

Comments
 (0)