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

Unexpected error when casting to a type in a different pacakge #88

Open
flysand7 opened this issue Dec 18, 2023 · 2 comments
Open

Unexpected error when casting to a type in a different pacakge #88

flysand7 opened this issue Dec 18, 2023 · 2 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@flysand7
Copy link

iterate_random :: () -> Iterator(i32) {
    return .{
        data = alloc.on_heap(random.Random.make()),
        next = (data: rawptr) -> (i32, bool) {
            random := cast(^random.Random) data;
            return cast(i32) random->int(), true;
        },
    };
}

Getting this error:

(/home/bumbread/test/test.onyx:20,36) Trying to resolve type of source expression.
 20 |             random := cast(^random.Random) data;
                                         ^~~~~~ 
(/home/bumbread/test/test.onyx:20,23) Trying to resolve destination type for cast.
 20 |             random := cast(^random.Random) data;
                            ^~~~ 
@brendanfh brendanfh self-assigned this Dec 18, 2023
@brendanfh brendanfh added the bug Something isn't working label Dec 18, 2023
@brendanfh brendanfh added this to the v0.1.9 milestone Dec 18, 2023
@brendanfh
Copy link
Collaborator

Looks like this is because of the random variable being declared on the same line. The := syntax is really a shorthand for this:

main :: () {
    random: #auto;
    random = cast(^random.Random) data;
}

So what's happening is the compiler is trying to lookup Random inside the random variable, which does not have a type yet. I believe if you call the variable something else, it will work successfully. Or, you can use explicitly say the parameter is a pointer to random.Random, then you can remove the cast all together.

iterate_random :: () -> Iterator(i32) {
    return .{
        data = alloc.on_heap(random.Random.make()),
        next = (state: ^random.Random) -> (i32, bool) {
            return cast(i32) state->int(), true;
        },
    };
}

This is definitely something that should be reconsidered because I have run into a similar issue in the past.

@flysand7
Copy link
Author

Maybe it might be worth it to warn/error on direct shadowing.

@onyx-lang onyx-lang modified the milestones: v0.1.9, v0.1.10 Feb 21, 2024
@brendanfh brendanfh modified the milestones: v0.1.10, v0.1.11 Apr 1, 2024
@brendanfh brendanfh modified the milestones: v0.1.11, v0.1.12 May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants