From 7ec3b66f7a3ac0a33cf435bc3596fdac542fc52a Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 20 Mar 2021 11:23:59 +0100 Subject: Turn Obligation into something similar to chalk_ir::DomainGoal This includes starting to make use of Chalk's `Cast` trait. --- crates/hir_ty/src/infer/coerce.rs | 7 +++---- crates/hir_ty/src/infer/expr.rs | 25 +++++++++++-------------- crates/hir_ty/src/infer/path.rs | 12 ++++++++---- crates/hir_ty/src/infer/unify.rs | 13 +++++-------- 4 files changed, 27 insertions(+), 30 deletions(-) (limited to 'crates/hir_ty/src/infer') diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs index b86474ed4..07eb96573 100644 --- a/crates/hir_ty/src/infer/coerce.rs +++ b/crates/hir_ty/src/infer/coerce.rs @@ -4,12 +4,11 @@ //! //! See: https://doc.rust-lang.org/nomicon/coercions.html -use chalk_ir::{Mutability, TyVariableKind}; +use chalk_ir::{cast::Cast, Mutability, TyVariableKind}; use hir_def::lang_item::LangItemTarget; use crate::{ - autoderef, to_chalk_trait_id, traits::Solution, Interner, Obligation, Substitution, TraitRef, - Ty, TyKind, + autoderef, to_chalk_trait_id, traits::Solution, Interner, Substitution, TraitRef, Ty, TyKind, }; use super::{InEnvironment, InferenceContext}; @@ -143,7 +142,7 @@ impl<'a> InferenceContext<'a> { .build(); let trait_ref = TraitRef { trait_id: to_chalk_trait_id(coerce_unsized_trait), substitution: substs }; - let goal = InEnvironment::new(self.trait_env.clone(), Obligation::Trait(trait_ref)); + let goal = InEnvironment::new(self.trait_env.clone(), trait_ref.cast(&Interner)); let canonicalizer = self.canonicalizer(); let canonicalized = canonicalizer.canonicalize_obligation(goal); diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 93548b6c0..79bbc5dab 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -3,7 +3,7 @@ use std::iter::{repeat, repeat_with}; use std::{mem, sync::Arc}; -use chalk_ir::{Mutability, TyVariableKind}; +use chalk_ir::{cast::Cast, Mutability, TyVariableKind}; use hir_def::{ expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, path::{GenericArg, GenericArgs}, @@ -21,7 +21,7 @@ use crate::{ to_assoc_type_id, to_chalk_trait_id, traits::{chalk::from_chalk, FnTrait, InEnvironment}, utils::{generics, variant_data, Generics}, - AdtId, Binders, CallableDefId, FnPointer, FnSig, Interner, Obligation, Rawness, Scalar, + AdtId, Binders, CallableDefId, DomainGoal, FnPointer, FnSig, Interner, Rawness, Scalar, Substitution, TraitRef, Ty, TyKind, }; @@ -90,10 +90,9 @@ impl<'a> InferenceContext<'a> { Substitution::build_for_generics(&generic_params).push(ty.clone()).push(arg_ty).build(); let trait_env = Arc::clone(&self.trait_env); - let implements_fn_trait = Obligation::Trait(TraitRef { - trait_id: to_chalk_trait_id(fn_once_trait), - substitution: substs.clone(), - }); + let implements_fn_trait: DomainGoal = + TraitRef { trait_id: to_chalk_trait_id(fn_once_trait), substitution: substs.clone() } + .cast(&Interner); let goal = self.canonicalizer().canonicalize_obligation(InEnvironment { value: implements_fn_trait.clone(), environment: trait_env, @@ -938,22 +937,20 @@ impl<'a> InferenceContext<'a> { let generic_predicates = self.db.generic_predicates(def.into()); for predicate in generic_predicates.iter() { let predicate = predicate.clone().subst(parameters); - if let Some(obligation) = Obligation::from_predicate(predicate) { - self.obligations.push(obligation); - } + self.obligations.push(predicate.cast(&Interner)); } // add obligation for trait implementation, if this is a trait method match def { CallableDefId::FunctionId(f) => { if let AssocContainerId::TraitId(trait_) = f.lookup(self.db.upcast()).container { - // construct a TraitDef + // construct a TraitRef let substs = parameters.prefix(generics(self.db.upcast(), trait_.into()).len()); - self.obligations.push(Obligation::Trait(TraitRef { - trait_id: to_chalk_trait_id(trait_), - substitution: substs, - })); + self.obligations.push( + TraitRef { trait_id: to_chalk_trait_id(trait_), substitution: substs } + .cast(&Interner), + ); } } CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => {} diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs index e15135fc1..58cce56ab 100644 --- a/crates/hir_ty/src/infer/path.rs +++ b/crates/hir_ty/src/infer/path.rs @@ -2,6 +2,7 @@ use std::iter; +use chalk_ir::cast::Cast; use hir_def::{ path::{Path, PathSegment}, resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs}, @@ -256,10 +257,13 @@ impl<'a> InferenceContext<'a> { .push(ty.clone()) .fill(std::iter::repeat_with(|| self.table.new_type_var())) .build(); - self.obligations.push(super::Obligation::Trait(TraitRef { - trait_id: to_chalk_trait_id(trait_), - substitution: trait_substs.clone(), - })); + self.obligations.push( + TraitRef { + trait_id: to_chalk_trait_id(trait_), + substitution: trait_substs.clone(), + } + .cast(&Interner), + ); Some(trait_substs) } AssocContainerId::ModuleId(_) => None, diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 5b7b423fa..1fc03c8f4 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -5,7 +5,7 @@ use std::borrow::Cow; use chalk_ir::{FloatTy, IntTy, TyVariableKind}; use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; -use super::{InferenceContext, Obligation}; +use super::{DomainGoal, InferenceContext}; use crate::{ AliasEq, AliasTy, BoundVar, Canonical, DebruijnIndex, FnPointer, InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyKind, TypeWalk, WhereClause, @@ -87,14 +87,11 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { pub(crate) fn canonicalize_obligation( mut self, - obligation: InEnvironment, - ) -> Canonicalized> { + obligation: InEnvironment, + ) -> Canonicalized> { let result = match obligation.value { - Obligation::Trait(tr) => { - Obligation::Trait(self.do_canonicalize(tr, DebruijnIndex::INNERMOST)) - } - Obligation::AliasEq(alias_eq) => { - Obligation::AliasEq(self.do_canonicalize(alias_eq, DebruijnIndex::INNERMOST)) + DomainGoal::Holds(wc) => { + DomainGoal::Holds(self.do_canonicalize(wc, DebruijnIndex::INNERMOST)) } }; self.into_canonicalized(InEnvironment { -- cgit v1.2.3