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

stage2 usingnamespace with private decl causes ambiguous reference #13062

Open
billzez opened this issue Oct 4, 2022 · 8 comments
Open

stage2 usingnamespace with private decl causes ambiguous reference #13062

billzez opened this issue Oct 4, 2022 · 8 comments
Labels
Milestone

Comments

@billzez
Copy link
Contributor

billzez commented Oct 4, 2022

Zig Version

0.10.0-dev.4217+9d8cdb855

Steps to Reproduce

const std = @import("std");

pub const Mixin = struct {
    const X = 1;
};

pub const Thing = struct {
    pub usingnamespace Mixin;
    pub const X = 1;
};

pub fn main() void {
    std.debug.print("{d}\n", .{Thing.X});
}

Expected Behavior

Compiles

Actual Behavior

$ zig build-exe main.zig 
main.zig:13:37: error: ambiguous reference
    std.debug.print("{d}\n", .{Thing.X});
                               ~~~~~^~
main.zig:9:9: note: declared here
    pub const X = 1;
    ~~~~^~~~~~~~~~~
main.zig:4:5: note: declared here
    const X = 1;
    ^~~~~~~~~~~
@billzez billzez added the bug Observed behavior contradicts documented or intended behavior label Oct 4, 2022
@nektro
Copy link
Contributor

nektro commented Oct 4, 2022

working as intended imo

@billzez
Copy link
Contributor Author

billzez commented Oct 4, 2022

Desired or not, it's surprising that usingnamespace consumes private declarations. It's a change in behavior from stage1 and not documented.

@Vexu Vexu added docs and removed bug Observed behavior contradicts documented or intended behavior labels Oct 4, 2022
@Vexu Vexu added this to the 0.11.0 milestone Oct 4, 2022
@nektro
Copy link
Contributor

nektro commented Oct 4, 2022

oh no, you're right. that is a bug. Thing.X should unambiguously reference only the pub one

@nektro
Copy link
Contributor

nektro commented Oct 4, 2022

it should only be an error if Mixin.X was also pub

@Vexu
Copy link
Member

Vexu commented Oct 4, 2022

pub only matters if you put Mixin in a separate file in which case the reference becomes unambiguous and compiles. If everything is in the same file then the compile error is correct.

@billzez
Copy link
Contributor Author

billzez commented Oct 5, 2022

@Vexu - that's actually how I hit this bug. It was in a separate file, and I was confused how a private const in one file used as a mixin was affected by the file that consumed it.

@Vexu
Copy link
Member

Vexu commented Oct 5, 2022

Was Mixin in the same file and Thing in another? That sounds like #13077

@billzez
Copy link
Contributor Author

billzez commented Oct 5, 2022

Yes, #13077 looks like my original issue. I thought the above was the simplified version, but it appears this issue and that (#13077) are distinct. I'm not advocating that this above issue be fixed, but I do find it confusing.

@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants