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

no error on invalid try before anon struct #21991

Open
travisstaloch opened this issue Nov 15, 2024 · 0 comments
Open

no error on invalid try before anon struct #21991

travisstaloch opened this issue Nov 15, 2024 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@travisstaloch
Copy link
Contributor

Zig Version

0.14.0-dev.2126+e27b4647d

Steps to Reproduce and Observed Behavior

The compiler doesn't report an error message here and these 3 tests all pass.

const std = @import("std");

test {
    const S = struct { a: u8 };
    const s: S = try .{ .a = 0 }; // try makes no sense here
    try std.testing.expectEqual(0, s.a);
}

I originally noticed this while refactoring some code and forgot to remove try from a switch statement, similar to these

test {
    const S = struct { a: u8 };
    const s: S = switch (0) {
        0 => try .{ .a = 0 }, // try makes no sense here
        else => unreachable,
    };
    try std.testing.expectEqual(0, s.a);
}

test {
    const S = struct { a: u8 };
    const s: S = if (true)
        try .{ .a = 0 } // try makes no sense here
    else
        unreachable;

    try std.testing.expectEqual(0, s.a);
}

Expected Behavior

I would expect an error message like this:

test {
    const S = struct { a: u8 };
    const s = try S{ .a = 0 };
    try std.testing.expectEqual(0, s.a);
}
$ zig test /tmp/tmp.zig
/tmp/tmp.zig:5:23: error: expected error union type, found 'tmp.test_0.S'
    const s: S = try S{ .a = 0 };
                     ~^~~~~~~~~~
/tmp/tmp.zig:4:15: note: struct declared here
    const S = struct { a: u8 };
              ^~~~~~~~~~~~~~~~
@travisstaloch travisstaloch added the bug Observed behavior contradicts documented or intended behavior label Nov 15, 2024
@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Nov 15, 2024
@Vexu Vexu added this to the 0.14.0 milestone Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

No branches or pull requests

2 participants