Skip to content

Missing compile error for reassigning parameter #4350

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
jayschwa opened this issue Feb 1, 2020 · 4 comments · Fixed by #4975
Closed

Missing compile error for reassigning parameter #4350

jayschwa opened this issue Feb 1, 2020 · 4 comments · Fixed by #4975
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@jayschwa
Copy link
Contributor

jayschwa commented Feb 1, 2020

Possibly related to #788.

I have a piece of code that processes smaller sequential slices of a larger slice. Rather than getting a slice of length zero at the end, I get a segmentation fault.

const std = @import("std");

const CHUNK_LEN = 2;

fn slice_to_zero(input: []const u8) void {
    while (input.len > 0) {
        const take = std.math.min(input.len, CHUNK_LEN);
        _ = input[0..take]; // Imagine this slice chunk is used for something
        input = input[take..];
    }
}

test "slicing down to zero length" {
    slice_to_zero("");
    slice_to_zero("foo");
}
@LemonBoy
Copy link
Contributor

LemonBoy commented Feb 1, 2020

input = input[take..];

This is illegal as parameters are const by default => #4015

_ = input[0..take];

This is a long-standing issue that affects many builtins (and also the slice operator), when the destionation is _ the codegen gets a nullptr for the result_loc and so it segfaults. I'm pretty sure there are a few tickets open about that.

@jayschwa
Copy link
Contributor Author

jayschwa commented Feb 1, 2020

It looks like my code works if I create a new slice variable from the const slice parameter. Lack of compiler error was allowing me to footgun. Thanks.

@jayschwa jayschwa closed this as completed Feb 1, 2020
@andrewrk
Copy link
Member

andrewrk commented Feb 1, 2020

I wasnt aware of this missing compile error, so I'm going to keep this open to track if that's ok

@andrewrk andrewrk reopened this Feb 1, 2020
@andrewrk andrewrk changed the title Allow zero length slices Missing compile error for reassigning parameter Feb 1, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Feb 1, 2020
@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 Feb 1, 2020
@ghost
Copy link

ghost commented Feb 1, 2020

Linking #3800 as well

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

Successfully merging a pull request may close this issue.

3 participants