Skip to content

Commit 420e937

Browse files
committed
Auto merge of #629 - jackh726:typename, r=matthewjasper
Remove TypeName and merge into TyKind
2 parents 30f4a12 + fd295d6 commit 420e937

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1328
-1267
lines changed

book/src/clauses/type_equality.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,11 @@ Placeholder associated types are never written directly by the user.
9393
They are used internally by the trait system only, as we will see
9494
shortly.
9595

96-
In rustc, they correspond to the `TyVariableKind::UnnormalizedProjectionTy` enum
96+
In rustc, they correspond to the `TyKind::UnnormalizedProjectionTy` enum
9797
variant, declared in [`compiler/rustc_middle/src/ty/sty.rs`][sty]. In chalk, we use an
98-
`ApplicationTy` with a name living in a special namespace dedicated to
99-
placeholder associated types (see the `TypeName` enum declared in
100-
[`chalk-ir/src/lib.rs`][chalk_type_name]).
98+
`AssociatedType`.
10199

102100
[sty]: https://github.com/rust-lang/rust/blob/master/compiler/rustc_middle/src/ty/sty.rs
103-
[chalk_type_name]: https://github.com/rust-lang-nursery/chalk/blob/master/chalk-ir/src/lib.rs
104101

105102
## Projection equality
106103

book/src/types/rust_types.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
Rust types are represented by the [`Ty`] and [`TyKind`] types.
44
You use [`Ty`] to represent "some Rust type". But to actually inspect
5-
what sort of type you have, you invoke the [`data`] method, which
5+
what sort of type you have, you invoke the [`kind`] method, which
66
returns a [`TyKind`]. As described earlier, the actual in-memory
77
representation of types is controlled by the [`Interner`] trait.
88

99
[`Interner`]: http://rust-lang.github.io/chalk/chalk_ir/interner/trait.Interner.html
1010
[`Ty`]: http://rust-lang.github.io/chalk/chalk_ir/struct.Ty.html
1111
[`TyKind`]: http://rust-lang.github.io/chalk/chalk_ir/enum.TyKind.html
12-
[`data`]: http://rust-lang.github.io/chalk/chalk_ir/struct.Ty.html#method.data
12+
[`kind`]: http://rust-lang.github.io/chalk/chalk_ir/struct.Ty.html#method.kind
1313

1414
## The `TyKind` variants and how they map to Rust syntax
1515

@@ -22,12 +22,13 @@ differences in how they are handled.
2222

2323
| Chalk variant | Example Rust types |
2424
| ------------- | ------------------ |
25-
| `Apply` | `Vec<u32>`, `f32` |
2625
| `Placeholder` | how we represent `T` when type checking `fn foo<T>() { .. }` |
2726
| `Dyn` | `dyn Trait` |
2827
| `Fn` | `fn(&u8)` |
2928
| `Alias` | `<T as Iterator>::Item`, or the `Foo` in `type Foo = impl Trait` and `type Foo = u32` |
3029
| `BoundVariable` | an uninstantiated generic parameter like the `T` in `struct Foo<T>` |
30+
| `Adt` | `struct Foo<T>` |
31+
| ... | ... |
3132

3233
## Justification for each variant
3334

chalk-engine/src/normalize_deep.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ where
107107
mod test {
108108
use super::*;
109109
use chalk_integration::interner::ChalkIr;
110-
use chalk_integration::{arg, ty, ty_name};
110+
use chalk_integration::{arg, ty};
111111

112112
const U0: UniverseIndex = UniverseIndex { counter: 0 };
113113

chalk-engine/src/slg.rs

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,6 @@ impl<I: Interner> MayInvalidate<'_, I> {
405405
);
406406
}
407407

