Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default switch case can panics compiler for pointer to union reference #3612

Open
hitchh1k3r opened this issue May 22, 2024 · 1 comment
Open

Comments

@hitchh1k3r
Copy link

Context

Odin: dev-2024-05:856537f0c
OS: Windows 10 Professional (version: 22H2), build 19045.4291
CPU: Intel(R) Core(TM) i7-4771 CPU @ 3.50GHz
RAM: 16311 MiB
Backend: LLVM 17.0.1

Expected Behavior

No compiler panic.

Current Behavior

Compiler crashes with: Panic: Invalid type conversion

Failure Information

In a reference switch for a union type, that takes a pointer to a union, the default case will not correctly recognize the type as a pointer when passing it as a parameter to a procedure.

Here is a small case to reproduce the bug:

package main

import "core:fmt"

Union :: union {
}

ptr_proc :: proc(ptr : ^Union) {
}

variable : ^Union

main :: proc() {
  switch &ref in variable {
    case:
      fmt.println(typeid_of(type_of(ref))) // ^Union
      ptr_proc(ref)
  }
}

Here is the error message:

main.main
lb_emit_conv: src -> dst
Not Identical Union != ^Union
Not Identical union {} != ^Union
Not Identical 24e9e7d5a50 != 24e9e7e0da0
Not Identical 24e9e7d5af0 != 24e9e7e0da0
src\llvm_backend_expr.cpp(2324): Panic: Invalid type conversion: 'Union' to '^Union' for procedure 'main.main'
@KotzaBoss
Copy link

Could this perhaps be linked to a similar issue i have?
https://discord.com/channels/568138951836172421/1244966374280003640

package err

import "core:testing"

import rl "vendor:raylib"

@test
t1 :: proc(t: ^testing.T) {
    Unit :: struct {
        target: union{ rl.Vector3 },
    }
    units := make_soa(#soa[dynamic]Unit, 5)

    ptrs := make([dynamic]#soa^ #soa[]Unit, 5)

    all := units[:]
    for _, i in all {
        ptrs[i] = &all[i]
    }

    for p, i in ptrs {
        // This seems to be the problem
        p.target = rl.Vector3{auto_cast i, auto_cast i, auto_cast i}
    }

    for u, i in units {
        testing.expect_value(t, u.target, rl.Vector3{auto_cast i, auto_cast i, auto_cast i})
    }
}
~/git/Odin/odin test err.odin -file
err.t1
lb_emit_conv: src -> dst
Not Identical [3]f32 != [^]union {[3]f32}
Not Identical [3]f32 != [^]union {[3]f32}
Not Identical 7fc0bba58280 != 7fc0a0a49720
Not Identical 7fc0bba58280 != 7fc0a0a49720
src/llvm_backend_expr.cpp(2324): Panic: Invalid type conversion: '[3]f32' to '[^]union {[3]f32}' for procedure 'err.t1'
[1]    7329 illegal hardware instruction (core dumped)  ~/git/Odin/odin test invalid_type_conversion.odin -file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants