ImHex-Patterns icon indicating copy to clipboard operation
ImHex-Patterns copied to clipboard

Cannot cast from aliased type to type for user defined types

Open d-Rickyy-b opened this issue 2 years ago • 5 comments

This issue was edited after finding out that it has a wider scope than initially assumed. When you create an alias for a type with using the alias and the type both represent the same pl type and casting between them should be possible. This works for builtin types

s32 test @ 0;

using int = s32;

int test2 = test; // works ok

However a similar check using user defined types give a casting operation error. A simplest case example is

struct A {
    u8 byte;
};

A a @ 0;

using B=A;

B b1=a; //error cannot cast from A to B

I initially found this issue by trying to parse a uuid like so:

type::UUID uuid;

When doing so, I recieve the error [E0004]: Type error. Cannot cast from type 'type::UUID' to type 'type::GUID' when displaying the UUID in the Pattern Data or when printing the variable with std::print. This is probably because the format_guid function expects a struct of type GUID and doesn't know anything about UUID.

The issue can be reproduced with this pattern.

#include <std/io.pat>
#include <type/guid.pat>

type::UUID test @0x00;
std::print("Test: {}", test);

If my assumption is correct, the issue does not only affect UUIDs but all types with function attributes.

d-Rickyy-b avatar Sep 17 '23 22:09 d-Rickyy-b

The statement

type::UUID uuid;

cannot in itself create the error involving guid since guid is never mentioned. Please post the part of the code that tries to cast uuid to guid. But in general it is impossible to cast from a non builtin type to a different non-builtin type regardless. You can place both pattersn on the same data to interpret it two different ways.

paxcut avatar Sep 17 '23 23:09 paxcut

Please post the part of the code that tries to cast uuid to guid.

Simply displaying the value in the Pattern Data window leads to this error. image

Otherwise you can also use std::print

type::UUID test @0x00;
std::print("Test: {}", test);

Output:

I: Test: error[E0004]: Type error.
I: Cannot cast from type 'type::UUID' to type 'type::GUID'.

I am not explicitly casting any data. It's an implicit cast that's probably done by the format_guid function. https://github.com/WerWolv/ImHex-Patterns/blob/c0c117ac1960c3df811763a4a485b859d876bdaf/includes/type/guid.pat#L21

The docs implied that I could use UUID as an alias for GUID.

d-Rickyy-b avatar Sep 17 '23 23:09 d-Rickyy-b

I was wrong in my assumption that UUID and GUID were two different structs. Being an alias allows you to use one in place of the other. The error is caused by the argument of the format function being 'GUID guid' instead of what normally should be 'auto guid' as this change fixes the error, but I am not sure if the error should exist at all.

paxcut avatar Sep 17 '23 23:09 paxcut

So I checked this and the problem is not just for UUID and GUID or even types with function attributes. The problem is much more general so the title of this issue should be changed accordingly. When you create an alias for a type with using the alias and the type both represent the same pl type and casting between them should be possible. This works for builtin types

s32 test @ 0;

using int = s32;

int test2 = test; // works ok

However a similar check using user defined types give a casting operation error. A simplest case example is

struct A {
    u8 byte;
};

A a @ 0;

using B=A;

B b1=a; //error cannot cast from A to B

Could you please change the title of this issue to read

Cannot cast from aliased type to type for user defined types and include the simple example in the body of the message? Let me know if you can't /won't so I can file a new issue since this is an important bug.

paxcut avatar Sep 18 '23 21:09 paxcut

Thank you for checking the issue. I changed the title and description of the issue accordingly.

d-Rickyy-b avatar Sep 18 '23 22:09 d-Rickyy-b