authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-16 15:50:28-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-16 15:50:28-07:00
logadc2aed587d009c0d112063fa0f3d03dedc9e50a
tree0e32f88c8729eadc2708af7e9d53f8517222cf96
parent333a577d73cdbac420d25167a3955956af91b2eb

AstGen: require `@import` operand to be string literal

See #2206

3 files changed, 16 insertions(+), 8 deletions(-)

src/AstGen.zig+11-2
...@@ -4674,8 +4674,17 @@ fn builtinCall(...@@ -4674,8 +4674,17 @@ fn builtinCall(
4674 return rvalue(gz, scope, rl, .void_value, node);4674 return rvalue(gz, scope, rl, .void_value, node);
4675 },4675 },
4676 .import => {4676 .import => {
4677 const target = try expr(gz, scope, .none, params[0]);4677 const node_tags = tree.nodes.items(.tag);
4678 const result = try gz.addUnNode(.import, target, node);4678 const node_datas = tree.nodes.items(.data);
4679 const operand_node = params[0];
4680
4681 if (node_tags[operand_node] != .string_literal) {
4682 // Spec reference: https://github.com/ziglang/zig/issues/2206
4683 return astgen.failNode(operand_node, "@import operand must be a string literal", .{});
4684 }
4685 const str_lit_token = main_tokens[operand_node];
4686 const str = try gz.strLitAsString(str_lit_token);
4687 const result = try gz.addStrTok(.import, str.index, str_lit_token);
4679 return rvalue(gz, scope, rl, result, node);4688 return rvalue(gz, scope, rl, result, node);
4680 },4689 },
4681 .error_to_int => {4690 .error_to_int => {
src/Sema.zig+2-3
...@@ -3900,10 +3900,9 @@ fn zirImport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!...@@ -3900,10 +3900,9 @@ fn zirImport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!
3900 defer tracy.end();3900 defer tracy.end();
39013901
3902 const mod = sema.mod;3902 const mod = sema.mod;
3903 const inst_data = sema.code.instructions.items(.data)[inst].un_node;3903 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;
3904 const src = inst_data.src();3904 const src = inst_data.src();
3905 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };3905 const operand = inst_data.get(sema.code);
3906 const operand = try sema.resolveConstString(block, operand_src, inst_data.operand);
39073906
3908 const file = mod.importFile(block.getFileScope().pkg, operand) catch |err| switch (err) {3907 const file = mod.importFile(block.getFileScope().pkg, operand) catch |err| switch (err) {
3909 error.ImportOutsidePkgPath => {3908 error.ImportOutsidePkgPath => {
src/Zir.zig+3-3
...@@ -377,8 +377,8 @@ pub const Inst = struct {...@@ -377,8 +377,8 @@ pub const Inst = struct {
377 /// Implements the `@hasDecl` builtin.377 /// Implements the `@hasDecl` builtin.
378 /// Uses the `pl_node` union field. Payload is `Bin`.378 /// Uses the `pl_node` union field. Payload is `Bin`.
379 has_decl,379 has_decl,
380 /// `@import(operand)`.380 /// Implements the `@import` builtin.
381 /// Uses the `un_node` field.381 /// Uses the `str_tok` field.
382 import,382 import,
383 /// Integer literal that fits in a u64. Uses the int union value.383 /// Integer literal that fits in a u64. Uses the int union value.
384 int,384 int,
...@@ -1699,7 +1699,6 @@ const Writer = struct {...@@ -1699,7 +1699,6 @@ const Writer = struct {
1699 .load,1699 .load,
1700 .ensure_result_used,1700 .ensure_result_used,
1701 .ensure_result_non_error,1701 .ensure_result_non_error,
1702 .import,
1703 .ptrtoint,1702 .ptrtoint,
1704 .ret_node,1703 .ret_node,
1705 .set_eval_branch_quota,1704 .set_eval_branch_quota,
...@@ -1871,6 +1870,7 @@ const Writer = struct {...@@ -1871,6 +1870,7 @@ const Writer = struct {
1871 .enum_literal,1870 .enum_literal,
1872 .decl_ref_named,1871 .decl_ref_named,
1873 .decl_val_named,1872 .decl_val_named,
1873 .import,
1874 => try self.writeStrTok(stream, inst),1874 => try self.writeStrTok(stream, inst),
18751875
1876 .func => try self.writeFunc(stream, inst, false),1876 .func => try self.writeFunc(stream, inst, false),