Skip to content

Commit 34f6443

Browse files
committed
remove usingnamespace from the language
closes #20663
1 parent aa52bb8 commit 34f6443

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+29
-910
lines changed

doc/langref.html.in

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3842,37 +3842,6 @@ void do_a_thing(struct Foo *foo) {
38423842
{#header_close#}
38433843
{#header_close#}
38443844

3845-
{#header_open|usingnamespace#}
3846-
<p>
3847-
{#syntax#}usingnamespace{#endsyntax#} is a declaration that mixes all the public
3848-
declarations of the operand, which must be a {#link|struct#}, {#link|union#}, {#link|enum#},
3849-
or {#link|opaque#}, into the namespace:
3850-
</p>
3851-
{#code|test_usingnamespace.zig#}
3852-
3853-
<p>
3854-
{#syntax#}usingnamespace{#endsyntax#} has an important use case when organizing the public
3855-
API of a file or package. For example, one might have <code class="file">c.zig</code> with all of the
3856-
{#link|C imports|Import from C Header File#}:
3857-
</p>
3858-
{#syntax_block|zig|c.zig#}
3859-
pub usingnamespace @cImport({
3860-
@cInclude("epoxy/gl.h");
3861-
@cInclude("GLFW/glfw3.h");
3862-
@cDefine("STBI_ONLY_PNG", "");
3863-
@cDefine("STBI_NO_STDIO", "");
3864-
@cInclude("stb_image.h");
3865-
});
3866-
{#end_syntax_block#}
3867-
<p>
3868-
The above example demonstrates using {#syntax#}pub{#endsyntax#} to qualify the
3869-
{#syntax#}usingnamespace{#endsyntax#} additionally makes the imported declarations
3870-
{#syntax#}pub{#endsyntax#}. This can be used to forward declarations, giving precise control
3871-
over what declarations a given file exposes.
3872-
</p>
3873-
{#header_close#}
3874-
3875-
38763845
{#header_open|comptime#}
38773846
<p>
38783847
Zig places importance on the concept of whether an expression is known at compile-time.
@@ -6545,7 +6514,7 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
65456514
</p>
65466515
<ul>
65476516
<li>If a call to {#syntax#}@import{#endsyntax#} is analyzed, the file being imported is analyzed.</li>
6548-
<li>If a type (including a file) is analyzed, all {#syntax#}comptime{#endsyntax#}, {#syntax#}usingnamespace{#endsyntax#}, and {#syntax#}export{#endsyntax#} declarations within it are analyzed.</li>
6517+
<li>If a type (including a file) is analyzed, all {#syntax#}comptime{#endsyntax#} and {#syntax#}export{#endsyntax#} declarations within it are analyzed.</li>
65496518
<li>If a type (including a file) is analyzed, and the compilation is for a {#link|test|Zig Test#}, and the module the type is within is the root module of the compilation, then all {#syntax#}test{#endsyntax#} declarations within it are also analyzed.</li>
65506519
<li>If a reference to a named declaration (i.e. a usage of it) is analyzed, the declaration being referenced is analyzed. Declarations are order-independent, so this reference may be above or below the declaration being referenced, or even in another file entirely.</li>
65516520
</ul>
@@ -7782,18 +7751,6 @@ fn readU32Be() u32 {}
77827751
</ul>
77837752
</td>
77847753
</tr>
7785-
<tr>
7786-
<th scope="row">
7787-
<pre>{#syntax#}usingnamespace{#endsyntax#}</pre>
7788-
</th>
7789-
<td>
7790-
{#syntax#}usingnamespace{#endsyntax#} is a top-level declaration that imports all the public declarations of the operand,
7791-
which must be a struct, union, or enum, into the current scope.
7792-
<ul>
7793-
<li>See also {#link|usingnamespace#}</li>
7794-
</ul>
7795-
</td>
7796-
</tr>
77977754
<tr>
77987755
<th scope="row">
77997756
<pre>{#syntax#}var{#endsyntax#}</pre>
@@ -7863,7 +7820,6 @@ ComptimeDecl <- KEYWORD_comptime Block
78637820
Decl
78647821
<- (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE? / KEYWORD_inline / KEYWORD_noinline)? FnProto (SEMICOLON / Block)
78657822
/ (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE?)? KEYWORD_threadlocal? GlobalVarDecl
7866-
/ KEYWORD_usingnamespace Expr SEMICOLON
78677823

78687824
FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? AddrSpace? LinkSection? CallConv? EXCLAMATIONMARK? TypeExpr
78697825

@@ -8408,7 +8364,6 @@ KEYWORD_threadlocal <- 'threadlocal' end_of_word
84088364
KEYWORD_try <- 'try' end_of_word
84098365
KEYWORD_union <- 'union' end_of_word
84108366
KEYWORD_unreachable <- 'unreachable' end_of_word
8411-
KEYWORD_usingnamespace <- 'usingnamespace' end_of_word
84128367
KEYWORD_var <- 'var' end_of_word
84138368
KEYWORD_volatile <- 'volatile' end_of_word
84148369
KEYWORD_while <- 'while' end_of_word
@@ -8424,7 +8379,7 @@ keyword <- KEYWORD_addrspace / KEYWORD_align / KEYWORD_allowzero / KEYWORD_and
84248379
/ KEYWORD_pub / KEYWORD_resume / KEYWORD_return / KEYWORD_linksection
84258380
/ KEYWORD_struct / KEYWORD_suspend / KEYWORD_switch / KEYWORD_test
84268381
/ KEYWORD_threadlocal / KEYWORD_try / KEYWORD_union / KEYWORD_unreachable
8427-
/ KEYWORD_usingnamespace / KEYWORD_var / KEYWORD_volatile / KEYWORD_while
8382+
/ KEYWORD_var / KEYWORD_volatile / KEYWORD_while
84288383
{#end_syntax_block#}
84298384
{#header_close#}
84308385
{#header_open|Zen#}

doc/langref/test_usingnamespace.zig

Lines changed: 0 additions & 8 deletions
This file was deleted.

lib/compiler/reduce/Walk.zig

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,6 @@ fn walkMember(w: *Walk, decl: Ast.Node.Index) Error!void {
160160
try walkExpression(w, decl);
161161
},
162162

163-
.@"usingnamespace" => {
164-
try w.transformations.append(.{ .delete_node = decl });
165-
const expr = ast.nodeData(decl).node;
166-
try walkExpression(w, expr);
167-
},
168-
169163
.global_var_decl,
170164
.local_var_decl,
171165
.simple_var_decl,
@@ -520,7 +514,6 @@ fn walkExpression(w: *Walk, node: Ast.Node.Index) Error!void {
520514
.local_var_decl => unreachable,
521515
.simple_var_decl => unreachable,
522516
.aligned_var_decl => unreachable,
523-
.@"usingnamespace" => unreachable,
524517
.test_decl => unreachable,
525518
.asm_output => unreachable,
526519
.asm_input => unreachable,

lib/docs/wasm/Walk.zig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,6 @@ fn struct_decl(
567567
},
568568

569569
.@"comptime",
570-
.@"usingnamespace",
571570
=> try w.expr(&namespace.base, parent_decl, ast.nodeData(member).node),
572571

573572
.test_decl => try w.expr(&namespace.base, parent_decl, ast.nodeData(member).opt_token_and_node[1]),
@@ -639,7 +638,6 @@ fn expr(w: *Walk, scope: *Scope, parent_decl: Decl.Index, node: Ast.Node.Index)
639638
const ast = w.file.get_ast();
640639
switch (ast.nodeTag(node)) {
641640
.root => unreachable, // Top-level declaration.
642-
.@"usingnamespace" => unreachable, // Top-level declaration.
643641
.test_decl => unreachable, // Top-level declaration.
644642
.container_field_init => unreachable, // Top-level declaration.
645643
.container_field_align => unreachable, // Top-level declaration.

lib/docs/wasm/html_render.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ pub fn fileSourceHtml(
137137
.keyword_try,
138138
.keyword_union,
139139
.keyword_unreachable,
140-
.keyword_usingnamespace,
141140
.keyword_var,
142141
.keyword_volatile,
143142
.keyword_allowzero,

lib/std/zig/Ast.zig

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -755,13 +755,6 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {
755755
return i - end_offset;
756756
},
757757

758-
.@"usingnamespace" => {
759-
const main_token: TokenIndex = tree.nodeMainToken(n);
760-
const has_visib_token = tree.isTokenPrecededByTags(main_token, &.{.keyword_pub});
761-
end_offset += @intFromBool(has_visib_token);
762-
return main_token - end_offset;
763-
},
764-
765758
.container_field_init,
766759
.container_field_align,
767760
.container_field,
@@ -881,7 +874,6 @@ pub fn lastToken(tree: Ast, node: Node.Index) TokenIndex {
881874
while (true) switch (tree.nodeTag(n)) {
882875
.root => return @intCast(tree.tokens.len - 1),
883876

884-
.@"usingnamespace",
885877
.bool_not,
886878
.negation,
887879
.bit_not,
@@ -3033,12 +3025,6 @@ pub const Node = struct {
30333025
///
30343026
/// The `main_token` field is the first token for the source file.
30353027
root,
3036-
/// `usingnamespace expr;`.
3037-
///
3038-
/// The `data` field is a `.node` to expr.
3039-
///
3040-
/// The `main_token` field is the `usingnamespace` token.
3041-
@"usingnamespace",
30423028
/// `test {}`,
30433029
/// `test "name" {}`,
30443030
/// `test identifier {}`.

lib/std/zig/AstGen.zig

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,6 @@ fn lvalExpr(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Ins
442442
const tree = astgen.tree;
443443
switch (tree.nodeTag(node)) {
444444
.root => unreachable,
445-
.@"usingnamespace" => unreachable,
446445
.test_decl => unreachable,
447446
.global_var_decl => unreachable,
448447
.local_var_decl => unreachable,
@@ -637,7 +636,6 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
637636

638637
switch (tree.nodeTag(node)) {
639638
.root => unreachable, // Top-level declaration.
640-
.@"usingnamespace" => unreachable, // Top-level declaration.
641639
.test_decl => unreachable, // Top-level declaration.
642640
.container_field_init => unreachable, // Top-level declaration.
643641
.container_field_align => unreachable, // Top-level declaration.
@@ -4700,69 +4698,6 @@ fn comptimeDecl(
47004698
});
47014699
}
47024700

4703-
fn usingnamespaceDecl(
4704-
astgen: *AstGen,
4705-
gz: *GenZir,
4706-
scope: *Scope,
4707-
wip_members: *WipMembers,
4708-
node: Ast.Node.Index,
4709-
) InnerError!void {
4710-
const tree = astgen.tree;
4711-
4712-
const old_hasher = astgen.src_hasher;
4713-
defer astgen.src_hasher = old_hasher;
4714-
astgen.src_hasher = std.zig.SrcHasher.init(.{});
4715-
astgen.src_hasher.update(tree.getNodeSource(node));
4716-
astgen.src_hasher.update(std.mem.asBytes(&astgen.source_column));
4717-
4718-
const type_expr = tree.nodeData(node).node;
4719-
const is_pub = tree.isTokenPrecededByTags(tree.nodeMainToken(node), &.{.keyword_pub});
4720-
4721-
// Up top so the ZIR instruction index marks the start range of this
4722-
// top-level declaration.
4723-
const decl_inst = try gz.makeDeclaration(node);
4724-
wip_members.nextDecl(decl_inst);
4725-
astgen.advanceSourceCursorToNode(node);
4726-
4727-
// This is just needed for the `setDeclaration` call.
4728-
var dummy_gz = gz.makeSubBlock(scope);
4729-
defer dummy_gz.unstack();
4730-
4731-
var usingnamespace_gz: GenZir = .{
4732-
.is_comptime = true,
4733-
.decl_node_index = node,
4734-
.decl_line = astgen.source_line,
4735-
.parent = scope,
4736-
.astgen = astgen,
4737-
.instructions = gz.instructions,
4738-
.instructions_top = gz.instructions.items.len,
4739-
};
4740-
defer usingnamespace_gz.unstack();
4741-
4742-
const decl_column = astgen.source_column;
4743-
4744-
const namespace_inst = try typeExpr(&usingnamespace_gz, &usingnamespace_gz.base, type_expr);
4745-
_ = try usingnamespace_gz.addBreak(.break_inline, decl_inst, namespace_inst);
4746-
4747-
var hash: std.zig.SrcHash = undefined;
4748-
astgen.src_hasher.final(&hash);
4749-
try setDeclaration(decl_inst, .{
4750-
.src_hash = hash,
4751-
.src_line = usingnamespace_gz.decl_line,
4752-
.src_column = decl_column,
4753-
.kind = .@"usingnamespace",
4754-
.name = .empty,
4755-
.is_pub = is_pub,
4756-
.is_threadlocal = false,
4757-
.linkage = .normal,
4758-
.type_gz = &dummy_gz,
4759-
.align_gz = &dummy_gz,
4760-
.linksection_gz = &dummy_gz,
4761-
.addrspace_gz = &dummy_gz,
4762-
.value_gz = &usingnamespace_gz,
4763-
});
4764-
}
4765-
47664701
fn testDecl(
47674702
astgen: *AstGen,
47684703
gz: *GenZir,
@@ -5932,23 +5867,6 @@ fn containerMember(
59325867
},
59335868
};
59345869
},
5935-
.@"usingnamespace" => {
5936-
const prev_decl_index = wip_members.decl_index;
5937-
astgen.usingnamespaceDecl(gz, scope, wip_members, member_node) catch |err| switch (err) {
5938-
error.OutOfMemory => return error.OutOfMemory,
5939-
error.AnalysisFail => {
5940-
wip_members.decl_index = prev_decl_index;
5941-
try addFailedDeclaration(
5942-
wip_members,
5943-
gz,
5944-
.@"usingnamespace",
5945-
.empty,
5946-
member_node,
5947-
tree.isTokenPrecededByTags(tree.nodeMainToken(member_node), &.{.keyword_pub}),
5948-
);
5949-
},
5950-
};
5951-
},
59525870
.test_decl => {
59535871
const prev_decl_index = wip_members.decl_index;
59545872
// We need to have *some* decl here so that the decl count matches what's expected.
@@ -10398,7 +10316,6 @@ fn nodeMayEvalToError(tree: *const Ast, start_node: Ast.Node.Index) BuiltinFn.Ev
1039810316
while (true) {
1039910317
switch (tree.nodeTag(node)) {
1040010318
.root,
10401-
.@"usingnamespace",
1040210319
.test_decl,
1040310320
.switch_case,
1040410321
.switch_case_inline,
@@ -10606,7 +10523,6 @@ fn nodeImpliesMoreThanOnePossibleValue(tree: *const Ast, start_node: Ast.Node.In
1060610523
while (true) {
1060710524
switch (tree.nodeTag(node)) {
1060810525
.root,
10609-
.@"usingnamespace",
1061010526
.test_decl,
1061110527
.switch_case,
1061210528
.switch_case_inline,
@@ -10845,7 +10761,6 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {
1084510761
while (true) {
1084610762
switch (tree.nodeTag(node)) {
1084710763
.root,
10848-
.@"usingnamespace",
1084910764
.test_decl,
1085010765
.switch_case,
1085110766
.switch_case_inline,
@@ -13518,7 +13433,7 @@ fn scanContainer(
1351813433
break :blk .{ .decl, ident };
1351913434
},
1352013435

13521-
.@"comptime", .@"usingnamespace" => {
13436+
.@"comptime" => {
1352213437
decl_count += 1;
1352313438
continue;
1352413439
},
@@ -13896,7 +13811,6 @@ const DeclarationName = union(enum) {
1389613811
decltest: Ast.TokenIndex,
1389713812
unnamed_test,
1389813813
@"comptime",
13899-
@"usingnamespace",
1390013814
};
1390113815

1390213816
fn addFailedDeclaration(
@@ -13986,7 +13900,6 @@ fn setDeclaration(
1398613900
.@"test" => .@"test",
1398713901
.decltest => .decltest,
1398813902
.@"comptime" => .@"comptime",
13989-
.@"usingnamespace" => if (args.is_pub) .pub_usingnamespace else .@"usingnamespace",
1399013903
.@"const" => switch (args.linkage) {
1399113904
.normal => if (args.is_pub) id: {
1399213905
if (has_special_body) break :id .pub_const;

lib/std/zig/AstRlAnnotate.zig

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,6 @@ fn expr(astrl: *AstRlAnnotate, node: Ast.Node.Index, block: ?*Block, ri: ResultI
165165
}
166166
return false;
167167
},
168-
.@"usingnamespace" => {
169-
_ = try astrl.expr(tree.nodeData(node).node, block, ResultInfo.type_only);
170-
return false;
171-
},
172168
.test_decl => {
173169
_ = try astrl.expr(tree.nodeData(node).opt_token_and_node[1], block, ResultInfo.none);
174170
return false;

0 commit comments

Comments
 (0)