Skip to content

Commit 82f17a5

Browse files
committed
Added basic error message for attempting to use undeclared polymorphs inside structure definitions
1 parent 4250764 commit 82f17a5

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

+38
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,44 @@ impl TypeKind {
117117
}
118118
}
119119

120+
pub fn for_each_polymorph(&self, f: &mut impl FnMut(&str) -> ()) {
121+
match self {
122+
TypeKind::Unresolved => panic!("unresolved type"),
123+
TypeKind::Boolean
124+
| TypeKind::Integer(_, _)
125+
| TypeKind::CInteger(_, _)
126+
| TypeKind::IntegerLiteral(_)
127+
| TypeKind::FloatLiteral(_)
128+
| TypeKind::Floating(_) => (),
129+
TypeKind::Ptr(inner) => inner.kind.for_each_polymorph(f),
130+
TypeKind::Void
131+
| TypeKind::Never
132+
| TypeKind::AnonymousStruct()
133+
| TypeKind::AnonymousUnion()
134+
| TypeKind::AnonymousEnum(_) => (),
135+
TypeKind::FixedArray(fixed_array) => fixed_array.inner.kind.for_each_polymorph(f),
136+
TypeKind::FuncPtr(func) => {
137+
for param in func.params.required.iter() {
138+
param.ty.kind.for_each_polymorph(f);
139+
}
140+
func.return_type.kind.for_each_polymorph(f);
141+
}
142+
TypeKind::Enum(_, _) => (),
143+
TypeKind::Structure(_, _, params) => {
144+
for param in params.iter() {
145+
param.kind.for_each_polymorph(f);
146+
}
147+
}
148+
TypeKind::TypeAlias(_, _) => (),
149+
TypeKind::Polymorph(name, _) => f(name),
150+
TypeKind::Trait(_, _, params) => {
151+
for param in params.iter() {
152+
param.kind.for_each_polymorph(f);
153+
}
154+
}
155+
}
156+
}
157+
120158
pub fn map_type_params<E>(
121159
&self,
122160
mut mapper: impl FnMut(TypeParam) -> Result<TypeParam, E>,

Diff for: src/resolve/type_definition/resolve.rs

+10
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ fn resolve_structure(
119119
let ty =
120120
type_ctx.resolve_or_undeclared(&field.ast_type, ResolveTypeOptions::KeepAliases)?;
121121

122+
// TODO: CLEANUP: Cleanup this code
123+
let mut ok = Ok(());
124+
ty.kind.for_each_polymorph(&mut |name| {
125+
if structure.params.params.keys().filter(|n| *n == name).next().is_none() {
126+
ok = Err(ResolveError::other(format!("Cannot use polymorph '${}' inside type that is not declared by enclosing structure", name), ty.source));
127+
}
128+
});
129+
130+
ok?;
131+
122132
let resolved_struct = asg.structs.get_mut(struct_ref).expect("valid struct");
123133

124134
resolved_struct.fields.insert(

0 commit comments

Comments
 (0)