Skip to content

support two structs with recursive optional references #2746

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
CurtisFenner opened this issue Jun 25, 2019 · 7 comments
Closed

support two structs with recursive optional references #2746

CurtisFenner opened this issue Jun 25, 2019 · 7 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@CurtisFenner
Copy link

The following causes zig test to exit unsuccessfully without printing any output on Windows 10 x64, zig 0.4.0+fcc0728:

const Alpha = struct {
    beta: Beta,
};

const Beta = struct {
    Alpha: *?Alpha,
};

test "crashes" {
    const a = Alpha;
}

The crash also happens if the second field declaration is instead Alpha: ?Alpha (without the pointer indirection), which is a slightly smaller test case. But I also wanted to demonstrate the crash on what I believe is a correct program

@CurtisFenner
Copy link
Author

For reference, here's a slightly-less reduced piece of the code I was trying to write when I hit this:

pub const Block = struct {
    statements: []const Statement,
};

pub const Statement = union(enum) {
    DoSt: *const DoSt,
    IfSt: *const IfSt,
};

pub const DoSt = struct {
    expression: Expression,
};

pub const IfSt = struct {
    condition: Expression,
    then_body: Block,
    else_clause: ?ElseClause,
};

pub const ElseClause = struct {
    body: Block,
};

pub const Expression = struct {};

test "use a block" {
    var block = Block.foo;
}

This doesn't do anything dubious like a pointer to an optional.

@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label Jun 27, 2019
@andrewrk andrewrk added this to the 0.5.0 milestone Jun 27, 2019
@daurnimator
Copy link
Contributor

Related to #1735

@andrewrk
Copy link
Member

After #2174, this is now a compile error instead of a crash:

/home/andy/dev/zig/build/test.zig:1:15: error: struct 'Alpha' depends on itself
const Alpha = struct {
              ^
/home/andy/dev/zig/build/test.zig:6:13: note: referenced here
    Alpha: *?Alpha,
            ^
/home/andy/dev/zig/build/test.zig:6:5: note: referenced here
    Alpha: *?Alpha,
    ^

So this issue is now to improve lazy values a bit more so that this case is handled.

@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. stage1 The process of building from source via WebAssembly and the C backend. and removed bug Observed behavior contradicts documented or intended behavior labels Aug 28, 2019
@andrewrk andrewrk changed the title zig test crashes on two structs with recursive optional references support two structs with recursive optional references Aug 28, 2019
@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Aug 28, 2019
@CurtisFenner
Copy link
Author

I'm a bit confused by the current state of this.

This works fine:

const Bin = struct {
    value: usize,
    left: ?*Bin,
    right: ?*Bin,
};

but this doesn't:

const Bin = struct {
    value: usize,
    left: ?*Bin,
    right: [1]?*Bin,
};

Is this an oversight, or is there actually something different between these two versions?

@courtc
Copy link

courtc commented Sep 7, 2019

There seems to be a remaining issue related to arrays of self referencing pointers, optional or not.
Seems directly related, and likely the issue @CurtisFenner is referring to.

const Node = struct {
    links: [2]?*@This(),
};
.../test/sr.zig:2:18: error: struct 'Node' depends on itself
    const Node = struct {
                 ^
.../test/sr.zig:3:19: note: referenced here
        links: [2]?*@This(),
               ^

@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior and removed enhancement Solving this issue will likely involve adding new logic or components to the codebase. labels Jan 8, 2020
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Jan 8, 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
@zackpete
Copy link

I ran into a similar issue that causes a segfault on v0.9.1

  _ = struct {
      const n = 1;

      const Node = struct {
          children: *[n]Node,
      };

      root: *Node,
  };

I get a compiler error when I make children optional

@nektro
Copy link
Contributor

nektro commented Sep 16, 2022

issue snippet passes on master now

Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 29, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 29, 2022
TUSF pushed a commit to TUSF/zig that referenced this issue May 9, 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 stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

6 participants