Unclear compiler messages

Unclear compiler messages

Overall the Zig compiler is very good at giving you clear and concise error messages. However, there are some cases where the error messages are not as clear as they could be. Here are a few examples of unclear compiler messages (at least for beginners) that you might encounter.

Trying to print without the incorrect struct syntax

We are going to start with the most basic one, everyone doing Zig did at least one time

  std.debug.print("Hello I am {} years old", 12);
  /home/tetratrux/.zvm/0.11.0/lib/std/fmt.zig:87:9: error: expected tuple or struct argument, found comptime_int
          @compileError("expected tuple or struct argument, found " ++ @typeName(ArgsType));

Is the type of error you should get, from reading that we cant even know from where the problem is in our code, no line number, no stacktrace, nothing.

But if you read the message carefully and you know that print second argument except a struct with the .{} syntax, you realize that you passed a value with the wrong type.

So when you get that type of error message make sure that you are printing your message correctly with the second argument that should be a struct with the .{} syntax.

  std.debug.print("Hello I am {} years old", .{12});

This is an ongoing issue by the time I am writing this in 0.12.0

If you have trouble understanding how to correctly format you string, I highly suggest you to refer to this piece of documentation, that is not really is to find. This documentation basically gives you those informations that are wrote here exactly the same way they are in the documentation:

TODO Est ce que c'est bon comme ca ou c'est du plagiat sachant que je precise juste au dessus d ou je prends ?

  - x and X: output numeric value in hexadecimal notation
  - s:
  - for pointer-to-many and C pointers of u8, print as a C-string using zero-termination
  - for slices of u8, print the entire slice as a string without zero-termination
  - e: output floating point value in scientific notation
  - d: output numeric value in decimal notation
  - b: output integer value in binary notation
  - o: output integer value in octal notation
  - c: output integer as an ASCII character. Integer type must have 8 bits at max.
  - u: output integer as an UTF-8 sequence. Integer type must have 21 bits at max.
  - ?: output optional value as either the unwrapped value, or null; may be followed by a format specifier for the underlying value.
  - !: output error union value as either the unwrapped value, or the formatted error value; may be followed by a format specifier for the underlying value.
  - *: output the address of the value instead of the value itself.
  - any: output a value of any type using its default format.

Assigning to an arrray of array of u8

This is a very common thing to, if you basically want to make an "array of strings", the way you would want to do it is that way:

  const app_name: []const u8 = "/bin/i3-msg";
  var params: []const []const u8 = .{ app_name, "-t", "get_workspaces" };

But this is going to give the following error:

  error: type '[]const []const u8' does not support array initialization syntax

With the following hint:

  note: inferred array length is specified with an underscore: '[_][]const u8'

So you might try to follow the hint, but the hint here is not that good all, and give you false directions:

  var params: []const []const u8 = [_][]const u8{ app_name, "-t", "get_workspaces};

Ending up with the following error:

  error: array literal requires address-of operator (&) to coerce to slice type '[]const []const u8'

And then you can finally work you way around by coming back to the first syntax and adding the & operator like so:

  var params: []const []const u8 = &.{ app_name, "-t", "get_workspaces" };

This is because that is the way you write slice literals according to this discord discussion. There were no documentions related to that on the official website at the time of writing.