Skip to content

Commit 1a622d7

Browse files
committed
Removed old constraints system code, refactored type parameter code, and added error message for attempting to use undeclared polymorphs in type alias definitions
1 parent c4dc638 commit 1a622d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+215
-707
lines changed

Diff for: src/asg/datatype/kind/constraint.rs

-40
This file was deleted.

Diff for: src/asg/datatype/kind/mod.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
mod anonymous_enum;
2-
mod constraint;
32
mod fixed_array;
43
mod func_ptr;
54

@@ -11,7 +10,6 @@ use crate::{
1110
target::Target,
1211
};
1312
pub use anonymous_enum::AnonymousEnum;
14-
pub use constraint::Constraint;
1513
use core::hash::Hash;
1614
use derive_more::{IsVariant, Unwrap};
1715
pub use fixed_array::FixedArray;
@@ -40,7 +38,7 @@ pub enum TypeKind {
4038
Enum(HumanName, EnumRef),
4139
Structure(HumanName, StructRef, Vec<Type>),
4240
TypeAlias(HumanName, TypeAliasRef, Vec<Type>),
43-
Polymorph(String, Vec<Constraint>),
41+
Polymorph(String),
4442
Trait(HumanName, TraitRef, Vec<Type>),
4543
}
4644

@@ -73,7 +71,7 @@ impl TypeKind {
7371
| TypeKind::TypeAlias(_, _, parameters) => parameters
7472
.iter()
7573
.any(|parameter| parameter.kind.contains_polymorph()),
76-
TypeKind::Polymorph(_, _) => true,
74+
TypeKind::Polymorph(_) => true,
7775
}
7876
}
7977

