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

Assertion fails when pointer casting to `*void` #960

Closed
Hejsil opened this Issue Apr 28, 2018 · 5 comments

Comments

Projects
None yet
3 participants
@Hejsil
Member

Hejsil commented Apr 28, 2018

test "" {
    var i = usize(0);
    var v = @ptrCast(*void, &i);
}
zig: zig/src/analyze.cpp:449: TypeTableEntry* get_pointer_to_type_extra(CodeGen*, TypeTableEntry*, bool, bool, uint32_t, uint32_t, uint32_t): Assertion `byte_alignment == 0' failed.

@Hejsil Hejsil added the bug label Apr 28, 2018

@andrewrk andrewrk added this to the 0.3.0 milestone Apr 28, 2018

@andrewrk andrewrk changed the title from Assertion fails when pointer casting to `&void` to Assertion fails when pointer casting to `*void` Sep 3, 2018

@andrewrk

This comment has been minimized.

Member

andrewrk commented Sep 3, 2018

I'm not actually sure what this should do. Should it be a compile error? No-op?

The line that the assertion failure crashed on is asserting that a pointer to void (or anything with 0 size) has no alignment. But since we cast from a *usize the alignment is 8, so the compiler panics.

@andrewrk andrewrk closed this in 95636c7 Sep 3, 2018

@andrewrk

This comment has been minimized.

Member

andrewrk commented Sep 3, 2018

Nevermind, I thought through it and I think it makes sense to work. See above commit for more details.

@thejoshwolfe

This comment has been minimized.

Member

thejoshwolfe commented Sep 3, 2018

a single-item pointer to a 0-size type is also a 0-size type. this should be a compile error at the @ptrCast() for an impossible cast.

@thejoshwolfe

This comment has been minimized.

Member

thejoshwolfe commented Sep 3, 2018

Do you think casting from *void to *usize should work? if so, the pointer would always have an undefined value. that seems wrong.

@andrewrk

This comment has been minimized.

Member

andrewrk commented Sep 3, 2018

No I don't think it should work from *void to *usize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment