Skip to content

Commit d5797e9

Browse files
Make it possible to derive Lift/TypeVisitable/TypeFoldable in rustc_type_ir
1 parent 78a7751 commit d5797e9

File tree

10 files changed

+218
-331
lines changed

10 files changed

+218
-331
lines changed

Diff for: Cargo.lock

+11
Original file line numberDiff line numberDiff line change
@@ -4837,9 +4837,20 @@ dependencies = [
48374837
"rustc_macros",
48384838
"rustc_serialize",
48394839
"rustc_span",
4840+
"rustc_type_ir_macros",
48404841
"smallvec",
48414842
]
48424843

4844+
[[package]]
4845+
name = "rustc_type_ir_macros"
4846+
version = "0.0.0"
4847+
dependencies = [
4848+
"proc-macro2",
4849+
"quote",
4850+
"syn 2.0.62",
4851+
"synstructure",
4852+
]
4853+
48434854
[[package]]
48444855
name = "rustc_version"
48454856
version = "0.4.0"

Diff for: compiler/rustc_type_ir/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ rustc_ast_ir = { path = "../rustc_ast_ir" }
1111
rustc_data_structures = { path = "../rustc_data_structures", optional = true }
1212
rustc_index = { path = "../rustc_index", default-features = false }
1313
rustc_macros = { path = "../rustc_macros", optional = true }
14+
rustc_type_ir_macros = { path = "../rustc_type_ir_macros" }
1415
rustc_serialize = { path = "../rustc_serialize", optional = true }
1516
rustc_span = { path = "../rustc_span", optional = true }
1617
smallvec = { version = "1.8.1" }

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

+21-130
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
use rustc_ast_ir::try_visit;
2-
use rustc_ast_ir::visit::VisitorResult;
31
#[cfg(feature = "nightly")]
42
use rustc_macros::{HashStable_NoContext, TyDecodable, TyEncodable};
3+
use rustc_type_ir_macros::{TypeFoldable_Generic, TypeVisitable_Generic};
54
use std::fmt;
65
use std::hash::Hash;
76

8-
use crate::fold::{FallibleTypeFolder, TypeFoldable};
97
use crate::inherent::*;
10-
use crate::visit::{TypeVisitable, TypeVisitor};
118
use crate::{Interner, 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.
1613
#[derive(derivative::Derivative)]
17-
#[derivative(Clone(bound = "V: Clone"), Hash(bound = "V: Hash"))]
14+
#[derivative(
15+
Clone(bound = "V: Clone"),
16+
Hash(bound = "V: Hash"),
17+
PartialEq(bound = "V: PartialEq"),
18+
Eq(bound = "V: Eq"),
19+
Debug(bound = "V: fmt::Debug"),
20+
Copy(bound = "V: Copy")
21+
)]
22+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
1823
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable, HashStable_NoContext))]
1924
pub struct Canonical<I: Interner, V> {
2025
pub value: V,
@@ -64,18 +69,6 @@ impl<I: Interner, V> Canonical<I, V> {
6469
}
6570
}
6671

67-
impl<I: Interner, V: Eq> Eq for Canonical<I, V> {}
68-
69-
impl<I: Interner, V: PartialEq> PartialEq for Canonical<I, V> {
70-
fn eq(&self, other: &Self) -> bool {
71-
let Self { value, max_universe, variables, defining_opaque_types } = self;
72-
*value == other.value
73-
&& *max_universe == other.max_universe
74-
&& *variables == other.variables
75-
&& *defining_opaque_types == other.defining_opaque_types
76-
}
77-
}
78-
7972
impl<I: Interner, V: fmt::Display> fmt::Display for Canonical<I, V> {
8073
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8174
let Self { value, max_universe, variables, defining_opaque_types } = self;
@@ -86,84 +79,25 @@ impl<I: Interner, V: fmt::Display> fmt::Display for Canonical<I, V> {
8679
}
8780
}
8881