408-
(TyKind::Apply(apply1), TyKind::Apply(apply2)) => {
409-
self.aggregate_application_tys(apply1, apply2)
410-
}
411-
412408
(TyKind::Placeholder(p1), TyKind::Placeholder(p2)) => {
413409
self.aggregate_placeholders(p1, p2)
414410
}
@@ -423,12 +419,52 @@ impl<I: Interner> MayInvalidate<'_, I> {
423419
TyKind::Alias(AliasTy::Opaque(opaque_ty2)),
424420
) => self.aggregate_opaque_ty_tys(opaque_ty1, opaque_ty2),
425421

426-
// For everything else, be conservative here and just say we may invalidate.
427-
(TyKind::Function(_), _)
428-
| (TyKind::Dyn(_), _)
429-
| (TyKind::Apply(_), _)
430-
| (TyKind::Placeholder(_), _)
431-
| (TyKind::Alias(_), _) => true,
422+
(TyKind::Adt(id_a, substitution_a), TyKind::Adt(id_b, substitution_b)) => {
423+
self.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b)
424+
}
425+
(
426+
TyKind::AssociatedType(id_a, substitution_a),
427+
TyKind::AssociatedType(id_b, substitution_b),
428+
) => self.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b),
429+
(TyKind::Scalar(scalar_a), TyKind::Scalar(scalar_b)) => scalar_a != scalar_b,
430+
(TyKind::Str, TyKind::Str) => false,
431+
(TyKind::Tuple(arity_a, substitution_a), TyKind::Tuple(arity_b, substitution_b)) => {
432+
self.aggregate_name_and_substs(arity_a, substitution_a, arity_b, substitution_b)
433+
}
434+
(
435+
TyKind::OpaqueType(id_a, substitution_a),
436+
TyKind::OpaqueType(id_b, substitution_b),
437+
) => self.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b),
438+
(TyKind::Slice(ty_a), TyKind::Slice(ty_b)) => self.aggregate_tys(ty_a, ty_b),
439+
(TyKind::FnDef(id_a, substitution_a), TyKind::FnDef(id_b, substitution_b)) => {
440+
self.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b)
441+
}
442+
(TyKind::Ref(id_a, lifetime_a, ty_a), TyKind::Ref(id_b, lifetime_b, ty_b)) => {
443+
id_a != id_b
444+
|| self.aggregate_lifetimes(lifetime_a, lifetime_b)
445+
|| self.aggregate_tys(ty_a, ty_b)
446+
}
447+
(TyKind::Raw(id_a, ty_a), TyKind::Raw(id_b, ty_b)) => {
448+
id_a != id_b || self.aggregate_tys(ty_a, ty_b)
449+
}
450+
(TyKind::Never, TyKind::Never) => false,
451+
(TyKind::Array(ty_a, const_a), TyKind::Array(ty_b, const_b)) => {
452+
self.aggregate_tys(ty_a, ty_b) || self.aggregate_consts(const_a, const_b)
453+
}
454+
(TyKind::Closure(id_a, substitution_a), TyKind::Closure(id_b, substitution_b)) => {
455+
self.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b)
456+
}
457+
(TyKind::Generator(id_a, substitution_a), TyKind::Generator(id_b, substitution_b)) => {
458+
self.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b)
459+
}
460+
(
461+
TyKind::GeneratorWitness(id_a, substitution_a),
462+
TyKind::GeneratorWitness(id_b, substitution_b),
463+
) => self.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b),
464+
(TyKind::Foreign(id_a), TyKind::Foreign(id_b)) => id_a != id_b,
465+
(TyKind::Error, TyKind::Error) => false,
466+
467+
(_, _) => true,
432468
}
433469
}
434470

@@ -484,28 +520,6 @@ impl<I: Interner> MayInvalidate<'_, I> {
484520
}
485521
}
486522

