Skip to content

Commit 699c6e4

Browse files
committed
Derive traversable impls for more type lib kinds
1 parent 51b5454 commit 699c6e4

File tree

3 files changed

+3
-159
lines changed

3 files changed

+3
-159
lines changed

compiler/rustc_type_ir/src/canonical.rs

+1-28
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
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

7-
use crate::fold::{FallibleTypeFolder, TypeFoldable};
8-
use crate::visit::{TypeVisitable, TypeVisitor};
96
use crate::{HashStableContext, Interner, UniverseIndex};
107

118
/// A "canonicalized" type `V` is one where all free inference
129
/// variables have been rewritten to "canonical vars". These are
1310
/// numbered starting from 0 in order of first appearance.
14-
#[derive(derivative::Derivative)]
11+
#[derive(derivative::Derivative, TypeFoldable, TypeVisitable)]
1512
#[derivative(Clone(bound = "V: Clone"), Hash(bound = "V: Hash"))]
1613
#[derive(TyEncodable, TyDecodable)]
1714
pub struct Canonical<I: Interner, V> {
@@ -102,27 +99,3 @@ impl<I: Interner, V: fmt::Debug> fmt::Debug for Canonical<I, V> {
10299
}
103100

104101
impl<I: Interner, V: Copy> Copy for Canonical<I, V> where I::CanonicalVars: Copy {}
105-
106-
impl<I: Interner, V: TypeFoldable<I>> TypeFoldable<I> for Canonical<I, V>
107-
where
108-
I::CanonicalVars: TypeFoldable<I>,
109-
{
110-
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
111-
Ok(Canonical {
112-
value: self.value.try_fold_with(folder)?,
113-
max_universe: self.max_universe.try_fold_with(folder)?,
114-
variables: self.variables.try_fold_with(folder)?,
115-
})
116-
}
117-
}
118-
119-
impl<I: Interner, V: TypeVisitable<I>> TypeVisitable<I> for Canonical<I, V>
120-
where
121-
I::CanonicalVars: TypeVisitable<I>,
122-
{
123-
fn visit_with<F: TypeVisitor<I>>(&self, folder: &mut F) -> ControlFlow<F::BreakTy> {
124-
self.value.visit_with(folder)?;
125-
self.max_universe.visit_with(folder)?;
126-
self.variables.visit_with(folder)
127-
}
128-
}

compiler/rustc_type_ir/src/macros.rs

-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ macro_rules! TrivialTypeTraversalImpls {
4242

4343
TrivialTypeTraversalImpls! {
4444
(),
45-
crate::AliasRelationDirection,
46-
crate::UniverseIndex,
4745
}
4846

4947
#[macro_export]

compiler/rustc_type_ir/src/predicate_kind.rs

+2-129
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
22
use std::fmt;
3-
use std::ops::ControlFlow;
43

5-
use crate::fold::{FallibleTypeFolder, TypeFoldable};
6-
use crate::visit::{TypeVisitable, TypeVisitor};
74
use crate::{HashStableContext, Interner};
85

96
/// A clause is something that can appear in where bounds or be inferred
107
/// by implied bounds.
11-
#[derive(derivative::Derivative)]
8+
#[derive(derivative::Derivative, TypeFoldable, TypeVisitable)]
129
#[derivative(Clone(bound = ""), Hash(bound = ""))]
1310
#[derive(TyEncodable, TyDecodable)]
1411
pub enum ClauseKind<I: Interner> {
@@ -106,60 +103,7 @@ where
106103
}
107104
}
108105

109-
impl<I: Interner> TypeFoldable<I> for ClauseKind<I>
110-
where
111-
I::Ty: TypeFoldable<I>,
112-
I::Const: TypeFoldable<I>,
113-
I::GenericArg: TypeFoldable<I>,
114-
I::TraitPredicate: TypeFoldable<I>,
115-
I::ProjectionPredicate: TypeFoldable<I>,
116-
I::TypeOutlivesPredicate: TypeFoldable<I>,
117-
I::RegionOutlivesPredicate: TypeFoldable<I>,
118-
{
119-
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
120-
Ok(match self {
121-
ClauseKind::Trait(p) => ClauseKind::Trait(p.try_fold_with(folder)?),
122-
ClauseKind::RegionOutlives(p) => ClauseKind::RegionOutlives(p.try_fold_with(folder)?),
123-
ClauseKind::TypeOutlives(p) => ClauseKind::TypeOutlives(p.try_fold_with(folder)?),
124-
ClauseKind::Projection(p) => ClauseKind::Projection(p.try_fold_with(folder)?),
125-
ClauseKind::ConstArgHasType(c, t) => {
126-
ClauseKind::ConstArgHasType(c.try_fold_with(folder)?, t.try_fold_with(folder)?)
127-
}
128-
ClauseKind::WellFormed(p) => ClauseKind::WellFormed(p.try_fold_with(folder)?),
129-
ClauseKind::ConstEvaluatable(p) => {
130-
ClauseKind::ConstEvaluatable(p.try_fold_with(folder)?)
131-
}
132-
})
133-
}
134-
}
135-
136-
impl<I: Interner> TypeVisitable<I> for ClauseKind<I>
137-
where
138-
I::Ty: TypeVisitable<I>,
139-
I::Const: TypeVisitable<I>,
140-
I::GenericArg: TypeVisitable<I>,
141-
I::TraitPredicate: TypeVisitable<I>,
142-
I::ProjectionPredicate: TypeVisitable<I>,
143-
I::TypeOutlivesPredicate: TypeVisitable<I>,
144-
I::RegionOutlivesPredicate: TypeVisitable<I>,
145-
{
146-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
147-
match self {
148-
ClauseKind::Trait(p) => p.visit_with(visitor),
149-
ClauseKind::RegionOutlives(p) => p.visit_with(visitor),
150-
ClauseKind::TypeOutlives(p) => p.visit_with(visitor),
151-
ClauseKind::Projection(p) => p.visit_with(visitor),
152-
ClauseKind::ConstArgHasType(c, t) => {
153-
c.visit_with(visitor)?;
154-
t.visit_with(visitor)
155-
}
156-
ClauseKind::WellFormed(p) => p.visit_with(visitor),
157-
ClauseKind::ConstEvaluatable(p) => p.visit_with(visitor),
158-
}
159-
}
160-
}
161-
162-
#[derive(derivative::Derivative)]
106+
#[derive(derivative::Derivative, TypeFoldable, TypeVisitable)]
163107
#[derivative(Clone(bound = ""), Hash(bound = ""))]
164108
#[derive(TyEncodable, TyDecodable)]
165109
pub enum PredicateKind<I: Interner> {
@@ -289,77 +233,6 @@ where
289233
}
290234
}
291235

