can theoretically infer generics now (even tho they don't exist yet lmao)

This commit is contained in:
2025-04-11 02:32:16 -04:00
parent 07a9d6fee7
commit 44824b8b5a
4 changed files with 28 additions and 9 deletions

View File

@@ -30,6 +30,7 @@ fn start() {
x = add(x, 1);
};
println("after");
let infer_me: slice<_> = "hello";
print(tester());
let test: Test = Test {
a: 10,

View File

@@ -127,12 +127,23 @@ pub fn match_types(dest: &Type, src: &Type) -> Option<Type> {
if dest_id != src_id {
return None;
}
None
// TODO
// let mut args = Vec::new();
// for (darg, sarg) in dest_args.iter().zip(src_args) {
// }
// Some(Type::Struct { id: *dest_id, args })
let mut args = Vec::new();
let mut changed = false;
for (darg, sarg) in dest_args.iter().zip(src_args) {
if let Some(ty) = match_types(darg, sarg) {
args.push(ty);
changed = true;
} else if darg != sarg {
return None;
} else {
args.push(darg.clone());
}
}
if changed {
Some(Type::Struct { id: *dest_id, args })
} else {
None
}
}
(
Type::Fn {
@@ -147,13 +158,13 @@ pub fn match_types(dest: &Type, src: &Type) -> Option<Type> {
// TODO
None
}
(Type::Ref(dest), Type::Ref(src)) => Some(match_types(dest, src)?),
(Type::Slice(dest), Type::Slice(src)) => Some(match_types(dest, src)?),
(Type::Ref(dest), Type::Ref(src)) => Some(match_types(dest, src)?.rf()),
(Type::Slice(dest), Type::Slice(src)) => Some(match_types(dest, src)?.slice()),
(Type::Array(dest, dlen), Type::Array(src, slen)) => {
if dlen != slen {
return None;
}
match_types(dest, src)
Some(match_types(dest, src)?.arr(*dlen))
}
_ => None,
}

View File

@@ -15,6 +15,12 @@ impl UProgram {
spans: vec![var.origin],
});
}
if var.ty == Type::Infer {
output.err(CompilerMsg {
msg: format!("Var {:?} cannot be inferred", id),
spans: vec![var.origin],
});
}
}
output
}

View File

@@ -67,6 +67,7 @@ impl PType {
let inner = self.args[0].lower(namespace, output);
Type::Slice(Box::new(inner))
}
"_" => Type::Infer,
_ => {
output.err(CompilerMsg::from_span(span, "Type not found".to_string()));
Type::Error