From 638100dc8bea69cc4093d15f1641ed39a8d27a43 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 29 Jun 2019 17:40:00 +0200 Subject: Refactor a bit & introduce Environment struct --- crates/ra_hir/src/ty/traits/chalk.rs | 56 +++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir/src/ty/traits') diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 4c3744b44..bee35fa62 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -12,7 +12,7 @@ use chalk_rust_ir::{AssociatedTyDatum, ImplDatum, StructDatum, TraitDatum}; use ra_db::salsa::{InternId, InternKey}; use test_utils::tested_by; -use super::ChalkContext; +use super::{Canonical, ChalkContext}; use crate::{ db::HirDatabase, generics::GenericDef, @@ -218,6 +218,60 @@ impl ToChalk for ProjectionTy { } } +impl ToChalk for Canonical +where + T: ToChalk, +{ + type Chalk = chalk_ir::Canonical; + + fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::Canonical { + let parameter = chalk_ir::ParameterKind::Ty(chalk_ir::UniverseIndex::ROOT); + let value = self.value.to_chalk(db); + let canonical = chalk_ir::Canonical { value, binders: vec![parameter; self.num_vars] }; + canonical + } + + fn from_chalk(db: &impl HirDatabase, canonical: chalk_ir::Canonical) -> Canonical { + Canonical { num_vars: canonical.binders.len(), value: from_chalk(db, canonical.value) } + } +} + +impl ToChalk for Arc { + type Chalk = Arc; + + fn to_chalk(self, _db: &impl HirDatabase) -> Arc { + chalk_ir::Environment::new() + } + + fn from_chalk( + _db: &impl HirDatabase, + _env: Arc, + ) -> Arc { + Arc::new(super::Environment) + } +} + +impl ToChalk for super::InEnvironment { + type Chalk = chalk_ir::InEnvironment; + + fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::InEnvironment { + chalk_ir::InEnvironment { + environment: self.environment.to_chalk(db), + goal: self.value.to_chalk(db), + } + } + + fn from_chalk( + db: &impl HirDatabase, + in_env: chalk_ir::InEnvironment, + ) -> super::InEnvironment { + super::InEnvironment { + environment: from_chalk(db, in_env.environment), + value: from_chalk(db, in_env.goal), + } + } +} + fn make_binders(value: T, num_vars: usize) -> chalk_ir::Binders { chalk_ir::Binders { value, -- cgit v1.2.3 From b1b12072eddaf989fb08ed7a2e39ec2dbbb83dde Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 29 Jun 2019 19:14:52 +0200 Subject: Start handling environment in trait resolution I.e. if we are inside a function with some where clauses, we assume these where clauses hold. --- crates/ra_hir/src/ty/traits/chalk.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src/ty/traits') diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index bee35fa62..f36ff2fc6 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -239,15 +239,28 @@ where impl ToChalk for Arc { type Chalk = Arc; - fn to_chalk(self, _db: &impl HirDatabase) -> Arc { - chalk_ir::Environment::new() + fn to_chalk(self, db: &impl HirDatabase) -> Arc { + let mut clauses = Vec::new(); + for pred in &self.predicates { + if pred.is_error() { + // for env, we just ignore errors + continue; + } + if let GenericPredicate::Implemented(trait_ref) = pred { + if blacklisted_trait(db, trait_ref.trait_) { + continue; + } + } + clauses.push(pred.clone().to_chalk(db).cast()); + } + chalk_ir::Environment::new().add_clauses(clauses) } fn from_chalk( _db: &impl HirDatabase, _env: Arc, ) -> Arc { - Arc::new(super::Environment) + unimplemented!() } } -- cgit v1.2.3 From 15862fc04183c7f9b3f3af666336a594a6a52cd9 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 7 Jul 2019 18:14:56 +0200 Subject: Use environment for associated type normalization as well --- crates/ra_hir/src/ty/traits/chalk.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'crates/ra_hir/src/ty/traits') diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index f36ff2fc6..32a45731d 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -218,6 +218,21 @@ impl ToChalk for ProjectionTy { } } +impl ToChalk for super::ProjectionPredicate { + type Chalk = chalk_ir::Normalize; + + fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::Normalize { + chalk_ir::Normalize { + projection: self.projection_ty.to_chalk(db), + ty: self.ty.to_chalk(db), + } + } + + fn from_chalk(_db: &impl HirDatabase, _normalize: chalk_ir::Normalize) -> Self { + unimplemented!() + } +} + impl ToChalk for Canonical where T: ToChalk, -- cgit v1.2.3 From 9afbf2dff43dee3227358f10162d4c77d192ce7a Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 8 Jul 2019 21:43:52 +0200 Subject: Unify `normalize` and `implements` to simplify code --- crates/ra_hir/src/ty/traits/chalk.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir/src/ty/traits') diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 32a45731d..2df4dd13f 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -12,7 +12,7 @@ use chalk_rust_ir::{AssociatedTyDatum, ImplDatum, StructDatum, TraitDatum}; use ra_db::salsa::{InternId, InternKey}; use test_utils::tested_by; -use super::{Canonical, ChalkContext}; +use super::{Canonical, ChalkContext, Obligation}; use crate::{ db::HirDatabase, generics::GenericDef, @@ -233,6 +233,21 @@ impl ToChalk for super::ProjectionPredicate { } } +impl ToChalk for Obligation { + type Chalk = chalk_ir::DomainGoal; + + fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::DomainGoal { + match self { + Obligation::Trait(tr) => tr.to_chalk(db).cast(), + Obligation::Projection(pr) => pr.to_chalk(db).cast(), + } + } + + fn from_chalk(_db: &impl HirDatabase, _goal: chalk_ir::DomainGoal) -> Self { + unimplemented!() + } +} + impl ToChalk for Canonical where T: ToChalk, -- cgit v1.2.3