Skip to content

Confusing compile error with for loops and pointers #11343

@Srekel

Description

@Srekel

Zig Version

0.10.0-dev.1462+b48d8cce5

Steps to Reproduce

I wrote some wrong piece of code that was essentially this:

    var lol : i32 = 1;
    var lolpointer : *i32 = &lol;
    var items = [_]i32 { 4, 5, 3, 4, 0 };
    for (items) |*item| {
        lolpointer = &item;
    }

The error is on the lolpointer = &item line, but the error message is:

.\src\main.zig:14:19: error: expected type '*i32', found '*const *i32'
    for (items) |*item| {
                  ^
.\src\main.zig:14:19: note: cast discards const qualifier
    for (items) |*item| {

My code was a bit more complex so it took me a while to figure out what I had done wrong.

Expected Behavior

I would expect the compile error to point to the place where I'm doing the assignment, not the loop "header".

Actual Behavior

See above

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorerror messageThis issue points out an error message that is unhelpful and should be improved.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions