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

Unexpected comptime type coercion error #22597

Closed
brianferri opened this issue Jan 24, 2025 · 3 comments
Closed

Unexpected comptime type coercion error #22597

brianferri opened this issue Jan 24, 2025 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@brianferri
Copy link

Zig Version

0.14.0-dev.2922+f77e1b862

Steps to Reproduce and Observed Behavior

0.13.0 Godbolt Working
0.14.0 Godbolt Erroring

  • Set up a Zig project and compile the following minimal repro using the specified version:
const std = @import("std");

const L = struct { a: u1 };

const S = struct {
    items: []const L = &.{},

    pub fn a(comptime self: *S) *S {
        self.items = self.items ++ .{.{ .a = 0 }};
        return self;
    }
};

pub fn main() !void {
    comptime {
        var s = S{};
        _ = s.a();
    }
}
  • Notice the error:
run
└─ run ZigUnexpectedTypeCoercionError
   └─ zig build-exe ZigUnexpectedTypeCoercionError Debug native 1 errors
src/main.zig:9:39: error: expected type 'main.L', found 'main.S.a__struct_2486'
        self.items = self.items ++ .{.{ .a = 0 }};
                                     ~^~~~~~~~~~
src/main.zig:9:39: note: struct declared here
src/main.zig:3:11: note: struct declared here
const L = struct { a: u1 };
          ^~~~~~~~~~~~~~~~
src/main.zig:17:16: note: called from here
        _ = s.a();
            ~~~^~

Expected Behavior

As it was a feature that worked in 0.13.0 I'd expect this to work the same way in 0.14.0

@brianferri brianferri added the bug Observed behavior contradicts documented or intended behavior label Jan 24, 2025
@MatthiasPortzel
Copy link

This is an intentional change; see the following proposal and PR.

#16865
#21817

As it was a feature that worked in 0.13.0 I'd expect this to work the same way in 0.14.0

There are breaking changes between pre-1.0 Zig versions.

@brianferri
Copy link
Author

I see how the Proposal/PR is related, regarding mostly this comment
However I fail to understand how or why it should be considered an anonymous struct when the type is effectively known (self.items: []const L), in which case it wouldn't be so much a regression but rather a bug in the removal of anonymous struct types

@mlugg
Copy link
Member

mlugg commented Jan 25, 2025

The type isn't known through RLS because ++ is quite loose on types (the LHS and RHS can both be slices, or arrays, or tuples, and you can even mix and match). So, unfortunately, you do currently need to specify the inner type to make this work.

@mlugg mlugg closed this as not planned Won't fix, can't repro, duplicate, stale Jan 25, 2025
@brianferri brianferri changed the title Unexpected comptime type coercion error [Regression] Unexpected comptime type coercion error Jan 25, 2025
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
Projects
None yet
Development

No branches or pull requests

3 participants