`usingnamespace` not possible in current file scope/namespace?

I am aware that usingnamespace may be about to be axed from the language, but I was wondering, why it currently seems to be impossible to import declarations into the “top-level” (of the current file) namespace:

const std = @import("std");

const S = struct {
    const foo = "foo";
};

const U = struct {
    usingnamespace S;
};
const f1 = U.foo;

usingnamespace S;
const f2 = foo; // <-- error: use of undeclared identifier 'foo'

pub fn main() void {
    std.debug.print("{s}\n", .{f1});
    std.debug.print("{s}\n", .{f2});
}

Is this a bug or an undocumented limitation of the keyword?

This is the intended behavior. see also: make `usingnamespace` not put identifiers in scope · Issue #9629 · ziglang/zig · GitHub

Also note that this does not only apply to top-level namespaces. You cannot do this inside of U either:

const U = struct {
    usingnamespace S;
    const f3 = foo; // also an error
};
5 Likes
const self = @This();
const f2 = self.foo();