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

Can't pass non-nullable pointer to nullable #731

Closed
tjpalmer opened this issue Jan 31, 2018 · 9 comments
Closed

Can't pass non-nullable pointer to nullable #731

tjpalmer opened this issue Jan 31, 2018 · 9 comments
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@tjpalmer
Copy link

Seems like this should be allowed as implicitly compatible (of course, not the other way around):

D:\Workspace\beast\src\main.zig:120:34: error: expected type '?&const ?&const u8', found '&const &const u8'
    c.glShaderSource(shader, 1, &sources, &sizes);
@tjpalmer
Copy link
Author

Maybe it's the nested pointer risk in case the callee wanted to change things? But they are const in this case, so seems like it should be presumed safe.

@tjpalmer
Copy link
Author

Moved some code to a separate file, and then this error popped up, too:

D:\Workspace\beast\src\main.zig:29:29: error: expected type '?&struct_SDL_Window', found '&struct_SDL_Window'
  c.SDL_GL_SwapWindow(window.window);
                            ^
D:\Workspace\beast\src\main.zig:46:39: error: expected type '?&struct_SDL_Window', found '&struct_SDL_Window'
            c.SDL_GL_SwapWindow(window.window);

It wasn't previously causing this error, so something subtle might be going on, and it might be related to the original error report above, since it's about non-null to null, again. And no nested pointers here, so that should be an issue in this case either.

@tjpalmer tjpalmer changed the title Nullable pointer incompatible with non-nullable Can't pass non-nullable pointer to nullable Jan 31, 2018
@andrewrk andrewrk added this to the 0.2.0 milestone Jan 31, 2018
@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label Jan 31, 2018
@andrewrk
Copy link
Member

I'll get these fixed soon. In the meantime, a workaround is using @ptrCast. It should work even with nullable pointers.

@tjpalmer
Copy link
Author

Thanks much. I wasn't fully sure which cast I ought to use. Interestingly enough, this also worked and allowed the later use of &sources without casting:

    const sources: ?&const u8 = &source[0];

And for the second case, switching which file the call was in also worked.

@andrewrk
Copy link
Member

Is your code open source by any chance? So that I could try to repro these bugs?

@tjpalmer
Copy link
Author

tjpalmer commented Feb 1, 2018

It's in this repo: https://github.com/tjpalmer/beast

The second case above was happening when the code was in main.

@andrewrk
Copy link
Member

andrewrk commented Feb 1, 2018

Thanks! I'll have a look. Unrelated to this bug, one thing I would recommend is to have only a single @cImport in your entire program. The reason for having multiple is advanced, for situations where, in C, you would want to compile different .o files with different preprocessor defines, or something like this.

@tjpalmer
Copy link
Author

tjpalmer commented Feb 1, 2018

I had them separate so I could more easily relate dependencies and encapsulate. (This code is also early and on limited time, so don't presume everything there is on purpose, though.) So I understand better, what are the reasons for a single @cImport?

@tjpalmer tjpalmer closed this as completed Feb 1, 2018
@tjpalmer tjpalmer reopened this Feb 1, 2018
@andrewrk
Copy link
Member

andrewrk commented Feb 1, 2018

I added to the docs to answer this: http://ziglang.org/documentation/master/#cImport

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
Projects
None yet
Development

No branches or pull requests

2 participants