odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.13k stars 551 forks source link

Poly code crashes compiler. #3730

Closed Chamberlain91 closed 3 weeks ago

Chamberlain91 commented 3 weeks ago

Context

I was trying to make some silly polymorphic thing, and encountered a compiler crash. The code itself is probably invalid and bad.

Odin: dev-2024-06:f1779c85d OS: Windows 11 Home Basic (version: 24H2), build 26200.5001 CPU: AMD Ryzen 7 5700X 8-Core Processor RAM: 32689 MiB Backend: LLVM 17.0.1

Expected Behavior

For the compiler to report error or successfully build.

Current Behavior

Just exits with an error status, no errors printed.

Failure Information (for bugs)

Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.

Steps to Reproduce

package poly_bug

import "core:fmt"

BaseType :: struct {}

SubType :: struct {
    using _: BaseType,
}

some_proc :: proc($T: typeid/BaseType) -> ^T {
    return nil
}

main :: proc() {

    fmt.printfln("[{}] BEFORE", #procedure)

    student := some_proc(SubType)

    fmt.printfln("[{}] AFTER", #procedure)

}

Failure Logs

PowerShell reports exit code -1073741819.

Feoramund commented 3 weeks ago

I don't use type specialization much in Odin, but from reading the overview and searching the core library, it seems like this feature is designed to be used with parametric polymorphism, not subtype polymorphism.

I.e. some_proc will accept any type that is derived from BaseType, if BaseType has a polymorphic parameter:

BaseType :: struct ($T: typeid) { }

That said, I think I can prevent the compiler from crashing.