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

Make invalidFmtError public and use in place of compileErrors for bad format strings #13526

Merged
merged 5 commits into from
Nov 12, 2022

Conversation

nickcernis
Copy link
Contributor

@nickcernis nickcernis commented Nov 11, 2022

  • Renames invalidFmtErr() to invalidFmtError() (for consistency; Zig seems to use Error more than Err).
  • Makes invalidFmtError public, as suggested by @Vexu in Improve Version format compile error #13489.
  • Uses invalidFmtError in place of @compileError when bailing due to bad format strings.

Goals are to:

  • Reduce use of custom strings to give more consistent format errors.
  • Give users info about the type of the value they formatted incorrectly to help them find and fix it.
  • Make invalidFmtError the standard way of reporting bad format strings in Zig and for users.

Example

const std = @import("std");
const builtin = @import("builtin");
const Guid = std.os.uefi.Guid;

pub fn main() !void {
    std.debug.print("{s}", .{builtin.zig_version});

    const guid = Guid{
        .time_low = 0x09576e91,
        .time_mid = 0x6d3f,
        .time_high_and_version = 0x11d2,
        .clock_seq_high_and_reserved = 0x8e,
        .clock_seq_low = 0x39,
        .node = [_]u8{ 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b },
    };

    std.debug.print("{badformat}", .{guid});
}
Error before Error after
Unknown format string: 's' invalid format string 's' for type 'SemanticVersion'
Unknown format character: 'badformat' invalid format string 'badformat' for type 'os.uefi.Guid'

To allow consistent use of "invalid format string" compile error
response for badly formatted format strings.

See ziglang#13489 (comment).
- Provides more consistent compile errors.
- Gives user info about the type of the badly formated value.
For consistency. Zig seems to use “Error” more often than “Err”.
Copy link
Member

@Vexu Vexu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is nice improvement to make using custom formatters more consistent. I went ahead and added these checks to the remaining custom formatters in std.

@Vexu Vexu merged commit 8a58185 into ziglang:master Nov 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants