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 behavior when creating a slice of another slice #10684

Closed
hronro opened this issue Jan 24, 2022 · 3 comments
Closed

Unexpected behavior when creating a slice of another slice #10684

hronro opened this issue Jan 24, 2022 · 3 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

@hronro
Copy link

hronro commented Jan 24, 2022

Zig Version

0.9.0

Steps to Reproduce

const foo = "12345678";
const bar = foo[2..7]; // "34567"
const baz = bar[1..3];

Expected Behavior

baz is "45".

Actual Behavior

baz is "34".

@hronro hronro added the bug Observed behavior contradicts documented or intended behavior label Jan 24, 2022
@squeek502
Copy link
Collaborator

squeek502 commented Jan 24, 2022

Note: changing bar to var makes it work as expected:

const foo = "12345678";
var bar = foo[2..7]; // "34567"
const baz = bar[1..3]; // "45"

@nektro
Copy link
Contributor

nektro commented Jan 24, 2022

using slices also makes it work

const foo: []const u8 = "hello world";
const bar: []const u8 = foo[2..7];
const baz: []const u8 = bar[1..3];

but the bug remains when using the default types

const foo: *const [11:0]u8 = "hello world";
const bar: *const [5]u8 = foo[2..7];
const baz: *const [2]u8 = bar[1..3];

@ifreund ifreund added the stage1 The process of building from source via WebAssembly and the C backend. label Jan 29, 2022
@Vexu Vexu added this to the 0.11.0 milestone Jan 29, 2022
@matu3ba
Copy link
Contributor

matu3ba commented Feb 21, 2022

More awkwardness:

const std = @import("std");
pub fn main() !void {
    const foo = "1234";
    const bar = foo[0..4]; // "1234"
    const s2 = bar[1..2]; // "2"
    std.debug.print("s2: {s} {s}\n", .{ s2, @TypeOf(s2) }); // outputs 1 with type [1]u8
    const s3 = bar[2..3]; // "3"
    std.debug.print("s3: {s} {s}\n", .{ s3, @TypeOf(s3) }); // outputs 1 with type [1]u8
    const s4 = bar[3..4]; // "4"
    std.debug.print("s4: {s} {s}\n", .{ s4, @TypeOf(s4) }); // outputs 1 with type [1:0]u8
    const s5 = bar[2..4]; // "3"
    std.debug.print("s5: {s} {s}\n", .{ s5, @TypeOf(s5) }); // outputs 12 with type [2:0]u8
    const s6 = bar[3..4]; // "3"
    std.debug.print("s6: {s} {s}\n", .{ s6, @TypeOf(s6) }); // outputs 1 with type [1:0]u8
}

output

!zig run test.zig
s2: 1 *const [1]u8
s3: 1 *const [1]u8
s4: 1 *const [1:0]u8
s5: 12 *const [2:0]u8
s6: 1 *const [1:0]u8

I suspect quite abit comptime slice stuff is related (and was never fixed): #8304 #6905
Probably also #8646 and others.

wooster0 added a commit to wooster0/zig that referenced this issue Dec 7, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 7, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 7, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 8, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 8, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 9, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 10, 2022
kcbanner pushed a commit to kcbanner/zig that referenced this issue Dec 10, 2022
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

7 participants