89-
impl<I: Interner, V: fmt::Debug> fmt::Debug for Canonical<I, V> {
90-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91-
let Self { value, max_universe, variables, defining_opaque_types } = self;
92-
f.debug_struct("Canonical")
93-
.field("value", &value)
94-
.field("max_universe", &max_universe)
95-
.field("variables", &variables)
96-
.field("defining_opaque_types", &defining_opaque_types)
97-
.finish()
98-
}
99-
}
100-
101-
impl<I: Interner, V: Copy> Copy for Canonical<I, V> where I::CanonicalVars: Copy {}
102-
103-
impl<I: Interner, V: TypeFoldable<I>> TypeFoldable<I> for Canonical<I, V>
104-
where
105-
I::CanonicalVars: TypeFoldable<I>,
106-
{
107-
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
108-
Ok(Canonical {
109-
value: self.value.try_fold_with(folder)?,
110-
max_universe: self.max_universe.try_fold_with(folder)?,
111-
variables: self.variables.try_fold_with(folder)?,
112-
defining_opaque_types: self.defining_opaque_types,
113-
})
114-
}
115-
}
116-
117-
impl<I: Interner, V: TypeVisitable<I>> TypeVisitable<I> for Canonical<I, V>
118-
where
119-
I::CanonicalVars: TypeVisitable<I>,
120-
{
121-
fn visit_with<F: TypeVisitor<I>>(&self, folder: &mut F) -> F::Result {
122-
let Self { value, max_universe, variables, defining_opaque_types } = self;
123-
try_visit!(value.visit_with(folder));
124-
try_visit!(max_universe.visit_with(folder));
125-
try_visit!(defining_opaque_types.visit_with(folder));
126-
variables.visit_with(folder)
127-
}
128-
}
129-
13082
/// Information about a canonical variable that is included with the
13183
/// canonical value. This is sufficient information for code to create
13284
/// a copy of the canonical value in some other inference context,
13385
/// with fresh inference variables replacing the canonical values.
13486
#[derive(derivative::Derivative)]
135-
#[derivative(Clone(bound = ""), Copy(bound = ""), Hash(bound = ""), Debug(bound = ""))]
87+
#[derivative(
88+
Clone(bound = ""),
89+
Copy(bound = ""),
90+
Hash(bound = ""),
91+
Debug(bound = ""),
92+
Eq(bound = ""),
93+
PartialEq(bound = "")
94+
)]
95+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
13696
#[cfg_attr(feature = "nightly", derive(TyDecodable, TyEncodable, HashStable_NoContext))]
13797
pub struct CanonicalVarInfo<I: Interner> {
13898
pub kind: CanonicalVarKind<I>,
13999
}
140100

