vtereshkov / umka-lang

Umka: a statically typed embeddable scripting language
BSD 2-Clause "Simplified" License
1.08k stars 53 forks source link

should the interpreter trigger an error when a function that returns in all possible branches doesn't have an explicit return statement at the end? #284

Closed thacuber2a03 closed 1 year ago

thacuber2a03 commented 1 year ago

I have a function with a switch statement at the end, whose default case ends with a return that should be enough to stop the "Non-void function must have a return statement" error, yet somehow it's not

fn disassembleInstruction*(c: chunk.Chunk, offset: int): int {
    printf("%04d ", offset)

    instruction := c.code[offset]
    switch instruction {
        case chunk.OP_RETURN:
            return simpleInstruction("OP_RETURN", offset)
        default:
            printf("Unknown opcode %d\n", instruction)
            return offset + 1
    }
}

(yes I am reading the Crafting Interpreters book, how'd you guess) is this intended?

vtereshkov commented 1 year ago

@ThaCuber Certainly your code could be treated as valid. However, it would require much more advanced control flow analysis to detect all execution paths and determine if all of them end with a return statement. Umka's approach is based on much simpler, purely syntactic considerations. At least this is safe, even if not always convenient.