Skip to content

Commit 72ad5cb

Browse files
bors[bot]flodieboldkjeremy
authored
Merge #8419 #8423
8419: Move hir_ty to Chalk IR r=flodiebold a=flodiebold Closes #8313. There's some further cleanups to do: - we're still using our `TypeWalk` in lots of places (not for mutating/folding though, just for walking) - we're still using our own canonicalization and unification and our `InferenceTable` - ~`ToChalk` still exists and gets called, it's just the identity in most cases now (I'll probably clean those up before merging this)~ 8423: Bump lsp-types and syn r=kjeremy a=kjeremy This lsp-types now supports a default InsertTextMode for completion and a per-completion item commit_characters Co-authored-by: Florian Diebold <flodiebold@gmail.com> Co-authored-by: kjeremy <kjeremy@gmail.com>
3 parents 5810299 + d992736 + 3634b21 commit 72ad5cb

File tree

19 files changed

+344
-1550
lines changed

19 files changed

+344
-1550
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,9 @@ impl Type {
19031903
| TyKind::Dyn(_)
19041904
| TyKind::Function(_)
19051905
| TyKind::Alias(_)
1906-
| TyKind::Foreign(_) => false,
1906+
| TyKind::Foreign(_)
1907+
| TyKind::Generator(..)
1908+
| TyKind::GeneratorWitness(..) => false,
19071909
}
19081910
}
19091911
}

crates/hir_ty/src/builder.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::iter;
44

55
use chalk_ir::{
66
cast::{Cast, CastTo, Caster},
7+
fold::Fold,
78
interner::HasInterner,
89
AdtId, BoundVar, DebruijnIndex, Safety, Scalar,
910
};
@@ -13,7 +14,7 @@ use smallvec::SmallVec;
1314
use crate::{
1415
db::HirDatabase, primitive, to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders,
1516
CallableSig, FnPointer, FnSig, FnSubst, GenericArg, Interner, ProjectionTy, Substitution,
16-
TraitRef, Ty, TyDefId, TyExt, TyKind, TypeWalk, ValueTyDefId,
17+
TraitRef, Ty, TyDefId, TyExt, TyKind, ValueTyDefId,
1718
};
1819

1920
/// This is a builder for `Ty` or anything that needs a `Substitution`.
@@ -32,8 +33,7 @@ impl<D> TyBuilder<D> {
3233

3334
fn build_internal(self) -> (D, Substitution) {
3435
assert_eq!(self.vec.len(), self.param_count);
35-
// FIXME: would be good to have a way to construct a chalk_ir::Substitution from the interned form
36-
let subst = Substitution::intern(self.vec);
36+
let subst = Substitution::from_iter(&Interner, self.vec);
3737
(self.data, subst)
3838
}
3939

@@ -141,7 +141,7 @@ impl TyBuilder<hir_def::AdtId> {
141141
self.vec.push(fallback().cast(&Interner));
142142
} else {
143143
// each default can depend on the previous parameters
144-
let subst_so_far = Substitution::intern(self.vec.clone());
144+
let subst_so_far = Substitution::from_iter(&Interner, self.vec.clone());
145145
self.vec
146146
.push(default_ty.clone().substitute(&Interner, &subst_so_far).cast(&Interner));
147147
}
@@ -196,13 +196,13 @@ impl TyBuilder<TypeAliasId> {
196196
}
197197
}
198198

