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(
46744674 return rvalue(gz, scope, rl, .void_value, node);
46754675 },
46764676 .import => {
4677 const target = try expr(gz, scope, .none, params[0]);
4678 const result = try gz.addUnNode(.import, target, node);
4677 const node_tags = tree.nodes.items(.tag);
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);
46794688 return rvalue(gz, scope, rl, result, node);
46804689 },
46814690 .error_to_int => {
src/Sema.zig+2-3
......@@ -3900,10 +3900,9 @@ fn zirImport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!
39003900 defer tracy.end();
39013901
39023902 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;
39043904 const src = inst_data.src();
3905 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
3906 const operand = try sema.resolveConstString(block, operand_src, inst_data.operand);
3905 const operand = inst_data.get(sema.code);
39073906
39083907 const file = mod.importFile(block.getFileScope().pkg, operand) catch |err| switch (err) {
39093908 error.ImportOutsidePkgPath => {
src/Zir.zig+3-3
......@@ -377,8 +377,8 @@ pub const Inst = struct {
377377 /// Implements the `@hasDecl` builtin.
378378 /// Uses the `pl_node` union field. Payload is `Bin`.
379379 has_decl,
380 /// `@import(operand)`.
381 /// Uses the `un_node` field.
380 /// Implements the `@import` builtin.
381 /// Uses the `str_tok` field.
382382 import,
383383 /// Integer literal that fits in a u64. Uses the int union value.
384384 int,
......@@ -1699,7 +1699,6 @@ const Writer = struct {
16991699 .load,
17001700 .ensure_result_used,
17011701 .ensure_result_non_error,
1702 .import,
17031702 .ptrtoint,
17041703 .ret_node,
17051704 .set_eval_branch_quota,
......@@ -1871,6 +1870,7 @@ const Writer = struct {
18711870 .enum_literal,
18721871 .decl_ref_named,
18731872 .decl_val_named,
1873 .import,
18741874 => try self.writeStrTok(stream, inst),
18751875
18761876 .func => try self.writeFunc(stream, inst, false),