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

Function not visible inside itself through generic struct with usingnamespace #17872

Open
LordMZTE opened this issue Nov 5, 2023 · 3 comments · May be fixed by #19786
Open

Function not visible inside itself through generic struct with usingnamespace #17872

LordMZTE opened this issue Nov 5, 2023 · 3 comments · May be fixed by #19786
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@LordMZTE
Copy link
Contributor

LordMZTE commented Nov 5, 2023

I apologize in advance for the title of this issue.

Zig Version

0.12.0-dev.1396+f6de3ec96

Steps to Reproduce and Observed Behavior

  1. Create a file bug.zig:
const B = struct {
    pub fn func() void {
        @compileLog(@typeInfo(C).Struct.decls); // .{ .{ .name = "func" } }
        @compileLog(@hasDecl(C, "func")); // false
        _ = C.func; // ERROR: no member named func
    }
};

fn A(comptime T: type) type {
    return struct {
        pub usingnamespace T;
    };
}

const C = A(B);

pub fn main() !void {
    C.func();
}
  1. zig run bug.zig
bug.zig:5:14: error: struct 'bug.A(bug.B)' has no member named 'func'
        _ = C.func; // ERROR: no member named func
            ~^~~~~
bug.zig:10:12: note: struct declared here
    return struct {
           ^~~~~~
referenced by:
    main: bug.zig:18:6
    callMain: /home/lordmzte/.local/share/zupper/installs/master/lib/std/start.zig:581:32
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

Compile Log Output:
@as([]const builtin.Type.Declaration, .{ .{.name = "func"} })
@as(bool, false)

According to @typeInfo, the declaration func is present within C, but according to @hasDecl, it is not. Schrödinger's declaration!

Expected Behavior

The code compiles.

@LordMZTE LordMZTE added the bug Observed behavior contradicts documented or intended behavior label Nov 5, 2023
@LordMZTE LordMZTE changed the title Function not visible inside inside itself through generic function with usingnamespace Function not visible inside itself through generic struct with usingnamespace Nov 5, 2023
@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Nov 5, 2023
@Vexu Vexu added this to the 0.13.0 milestone Nov 5, 2023
@expikr
Copy link
Contributor

expikr commented Nov 8, 2023

Equivalent to

const B = struct {
    pub fn func() void {
        @compileLog(@typeInfo(C).Struct.decls); // .{ .{ .name = "func" } }
        @compileLog(@hasDecl(C, "func")); // false
        _ = C.func; // ERROR: no member named func
    }
};

const C = struct {
    pub usingnamespace B;
};

pub fn main() !void {
    C.func();
}

Is the circular dependency supposed to be allowed here?

@Vexu
Copy link
Member

Vexu commented Nov 8, 2023

Dependency loops only happen when analyzing a declaration depends on the declaration already being analyzed. A function referencing itself does not require it to be analyzed.

export fn foo() void {
    foo();
}

@akarpovskii
Copy link

akarpovskii commented Apr 28, 2024

Also encountered this issue on both 0.12.0 and 0.13.0-dev.46+3648d7df1, maybe a simpler reproduction:

const std = @import("std");

pub const Example = struct {
    usingnamespace struct {}; // works without this line

    pub fn foo() void {
        _ = Example.foo;  // error: struct 'main.Example' has no member named 'foo'
    }
};

pub fn main() !void {
    Example.foo();
}

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

Successfully merging a pull request may close this issue.

4 participants