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

while(!?T catch {}) |payload| {}: Type resolution bug for |*payload| #13008

Open
rened opened this issue Sep 29, 2022 · 3 comments
Open

while(!?T catch {}) |payload| {}: Type resolution bug for |*payload| #13008

rened opened this issue Sep 29, 2022 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@rened
Copy link

rened commented Sep 29, 2022

Zig Version

0.10.0-dev.4197+9a2f17f9f

Steps to Reproduce

Put the following in test.zig and run zig test test.zig:

const std = @import("std");

pub fn Reader(comptime T: type) type {
    return struct {
        buf: std.ArrayList(u8) = undefined,
        const Self = @This();

        pub fn init(allocator: std.mem.Allocator) Self {
            return Self{
                .buf = std.ArrayList(u8).init(allocator),
            };
        }

        pub fn next(self: *Self) !?T {
            if (self.buf.items.len >= 4) {
                var stream = std.io.fixedBufferStream(self.buf.items);
                const len: u32 = try stream.reader().readInt(u32, .little);
                if (self.buf.items.len >= len + 4) {
                    return @as(usize, 1);
                } else return null;
            } else return null;
        }
    };
}

test "iterator" {
    const allocator = std.testing.allocator_instance.allocator();
    var reader = Reader(usize).init(allocator);
    while (reader.next() catch label: {
        break :label null;
    }) |*x| { // fails, |x| works
        _ = x;
    }
}

Expected Behavior

The test should pass.

Actual Behavior

It fails for stage2 with

test.zig:29:26: error: incompatible types: '*const ?usize' and '*const @TypeOf(null)'
    while (reader.next() catch label: {
           ~~~~~~~~~~~~~~^~~~~

It passes for zig test test.zig -fstage1 and also for 0.8x and 0.9.x.

It also passes for stage2 when omitting the pointer to the payload. It seems that the type resolution looks at the loop payload type instead of the type used in the while condition.

@rened rened added the bug Observed behavior contradicts documented or intended behavior label Sep 29, 2022
@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Sep 29, 2022
@Vexu Vexu added this to the 0.10.0 milestone Sep 29, 2022
@Vexu
Copy link
Member

Vexu commented Sep 29, 2022

Related to #11969, #11812, etc.

@andrewrk andrewrk modified the milestones: 0.10.0, 0.10.1 Oct 12, 2022
@andrewrk andrewrk modified the milestones: 0.10.1, 0.11.0 Jan 10, 2023
@andrewrk andrewrk modified the milestones: 0.11.0, 0.11.1 Jul 20, 2023
@BratishkaErik
Copy link
Contributor

Can reproduce for 0.11.0-dev.4403+e84cda0eb:

test.zig:29:26: error: incompatible types: '*const ?usize' and '*const @TypeOf(null)'
    while (reader.next() catch label: {
           ~~~~~~~~~~~~~~^~~~~
test.zig:29:26: note: type '*const ?usize' here
test.zig:29:39: note: type '*const @TypeOf(null)' here
    while (reader.next() catch label: {
                               ~~~~~~~^

@andrewrk andrewrk modified the milestones: 0.11.1, 0.12.0, 0.13.0 Jan 29, 2024
@rened
Copy link
Author

rened commented Nov 27, 2024

Updated code for 0.14, issue still present in 0.14.0-dev.2183+ee9f00d67

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

4 participants