292-
impl<I: Interner> TypeFoldable<I> for PredicateKind<I>
293-
where
294-
I::DefId: TypeFoldable<I>,
295-
I::Const: TypeFoldable<I>,
296-
I::GenericArgs: TypeFoldable<I>,
297-
I::Term: TypeFoldable<I>,
298-
I::CoercePredicate: TypeFoldable<I>,
299-
I::SubtypePredicate: TypeFoldable<I>,
300-
I::ClosureKind: TypeFoldable<I>,
301-
ClauseKind<I>: TypeFoldable<I>,
302-
{
303-
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
304-
Ok(match self {
305-
PredicateKind::Clause(c) => PredicateKind::Clause(c.try_fold_with(folder)?),
306-
PredicateKind::ObjectSafe(d) => PredicateKind::ObjectSafe(d.try_fold_with(folder)?),
307-
PredicateKind::ClosureKind(d, g, k) => PredicateKind::ClosureKind(
308-
d.try_fold_with(folder)?,
309-
g.try_fold_with(folder)?,
310-
k.try_fold_with(folder)?,
311-
),
312-
PredicateKind::Subtype(s) => PredicateKind::Subtype(s.try_fold_with(folder)?),
313-
PredicateKind::Coerce(s) => PredicateKind::Coerce(s.try_fold_with(folder)?),
314-
PredicateKind::ConstEquate(a, b) => {
315-
PredicateKind::ConstEquate(a.try_fold_with(folder)?, b.try_fold_with(folder)?)
316-
}
317-
PredicateKind::Ambiguous => PredicateKind::Ambiguous,
318-
PredicateKind::AliasRelate(a, b, d) => PredicateKind::AliasRelate(
319-
a.try_fold_with(folder)?,
320-
b.try_fold_with(folder)?,
321-
d.try_fold_with(folder)?,
322-
),
323-
})
324-
}
325-
}
326-
327-
impl<I: Interner> TypeVisitable<I> for PredicateKind<I>
328-
where
329-
I::DefId: TypeVisitable<I>,
330-
I::Const: TypeVisitable<I>,
331-
I::GenericArgs: TypeVisitable<I>,
332-
I::Term: TypeVisitable<I>,
333-
I::CoercePredicate: TypeVisitable<I>,
334-
I::SubtypePredicate: TypeVisitable<I>,
335-
I::ClosureKind: TypeVisitable<I>,
336-
ClauseKind<I>: TypeVisitable<I>,
337-
{
338-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
339-
match self {
340-
PredicateKind::Clause(p) => p.visit_with(visitor),
341-
PredicateKind::ObjectSafe(d) => d.visit_with(visitor),
342-
PredicateKind::ClosureKind(d, g, k) => {
343-
d.visit_with(visitor)?;
344-
g.visit_with(visitor)?;
345-
k.visit_with(visitor)
346-
}
347-
PredicateKind::Subtype(s) => s.visit_with(visitor),
348-
PredicateKind::Coerce(s) => s.visit_with(visitor),
349-
PredicateKind::ConstEquate(a, b) => {
350-
a.visit_with(visitor)?;
351-
b.visit_with(visitor)
352-
}
353-
PredicateKind::Ambiguous => ControlFlow::Continue(()),
354-
PredicateKind::AliasRelate(a, b, d) => {
355-
a.visit_with(visitor)?;
356-
b.visit_with(visitor)?;
357-
d.visit_with(visitor)
358-
}
359-
}
360-
}
361-
}
362-
363236
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
364237
#[derive(HashStable_Generic, Encodable, Decodable)]
365238
pub enum AliasRelationDirection {

0 commit comments

Comments
 (0)