@@ -103,7 +101,7 @@ impl TypeKind {
103101
| TypeKind::FixedArray(..)
104102
| TypeKind::FuncPtr(..)
105103
| TypeKind::Enum(_, _)
106-
| TypeKind::Polymorph(_, _)
104+
| TypeKind::Polymorph(_)
107105
| TypeKind::Trait(_, _, _) => None,
108106
}
109107
}
@@ -149,7 +147,7 @@ impl TypeKind {
149147
param.kind.for_each_polymorph(f);
150148
}
151149
}
152-
TypeKind::Polymorph(name, _) => f(name),
150+
TypeKind::Polymorph(name) => f(name),
153151
TypeKind::Trait(_, _, params) => {
154152
for param in params.iter() {
155153
param.kind.for_each_polymorph(f);
@@ -258,7 +256,7 @@ impl TypeKind {
258256
mapped,
259257
)))
260258
}
261-
TypeKind::Polymorph(_, _) => Ok(Cow::Borrowed(self)),
259+
TypeKind::Polymorph(_) => Ok(Cow::Borrowed(self)),
262260
TypeKind::Trait(human_name, trait_ref, type_args) => {
263261
if type_args.is_empty() {
264262
return Ok(Cow::Borrowed(self));
@@ -355,16 +353,8 @@ impl Display for TypeKind {
355353
}
356354
TypeKind::FuncPtr(..) => f.write_str("function-pointer-type")?,
357355
TypeKind::Enum(name, _) => write!(f, "{}", name)?,
358-
TypeKind::Polymorph(name, constaints) => {
356+
TypeKind::Polymorph(name) => {
359357
write!(f, "${}", name)?;
360-
361-
if !constaints.is_empty() {
362-
write!(f, ": ")?;
363-
}
364-
365-
for constaint in constaints {
366-
write!(f, "{}", constaint)?;
367-
}
368358
}
369359
TypeKind::Trait(name, _, parameters) => {
370360
write!(f, "{}", name)?;

Diff for: src/asg/datatype/mod.rs

-39
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,6 @@ impl Type {
2828
pub fn is_ambiguous(&self) -> bool {
2929
self.kind.is_ambiguous()
3030
}
31-
32-
pub fn strip_constraints(&mut self) {
33-
match &mut self.kind {
34-
TypeKind::Unresolved => panic!(),
35-
TypeKind::Boolean => (),
36-
TypeKind::Integer(_, _) => (),
37-
TypeKind::CInteger(_, _) => (),
38-
TypeKind::IntegerLiteral(_) => (),
39-
TypeKind::FloatLiteral(_) => (),
40-
TypeKind::Floating(_) => (),
41-
TypeKind::Ptr(inner) => inner.strip_constraints(),
42-
TypeKind::Void => (),
43-
TypeKind::Never => (),
44-
TypeKind::AnonymousStruct() => todo!("strip_constraints for anonymous struct"),
45-
TypeKind::AnonymousUnion() => todo!("strip_constraints for anonymous union"),
46-
TypeKind::AnonymousEnum(_) => (),
47-
TypeKind::FixedArray(fixed_array) => fixed_array.inner.strip_constraints(),
48-
TypeKind::FuncPtr(func) => {
49-
for param in func.params.required.iter_mut() {
50-
param.ty.strip_constraints();
51-
}
52-
func.return_type.strip_constraints();
53-
}
54-
TypeKind::Enum(_, _) => (),
55-
TypeKind::Structure(_, _, params) | TypeKind::TypeAlias(_, _, params) => {
56-
for parameter in params {
57-
parameter.strip_constraints();
58-
}
59-
}
60-
TypeKind::Polymorph(_, constraints) => {
61-
constraints.drain(..);
62-
}
63-
TypeKind::Trait(_, _, parameters) => {
64-
for parameter in parameters {
65-
parameter.strip_constraints();
66-
}
67-
}
68-
}
69-
}
7031
}
7132

7233
impl PartialEq for Type {

Diff for: src/asg/func.rs

+2-48
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,10 @@
11
use crate::{asg::*, name::ResolvedName, source_files::Source, tag::Tag};
2-
use std::{collections::HashSet, fmt::Display};
3-
4-
#[derive(Clone, Debug)]
5-
pub struct CurrentConstraints {
6-
constraints: HashMap<String, HashSet<Constraint>>,
7-
}
8-
9-
impl<'a> CurrentConstraints {
10-
pub fn new(constraints: HashMap<String, HashSet<Constraint>>) -> Self {
11-
Self { constraints }
12-
}
13-
14-
pub fn new_empty() -> Self {
15-
Self {
16-
constraints: Default::default(),
17-
}
18-
}
19-
20-
pub fn satisfies(&self, ty: &Type, constraint: &Constraint) -> bool {
21-
match constraint {
22-
Constraint::PrimitiveAdd => match &ty.kind {
23-
TypeKind::Integer(..) | TypeKind::CInteger(..) | TypeKind::Floating(..) => true,
24-
TypeKind::Polymorph(name, constraints) => {
25-
constraints.contains(constraint)
26-
|| self
27-
.constraints
28-
.get(name)
29-
.map_or(false, |in_scope| in_scope.contains(constraint))
30-
}
31-
_ => false,
32-
},
33-
Constraint::Trait(name, _trait_ref, _trait_arguments) => match &ty.kind {
34-
TypeKind::Polymorph(name, constraints) => {
35-
constraints.contains(constraint)
36-
|| self
37-
.constraints
38-
.get(name)
39-
.map_or(false, |in_scope| in_scope.contains(constraint))
40-
}
41-
_ => {
42-
todo!("test if user-defined trait '{}' is satisfied", name)
43-
}
44-
},
45-
}
46-
}
47-
}
2+
use std::fmt::Display;
483

494
#[derive(Clone, Debug)]
505
pub struct Func {
516
pub name: ResolvedName,
52-
pub named_type_args: Vec<String>,
7+
pub type_params: TypeParams,
538
pub params: Params,
549
pub return_type: Type,
5510
pub stmts: Vec<Stmt>,
@@ -59,7 +14,6 @@ pub struct Func {
5914
pub source: Source,
6015
pub abide_abi: bool,
6116
pub tag: Option<Tag>,
62-
pub constraints: CurrentConstraints,
6317
pub impl_params: ImplParams,
6418
}
6519

Diff for: src/asg/implementation.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use super::{FuncRef, GenericTraitRef, ImplRef};
1+
use super::{FuncRef, GenericTraitRef, ImplRef, TypeParams};
22
use crate::{ast::Privacy, source_files::Source};
3-
use indexmap::IndexMap;
43
use std::collections::HashMap;
54

65
#[derive(Clone, Debug)]
76
pub struct Impl {
8-
pub name_params: IndexMap<String, ()>,
7+
pub params: TypeParams,
98
pub target: GenericTraitRef,
109
pub source: Source,
1110
pub body: HashMap<String, FuncRef>,

Diff for: src/asg/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ pub use implementation::*;
4040
use indexmap::IndexMap;
4141
pub use overload::*;
4242
use slotmap::{new_key_type, SlotMap};
43-
use std::{
44-
borrow::Cow,
45-
collections::{HashMap, HashSet},
46-
};
43+
use std::{borrow::Cow, collections::HashSet};
4744
pub use stmt::*;
4845
pub use structure::*;
4946
pub use trait_constraint::*;
@@ -115,7 +112,7 @@ impl<'a> Asg<'a> {
115112
let polymorphs = IndexMap::<String, PolyValue>::from_iter(
116113
alias
117114
.params
118-
.iter()
115+
.names()
119116
.cloned()
120117
.zip(type_args.iter().cloned().map(PolyValue::Type)),
121118
);

Diff for: src/asg/structure.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{Type, TypeParameters};
1+
use super::{Type, TypeParams};
22
use crate::{ast::Privacy, name::ResolvedName, source_files::Source};
33
use indexmap::IndexMap;
44

@@ -7,7 +7,7 @@ pub struct Struct {
77
pub name: ResolvedName,
88
pub fields: IndexMap<String, Field>,
99
pub is_packed: bool,
10-
pub params: TypeParameters,
10+
pub params: TypeParams,
1111
pub source: Source,
1212
}
1313

Diff for: src/asg/trait_constraint.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use super::{HumanName, Params, Type};
1+
use super::{HumanName, Params, Type, TypeParams};
22
use crate::source_files::Source;
3-
use indexmap::{IndexMap, IndexSet};
3+
use indexmap::IndexMap;
44

55
#[derive(Clone, Debug)]
66
pub struct Trait {
77
pub human_name: HumanName,
88
pub source: Source,
9-
pub params: IndexSet<String>,
9+
pub params: TypeParams,
1010
pub funcs: IndexMap<String, TraitFunc>,
1111
}
1212

Diff for: src/asg/type_alias.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use super::{HumanName, Type};
1+
use super::{HumanName, Type, TypeParams};
22
use crate::source_files::Source;
3-
use indexmap::IndexSet;
43

54
#[derive(Clone, Debug)]
65
pub struct TypeAlias {
76
pub human_name: HumanName,
87
pub source: Source,
9-
pub params: IndexSet<String>,
8+
pub params: TypeParams,
109
pub becomes: Type,
1110
}

Diff for: src/asg/type_decl.rs

+6-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use super::{Asg, Constraint, TypeKind};
2-
use crate::{ast::Privacy, source_files::Source};
3-
use indexmap::IndexMap;
1+
use super::{Asg, TypeKind};
2+
use crate::{
3+
ast::{self, Privacy},
4+
source_files::Source,
5+
};
46

57
#[derive(Clone, Debug)]
68
pub struct TypeDecl {
@@ -15,26 +17,4 @@ impl TypeDecl {
1517
}
1618
}
1719

18-
#[derive(Clone, Debug, Default)]
19-
pub struct TypeParameters {
20-
pub parameters: IndexMap<String, TypeParameter>,
21-
}
22-
23-
impl TypeParameters {
24-
pub fn len(&self) -> usize {
25-
self.parameters.len()
26-
}
27-
28-
pub fn iter(&self) -> impl Iterator<Item = (&String, &TypeParameter)> {
29-
self.parameters.iter()
30-
}
31-
32-
pub fn names(&self) -> impl Iterator<Item = &String> {
33-
self.parameters.keys()
34-
}
35-
}
36-
37-
#[derive(Clone, Debug)]
38-
pub struct TypeParameter {
39-
pub constraints: Vec<Constraint>,
40-
}
20+
pub type TypeParams = ast::TypeParams;

Diff for: src/ast/datatype/kind/display.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::{
33
ast::{fmt_c_integer, FloatSize, IntegerBits, TypeArg},
44
ir::IntegerSign,
55
};
6-
use itertools::Itertools;
76
use std::fmt::Display;
87

98
impl Display for &TypeKind {
@@ -69,13 +68,8 @@ impl Display for &TypeKind {
6968
TypeKind::FuncPtr(_function) => {
7069
write!(f, "(function pointer type)")?;
7170
}
72-
TypeKind::Polymorph(polymorph, constraints) => {
71+
TypeKind::Polymorph(polymorph) => {
7372
write!(f, "${}", polymorph)?;
74-
75-
if !constraints.is_empty() {
76-
write!(f, ": ")?;
77-
write!(f, "{}", constraints.iter().map(|x| x.to_string()).join("+"))?;
78-
}
7973
}
8074
}
8175

Diff for: src/ast/datatype/kind/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub enum TypeKind {
2222
AnonymousUnion(AnoymousUnion),
2323
AnonymousEnum(AnonymousEnum),
2424
FuncPtr(FuncPtr),
25-
Polymorph(String, Vec<Type>),
25+
Polymorph(String),
2626
}
2727

2828
impl TypeKind {

Diff for: src/ast/datatype/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Type {
6161
.flat_map(|param| param.ast_type.contains_polymorph())
6262
.next()
6363
.or_else(|| func_pointer.return_type.contains_polymorph()),
64-
TypeKind::Polymorph(_, _) => Some(self.source),
64+
TypeKind::Polymorph(_) => Some(self.source),
6565
}
6666
}
6767
}

0 commit comments

Comments
 (0)