199-
impl<T: TypeWalk + HasInterner<Interner = Interner>> TyBuilder<Binders<T>> {
199+
impl<T: HasInterner<Interner = Interner> + Fold<Interner>> TyBuilder<Binders<T>> {
200200
fn subst_binders(b: Binders<T>) -> Self {
201201
let param_count = b.binders.len(&Interner);
202202
TyBuilder::new(b, param_count)
203203
}
204204

205-
pub fn build(self) -> T {
205+
pub fn build(self) -> <T as Fold<Interner>>::Result {
206206
let (b, subst) = self.build_internal();
207207
b.substitute(&Interner, &subst)
208208
}

crates/hir_ty/src/chalk_cast.rs

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
//! Implementations of the Chalk `Cast` trait for our types.
22
3-
use chalk_ir::{
4-
cast::{Cast, CastTo},
5-
interner::HasInterner,
6-
};
3+
use chalk_ir::interner::HasInterner;
74

8-
use crate::{AliasEq, DomainGoal, GenericArg, GenericArgData, Interner, TraitRef, Ty, WhereClause};
5+
use crate::{CallableSig, ReturnTypeImplTraits};
96

107
macro_rules! has_interner {
118
($t:ty) => {
@@ -15,59 +12,5 @@ macro_rules! has_interner {
1512
};
1613
}
1714

18-
has_interner!(WhereClause);
19-
has_interner!(DomainGoal);
20-
has_interner!(GenericArg);
21-
has_interner!(Ty);
22-
23-
impl CastTo<WhereClause> for TraitRef {
24-
fn cast_to(self, _interner: &Interner) -> WhereClause {
25-
WhereClause::Implemented(self)
26-
}
27-
}
28-
29-
impl CastTo<WhereClause> for AliasEq {
30-
fn cast_to(self, _interner: &Interner) -> WhereClause {
31-
WhereClause::AliasEq(self)
32-
}
33-
}
34-
35-
impl CastTo<DomainGoal> for WhereClause {
36-
fn cast_to(self, _interner: &Interner) -> DomainGoal {
37-
DomainGoal::Holds(self)
38-
}
39-
}
40-
41-
impl CastTo<GenericArg> for Ty {
42-
fn cast_to(self, interner: &Interner) -> GenericArg {
43-
GenericArg::new(interner, GenericArgData::Ty(self))
44-
}
45-
}
46-
47-
macro_rules! transitive_impl {
48-
($a:ty, $b:ty, $c:ty) => {
49-
impl CastTo<$c> for $a {
50-
fn cast_to(self, interner: &Interner) -> $c {
51-
self.cast::<$b>(interner).cast(interner)
52-
}
53-
}
54-
};
55-
}
56-
57-
// In Chalk, these can be done as blanket impls, but that doesn't work here
58-
// because of coherence
59-
60-
transitive_impl!(TraitRef, WhereClause, DomainGoal);
61-
transitive_impl!(AliasEq, WhereClause, DomainGoal);
62-
63-
macro_rules! reflexive_impl {
64-
($a:ty) => {
65-
impl CastTo<$a> for $a {
66-
fn cast_to(self, _interner: &Interner) -> $a {
67-
self
68-
}
69-
}
70-
};
71-
}
72-
73-
reflexive_impl!(GenericArg);
15+
has_interner!(CallableSig);
16+
has_interner!(ReturnTypeImplTraits);

crates/hir_ty/src/display.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ impl HirDisplay for GenericArg {
287287
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
288288
match self.interned() {
289289
crate::GenericArgData::Ty(ty) => ty.hir_fmt(f),
290+
crate::GenericArgData::Lifetime(lt) => lt.hir_fmt(f),
291+
crate::GenericArgData::Const(c) => c.hir_fmt(f),
290292
}
291293
}
292294
}
@@ -664,6 +666,8 @@ impl HirDisplay for Ty {
664666
write!(f, "{{unknown}}")?;
665667
}
666668
TyKind::InferenceVar(..) => write!(f, "_")?,
669+
TyKind::Generator(..) => write!(f, "{{generator}}")?,
670+
TyKind::GeneratorWitness(..) => write!(f, "{{generator witness}}")?,
667671
}
668672
Ok(())
669673
}
@@ -741,7 +745,7 @@ fn write_bounds_like_dyn_trait(
741745
if !first {
742746
write!(f, " + ")?;
743747
}
744-
// We assume that the self type is $0 (i.e. the
748+
// We assume that the self type is ^0.0 (i.e. the
745749
// existential) here, which is the only thing that's
746750
// possible in actual Rust, and hence don't print it
747751
write!(f, "{}", f.db.trait_data(trait_).name)?;
@@ -783,6 +787,10 @@ fn write_bounds_like_dyn_trait(
783787
}
784788
ty.hir_fmt(f)?;
785789
}
790+
791+
// FIXME implement these
792+
WhereClause::LifetimeOutlives(_) => {}
793+
WhereClause::TypeOutlives(_) => {}
786794
}
787795
first = false;
788796
}
@@ -837,6 +845,10 @@ impl HirDisplay for WhereClause {
837845
ty.hir_fmt(f)?;
838846
}
839847
WhereClause::AliasEq(_) => write!(f, "{{error}}")?,
848+
849+
// FIXME implement these
850+
WhereClause::TypeOutlives(..) => {}
851+
WhereClause::LifetimeOutlives(..) => {}
840852
}
841853
Ok(())
842854
}
@@ -881,9 +893,11 @@ impl HirDisplay for DomainGoal {
881893
DomainGoal::Holds(wc) => {
882894
write!(f, "Holds(")?;
883895
wc.hir_fmt(f)?;
884-
write!(f, ")")
896+
write!(f, ")")?;
885897
}
898+
_ => write!(f, "?")?,
886899
}
900+
Ok(())
887901
}
888902
}
889903

