os: improve the unexpectedErrno function#19106
os: improve the unexpectedErrno function#19106perillo wants to merge 1 commit intoziglang:masterfrom
Conversation
lib/std/os.zig
Outdated
| pub fn unexpectedErrno(err: E) UnexpectedError { | ||
| if (unexpected_error_tracing) { | ||
| std.debug.print("unexpected errno: {d}\n", .{@intFromEnum(err)}); | ||
| std.debug.print("unexpected error: E.{s}\n", .{@tagName(err)}); |
There was a problem hiding this comment.
@tagName is checked illegal behavior for unnamed values of non-exhaustive enums, which E seems to be on most (all?) platforms.
However, I believe that the default formatting for enums (so just using {} and passing .{err} directly) already has handling for printing the name if available, and the numeric code otherwise. Perhaps we could use that?
There was a problem hiding this comment.
Thanks for the correction. I assumed it was exhaustive without checking.
I avoided using the default formatting of enums, since it prints the full type name; e.g. on linux x86_64 I got os.linux.errno.generic.E.ACCES. I think the additional information is unnecessary.
A simple solution it to copy the code from fmt when formatting a non-exhaustive enum.
There was a problem hiding this comment.
I fixed the incorrect handling of @TagName, but I had to add a new function.
Does it make sense to use @setCold(true); in both errorName and unexpectedErrno functions?
There was a problem hiding this comment.
Isn't the new function just a copy std.enums.tagName?
There was a problem hiding this comment.
Isn't the new function just a copy
std.enums.tagName?
You are right; I was unaware of the std.enums namespace. Thanks!
The code of the errorName function has been adapted from fmt.formatType. Maybe that code should be updated?
Unrelated: does @intFromEnum cache the result value? In my code I called @intFromEnum only once.
|
What's the binary size difference after this change? |
5d2f655 to
abfa99e
Compare
Using the official compiler, version 0.12.0-dev.3008+b2374c4d7 and using the source code, commit 81aa74e. Build command: size with this change: 91277752 bytes |
|
The code probably needs some tests, to ensure that an error not belonging to the |
|
I should have clarified. This change should likely be opt-in/out (off by default in ReleaseSmall) given the size increase matters for embedded targets where size is a concern. Otherwise one would have to completely avoid std.os. |
The error number or name is printed only if |
Print the error name, instead of the error number, when possible. Error numbers are platform specific.
abfa99e to
5e9cb65
Compare
|
Since you are at it, could you fix the |
Ok, thanks for the suggestion. The current filter only enables error return tracing in Debug mode, but the documentation says that it is also enabled for ReleaseSafe mode. |
@perillo This was changed recently in #18160 (heading "Error Return Tracing in ReleaseSafe Optimization Mode"), so I assume the documentation is out-of-date and should be changed. |
Yes but the user can override it, no matter which optimization mode is chosen. |
|
Since the change I proposed does not seem to conflict with this branch, I opened a separate PR for it: #19140 |
|
I'm sorry, I didn't review this in time, and now it has bitrotted. Furthermore, so many pull requests have stacked up that I can't keep up and I am therefore declaring Pull Request Bankruptcy and closing old PRs that now have conflicts with master branch. If you want to reroll, you are by all means welcome to revisit this changeset with respect to the current state of master branch, and there's a decent chance your patch will be reviewed the second time around. Either way, I'm closing this now, otherwise the PR queue will continue to grow indefinitely. |
Print the error name, instead of the error number.
Error numbers are platform specific.
I have only tested this change with Linux.