Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle well known traits for more types #591

Merged
merged 7 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 3 additions & 1 deletion chalk-solve/src/clauses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ fn program_clauses_that_could_match<I: Interner>(
}

if let Some(well_known) = trait_datum.well_known {
builtin_traits::add_builtin_program_clauses(db, builder, well_known, trait_ref)?;
builtin_traits::add_builtin_program_clauses(
db, builder, well_known, trait_ref, binders,
)?;
}
}
DomainGoal::Holds(WhereClause::AliasEq(alias_eq)) => match &alias_eq.alias {
Expand Down
15 changes: 11 additions & 4 deletions chalk-solve/src/clauses/builtin_traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{builder::ClauseBuilder, generalize};
use crate::{Interner, RustIrDatabase, TraitRef, WellKnownTrait};
use crate::{CanonicalVarKinds, Interner, RustIrDatabase, TraitRef, WellKnownTrait};
use chalk_ir::{Floundered, Substitution, Ty};

mod clone;
Expand All @@ -15,6 +15,7 @@ pub fn add_builtin_program_clauses<I: Interner>(
builder: &mut ClauseBuilder<'_, I>,
well_known: WellKnownTrait,
trait_ref: &TraitRef<I>,
binders: &CanonicalVarKinds<I>,
) -> Result<(), Floundered> {
// If `trait_ref` contains bound vars, we want to universally quantify them.
// `Generalize` collects them for us.
Expand All @@ -25,9 +26,15 @@ pub fn add_builtin_program_clauses<I: Interner>(
let ty = self_ty.data(db.interner());

match well_known {
WellKnownTrait::Sized => sized::add_sized_program_clauses(db, builder, &trait_ref, ty),
WellKnownTrait::Copy => copy::add_copy_program_clauses(db, builder, &trait_ref, ty),
WellKnownTrait::Clone => clone::add_clone_program_clauses(db, builder, &trait_ref, ty),
WellKnownTrait::Sized => {
sized::add_sized_program_clauses(db, builder, &trait_ref, ty, binders)
}
WellKnownTrait::Copy => {
copy::add_copy_program_clauses(db, builder, &trait_ref, ty, binders)
}
WellKnownTrait::Clone => {
clone::add_clone_program_clauses(db, builder, &trait_ref, ty, binders)
}
WellKnownTrait::FnOnce | WellKnownTrait::FnMut | WellKnownTrait::Fn => {
fn_family::add_fn_trait_program_clauses(db, builder, well_known, self_ty)?
}
Expand Down
7 changes: 3 additions & 4 deletions chalk-solve/src/clauses/builtin_traits/clone.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::clauses::ClauseBuilder;
use crate::{Interner, RustIrDatabase, TraitRef};
use chalk_ir::TyData;
use chalk_ir::{CanonicalVarKinds, TyData};

use super::copy::add_copy_program_clauses;

Expand All @@ -9,9 +9,8 @@ pub fn add_clone_program_clauses<I: Interner>(
builder: &mut ClauseBuilder<'_, I>,
trait_ref: &TraitRef<I>,
ty: &TyData<I>,
binders: &CanonicalVarKinds<I>,
) {
let _interner = db.interner();

// Implement Clone for types that automaticly implement Copy
add_copy_program_clauses(db, builder, trait_ref, ty);
add_copy_program_clauses(db, builder, trait_ref, ty, binders);
}
41 changes: 38 additions & 3 deletions chalk-solve/src/clauses/builtin_traits/copy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::clauses::builtin_traits::needs_impl_for_tys;
use crate::clauses::ClauseBuilder;
use crate::{Interner, RustIrDatabase, TraitRef};
use chalk_ir::{ApplicationTy, Substitution, TyData, TypeName};
use chalk_ir::{
ApplicationTy, CanonicalVarKinds, Mutability, Substitution, TyData, TyKind, TypeName,
VariableKind,
};
use std::iter;
use tracing::instrument;

fn push_tuple_copy_conditions<I: Interner>(
db: &dyn RustIrDatabase<I>,
Expand All @@ -29,11 +33,13 @@ fn push_tuple_copy_conditions<I: Interner>(
);
}

#[instrument(skip(db, builder))]
pub fn add_copy_program_clauses<I: Interner>(
db: &dyn RustIrDatabase<I>,
builder: &mut ClauseBuilder<'_, I>,
trait_ref: &TraitRef<I>,
ty: &TyData<I>,
binders: &CanonicalVarKinds<I>,
) {
match ty {
TyData::Apply(ApplicationTy { name, substitution }) => match name {
Expand All @@ -58,11 +64,40 @@ pub fn add_copy_program_clauses<I: Interner>(
let upvars = upvars.substitute(db.interner(), &closure_fn_substitution);
needs_impl_for_tys(db, builder, trait_ref, Some(upvars).into_iter());
}
_ => {}

TypeName::Ref(Mutability::Not)
| TypeName::Raw(_)
| TypeName::Scalar(_)
| TypeName::Never => builder.push_fact(trait_ref.clone()),
nathanwhit marked this conversation as resolved.
Show resolved Hide resolved

TypeName::Ref(Mutability::Mut)
| TypeName::Adt(_)
| TypeName::AssociatedType(_)
| TypeName::Slice
| TypeName::OpaqueType(_)
| TypeName::Str
| TypeName::Error => {}
},

TyData::Function(_) => builder.push_fact(trait_ref.clone()),

TyData::InferenceVar(_, kind) => match kind {
TyKind::Integer | TyKind::Float => builder.push_fact(trait_ref.clone()),
TyKind::General => {}
},

TyData::BoundVar(bound_var) => {
let var_kind = &binders.at(db.interner(), bound_var.index).kind;
match var_kind {
VariableKind::Ty(TyKind::Integer) | VariableKind::Ty(TyKind::Float) => {
builder.push_fact(trait_ref.clone())
}
VariableKind::Ty(_) | VariableKind::Const(_) | VariableKind::Lifetime => {}
}
}

// TODO(areredify)
// when #368 lands, extend this to handle everything accordingly
nathanwhit marked this conversation as resolved.
Show resolved Hide resolved
_ => {}
TyData::Alias(_) | TyData::Dyn(_) | TyData::Placeholder(_) => {}
};
}
37 changes: 31 additions & 6 deletions chalk-solve/src/clauses/builtin_traits/sized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::clauses::builtin_traits::needs_impl_for_tys;
use crate::clauses::ClauseBuilder;
use crate::rust_ir::AdtKind;
use crate::{Interner, RustIrDatabase, TraitRef};
use chalk_ir::{AdtId, ApplicationTy, Substitution, TyData, TypeName};
use chalk_ir::{
AdtId, ApplicationTy, CanonicalVarKinds, Substitution, TyData, TyKind, TypeName, VariableKind,
};

fn push_adt_sized_conditions<I: Interner>(
db: &dyn RustIrDatabase<I>,
Expand Down Expand Up @@ -69,7 +71,10 @@ pub fn add_sized_program_clauses<I: Interner>(
builder: &mut ClauseBuilder<'_, I>,
trait_ref: &TraitRef<I>,
ty: &TyData<I>,
binders: &CanonicalVarKinds<I>,
) {
// TODO(areredify)
// when #368 lands, extend this to handle everything accordingly
nathanwhit marked this conversation as resolved.
Show resolved Hide resolved
match ty {
TyData::Apply(ApplicationTy { name, substitution }) => match name {
TypeName::Adt(adt_id) => {
Expand All @@ -85,11 +90,31 @@ pub fn add_sized_program_clauses<I: Interner>(
| TypeName::Scalar(_)
| TypeName::Raw(_)
| TypeName::Ref(_) => builder.push_fact(trait_ref.clone()),
_ => {}

TypeName::AssociatedType(_)
| TypeName::Slice
| TypeName::OpaqueType(_)
| TypeName::Str
| TypeName::Error => {}
},
TyData::Function(_) => builder.push_fact(trait_ref.clone()),
// TODO(areredify)
// when #368 lands, extend this to handle everything accordingly
_ => {}

TyData::Function(_)
| TyData::InferenceVar(_, TyKind::Float)
| TyData::InferenceVar(_, TyKind::Integer) => builder.push_fact(trait_ref.clone()),

TyData::BoundVar(bound_var) => {
let var_kind = &binders.at(db.interner(), bound_var.index).kind;
match var_kind {
VariableKind::Ty(TyKind::Integer) | VariableKind::Ty(TyKind::Float) => {
builder.push_fact(trait_ref.clone())
}
VariableKind::Ty(_) | VariableKind::Const(_) | VariableKind::Lifetime => {}
}
}

TyData::InferenceVar(_, TyKind::General)
| TyData::Placeholder(_)
| TyData::Dyn(_)
| TyData::Alias(_) => {}
}
}
5 changes: 0 additions & 5 deletions tests/test/builtin_impls.rs

This file was deleted.

64 changes: 0 additions & 64 deletions tests/test/builtin_impls/clone.rs

This file was deleted.

67 changes: 0 additions & 67 deletions tests/test/builtin_impls/copy.rs

This file was deleted.

Loading