Skip to content

Slicing plus pointer decay segfaults the compiler #4423

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
LemonBoy opened this issue Feb 10, 2020 · 3 comments
Closed

Slicing plus pointer decay segfaults the compiler #4423

LemonBoy opened this issue Feb 10, 2020 · 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

@LemonBoy
Copy link
Contributor

LemonBoy commented Feb 10, 2020

I wonder if I'm doing something wrong or (and that's very likely) it's just another bug.

pub fn main() void {
    var buf: [16]u8 = undefined;
    const fo1: [*:0]const u8 = buf[0..:0];
}
@andrewrk
Copy link
Member

Looks like yet another result location bug. You can work around like this:

    const x = buf[0..:0];
    foo(x);

@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 10, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Feb 10, 2020
@LemonBoy LemonBoy changed the title Slicing in parameter segfaults the compiler Slicing plus pointer decay segfaults the compiler Feb 11, 2020
@LemonBoy
Copy link
Contributor Author

Looks like yet another result location bug

Indeed, the problem seems to be the cast from slice to pointer that generates a i8* result_loc instead of a slice struct one.

@mikdusan
Copy link
Member

I think master is now correct. If someone else can eyeball and confirm, I'll add the following tests:

compiler-error

export fn foo() void {
    var buf: [16]u8 = undefined;
    const p: [*:0]const u8 = buf[0..:0];
}
./bug0.zig:3:33: error: expected type '[*:0]const u8', found '*[16]u8'
    const p: [*:0]const u8 = buf[0..:0];
                                ^
./bug0.zig:3:33: note: destination pointer requires a terminating '0' sentinel
    const p: [*:0]const u8 = buf[0..:0];
                                ^

behaviour

export fn foo() void {
    var buf: [16]u8 = undefined;
    const p: [*:0]const u8 = buf[0..15 :0];
}

@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