Skip to content

Commit

Permalink
Started implementing structure polymorph constraint enforcement
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Dec 13, 2024
1 parent aed13a2 commit d598de4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
31 changes: 29 additions & 2 deletions src/resolve/type_ctx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ mod find;
mod find_error;
mod resolve_type;

use super::expr::ResolveExprCtx;
use crate::{name::ResolvedName, resolved, workspace::fs::FsNodeId};
use super::{
error::{ResolveError, ResolveErrorKind},
expr::ResolveExprCtx,
};
use crate::{
ast,
name::ResolvedName,
resolved::{self, Constraint},
workspace::fs::FsNodeId,
};
use std::collections::{HashMap, HashSet};

#[derive(Debug)]
Expand Down Expand Up @@ -42,3 +50,22 @@ impl<'a, 'b, 'c> From<&'c ResolveExprCtx<'a, 'b>> for ResolveTypeCtx<'c> {
)
}
}

pub fn resolve_constraints(constraints: &[ast::Type]) -> Result<Vec<Constraint>, ResolveError> {
let mut resolved_constraints = vec![];

for constraint in constraints {
if let ast::TypeKind::Named(name, arguments) = &constraint.kind {
resolved_constraints.push(match name.as_plain_str() {
Some("PrimitiveAdd") if arguments.is_empty() => Constraint::PrimitiveAdd,
_ => {
return Err(
ResolveErrorKind::UndeclaredTrait(name.to_string()).at(constraint.source)
)
}
});
}
}

Ok(resolved_constraints)
}
30 changes: 6 additions & 24 deletions src/resolve/type_ctx/resolve_type.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::ResolveTypeCtx;
use super::{resolve_constraints, ResolveTypeCtx};
use crate::{
ast::{self, IntegerBits},
ir::IntegerSign,
resolve::error::{ResolveError, ResolveErrorKind},
resolved::{self, Constraint},
resolved,
source_files::Source,
};
use std::borrow::Borrow;
Expand Down Expand Up @@ -81,28 +81,10 @@ impl<'a> ResolveTypeCtx<'a> {
},
))
}
ast::TypeKind::Polymorph(polymorph, constraints) => {
let mut resolved_constraints = vec![];

for constraint in constraints {
if let ast::TypeKind::Named(name, arguments) = &constraint.kind {
resolved_constraints.push(match name.as_plain_str() {
Some("PrimitiveAdd") if arguments.is_empty() => {
Constraint::PrimitiveAdd
}
_ => {
return Err(ResolveErrorKind::UndeclaredTrait(name.to_string())
.at(constraint.source))
}
});
}
}

Ok(resolved::TypeKind::Polymorph(
polymorph.clone(),
resolved_constraints,
))
}
ast::TypeKind::Polymorph(polymorph, constraints) => Ok(resolved::TypeKind::Polymorph(
polymorph.clone(),
resolve_constraints(constraints)?,
)),
}
.map(|kind| kind.at(ast_type.source))
}
Expand Down
13 changes: 5 additions & 8 deletions src/resolve/type_definition/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
ctx::ResolveCtx,
error::{ResolveError, ResolveErrorKind},
job::TypeJob,
type_ctx::resolve_constraints,
},
resolved::{self, EnumRef, HumanName, StructureRef, TypeAliasRef, TypeDecl, TypeParameters},
workspace::fs::FsNodeId,
Expand Down Expand Up @@ -37,7 +38,7 @@ pub fn prepare_type_jobs(
resolved_ast,
module_fs_node_id,
structure,
));
)?);
}

for definition in file.enums.iter() {
Expand Down Expand Up @@ -69,17 +70,13 @@ fn prepare_structure(
resolved_ast: &mut resolved::Ast,
module_fs_node_id: FsNodeId,
structure: &ast::Structure,
) -> StructureRef {
) -> Result<StructureRef, ResolveError> {
let source = structure.source;

let mut parameters = TypeParameters::default();

for (name, parameter) in structure.parameters.iter() {
let constraints = vec![];

if !parameter.constraints.is_empty() {
todo!("type parameters with constraints are not supported yet");
}
let constraints = resolve_constraints(&parameter.constraints)?;

if parameters
.parameters
Expand Down Expand Up @@ -123,7 +120,7 @@ fn prepare_structure(
},
);

structure_ref
Ok(structure_ref)
}

fn prepare_enum(
Expand Down

0 comments on commit d598de4

Please sign in to comment.