odin-lang / Odin

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

Add `-vet-identical-cast` #3832

Closed Feoramund closed 3 days ago

Feoramund commented 3 days ago

This is a feature I've wanted for a while, and I think I've finally figured out how to get it working. I had to disable this check on polymorphic procedures, because the information that a type was originally polymorphic didn't seem to be stored anywhere, as far as I could find. (is_type_polymorphic wasn't working for me on the individual types, at least, but it did work on the contextual proc.)

This should be a good feature for refactoring code when changing one type to another.

I would have done this for transmute too, but I saw the usage of a compile-time type-switching paradigm in base:runtime/internal.odin where __float16 is declared to be one type or another, and this was causing the identical transmute prevention to trigger. I wasn't sure what to do about that, so I left it as-is and didn't make transmute also check for identical types, for the time being until I get some feedback.

gingerBill commented 3 days ago

So I experimented with this before, but it might have too many false positives because of polymorphic procedures.

I think this might still have that problem but for nested procedures within polymorphic ones.

foo :: proc(x: $T) {
    bar :: proc(x: T) {
        ... // this isn't a polymorphic procedure to the compiler, per se
    }
}

So what might need to be done is check if the procedure's chain of parents is polymorphic anywhere and then disallow it.

gingerBill commented 3 days ago

I am going to merge this and then solve that specific problem.

Feoramund commented 3 days ago

This looks awesome. Thanks for figuring out the rest of it. :) The check for unneeded transmutes is really cool too.