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

analyze.cpp:type_requires_comptime:5127 #1862

Closed
daurnimator opened this Issue Dec 30, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@daurnimator
Copy link
Contributor

daurnimator commented Dec 30, 2018

I'd like to add methods to an opaque type. I attempted to do so by wrapping it in a struct. However am getting this failure:

bug.zig:

const c_ptr = @OpaqueType();

pub const State = struct {
    ptr: c_ptr,
};

extern var foo: c_ptr;

pub fn main() !void {
    const p = State { .ptr = foo };
}
$ zig build-exe bug.zig 
unreachable: zig/src/analyze.cpp:type_requires_comptime:5127
Aborted (core dumped)

@andrewrk andrewrk added this to the 0.4.0 milestone Jan 2, 2019

@andrewrk andrewrk added the bug label Jan 2, 2019

@andrewrk

This comment has been minimized.

Copy link
Member

andrewrk commented Jan 2, 2019

Correct behavior is error: opaque type has unknown size and therefore not allowed in struct.

Recommended pattern is to name the type after the opaque type, not the pointer. So it would be const Foo = @OpaqueType(); and then your field would be ptr: *Foo.

@daurnimator

This comment has been minimized.

Copy link
Contributor Author

daurnimator commented Mar 24, 2019

The goal of this snippet was to be able to add methods to an opaque type by wrapping it in a struct. Is there a reason that shouldn't be allowed?

@andrewrk

This comment has been minimized.

Copy link
Member

andrewrk commented Mar 24, 2019

There is a very good reason: structs must have known size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.