ooc-lang / rock

:ocean: self-hosted ooc compiler that generates c99
http://ooc-lang.org/
MIT License
403 stars 40 forks source link

Rock can't pick up generic type info from the typeargs of the return value of closure #921

Closed alexnask closed 8 years ago

alexnask commented 9 years ago

I know, quite the title.

Here is what I mean:


/* THIS WORKS */

foo: func <T> (c: Cell<T>) { T name println() }
foo(Cell new(42))

/* THIS WORKS TOO */

foo: func <T> (f: Func -> T) { T name println() }
foo(|| 42)  

/* THIS DOESN'T */

foo: func <T> (f: Func -> Cell<T>) { T name println() }
foo(|| Cell new(42))

The above fails with error Missing info for type argument T. Have you forgotten to qualify foo<T> (f: Func () -> Cell), e.g. List<Int>? (btw, this error message is pretty misleading to beginners since explicitly qualifying functions, rather than types, is illegal in ooc)

alexnask commented 9 years ago

That is harder to fix than expected, ACS's get their return type from the function type in the call's function definition, so the closure is resolved to be of type Func -> Cell<T> and trying to fetch it from there obviously returns T.

alexnask commented 9 years ago

I wonder why the Func -> T case works though...

fasterthanlime commented 9 years ago

@shamanas Realtypizing does pick up on quite a few cases, apparently Func -> Cell<T> wasn't one of them, but it shouldn't be too hard to add.

alexnask commented 9 years ago

I tried adding it last night but I got stuck because I am apparently getting Func -> Cell<T> as the type of the argument itself (not the function type in the functiondecl), which seems weird.
I'll be on it a bit later, I just need to reintroduce myself into rock gently <creepy smile>

fasterthanlime commented 9 years ago

getting Func -> Cell<T> as the type of the argument itself

Ah.. maybe the function's returnType is being realtypized, but not the function's type (if you see what I mean)

alexnask commented 9 years ago

Just for reference (so I don't forget and I fix this after going to the beach :P), I was assuming the return type of the function pointer was set to the inferred type but there is a special field, inferredReturnedType, which should work (I changed this back on my other PC and it resulted in crashes on some test programs, probably didn't do correct null checking)

I wonder whether BaseType searchTypeArg will work for "just" T so I can use the same code for -> T and -> Foo <T, K, V, ...>

Will probably fix that later, perhaps tomorrow morning in the worst case scenario.

fasterthanlime commented 9 years ago

after going to the beach :P

who says ooc devs have no life!

perhaps tomorrow morning in the worst case scenario.

famous last words, but the rest of your post is promising.

alexnask commented 9 years ago

searchTypeArgs doesn't actually work in this case, because it obviously looks into the type's ref to find the typearg name you requested.

Instead, we should be able to provide two types, one unqualified (or partially qualified) and one fully qualified and extract a type arg from there.

This should work for all types though, not just "base" types, I guess I'll write something in typeAnalysis