487-
fn aggregate_application_tys(
488-
&mut self,
489-
new: &ApplicationTy<I>,
490-
current: &ApplicationTy<I>,
491-
) -> bool {
492-
let ApplicationTy {
493-
name: new_name,
494-
substitution: new_substitution,
495-
} = new;
496-
let ApplicationTy {
497-
name: current_name,
498-
substitution: current_substitution,
499-
} = current;
500-
501-
self.aggregate_name_and_substs(
502-
new_name,
503-
new_substitution,
504-
current_name,
505-
current_substitution,
506-
)
507-
}
508-
509523
fn aggregate_placeholders(
510524
&mut self,
511525
new: &PlaceholderIndex,

chalk-engine/src/slg/aggregate.rs

Lines changed: 98 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,6 @@ impl<I: Interner> AntiUnifier<'_, '_, I> {
247247
| (TyKind::Function(_), TyKind::Function(_))
248248
| (TyKind::Dyn(_), TyKind::Dyn(_)) => self.new_ty_variable(),
249249

250-
(TyKind::Apply(apply1), TyKind::Apply(apply2)) => {
251-
self.aggregate_application_tys(apply1, apply2)
252-
}
253-
254250
(
255251
TyKind::Alias(AliasTy::Projection(proj1)),
256252
TyKind::Alias(AliasTy::Projection(proj2)),
@@ -265,37 +261,104 @@ impl<I: Interner> AntiUnifier<'_, '_, I> {
265261
self.aggregate_placeholder_tys(placeholder1, placeholder2)
266262
}
267263

268-
// Mismatched base kinds.
269-
(TyKind::InferenceVar(_, _), _)
270-
| (TyKind::BoundVar(_), _)
271-
| (TyKind::Dyn(_), _)
272-
| (TyKind::Function(_), _)
273-
| (TyKind::Apply(_), _)
274-
| (TyKind::Alias(_), _)
275-
| (TyKind::Placeholder(_), _) => self.new_ty_variable(),
276-
}
277-
}
278-
279-
fn aggregate_application_tys(
280-
&mut self,
281-
apply1: &ApplicationTy<I>,
282-
apply2: &ApplicationTy<I>,
283-
) -> Ty<I> {
284-
let interner = self.interner;
285-
let ApplicationTy {
286-
name: name1,
287-
substitution: substitution1,
288-
} = apply1;
289-
let ApplicationTy {
290-
name: name2,
291-
substitution: substitution2,
292-
} = apply2;
264+
(TyKind::Adt(id_a, substitution_a), TyKind::Adt(id_b, substitution_b)) => self
265+
.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b)
266+
.map(|(&name, substitution)| TyKind::Adt(name, substitution).intern(interner))
267+
.unwrap_or_else(|| self.new_ty_variable()),
268+
(
269+
TyKind::AssociatedType(id_a, substitution_a),
270+
TyKind::AssociatedType(id_b, substitution_b),
271+
) => self
272+
.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b)
273+
.map(|(&name, substitution)| {
274+
TyKind::AssociatedType(name, substitution).intern(interner)
275+
})
276+
.unwrap_or_else(|| self.new_ty_variable()),
277+
(TyKind::Scalar(scalar_a), TyKind::Scalar(scalar_b)) => {
278+
if scalar_a == scalar_b {
279+
TyKind::Scalar(*scalar_a).intern(interner)
280+
} else {
281+
self.new_ty_variable()
282+
}
283+
}
284+
(TyKind::Str, TyKind::Str) => TyKind::Str.intern(interner),
285+
(TyKind::Tuple(arity_a, substitution_a), TyKind::Tuple(arity_b, substitution_b)) => {
286+
self.aggregate_name_and_substs(arity_a, substitution_a, arity_b, substitution_b)
287+
.map(|(&name, substitution)| TyKind::Tuple(name, substitution).intern(interner))
288+
.unwrap_or_else(|| self.new_ty_variable())
289+
}
290+
(
291+
TyKind::OpaqueType(id_a, substitution_a),
292+
TyKind::OpaqueType(id_b, substitution_b),
293+
) => self
294+
.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b)
295+
.map(|(&name, substitution)| {
296+
TyKind::OpaqueType(name, substitution).intern(interner)
297+
})
298+
.unwrap_or_else(|| self.new_ty_variable()),
299+
(TyKind::Slice(ty_a), TyKind::Slice(ty_b)) => {
300+
TyKind::Slice(self.aggregate_tys(ty_a, ty_b)).intern(interner)
301+
}
302+
(TyKind::FnDef(id_a, substitution_a), TyKind::FnDef(id_b, substitution_b)) => self
303+
.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b)
304+
.map(|(&name, substitution)| TyKind::FnDef(name, substitution).intern(interner))
305+
.unwrap_or_else(|| self.new_ty_variable()),
306+
(TyKind::Ref(id_a, lifetime_a, ty_a), TyKind::Ref(id_b, lifetime_b, ty_b)) => {
307+
if id_a == id_b {
308+
TyKind::Ref(
309+
*id_a,
310+
self.aggregate_lifetimes(lifetime_a, lifetime_b),
311+
self.aggregate_tys(ty_a, ty_b),
312+
)
313+
.intern(interner)
314+
} else {
315+
self.new_ty_variable()
316+
}
317+
}
318+
(TyKind::Raw(id_a, ty_a), TyKind::Raw(id_b, ty_b)) => {
319+
if id_a == id_b {
320+
TyKind::Raw(*id_a, self.aggregate_tys(ty_a, ty_b)).intern(interner)
321+
} else {
322+
self.new_ty_variable()
323+
}
324+
}
325+
(TyKind::Never, TyKind::Never) => TyKind::Never.intern(interner),
326+
(TyKind::Array(ty_a, const_a), TyKind::Array(ty_b, const_b)) => TyKind::Array(
327+
self.aggregate_tys(ty_a, ty_b),
328+
self.aggregate_consts(const_a, const_b),
329+
)
330+
.intern(interner),
331+
(TyKind::Closure(id_a, substitution_a), TyKind::Closure(id_b, substitution_b)) => self
332+
.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b)
333+
.map(|(&name, substitution)| TyKind::Closure(name, substitution).intern(interner))
334+
.unwrap_or_else(|| self.new_ty_variable()),
335+
(TyKind::Generator(id_a, substitution_a), TyKind::Generator(id_b, substitution_b)) => {
336+
self.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b)
337+
.map(|(&name, substitution)| {
338+
TyKind::Generator(name, substitution).intern(interner)
339+
})
340+
.unwrap_or_else(|| self.new_ty_variable())
341+
}
342+
(
343+
TyKind::GeneratorWitness(id_a, substitution_a),
344+
TyKind::GeneratorWitness(id_b, substitution_b),
345+
) => self
346+
.aggregate_name_and_substs(id_a, substitution_a, id_b, substitution_b)
347+
.map(|(&name, substitution)| {
348+
TyKind::GeneratorWitness(name, substitution).intern(interner)
349+
})
350+
.unwrap_or_else(|| self.new_ty_variable()),
351+
(TyKind::Foreign(id_a), TyKind::Foreign(id_b)) => {
352+
if id_a == id_b {
353+
TyKind::Foreign(*id_a).intern(interner)
354+
} else {
355+
self.new_ty_variable()
356+
}
357+
}
358+
(TyKind::Error, TyKind::Error) => TyKind::Error.intern(interner),
293359

294-
self.aggregate_name_and_substs(name1, substitution1, name2, substitution2)
295-
.map(|(&name, substitution)| {
296-
TyKind::Apply(ApplicationTy { name, substitution }).intern(interner)
297-
})
298-
.unwrap_or_else(|| self.new_ty_variable())
360+
(_, _) => self.new_ty_variable(),
361+
}
299362
}
300363

301364
fn aggregate_placeholder_tys(
@@ -501,7 +564,7 @@ impl<I: Interner> AntiUnifier<'_, '_, I> {
501564
#[cfg(test)]
502565
mod test {
503566
use crate::slg::aggregate::AntiUnifier;
504-
use chalk_integration::{arg, ty, ty_name};
567+
use chalk_integration::{arg, ty};
505568
use chalk_ir::UniverseIndex;
506569
use chalk_solve::infer::InferenceTable;
507570

0 commit comments

Comments
 (0)