Skip to content

Type casting *T to *[1]T and then []const T gives bogus compile error in some contexts #5166

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
thejoshwolfe opened this issue Apr 25, 2020 · 0 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

@thejoshwolfe
Copy link
Contributor

I added some whitespace to make the problem more obvious:

const assert = @import("std").debug.assert;
test "asdf" {
    var x: i32 = 1234;
    assert(    @as([]const i32, @as(*[1]i32, &x))[0] == 1234);
    const z1 = @as([]const i32, @as(*[1]i32, &x)); // COMPILE ERROR HERE
    assert(z1[0] == 1234);
}
./test.zig:3:5: error: expected type '[]const i32', found '*i32'
    var x: i32 = 1234;
    ^
./test.zig:5:47: note: referenced here
    const z1 = @as([]const i32, @as(*[1]i32, &x)); // COMPILE ERROR HERE
                                              ^

There are other manifestations of this problem, such as this:

const assert = @import("std").debug.assert;
test "asdf" {
    var x: i32 = 1234;
    assert(foo(&x)[0] == 1234);
    assert(bar(&x)[0] == 1234);
}
fn foo(x: *i32) []const i32 {
    const ret = @as(*[1]i32, x);
    return ret;
}
fn bar(x: *i32) []const i32 {
    return @as(*[1]i32, x); // COMPILE ERROR HERE
}
./test.zig:12:25: error: expected type '[]const i32', found '*i32'
    return @as(*[1]i32, x); // COMPILE ERROR HERE
                        ^

using zig-linux-x86_64-0.6.0+a7a8c433d.

@thejoshwolfe thejoshwolfe added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Apr 25, 2020
@Vexu Vexu added this to the 0.7.0 milestone Apr 29, 2020
@thejoshwolfe thejoshwolfe changed the title Type casting to *[1]T and then []const T gives bogus compile error in some contexts Type casting *T to *[1]T and then []const T gives bogus compile error in some contexts Apr 29, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
@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
Vexu added a commit to Vexu/zig that referenced this issue Dec 31, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 31, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Jan 1, 2023
TUSF pushed a commit to TUSF/zig that referenced this issue May 9, 2024
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

3 participants