Skip to content

Add new entries to TypeName #371

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

Closed
Closed
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: 12 additions & 0 deletions chalk-integration/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use chalk_ir::interner::ChalkIr;
use chalk_ir::tls;
use chalk_ir::AssocTypeId;
use chalk_ir::Canonical;
use chalk_ir::ClosureId;
use chalk_ir::ConstrainedSubst;
use chalk_ir::FnDefId;
use chalk_ir::Goal;
use chalk_ir::ImplId;
use chalk_ir::InEnvironment;
Expand All @@ -20,6 +22,8 @@ use chalk_ir::UCanonical;
use chalk_rust_ir::AssociatedTyDatum;
use chalk_rust_ir::AssociatedTyValue;
use chalk_rust_ir::AssociatedTyValueId;
use chalk_rust_ir::ClosureDatum;
use chalk_rust_ir::FnDefDatum;
use chalk_rust_ir::ImplDatum;
use chalk_rust_ir::StructDatum;
use chalk_rust_ir::TraitDatum;
Expand Down Expand Up @@ -111,6 +115,14 @@ impl RustIrDatabase<ChalkIr> for ChalkDatabase {
self.program_ir().unwrap().as_struct_id(type_name)
}

fn fn_def_datum(&self, id: FnDefId<ChalkIr>) -> Arc<FnDefDatum<ChalkIr>> {
self.program_ir().unwrap().fn_def_datum(id)
}

fn closure_datum(&self, id: ClosureId<ChalkIr>) -> Arc<ClosureDatum<ChalkIr>> {
self.program_ir().unwrap().closure_datum(id)
}

