Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ use hir_ty::{
to_assoc_type_id,
traits::{FnTrait, Solution, SolutionVariables},
AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex,
GenericPredicate, InEnvironment, Interner, Obligation, ProjectionTy, Scalar, Substitution, Ty,
TyDefId, TyKind, TyVariableKind,
InEnvironment, Interner, Obligation, ProjectionTy, Scalar, Substitution, Ty, TyDefId, TyKind,
TyVariableKind, WhereClause,
};
use itertools::Itertools;
use rustc_hash::FxHashSet;
Expand Down Expand Up @@ -1461,7 +1461,7 @@ impl TypeParam {
db.generic_predicates_for_param(self.id)
.into_iter()
.filter_map(|pred| match &pred.value {
hir_ty::GenericPredicate::Implemented(trait_ref) => {
hir_ty::WhereClause::Implemented(trait_ref) => {
Some(Trait::from(trait_ref.hir_trait_id()))
}
_ => None,
Expand Down Expand Up @@ -2022,7 +2022,7 @@ impl Type {
self.ty.value.impl_trait_bounds(db).map(|it| {
it.into_iter()
.filter_map(|pred| match pred {
hir_ty::GenericPredicate::Implemented(trait_ref) => {
hir_ty::WhereClause::Implemented(trait_ref) => {
Some(Trait::from(trait_ref.hir_trait_id()))
}
_ => None,
Expand Down Expand Up @@ -2060,12 +2060,12 @@ impl Type {
fn walk_bounds(
db: &dyn HirDatabase,
type_: &Type,
bounds: &[GenericPredicate],
bounds: &[WhereClause],
cb: &mut impl FnMut(Type),
) {
for pred in bounds {
match pred {
GenericPredicate::Implemented(trait_ref) => {
WhereClause::Implemented(trait_ref) => {
cb(type_.clone());
walk_substs(db, type_, &trait_ref.substitution, cb);
}
Expand Down
11 changes: 4 additions & 7 deletions crates/hir_ty/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use la_arena::ArenaMap;
use crate::{
method_resolution::{InherentImpls, TraitImpls},
traits::chalk,
Binders, CallableDefId, FnDefId, GenericPredicate, ImplTraitId, InferenceResult, PolyFnSig,
ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId,
Binders, CallableDefId, FnDefId, ImplTraitId, InferenceResult, PolyFnSig, ReturnTypeImplTraits,
TraitRef, Ty, TyDefId, ValueTyDefId, WhereClause,
};
use hir_expand::name::Name;

Expand Down Expand Up @@ -57,13 +57,10 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {

#[salsa::invoke(crate::lower::generic_predicates_for_param_query)]
#[salsa::cycle(crate::lower::generic_predicates_for_param_recover)]
fn generic_predicates_for_param(
&self,
param_id: TypeParamId,
) -> Arc<[Binders<GenericPredicate>]>;
fn generic_predicates_for_param(&self, param_id: TypeParamId) -> Arc<[Binders<WhereClause>]>;

#[salsa::invoke(crate::lower::generic_predicates_query)]
fn generic_predicates(&self, def: GenericDefId) -> Arc<[Binders<GenericPredicate>]>;
fn generic_predicates(&self, def: GenericDefId) -> Arc<[Binders<WhereClause>]>;

#[salsa::invoke(crate::lower::trait_environment_query)]
fn trait_environment(&self, def: GenericDefId) -> Arc<crate::TraitEnvironment>;
Expand Down
29 changes: 13 additions & 16 deletions crates/hir_ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use hir_expand::name::Name;
use crate::{
db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive,
to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy,
CallableDefId, CallableSig, GenericPredicate, ImplTraitId, Interner, Lifetime, Obligation,
OpaqueTy, ProjectionTy, Scalar, Substitution, TraitRef, Ty, TyKind,
CallableDefId, CallableSig, ImplTraitId, Interner, Lifetime, Obligation, OpaqueTy,
ProjectionTy, Scalar, Substitution, TraitRef, Ty, TyKind, WhereClause,
};

pub struct HirFormatter<'a> {
Expand Down Expand Up @@ -353,7 +353,7 @@ impl HirDisplay for Ty {
_ => Cow::Borrowed(&[][..]),
};

if let [GenericPredicate::Implemented(trait_ref), _] = predicates.as_ref() {
if let [WhereClause::Implemented(trait_ref), _] = predicates.as_ref() {
let trait_ = trait_ref.hir_trait_id();
if fn_traits(f.db.upcast(), trait_).any(|it| it == trait_) {
return write!(f, "{}", ty_display);
Expand Down Expand Up @@ -652,7 +652,7 @@ fn fn_traits(db: &dyn DefDatabase, trait_: TraitId) -> impl Iterator<Item = Trai

pub fn write_bounds_like_dyn_trait_with_prefix(
prefix: &str,
predicates: &[GenericPredicate],
predicates: &[WhereClause],
f: &mut HirFormatter,
) -> Result<(), HirDisplayError> {
write!(f, "{}", prefix)?;
Expand All @@ -665,7 +665,7 @@ pub fn write_bounds_like_dyn_trait_with_prefix(
}

fn write_bounds_like_dyn_trait(
predicates: &[GenericPredicate],
predicates: &[WhereClause],
f: &mut HirFormatter,
) -> Result<(), HirDisplayError> {
// Note: This code is written to produce nice results (i.e.
Expand All @@ -679,7 +679,7 @@ fn write_bounds_like_dyn_trait(
let mut is_fn_trait = false;
for p in predicates.iter() {
match p {
GenericPredicate::Implemented(trait_ref) => {
WhereClause::Implemented(trait_ref) => {
let trait_ = trait_ref.hir_trait_id();
if !is_fn_trait {
is_fn_trait = fn_traits(f.db.upcast(), trait_).any(|it| it == trait_);
Expand Down Expand Up @@ -710,12 +710,12 @@ fn write_bounds_like_dyn_trait(
}
}
}
GenericPredicate::AliasEq(alias_eq) if is_fn_trait => {
WhereClause::AliasEq(alias_eq) if is_fn_trait => {
is_fn_trait = false;
write!(f, " -> ")?;
alias_eq.ty.hir_fmt(f)?;
}
GenericPredicate::AliasEq(AliasEq { ty, alias }) => {
WhereClause::AliasEq(AliasEq { ty, alias }) => {
// in types in actual Rust, these will always come
// after the corresponding Implemented predicate
if angle_open {
Expand All @@ -731,7 +731,7 @@ fn write_bounds_like_dyn_trait(
}
ty.hir_fmt(f)?;
}
GenericPredicate::Error => {
WhereClause::Error => {
if angle_open {
// impl Trait<X, {error}>
write!(f, ", ")?;
Expand Down Expand Up @@ -778,18 +778,15 @@ impl HirDisplay for TraitRef {
}
}

impl HirDisplay for GenericPredicate {
impl HirDisplay for WhereClause {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
if f.should_truncate() {
return write!(f, "{}", TYPE_HINT_TRUNCATION);
}

match self {
GenericPredicate::Implemented(trait_ref) => trait_ref.hir_fmt(f)?,
GenericPredicate::AliasEq(AliasEq {
alias: AliasTy::Projection(projection_ty),
ty,
}) => {
WhereClause::Implemented(trait_ref) => trait_ref.hir_fmt(f)?,
WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
write!(f, "<")?;
projection_ty.trait_ref(f.db).hir_fmt_ext(f, true)?;
write!(
Expand All @@ -799,7 +796,7 @@ impl HirDisplay for GenericPredicate {
)?;
ty.hir_fmt(f)?;
}
GenericPredicate::AliasEq(_) | GenericPredicate::Error => write!(f, "{{error}}")?,
WhereClause::AliasEq(_) | WhereClause::Error => write!(f, "{{error}}")?,
}
Ok(())
}
Expand Down
17 changes: 6 additions & 11 deletions crates/hir_ty/src/infer/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue};

use super::{InferenceContext, Obligation};
use crate::{
AliasEq, AliasTy, BoundVar, Canonical, DebruijnIndex, FnPointer, GenericPredicate,
InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyKind, TypeWalk,
AliasEq, AliasTy, BoundVar, Canonical, DebruijnIndex, FnPointer, InEnvironment, InferenceVar,
Interner, Scalar, Substitution, Ty, TyKind, TypeWalk, WhereClause,
};

impl<'a> InferenceContext<'a> {
Expand Down Expand Up @@ -382,21 +382,16 @@ impl InferenceTable {
}
}

fn unify_preds(
&mut self,
pred1: &GenericPredicate,
pred2: &GenericPredicate,
depth: usize,
) -> bool {
fn unify_preds(&mut self, pred1: &WhereClause, pred2: &WhereClause, depth: usize) -> bool {
match (pred1, pred2) {
(GenericPredicate::Implemented(tr1), GenericPredicate::Implemented(tr2))
(WhereClause::Implemented(tr1), WhereClause::Implemented(tr2))
if tr1.trait_id == tr2.trait_id =>
{
self.unify_substs(&tr1.substitution, &tr2.substitution, depth + 1)
}
(
GenericPredicate::AliasEq(AliasEq { alias: alias1, ty: ty1 }),
GenericPredicate::AliasEq(AliasEq { alias: alias2, ty: ty2 }),
WhereClause::AliasEq(AliasEq { alias: alias1, ty: ty1 }),
WhereClause::AliasEq(AliasEq { alias: alias2, ty: ty2 }),
) => {
let (substitution1, substitution2) = match (alias1, alias2) {
(AliasTy::Projection(projection_ty1), AliasTy::Projection(projection_ty2))
Expand Down
38 changes: 19 additions & 19 deletions crates/hir_ty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ pub enum TyKind {
/// represents the `Self` type inside the bounds. This is currently
/// implicit; Chalk has the `Binders` struct to make it explicit, but it
/// didn't seem worth the overhead yet.
Dyn(Arc<[GenericPredicate]>),
Dyn(Arc<[WhereClause]>),

/// A placeholder for a type which could not be computed; this is propagated
/// to avoid useless error messages. Doubles as a placeholder where type
Expand Down Expand Up @@ -564,7 +564,7 @@ impl TypeWalk for TraitRef {
/// Like `generics::WherePredicate`, but with resolved types: A condition on the
/// parameters of a generic item.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GenericPredicate {
pub enum WhereClause {
/// The given trait needs to be implemented for its type parameters.
Implemented(TraitRef),
/// An associated type bindings like in `Iterator<Item = T>`.
Expand All @@ -574,32 +574,32 @@ pub enum GenericPredicate {
Error,
}

impl GenericPredicate {
impl WhereClause {
pub fn is_error(&self) -> bool {
matches!(self, GenericPredicate::Error)
matches!(self, WhereClause::Error)
}

pub fn is_implemented(&self) -> bool {
matches!(self, GenericPredicate::Implemented(_))
matches!(self, WhereClause::Implemented(_))
}

pub fn trait_ref(&self, db: &dyn HirDatabase) -> Option<TraitRef> {
match self {
GenericPredicate::Implemented(tr) => Some(tr.clone()),
GenericPredicate::AliasEq(AliasEq { alias: AliasTy::Projection(proj), .. }) => {
WhereClause::Implemented(tr) => Some(tr.clone()),
WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(proj), .. }) => {
Some(proj.trait_ref(db))
}
GenericPredicate::AliasEq(_) | GenericPredicate::Error => None,
WhereClause::AliasEq(_) | WhereClause::Error => None,
}
}
}

impl TypeWalk for GenericPredicate {
impl TypeWalk for WhereClause {
fn walk(&self, f: &mut impl FnMut(&Ty)) {
match self {
GenericPredicate::Implemented(trait_ref) => trait_ref.walk(f),
GenericPredicate::AliasEq(alias_eq) => alias_eq.walk(f),
GenericPredicate::Error => {}
WhereClause::Implemented(trait_ref) => trait_ref.walk(f),
WhereClause::AliasEq(alias_eq) => alias_eq.walk(f),
WhereClause::Error => {}
}
}

Expand All @@ -609,9 +609,9 @@ impl TypeWalk for GenericPredicate {
binders: DebruijnIndex,
) {
match self {
GenericPredicate::Implemented(trait_ref) => trait_ref.walk_mut_binders(f, binders),
GenericPredicate::AliasEq(alias_eq) => alias_eq.walk_mut_binders(f, binders),
GenericPredicate::Error => {}
WhereClause::Implemented(trait_ref) => trait_ref.walk_mut_binders(f, binders),
WhereClause::AliasEq(alias_eq) => alias_eq.walk_mut_binders(f, binders),
WhereClause::Error => {}
}
}
}
Expand Down Expand Up @@ -815,7 +815,7 @@ impl Ty {
pub fn dyn_trait_ref(&self) -> Option<&TraitRef> {
match self.interned(&Interner) {
TyKind::Dyn(bounds) => bounds.get(0).and_then(|b| match b {
GenericPredicate::Implemented(trait_ref) => Some(trait_ref),
WhereClause::Implemented(trait_ref) => Some(trait_ref),
_ => None,
}),
_ => None,
Expand Down Expand Up @@ -894,7 +894,7 @@ impl Ty {
}
}

pub fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<GenericPredicate>> {
pub fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<WhereClause>> {
match self.interned(&Interner) {
TyKind::OpaqueType(opaque_ty_id, ..) => {
match db.lookup_intern_impl_trait_id((*opaque_ty_id).into()) {
Expand All @@ -907,7 +907,7 @@ impl Ty {
// This is only used by type walking.
// Parameters will be walked outside, and projection predicate is not used.
// So just provide the Future trait.
let impl_bound = GenericPredicate::Implemented(TraitRef {
let impl_bound = WhereClause::Implemented(TraitRef {
trait_id: to_chalk_trait_id(future_trait),
substitution: Substitution::empty(),
});
Expand Down Expand Up @@ -1166,7 +1166,7 @@ pub struct ReturnTypeImplTraits {

#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub(crate) struct ReturnTypeImplTrait {
pub(crate) bounds: Binders<Vec<GenericPredicate>>,
pub(crate) bounds: Binders<Vec<WhereClause>>,
}

pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId {
Expand Down
Loading