tonysparks / litac-lang

LitaC compiler in LitaC
7 stars 5 forks source link

Fix function ptr generic inferrence #34

Closed tonysparks closed 1 year ago

tonysparks commented 1 year ago

There was a bug that would allow unresolved generic function pointers to go thru compilation causing errors. This fix disallows function pointers with unresolved generic parameters from being used for generic type inferrence.

Example:

typedef func<K>(K) : i32 as HashFn<K>;
func PtrHashFn<K>(a: K) : i32 {
    return a + 8;
}

func run<K>(f: HashFn<K>, x: K) : i32 {
    return f(x)
}

// Here we can not properly infer the generic <K> type from the PtrHashFn, or at least how the compiler
// is currently constructed.  This would require the compiler to pause full type inferrence for PtrHashFn until
// is resolved all other parameters, and if run could be fully inferred, then use those inferred generic arguments
// to resolve PtrHashFn.
//
// Giving this is a significant enhancement to the compiler, I opted to just not allow it and require explicit 
// type arguments: PtrHashFn<i32>
assert(run(&PtrHashFn, 4_i32) == 12) // invalid type inferrence
assert(run(&PtrHashFn<i32>, 4_i32) == 12) // valid type inferrence