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

Non-pub decl occasionally polluting the pub namespace #21604

Closed
vadim-za opened this issue Oct 5, 2024 · 4 comments
Closed

Non-pub decl occasionally polluting the pub namespace #21604

vadim-za opened this issue Oct 5, 2024 · 4 comments
Labels
question No questions on the issue tracker, please.

Comments

@vadim-za
Copy link

vadim-za commented Oct 5, 2024

Zig Version

0.14.0-dev.1694+3b465ebec

Steps to Reproduce and Observed Behavior

Create and compile the following code distributed across the 4 files and 2 folders as the comments say.

/// ------- main.zig
const decls = @import("decls.zig");

pub fn main() void {
    _ = decls.get();
}

/// ------- decls.zig
pub const S = @import("decls/s.zig").S;
pub usingnamespace @import("decls/use_s.zig");

/// ------- decls/s.zig
pub const S = struct {};

/// ------- decls/use_s.zig
const decls = @import("../decls.zig");

// commenting out the following line avoids the compile error
const S = @import("s.zig").S;

pub fn get() decls.S {
    return .{};
}

Observe the following error:


src\decls\use_s.zig:7:19: error: ambiguous reference
pub fn get() decls.S {
             ~~~~~^~
src\decls.zig:2:5: note: declared here
pub const S = @import("decls/s.zig").S;
~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src\decls\use_s.zig:5:1: note: declared here
const S = @import("s.zig").S;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now comment out the declaration of const S in use_s.zig. The code compiles now.

Expected Behavior

The code compiles in both cases

@vadim-za vadim-za added the bug Observed behavior contradicts documented or intended behavior label Oct 5, 2024
@mlugg
Copy link
Member

mlugg commented Oct 5, 2024

This was an intentional simplification to the rules of usingnamespace, because the previous implementation was non-deterministic.

Note also #20663.

@mlugg mlugg closed this as not planned Won't fix, can't repro, duplicate, stale Oct 5, 2024
@mlugg mlugg added question No questions on the issue tracker, please. and removed bug Observed behavior contradicts documented or intended behavior labels Oct 5, 2024
@vadim-za
Copy link
Author

vadim-za commented Oct 5, 2024

Is there any information to what exactly the simplification was? The error message is totally puzzling and leaving me clueless of what is wrong in the source code.

In particular, I was assuming that maybe usingnamespace now also includes the non-pub members, however that conjecture is refuted by replacing _ = decls.get(); in main.zig with _ = decls.S{};, which now compiles. So the ambiguity of decls.S seems totally random.

@mlugg
Copy link
Member

mlugg commented Oct 5, 2024

Non-pub declarations are accessible if they are declared in the same file which is performing the namespace lookup. So, writing decls.S in use_s.zig can see the declaration of S in use_s.zig, but writing decls.S in a different file cannot.

These rules may be reviewed again if #20663 is rejected.

@vadim-za
Copy link
Author

vadim-za commented Oct 5, 2024

Upon thinking about it, it still looks pretty awkward. It's one and the same S, just accessed through two different paths. Why would that be ambiguous? And it seems to me that two different S wouldn't ever be able to create this kind of ambiguity. Anyway, probably indeed worth waiting for a decision on #20663.

Edit: the latter part of the statement was not correct, so nevermind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question No questions on the issue tracker, please.
Projects
None yet
Development

No branches or pull requests

2 participants