Skip to content

anonymous-struct literal unable to be passed for anonymous-struct-typed arg #4305

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

Closed
metaleap opened this issue Jan 28, 2020 · 2 comments
Closed
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@metaleap
Copy link
Contributor

metaleap commented Jan 28, 2020

A minimal repro main.zig, currently rejected with ./main.zig:4:26: error: expected type 'struct:11:32', found 'struct:11:32':

const std = @import("std");

pub fn main() void {
    var sess = Protocol(.{
        .TIn = MyIn,
        .TOut = MyOut,
    }){};
    sess.serve(); // forever-loop
}

pub fn Protocol(comptime spec: struct {
    TIn: type,
    TOut: type,
}) type {
    return struct {
        subscribers: [@memberCount(spec.TIn)]?usize = ([_]?usize{null}) ** @memberCount(spec.TIn),
        pub fn on(self: *@This()) void {} // subscription
        pub fn serve(self: *@This()) void {} // transport-dealing forever-loop
    };
}

pub const MyIn = union(enum) {
    foo: fn (isize) bool,
    bar: fn (bool) isize,
};

pub const MyOut = union(enum) {
    baz: fn ([]u8) void,
};

The workaround is to extract the anonymous-struct of arg spec into a separated named definition ie. const Spec = struct {....

As a point of interest, if all fields in the anonymous-struct decl get default values (eg. TIn: type = union(enum) {},) and an empty .{} anon-struct-literal is passed instead, same compiler message.

@andrewrk andrewrk added this to the 0.6.0 milestone Jan 28, 2020
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Jan 28, 2020
@andrewrk andrewrk changed the title Proposal: allow anonymous-struct literal to be passed for anonymous-struct-typed arg (when structurally-identical / -compatible) anonymous-struct literal unable to be passed for anonymous-struct-typed arg Jan 28, 2020
@andrewrk
Copy link
Member

I believe this is a bug, which hopefully has a straightforward fix.

@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Mar 31, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@Vexu
Copy link
Member

Vexu commented Feb 18, 2022

Same root cause as #3707.

@Vexu Vexu closed this as completed Feb 18, 2022
@Vexu Vexu modified the milestones: 0.11.0, 0.10.0 Feb 18, 2022
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 stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

3 participants