crates/hir_ty/src/infer.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::mem;
1818
use std::ops::Index;
1919
use std::sync::Arc;
2020

21-
use chalk_ir::{cast::Cast, Mutability};
21+
use chalk_ir::{cast::Cast, DebruijnIndex, Mutability};
2222
use hir_def::{
2323
body::Body,
2424
data::{ConstData, FunctionData, StaticData},
@@ -38,11 +38,11 @@ use syntax::SmolStr;
3838

3939
use super::{
4040
DomainGoal, Guidance, InEnvironment, ProjectionTy, Solution, TraitEnvironment, TraitRef, Ty,
41-
TypeWalk,
4241
};
4342
use crate::{
44-
db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode,
45-
to_assoc_type_id, AliasEq, AliasTy, Canonical, Interner, TyBuilder, TyExt, TyKind,
43+
db::HirDatabase, fold_tys, infer::diagnostics::InferenceDiagnostic,
44+
lower::ImplTraitLoweringMode, to_assoc_type_id, AliasEq, AliasTy, Canonical, Interner,
45+
TyBuilder, TyExt, TyKind,
4646
};
4747

4848
// This lint has a false positive here. See the link below for details.
@@ -323,7 +323,7 @@ impl<'a> InferenceContext<'a> {
323323
}
324324

325325
fn insert_type_vars(&mut self, ty: Ty) -> Ty {
326-
ty.fold(&mut |ty| self.insert_type_vars_shallow(ty))
326+
fold_tys(ty, |ty, _| self.insert_type_vars_shallow(ty), DebruijnIndex::INNERMOST)
327327
}
328328

329329
fn resolve_obligations_as_possible(&mut self) {
@@ -434,12 +434,16 @@ impl<'a> InferenceContext<'a> {
434434
/// to do it as well.
435435
fn normalize_associated_types_in(&mut self, ty: Ty) -> Ty {
436436
let ty = self.resolve_ty_as_possible(ty);
437-
ty.fold(&mut |ty| match ty.kind(&Interner) {
438-
TyKind::Alias(AliasTy::Projection(proj_ty)) => {
439-
self.normalize_projection_ty(proj_ty.clone())
440-
}
441-
_ => ty,
442-
})
437+
fold_tys(
438+
ty,
439+
|ty, _| match ty.kind(&Interner) {
440+
TyKind::Alias(AliasTy::Projection(proj_ty)) => {
441+
self.normalize_projection_ty(proj_ty.clone())
442+
}
443+
_ => ty,
444+
},
445+
DebruijnIndex::INNERMOST,
446+
)
443447
}
444448

445449
fn normalize_projection_ty(&mut self, proj_ty: ProjectionTy) -> Ty {

crates/hir_ty/src/infer/coerce.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ impl<'a> InferenceContext<'a> {
7171
}
7272

7373
// Pointer weakening and function to pointer
74-
match (from_ty.interned_mut(), to_ty.kind(&Interner)) {
74+
match (from_ty.kind(&Interner), to_ty.kind(&Interner)) {
7575
// `*mut T` -> `*const T`
76+
(TyKind::Raw(_, inner), TyKind::Raw(m2 @ Mutability::Not, ..)) => {
77+
from_ty = TyKind::Raw(*m2, inner.clone()).intern(&Interner);
78+
}
7679
// `&mut T` -> `&T`
77-
(TyKind::Raw(m1, ..), TyKind::Raw(m2 @ Mutability::Not, ..))
78-
| (TyKind::Ref(m1, ..), TyKind::Ref(m2 @ Mutability::Not, ..)) => {
79-
*m1 = *m2;
80+
(TyKind::Ref(_, lt, inner), TyKind::Ref(m2 @ Mutability::Not, ..)) => {
81+
from_ty = TyKind::Ref(*m2, lt.clone(), inner.clone()).intern(&Interner);
8082
}
8183
// `&T` -> `*const T`
8284
// `&mut T` -> `*mut T`/`*const T`

crates/hir_ty/src/infer/expr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::iter::{repeat, repeat_with};
44
use std::{mem, sync::Arc};
55

6-
use chalk_ir::{cast::Cast, Mutability, TyVariableKind};
6+
use chalk_ir::{cast::Cast, fold::Shift, Mutability, TyVariableKind};
77
use hir_def::{
88
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
99
path::{GenericArg, GenericArgs},
@@ -24,7 +24,6 @@ use crate::{
2424
utils::{generics, Generics},
2525
AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner,
2626
ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyExt, TyKind,
27-
TypeWalk,
2827
};
2928

3029
use super::{

0 commit comments

Comments
 (0)