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 capture pointer to elements of const array in for loop #1726

Closed
Hejsil opened this Issue Nov 16, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@Hejsil
Copy link
Member

Hejsil commented Nov 16, 2018

test "" {
    const a = "111";
    for (a) |*c, i| // error: expected type '*u8', found '*const u8'
        @import("std").debug.assert(c.* == a[i]);
}
@thejoshwolfe

This comment has been minimized.

Copy link
Member

thejoshwolfe commented Nov 16, 2018

Why do you want *c instead of just c there? At first glance, this doesn't seem like a problem. Maybe take &c in the body of the loop if you want a *const u8, but you shouldn't be able to modify the elements of a in your example, so the error seems right to me.

@andrewrk andrewrk added this to the 0.4.0 milestone Nov 16, 2018

@andrewrk andrewrk added the bug label Nov 16, 2018

@Hejsil

This comment has been minimized.

Copy link
Member Author

Hejsil commented Nov 16, 2018

@thejoshwolfe This is just a simple example. There are plenty of reason to take const pointers over values (especially if you wanna store that pointer and avoid a copy). I'd say, either []const T should also generate this error, or this should work.

@andrewrk

This comment has been minimized.

Copy link
Member

andrewrk commented Nov 16, 2018

I believe @Hejsil found the bug while implementing another bug fix in #1725, and this is some extra effort to fix this new found bug.

The expected output from this test case is that it passes, and @typeOf(c) == *const u8.

@andrewrk

This comment has been minimized.

Copy link
Member

andrewrk commented Nov 16, 2018

I think the solution to this would clobber my copy-elision-2 branch, so unless there's a pressing need to have this solved, I'd recommend waiting until that lands.

@kristate

This comment has been minimized.

Copy link
Contributor

kristate commented Nov 18, 2018

Maybe we can create a milestone for after copy-elision-2 to separate tasks into two buckets? 4.0.1 ?

@Hejsil Hejsil referenced this issue Apr 6, 2019

Merged

fixed #1726 #2200

andrewrk added a commit that referenced this issue Apr 6, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.