Skip to content

literal number from a conditional expression is not being coerced to assignee type #5557

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

Closed
JPGygax68 opened this issue Jun 7, 2020 · 7 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@JPGygax68
Copy link

The following assignment

atlas.TexWidth = if (surface_sqrt >= 4096*0.7) 4096
    else if (surface_sqrt >= 2048*0.7) 2048
    else if (surface_sqrt >= 1024*0.7) 1024
    else 512;

yields the error

.\src\imdrawlist.zig:747:18: error: values of type 'comptime_int' must be comptime known
            else if (surface_sqrt >= 1024*0.7) 1024

To get rid of the error, I had to use @as(u32, ...) on all 4 constants.

It seems to me that type coercion should happen automatically here, since the required type is known.

@alexnask
Copy link
Contributor

alexnask commented Jun 7, 2020

I would personally have expected for peer type resolution to kick in here and only require a single @as in any of the branches.

@joachimschmidt557
Copy link
Contributor

The peer type resolution kicks in when the last else branch has the @as: https://zig.godbolt.org/z/jt6kEB

When other branches have the @as, this does not seem to work.

@alexnask
Copy link
Contributor

alexnask commented Jun 7, 2020

Huh, interesting, I guess this makes some sense as it is the matching branch to the root if expression.

@JPGygax68
Copy link
Author

This explains the behavior, but is there a reason why coercion can't or shouldn't happen without an @as() ?

@ghost
Copy link

ghost commented Jun 8, 2020

I think the problem is that result location stuff doesn't work through nested branches. if a else if b else c is technically nested (it's the same as if a else (if b else c)).

Result location works on a one-level-deep conditional like x = if (a) y else z.

Your example will work with an @as cast on one of the last two values, since that is the innermost if/else expression. Peer type resolution will propagate that type outwards.

I think the reason result location doesn't work through nested branches is just that no one has implemented it yet, and i hear that part of the compiler codebase is gnarly.

Similar issues: #3750, #4255, #4629

@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Oct 4, 2020
@andrewrk andrewrk added this to the 0.8.0 milestone Oct 4, 2020
@andrewrk
Copy link
Member

andrewrk commented Oct 4, 2020

@dbandstra is correct. The current plan is to implement this correctly in stage2 only and then delete the stage1 codebase.

@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@andrewrk
Copy link
Member

duplicate of #4629 and fixed in master branch.

@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale Feb 20, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.11.0 Feb 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

4 participants