Skip to content

Commit

Permalink
Merge #8419 #8423
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: kjeremy <[email protected]>
  • Loading branch information
3 people authored Apr 8, 2021
3 parents 5810299 + d992736 + 3634b21 commit 72ad5cb
Show file tree
Hide file tree
Showing 19 changed files with 344 additions and 1,550 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1903,7 +1903,9 @@ impl Type {
| TyKind::Dyn(_)
| TyKind::Function(_)
| TyKind::Alias(_)
| TyKind::Foreign(_) => false,
| TyKind::Foreign(_)
| TyKind::Generator(..)
| TyKind::GeneratorWitness(..) => false,
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/hir_ty/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::iter;

use chalk_ir::{
cast::{Cast, CastTo, Caster},
fold::Fold,
interner::HasInterner,
AdtId, BoundVar, DebruijnIndex, Safety, Scalar,
};
Expand All @@ -13,7 +14,7 @@ use smallvec::SmallVec;
use crate::{
db::HirDatabase, primitive, to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders,
CallableSig, FnPointer, FnSig, FnSubst, GenericArg, Interner, ProjectionTy, Substitution,
TraitRef, Ty, TyDefId, TyExt, TyKind, TypeWalk, ValueTyDefId,
TraitRef, Ty, TyDefId, TyExt, TyKind, ValueTyDefId,
};

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

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

Expand Down Expand Up @@ -141,7 +141,7 @@ impl TyBuilder<hir_def::AdtId> {
self.vec.push(fallback().cast(&Interner));
} else {
// each default can depend on the previous parameters
let subst_so_far = Substitution::intern(self.vec.clone());
let subst_so_far = Substitution::from_iter(&Interner, self.vec.clone());
self.vec
.push(default_ty.clone().substitute(&Interner, &subst_so_far).cast(&Interner));
}
Expand Down Expand Up @@ -196,13 +196,13 @@ impl TyBuilder<TypeAliasId> {
}
}

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

pub fn build(self) -> T {
pub fn build(self) -> <T as Fold<Interner>>::Result {
let (b, subst) = self.build_internal();
b.substitute(&Interner, &subst)
}
Expand Down
65 changes: 4 additions & 61 deletions crates/hir_ty/src/chalk_cast.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//! Implementations of the Chalk `Cast` trait for our types.
use chalk_ir::{
cast::{Cast, CastTo},
interner::HasInterner,
};
use chalk_ir::interner::HasInterner;

use crate::{AliasEq, DomainGoal, GenericArg, GenericArgData, Interner, TraitRef, Ty, WhereClause};
use crate::{CallableSig, ReturnTypeImplTraits};

macro_rules! has_interner {
($t:ty) => {
Expand All @@ -15,59 +12,5 @@ macro_rules! has_interner {
};
}

has_interner!(WhereClause);
has_interner!(DomainGoal);
has_interner!(GenericArg);
has_interner!(Ty);

impl CastTo<WhereClause> for TraitRef {
fn cast_to(self, _interner: &Interner) -> WhereClause {
WhereClause::Implemented(self)
}
}

impl CastTo<WhereClause> for AliasEq {
fn cast_to(self, _interner: &Interner) -> WhereClause {
WhereClause::AliasEq(self)
}
}

impl CastTo<DomainGoal> for WhereClause {
fn cast_to(self, _interner: &Interner) -> DomainGoal {
DomainGoal::Holds(self)
}
}

impl CastTo<GenericArg> for Ty {
fn cast_to(self, interner: &Interner) -> GenericArg {
GenericArg::new(interner, GenericArgData::Ty(self))
}
}

macro_rules! transitive_impl {
($a:ty, $b:ty, $c:ty) => {
impl CastTo<$c> for $a {
fn cast_to(self, interner: &Interner) -> $c {
self.cast::<$b>(interner).cast(interner)
}
}
};
}

// In Chalk, these can be done as blanket impls, but that doesn't work here
// because of coherence

transitive_impl!(TraitRef, WhereClause, DomainGoal);
transitive_impl!(AliasEq, WhereClause, DomainGoal);

macro_rules! reflexive_impl {
($a:ty) => {
impl CastTo<$a> for $a {
fn cast_to(self, _interner: &Interner) -> $a {
self
}
}
};
}

reflexive_impl!(GenericArg);
has_interner!(CallableSig);
has_interner!(ReturnTypeImplTraits);
18 changes: 16 additions & 2 deletions crates/hir_ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ impl HirDisplay for GenericArg {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
match self.interned() {
crate::GenericArgData::Ty(ty) => ty.hir_fmt(f),
crate::GenericArgData::Lifetime(lt) => lt.hir_fmt(f),
crate::GenericArgData::Const(c) => c.hir_fmt(f),
}
}
}
Expand Down Expand Up @@ -664,6 +666,8 @@ impl HirDisplay for Ty {
write!(f, "{{unknown}}")?;
}
TyKind::InferenceVar(..) => write!(f, "_")?,
TyKind::Generator(..) => write!(f, "{{generator}}")?,
TyKind::GeneratorWitness(..) => write!(f, "{{generator witness}}")?,
}
Ok(())
}
Expand Down Expand Up @@ -741,7 +745,7 @@ fn write_bounds_like_dyn_trait(
if !first {
write!(f, " + ")?;
}
// We assume that the self type is $0 (i.e. the
// We assume that the self type is ^0.0 (i.e. the
// existential) here, which is the only thing that's
// possible in actual Rust, and hence don't print it
write!(f, "{}", f.db.trait_data(trait_).name)?;
Expand Down Expand Up @@ -783,6 +787,10 @@ fn write_bounds_like_dyn_trait(
}
ty.hir_fmt(f)?;
}

// FIXME implement these
WhereClause::LifetimeOutlives(_) => {}
WhereClause::TypeOutlives(_) => {}
}
first = false;
}
Expand Down Expand Up @@ -837,6 +845,10 @@ impl HirDisplay for WhereClause {
ty.hir_fmt(f)?;
}
WhereClause::AliasEq(_) => write!(f, "{{error}}")?,

// FIXME implement these
WhereClause::TypeOutlives(..) => {}
WhereClause::LifetimeOutlives(..) => {}
}
Ok(())
}
Expand Down Expand Up @@ -881,9 +893,11 @@ impl HirDisplay for DomainGoal {
DomainGoal::Holds(wc) => {
write!(f, "Holds(")?;
wc.hir_fmt(f)?;
write!(f, ")")
write!(f, ")")?;
}
_ => write!(f, "?")?,
}
Ok(())
}
}

Expand Down
26 changes: 15 additions & 11 deletions crates/hir_ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::mem;
use std::ops::Index;
use std::sync::Arc;

use chalk_ir::{cast::Cast, Mutability};
use chalk_ir::{cast::Cast, DebruijnIndex, Mutability};
use hir_def::{
body::Body,
data::{ConstData, FunctionData, StaticData},
Expand All @@ -38,11 +38,11 @@ use syntax::SmolStr;

use super::{
DomainGoal, Guidance, InEnvironment, ProjectionTy, Solution, TraitEnvironment, TraitRef, Ty,
TypeWalk,
};
use crate::{
db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode,
to_assoc_type_id, AliasEq, AliasTy, Canonical, Interner, TyBuilder, TyExt, TyKind,
db::HirDatabase, fold_tys, infer::diagnostics::InferenceDiagnostic,
lower::ImplTraitLoweringMode, to_assoc_type_id, AliasEq, AliasTy, Canonical, Interner,
TyBuilder, TyExt, TyKind,
};

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

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

fn resolve_obligations_as_possible(&mut self) {
Expand Down Expand Up @@ -434,12 +434,16 @@ impl<'a> InferenceContext<'a> {
/// to do it as well.
fn normalize_associated_types_in(&mut self, ty: Ty) -> Ty {
let ty = self.resolve_ty_as_possible(ty);
ty.fold(&mut |ty| match ty.kind(&Interner) {
TyKind::Alias(AliasTy::Projection(proj_ty)) => {
self.normalize_projection_ty(proj_ty.clone())
}
_ => ty,
})
fold_tys(
ty,
|ty, _| match ty.kind(&Interner) {
TyKind::Alias(AliasTy::Projection(proj_ty)) => {
self.normalize_projection_ty(proj_ty.clone())
}
_ => ty,
},
DebruijnIndex::INNERMOST,
)
}

fn normalize_projection_ty(&mut self, proj_ty: ProjectionTy) -> Ty {
Expand Down
10 changes: 6 additions & 4 deletions crates/hir_ty/src/infer/coerce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ impl<'a> InferenceContext<'a> {
}

// Pointer weakening and function to pointer
match (from_ty.interned_mut(), to_ty.kind(&Interner)) {
match (from_ty.kind(&Interner), to_ty.kind(&Interner)) {
// `*mut T` -> `*const T`
(TyKind::Raw(_, inner), TyKind::Raw(m2 @ Mutability::Not, ..)) => {
from_ty = TyKind::Raw(*m2, inner.clone()).intern(&Interner);
}
// `&mut T` -> `&T`
(TyKind::Raw(m1, ..), TyKind::Raw(m2 @ Mutability::Not, ..))
| (TyKind::Ref(m1, ..), TyKind::Ref(m2 @ Mutability::Not, ..)) => {
*m1 = *m2;
(TyKind::Ref(_, lt, inner), TyKind::Ref(m2 @ Mutability::Not, ..)) => {
from_ty = TyKind::Ref(*m2, lt.clone(), inner.clone()).intern(&Interner);
}
// `&T` -> `*const T`
// `&mut T` -> `*mut T`/`*const T`
Expand Down
3 changes: 1 addition & 2 deletions crates/hir_ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::iter::{repeat, repeat_with};
use std::{mem, sync::Arc};

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

use super::{
Expand Down
Loading

0 comments on commit 72ad5cb

Please sign in to comment.