141-
impl<I: Interner> PartialEq for CanonicalVarInfo<I> {
142-
fn eq(&self, other: &Self) -> bool {
143-
self.kind == other.kind
144-
}
145-
}
146-
147-
impl<I: Interner> Eq for CanonicalVarInfo<I> {}
148-
149-
impl<I: Interner> TypeVisitable<I> for CanonicalVarInfo<I>
150-
where
151-
I::Ty: TypeVisitable<I>,
152-
{
153-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> V::Result {
154-
self.kind.visit_with(visitor)
155-
}
156-
}
157-
158-
impl<I: Interner> TypeFoldable<I> for CanonicalVarInfo<I>
159-
where
160-
I::Ty: TypeFoldable<I>,
161-
{
162-
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
163-
Ok(CanonicalVarInfo { kind: self.kind.try_fold_with(folder)? })
164-
}
165-
}
166-
167101
impl<I: Interner> CanonicalVarInfo<I> {
168102
pub fn universe(self) -> UniverseIndex {
169103
self.kind.universe()
@@ -216,6 +150,7 @@ impl<I: Interner> CanonicalVarInfo<I> {
216150
/// that analyzes type-like values.
217151
#[derive(derivative::Derivative)]
218152
#[derivative(Clone(bound = ""), Copy(bound = ""), Hash(bound = ""), Debug(bound = ""))]
153+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
219154
#[cfg_attr(feature = "nightly", derive(TyDecodable, TyEncodable, HashStable_NoContext))]
220155
pub enum CanonicalVarKind<I: Interner> {
221156
/// Some kind of type inference variable.
@@ -258,51 +193,6 @@ impl<I: Interner> PartialEq for CanonicalVarKind<I> {
258193
}
259194
}
260195

261-
impl<I: Interner> Eq for CanonicalVarKind<I> {}
262-
263-
impl<I: Interner> TypeVisitable<I> for CanonicalVarKind<I>
264-
where
265-
I::Ty: TypeVisitable<I>,
266-
{
267-
fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> V::Result {
268-
match self {
269-
CanonicalVarKind::Ty(_)
270-
| CanonicalVarKind::PlaceholderTy(_)
271-
| CanonicalVarKind::Region(_)
272-
| CanonicalVarKind::PlaceholderRegion(_)
273-
| CanonicalVarKind::Effect => V::Result::output(),
274-
CanonicalVarKind::Const(_, ty) | CanonicalVarKind::PlaceholderConst(_, ty) => {
275-
ty.visit_with(visitor)
276-
}
277-
}
278-
}
279-
}
280-
281-
impl<I: Interner> TypeFoldable<I> for CanonicalVarKind<I>
282-
where
283-
I::Ty: TypeFoldable<I>,
284-
{
285-
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
286-
Ok(match self {
287-
CanonicalVarKind::Ty(kind) => CanonicalVarKind::Ty(kind),
288-
CanonicalVarKind::Region(kind) => CanonicalVarKind::Region(kind),
289-
CanonicalVarKind::Const(kind, ty) => {
290-
CanonicalVarKind::Const(kind, ty.try_fold_with(folder)?)
291-
}
292-
CanonicalVarKind::PlaceholderTy(placeholder) => {
293-
CanonicalVarKind::PlaceholderTy(placeholder)
294-
}
295-
CanonicalVarKind::PlaceholderRegion(placeholder) => {
296-
CanonicalVarKind::PlaceholderRegion(placeholder)
297-
}
298-
CanonicalVarKind::PlaceholderConst(placeholder, ty) => {
299-
CanonicalVarKind::PlaceholderConst(placeholder, ty.try_fold_with(folder)?)
300-
}
301-
CanonicalVarKind::Effect => CanonicalVarKind::Effect,
302-
})
303-
}
304-
}
305-
306196
impl<I: Interner> CanonicalVarKind<I> {
307197
pub fn universe(self) -> UniverseIndex {
308198
match self {
@@ -355,6 +245,7 @@ impl<I: Interner> CanonicalVarKind<I> {
355245
/// usize or f32). In order to faithfully reproduce a type, we need to
356246
/// know what set of types a given type variable can be unified with.
357247
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
248+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
358249
#[cfg_attr(feature = "nightly", derive(TyDecodable, TyEncodable, HashStable_NoContext))]
359250
pub enum CanonicalTyVarKind {
360251
/// General type variable `?T` that can be unified with arbitrary types.

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use self::ConstKind::*;
1010

1111
/// Represents a constant in Rust.
1212
#[derive(derivative::Derivative)]
13-
#[derivative(Clone(bound = ""), Copy(bound = ""), Hash(bound = ""))]
13+
#[derivative(Clone(bound = ""), Copy(bound = ""), Hash(bound = ""), Eq(bound = ""))]
1414
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable, HashStable_NoContext))]
1515
pub enum ConstKind<I: Interner> {
1616
/// A const generic parameter.
@@ -58,8 +58,6 @@ impl<I: Interner> PartialEq for ConstKind<I> {
5858
}
5959
}
6060

61-
impl<I: Interner> Eq for ConstKind<I> {}
62-
6361
impl<I: Interner> fmt::Debug for ConstKind<I> {
6462
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6563
WithInfcx::with_no_infcx(self).fmt(f)

0 commit comments

Comments
 (0)