Skip to content

Improve Version format compile error #13489

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

Conversation

nickcernis
Copy link
Contributor

@nickcernis nickcernis commented Nov 8, 2022

  • Helps users identify the only legal Version format string ({}) more quickly.
  • Better matches other compile errors like those in lib/std/zig/fmt.zig:

zig/lib/std/zig/fmt.zig

Lines 68 to 77 in a65ba6c

@compileError("expected {} or {'}, found {" ++ fmt ++ "}");
}
},
'\'' => {
if (fmt.len == 1 and fmt[0] == '\'') {
try writer.writeAll("\\'");
} else if (fmt.len == 0) {
try writer.writeByte('\'');
} else {
@compileError("expected {} or {'}, found {" ++ fmt ++ "}");

To reproduce the compile error

const std = @import("std");
const builtin = @import("builtin");

pub fn main() !void {
    std.debug.print("{s}", .{builtin.zig_version});
}
Error Before Error After
error: Unknown format string: 's' error: expected {}, found {s}

- Help users identify the only legal format string more quickly.
- Better match other compile errors like those in lib/std/zig/fmt.zig.
@Vexu
Copy link
Member

Vexu commented Nov 11, 2022

lib/std/fmt.zig has a nice function for this error that it uses everywhere invalidFmtErr. I think a nicer solution would be making it public and changing all the custom formatters in the std lib to use it.

nickcernis added a commit to nickcernis/zig that referenced this pull request Nov 11, 2022
To allow consistent use of "invalid format string" compile error
response for badly formatted format strings.

See ziglang#13489 (comment).
@nickcernis
Copy link
Contributor Author

Thanks for the suggestion, @Vexu — I opened #13526 to replace this.

@nickcernis nickcernis closed this Nov 11, 2022
Vexu pushed a commit that referenced this pull request Nov 12, 2022
… format strings (#13526)

* Export invalidFmtErr

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

See #13489 (comment).

* Replace format compile errors with invalidFmtErr

- Provides more consistent compile errors.
- Gives user info about the type of the badly formated value.

* Rename invalidFmtErr as invalidFmtError

For consistency. Zig seems to use “Error” more often than “Err”.

* std: add invalid format string checks to remaining custom formatters

* pass reference-trace to comp when building build file; fix checkobjectstep
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