Skip to content

eradicate error.Unexpected from the standard library #1396

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
andrewrk opened this issue Aug 21, 2018 · 3 comments
Closed

eradicate error.Unexpected from the standard library #1396

andrewrk opened this issue Aug 21, 2018 · 3 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@andrewrk
Copy link
Member

At least where possible, if we can determine that all possible errors are accounted for, remove error.Unexpected from the error set. This issue is deceptively complicated and may involve reading kernel code or wine source code. There may be an argument to keep the error for some syscalls where the system reserves the right to add new error codes.

@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library. labels Aug 21, 2018
@andrewrk andrewrk added this to the 1.0.0 milestone Aug 21, 2018
@shawnl
Copy link
Contributor

shawnl commented Sep 20, 2018

I think this is a bad idea. We should not write such fragile code. The default switch case costs us little as it is peeled off into a branch. (Both gcc and llvm have done this for a few years)

@andrewrk andrewrk added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. and removed enhancement Solving this issue will likely involve adding new logic or components to the codebase. labels Sep 20, 2018
@shawnl
Copy link
Contributor

shawnl commented Apr 9, 2019

I am worried this will be rejected immediately, however I have an alternate proposal.

We need to preserve the native error codes in error messages. They are not a bad idea (as long as you don't use errno), and are important for debugging.

We have no control over these error codes. During the life-time of a Zig binary they may change, and a Zig binary must not act unpredictably when they do, nor should that binary obscure what the error was when it happens.

Because of this we should reserve the negative values of the internal error union for system errors (on applicable platforms).

For an idea of the syntax:

error.windows.FILE_NOT_FOUND
error.posix.ENOMEM

Only one of these system namespaces can be active in any one compile.

So there is a downside that we can no longer unify errors? NO! Instead of mapping system errors to zig errors, we can map zig errors to system errors. Zig provides no guarantees of what value an error is, so we can just map error.OutOfMemory to both error.posix.ENOMEM and error.windows.NOT_ENOUGH_MEMORY. This way most zig applications never have to see these errors, but they are there if they do need them, at every level and never abstracted out.

Should we provide strerror?

@daurnimator
Copy link
Contributor

Things like seccomp bpf can always add new error codes. Not to mention new kernels coming out with new return values (or old kernels returning ENOTSUP).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

3 participants