felixangell / kpd

an old compiler for Krug written in D - a prototype version
MIT License
44 stars 1 forks source link

calling a function lookup fails in the type environment for variables #48

Closed felixangell closed 6 years ago

felixangell commented 6 years ago

This runs fine:

func min(a s32, b s32) s32 {
    if a < b {
        return a;
    }
    return b;
}

func main() {
    let smaller = 3;
    let larger = 33;
    return min(larger, smaller); // NOTICE ME
}

/// .stdout
/// 3

However this does not:

func min(a s32, b s32) s32 {
    if a < b {
        return a;
    }
    return b;
}

func main() {
    let smaller = 3;
    let larger = 33;
    let c = min(larger, smaller);
    return c;
}

/// .stdout
/// 3

This is the error:

Fatal: type_infer: unhandled statement if([path: [sym a]]<[path: [sym b]])
Fatal: type_infer: unhandled statement ast.Return_Statement_Node
Error: Couldn't find type 'min' in environment:
core.exception.AssertError@source/middle/infer.d(191): Assertion failure
----------------
4
felixangell commented 6 years ago

Funnily enough this happens for the opposite of why I initially thought:

Return statements are not handled in type in type inference, if they were it would fail too. Because we do handle variables in the type inference, we can't actually find the function (for some reason) in the current type environment.

felixangell commented 6 years ago

Fixed