Skip to content

Incorrect Output for 15-errors-switch.zig on https://ziglang.org/learn/overview/ #425

Open
@dmitry-j-mikhin

Description

https://ziglang.org/learn/overview/:
image

Issue Summary

The current test code snippet, when executed using zig build-exe, displays a confusing error message due to a mismatch in usage. This is because zig build-exe expects an entry point with a main function, which is not present in the test file.

Current Output

Using zig build-exe on 15-errors-switch.zig results in the following error, which is uninformative for new users:

$ zig build-exe 15-errors-switch.zig
/home/ci/deps/zig-linux-x86_64-0.13.0/lib/std/start.zig:509:45: error: root struct of file '15-errors-switch' has no member named 'main'
    switch (@typeInfo(@typeInfo(@TypeOf(root.main)).Fn.return_type.?)) {
                                        ~~~~^~~~~
/home/ci/actions-runner-website/_work/www.ziglang.org/www.ziglang.org/assets/zig-code/features/15-errors-switch.zig:1:1: note: struct declared here
const std = @import("std");
^~~~~
/home/ci/deps/zig-linux-x86_64-0.13.0/lib/std/start.zig:482:20: note: called from here
    return callMain();
           ~~~~~~~~^~
/home/ci/deps/zig-linux-x86_64-0.13.0/lib/std/start.zig:438:36: note: called from here
    std.posix.exit(callMainWithArgs(argc, argv, envp));
                   ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
referenced by:
    _start: /home/ci/deps/zig-linux-x86_64-0.13.0/lib/std/start.zig:351:40
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

Suggested Solution

Running the file as a test with zig test provides a much clearer, relevant output. This clarifies the specific error being tested in 15-errors-switch.zig and would be more helpful for learners.

Expected Output

Using zig test 15-errors-switch.zig gives meaningful feedback regarding the issue:

$ zig test 15-errors-switch.zig
15-errors-switch.zig:4:40: error: switch must handle all possibilities
    _ = parseInt("hi", 10) catch |err| switch (err) {};
                                       ^~~~~~~~~~~~~~~
test.zig:4:40: note: unhandled error value: 'error.InvalidCharacter'
test.zig:4:40: note: unhandled error value: 'error.DigitExceedsRadix'
test.zig:4:40: note: unhandled error value: 'error.Overflow'

Resolution Suggestion

Update the documentation and code snippets on the overview page to specify using zig test instead of zig build-exe for 15-errors-switch.zig. This will ensure that learners receive relevant error information and improve clarity on Zig's error handling capabilities.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions