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

compiler: shorten the name of inferred error unions #19425

Closed
wants to merge 1 commit into from

Conversation

nektro
Copy link
Contributor

@nektro nektro commented Mar 24, 2024

consider the following Zig code:

const std = @import("std");

pub fn main() !void {
    var x = foo("hello");
    x.len -= 1;

    std.debug.print("{d}\n", .{x});
}

fn foo(a: []const u8) ![]const u8 {
    return a;
}

under status quo you might get an error like so:

test2.zig:5:6: error: error union type '@typeInfo(@typeInfo(@TypeOf(test2.foo)).Fn.return_type.?).ErrorUnion.error_set![]const u8' does not support field access
    x.len -= 1;
    ~^~~~
test2.zig:5:6: note: consider using 'try', 'catch', or 'if'

this, while technically accurate, is extremely verbose and not practically useful in most cases (it doesnt even fit in the default width of the github view!). many advanced users see it as noise and beginners get overwhelmed while they are still grasping Zig's error system. alternatively, this patch reduces it to the following.

test2.zig:5:6: error: error union type '![]const u8' does not support field access
    x.len -= 1;
    ~^~~~
test2.zig:5:6: note: consider using 'try', 'catch', or 'if'

now the "error union type does not support field access" text is much more accessible and the ! is retained to match the return types users see in functions that produce this message.

additionally added a test to ensure error messages with functions that have an explicit error set are retained.

further work would add a note where x in this example is defined but i decided to save that for a subsequent pr.

@mlugg
Copy link
Member

mlugg commented Mar 24, 2024

The idea of this isn't terrible (although I don't personally agree with it), but the implementation is fundamentally flawed. This could result in nonsensical errors like expected type '!void', found '!void' and expected type '', found 'u32'.

@VisenDev
Copy link

I would support something to this effect as user friendly error messages are very helpful, especially for beginners. One of the things rust gets so much applause for are its user friendly error messages. I would love to see zig going down this user friendly path more

@silversquirl
Copy link
Contributor

I like this idea in concept but I think it needs more discussion and should be a proposal.

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.

5 participants