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

Cannot cast from aliased type to type for user defined types #165

Open
d-Rickyy-b opened this issue Sep 17, 2023 · 5 comments
Open

Cannot cast from aliased type to type for user defined types #165

d-Rickyy-b opened this issue Sep 17, 2023 · 5 comments

Comments

@d-Rickyy-b
Copy link

d-Rickyy-b commented Sep 17, 2023

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.

@paxcut
Copy link
Contributor

paxcut commented Sep 17, 2023

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.

@d-Rickyy-b
Copy link
Author

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.

} [[sealed, format("type::impl::format_guid")]];

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

@paxcut
Copy link
Contributor

paxcut commented Sep 17, 2023

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
Copy link
Contributor

paxcut commented Sep 18, 2023

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.

@d-Rickyy-b d-Rickyy-b changed the title Issue with type::UUID - [E0004]: Type error. Cannot cast from type 'type::UUID' to type 'type::GUID' Cannot cast from aliased type to type for user defined types Sep 18, 2023
@d-Rickyy-b
Copy link
Author

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

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