fn impls_for_trait(
&self,
trait_id: TraitId<ChalkIr>,
Expand Down
1 change: 1 addition & 0 deletions chalk-integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use chalk_ir::Binders;
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TypeSort {
Struct,
Function,
Trait,
}

Expand Down
110 changes: 109 additions & 1 deletion chalk-integration/src/lowering.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chalk_ir::cast::{Cast, Caster};
use chalk_ir::interner::ChalkIr;
use chalk_ir::{self, AssocTypeId, BoundVar, DebruijnIndex, ImplId, StructId, TraitId};
use chalk_ir::{self, AssocTypeId, BoundVar, DebruijnIndex, FnDefId, ImplId, StructId, TraitId};
use chalk_parse::ast::*;
use chalk_rust_ir as rust_ir;
use chalk_rust_ir::{Anonymize, AssociatedTyValueId, IntoWhereClauses, ToParameter};
Expand All @@ -13,8 +13,12 @@ use crate::program::Program as LoweredProgram;
use crate::{Identifier as Ident, RawId, TypeKind, TypeSort};

type StructIds = BTreeMap<Ident, chalk_ir::StructId<ChalkIr>>;
type FnDefIds = BTreeMap<Ident, chalk_ir::FnDefId<ChalkIr>>;
type ClosureIds = BTreeMap<Ident, chalk_ir::ClosureId<ChalkIr>>;
type TraitIds = BTreeMap<Ident, chalk_ir::TraitId<ChalkIr>>;
type StructKinds = BTreeMap<chalk_ir::StructId<ChalkIr>, TypeKind>;
type FnDefKinds = BTreeMap<chalk_ir::FnDefId<ChalkIr>, TypeKind>;
type ClosureKinds = BTreeMap<chalk_ir::ClosureId<ChalkIr>, TypeKind>;
type TraitKinds = BTreeMap<chalk_ir::TraitId<ChalkIr>, TypeKind>;
type AssociatedTyLookups = BTreeMap<(chalk_ir::TraitId<ChalkIr>, Ident), AssociatedTyLookup>;
type AssociatedTyValueIds =
Expand All @@ -27,6 +31,10 @@ pub type LowerResult<T> = Result<T, RustIrError>;
struct Env<'k> {
struct_ids: &'k StructIds,
struct_kinds: &'k StructKinds,
fn_def_ids: &'k FnDefIds,
fn_def_kinds: &'k FnDefKinds,
closure_ids: &'k ClosureIds,
closure_kinds: &'k ClosureKinds,
trait_ids: &'k TraitIds,
trait_kinds: &'k TraitKinds,
associated_ty_lookups: &'k AssociatedTyLookups,
Expand Down Expand Up @@ -222,8 +230,12 @@ impl LowerProgram for Program {
}

let mut struct_ids = BTreeMap::new();
let mut fn_def_ids = BTreeMap::new();
let mut closure_ids = BTreeMap::new();
let mut trait_ids = BTreeMap::new();
let mut struct_kinds = BTreeMap::new();
let mut fn_def_kinds = BTreeMap::new();
let mut closure_kinds = BTreeMap::new();
let mut trait_kinds = BTreeMap::new();
for (item, &raw_id) in self.items.iter().zip(&raw_ids) {
match item {
Expand All @@ -233,6 +245,12 @@ impl LowerProgram for Program {
struct_ids.insert(type_kind.name, id);
struct_kinds.insert(id, type_kind);
}
Item::FnDefn(defn) => {
let type_kind = defn.lower_type_kind()?;
let id = FnDefId(raw_id);
fn_def_ids.insert(type_kind.name, id);
fn_def_kinds.insert(id, type_kind);
}
Item::TraitDefn(defn) => {
let type_kind = defn.lower_type_kind()?;
let id = TraitId(raw_id);
Expand All @@ -245,6 +263,8 @@ impl LowerProgram for Program {
}

let mut struct_data = BTreeMap::new();
let mut fn_def_data = BTreeMap::new();
let mut closure_data = BTreeMap::new();
let mut trait_data = BTreeMap::new();
let mut impl_data = BTreeMap::new();
let mut associated_ty_data = BTreeMap::new();
Expand All @@ -254,6 +274,10 @@ impl LowerProgram for Program {
let empty_env = Env {
struct_ids: &struct_ids,
struct_kinds: &struct_kinds,
fn_def_ids: &fn_def_ids,
fn_def_kinds: &fn_def_kinds,
closure_ids: &closure_ids,
closure_kinds: &closure_kinds,
trait_ids: &trait_ids,
trait_kinds: &trait_kinds,
associated_ty_lookups: &associated_ty_lookups,
Expand All @@ -265,6 +289,10 @@ impl LowerProgram for Program {
let struct_id = StructId(raw_id);
struct_data.insert(struct_id, Arc::new(d.lower_struct(struct_id, &empty_env)?));
}
Item::FnDefn(ref d) => {
let fn_def_id = FnDefId(raw_id);
fn_def_data.insert(fn_def_id, Arc::new(d.lower_fn(fn_def_id, &empty_env)?));
}
Item::TraitDefn(ref trait_defn) => {
let trait_id = TraitId(raw_id);
trait_data.insert(
Expand Down Expand Up @@ -362,10 +390,16 @@ impl LowerProgram for Program {

let program = LoweredProgram {
struct_ids,
fn_def_ids,
closure_ids,
trait_ids,
struct_kinds,
fn_def_kinds,
closure_kinds,
trait_kinds,
struct_data,
fn_def_data,
closure_data,
trait_data,
impl_data,
associated_ty_values,
Expand Down Expand Up @@ -441,6 +475,16 @@ impl LowerParameterMap for StructDefn {
}
}

impl LowerParameterMap for FnDefn {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<Ident>> {
None
}

fn declared_parameters(&self) -> &[ParameterKind] {
&self.generics
}
}

impl LowerParameterMap for Impl {
fn synthetic_parameters(&self) -> Option<chalk_ir::ParameterKind<Ident>> {
None
Expand Down Expand Up @@ -528,12 +572,31 @@ impl LowerTypeKind for StructDefn {
}
}

impl LowerTypeKind for FnDefn {
fn lower_type_kind(&self) -> LowerResult<TypeKind> {
Ok(TypeKind {
sort: TypeSort::Function,
name: self.name.str,
binders: chalk_ir::Binders {
binders: self.all_parameters().anonymize(),
value: (),
},
})
}
}

impl LowerWhereClauses for StructDefn {
fn where_clauses(&self) -> &[QuantifiedWhereClause] {
&self.where_clauses
}
}

impl LowerWhereClauses for FnDefn {
fn where_clauses(&self) -> &[QuantifiedWhereClause] {
&self.where_clauses
}
}

impl LowerTypeKind for TraitDefn {
fn lower_type_kind(&self) -> LowerResult<TypeKind> {
let binders: Vec<_> = self.parameter_kinds.iter().map(|p| p.lower()).collect();
Expand Down Expand Up @@ -725,6 +788,47 @@ impl LowerStructDefn for StructDefn {
}
}

trait LowerFnDefn {
fn lower_fn(
&self,
struct_id: chalk_ir::FnDefId<ChalkIr>,
env: &Env,
) -> LowerResult<rust_ir::FnDefDatum<ChalkIr>>;
}

impl LowerFnDefn for FnDefn {
fn lower_fn(
&self,
fn_def_id: chalk_ir::FnDefId<ChalkIr>,
env: &Env,
) -> LowerResult<rust_ir::FnDefDatum<ChalkIr>> {
if self.flags.fundamental && self.all_parameters().len() != 1 {
Err(RustIrError::InvalidFundamentalTypesParameters(self.name))?;
}

let binders = env.in_binders(self.all_parameters(), |env| {
let fields: LowerResult<_> = self.fields.iter().map(|f| f.ty.lower(env)).collect();
let where_clauses = self.lower_where_clauses(env)?;

Ok(rust_ir::FnDefDatumBound {
fields: fields?,
where_clauses,
})
})?;

let flags = rust_ir::FnDefFlags {
upstream: self.flags.upstream,
fundamental: self.flags.fundamental,
};

Ok(rust_ir::FnDefDatum {
id: fn_def_id,
binders,
flags,
})
}
}

trait LowerTraitRef {
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::TraitRef<ChalkIr>>;
}
Expand Down Expand Up @@ -1281,8 +1385,12 @@ impl LowerGoal<LoweredProgram> for Goal {

let env = Env {
struct_ids: &program.struct_ids,
fn_def_ids: &program.fn_def_ids,
closure_ids: &program.closure_ids,
trait_ids: &program.trait_ids,
struct_kinds: &program.struct_kinds,
fn_def_kinds: &program.fn_def_kinds,
closure_kinds: &program.closure_kinds,
trait_kinds: &program.trait_kinds,
associated_ty_lookups: &associated_ty_lookups,
parameter_map: BTreeMap::new(),
Expand Down
64 changes: 59 additions & 5 deletions chalk-integration/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use chalk_ir::debug::Angle;
use chalk_ir::interner::ChalkIr;
use chalk_ir::tls;
use chalk_ir::{
debug::SeparatorTraitRef, AliasTy, ApplicationTy, AssocTypeId, Goal, Goals, ImplId, Lifetime,
Parameter, ProgramClause, ProgramClauseImplication, StructId, Substitution, TraitId, Ty,
TyData, TypeName,
debug::SeparatorTraitRef, AliasTy, ApplicationTy, AssocTypeId, ClosureId, FnDefId, Goal, Goals,
ImplId, Lifetime, Parameter, ProgramClause, ProgramClauseImplication, StructId, Substitution,
TraitId, Ty, TyData, TypeName,
};
use chalk_rust_ir::{
AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ImplDatum, ImplType, StructDatum,
TraitDatum,
AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ClosureDatum, FnDefDatum, ImplDatum,
ImplType, StructDatum, TraitDatum,
};
use chalk_solve::split::Split;
use chalk_solve::RustIrDatabase;
Expand All @@ -26,6 +26,18 @@ pub struct Program {
/// For each struct:
pub struct_kinds: BTreeMap<StructId<ChalkIr>, TypeKind>,

/// From function name to item-id. Used during lowering only.
pub fn_def_ids: BTreeMap<Identifier, FnDefId<ChalkIr>>,

/// For each function definition:
pub fn_def_kinds: BTreeMap<FnDefId<ChalkIr>, TypeKind>,

/// From closure to item-id. Used during lowering only.
pub closure_ids: BTreeMap<Identifier, ClosureId<ChalkIr>>,

/// For each closure:
pub closure_kinds: BTreeMap<ClosureId<ChalkIr>, TypeKind>,

/// From trait name to item-id. Used during lowering only.
pub trait_ids: BTreeMap<Identifier, TraitId<ChalkIr>>,

Expand All @@ -35,6 +47,12 @@ pub struct Program {
/// For each struct:
pub struct_data: BTreeMap<StructId<ChalkIr>, Arc<StructDatum<ChalkIr>>>,

/// For each function definition:
pub fn_def_data: BTreeMap<FnDefId<ChalkIr>, Arc<FnDefDatum<ChalkIr>>>,

/// For each closure:
pub closure_data: BTreeMap<ClosureId<ChalkIr>, Arc<ClosureDatum<ChalkIr>>>,

/// For each impl:
pub impl_data: BTreeMap<ImplId<ChalkIr>, Arc<ImplDatum<ChalkIr>>>,

Expand Down Expand Up @@ -78,6 +96,34 @@ impl tls::DebugContext for Program {
}
}

fn debug_fn_def_id(
&self,
fn_def_id: FnDefId<ChalkIr>,
fmt: &mut fmt::Formatter<'_>,
) -> Result<(), fmt::Error> {
if let Some(k) = self.fn_def_kinds.get(&fn_def_id) {
write!(fmt, "{}", k.name)
} else {
fmt.debug_struct("InvalidFnDefId")
.field("index", &fn_def_id.0)
.finish()
}
}

fn debug_closure_id(
&self,
closure_id: ClosureId<ChalkIr>,
fmt: &mut fmt::Formatter<'_>,
) -> Result<(), fmt::Error> {
if let Some(k) = self.closure_kinds.get(&closure_id) {
write!(fmt, "{}", k.name)
} else {
fmt.debug_struct("InvalidClosureId")
.field("index", &closure_id.0)
.finish()
}
}

fn debug_trait_id(
&self,
trait_id: TraitId<ChalkIr>,
Expand Down Expand Up @@ -236,6 +282,14 @@ impl RustIrDatabase<ChalkIr> for Program {
}
}

fn fn_def_datum(&self, id: FnDefId<ChalkIr>) -> Arc<FnDefDatum<ChalkIr>> {
self.fn_def_data[&id].clone()
}

fn closure_datum(&self, id: ClosureId<ChalkIr>) -> Arc<ClosureDatum<ChalkIr>> {
self.closure_data[&id].clone()
}

fn impls_for_trait(
&self,
trait_id: TraitId<ChalkIr>,
Expand Down
18 changes: 18 additions & 0 deletions chalk-ir/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,24 @@ where
}
}

impl<I> CastTo<TypeName<I>> for FnDefId<I>
where
I: Interner,
{
fn cast_to(self, _interner: &I) -> TypeName<I> {
TypeName::FnDef(self)
}
}

impl<I> CastTo<TypeName<I>> for ClosureId<I>
where
I: Interner,
{
fn cast_to(self, _interner: &I) -> TypeName<I> {
TypeName::Closure(self)
}
}

impl<T> CastTo<T> for &T
where
T: Clone + HasInterner,
Expand Down
Loading