Commit 29daf10639

Andrew Kelley <andrew@ziglang.org>
2021-02-18 06:34:06
stage2: fix a couple more compilation errors
1 parent 5a2620f
Changed files (4)
lib/std/zig/ast.zig
@@ -2054,7 +2054,7 @@ pub const full = struct {
                             return null;
                         }
                         const param_type = it.fn_proto.ast.params[it.param_i];
-                        var tok_i = tree.firstToken(param_type) - 1;
+                        var tok_i = it.tree.firstToken(param_type) - 1;
                         while (true) : (tok_i -= 1) switch (token_tags[tok_i]) {
                             .colon => continue,
                             .identifier => name_token = tok_i,
@@ -2063,7 +2063,7 @@ pub const full = struct {
                             else => break,
                         };
                         it.param_i += 1;
-                        it.tok_i = tree.lastToken(param_type) + 1;
+                        it.tok_i = it.tree.lastToken(param_type) + 1;
                         it.tok_flag = true;
                         return Param{
                             .first_doc_comment = first_doc_comment,
src/astgen.zig
@@ -539,6 +539,7 @@ pub fn comptimeExpr(
     }
 
     const tree = parent_scope.tree();
+    const token_starts = tree.tokens.items(.start);
 
     // Make a scope to collect generated instructions in the sub-expression.
     var block_scope: Scope.GenZIR = .{
@@ -693,7 +694,7 @@ pub fn blockExpr(
     rl: ResultLoc,
     block_node: ast.Node.Index,
     statements: []const ast.Node.Index,
-) InnerError!void {
+) InnerError!*zir.Inst {
     const tracy = trace(@src());
     defer tracy.end();
 
@@ -1174,20 +1175,6 @@ fn negation(
     return addZIRBinOp(mod, scope, src, op_inst_tag, lhs, rhs);
 }
 
-fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir.Inst {
-    const tree = scope.tree();
-    const src = token_starts[node.op_token];
-    return ptrSliceType(mod, scope, src, &node.ptr_info, node.rhs, switch (tree.token_ids[node.op_token]) {
-        .Asterisk, .AsteriskAsterisk => .One,
-        // TODO stage1 type inference bug
-        .LBracket => @as(std.builtin.TypeInfo.Pointer.Size, switch (tree.token_ids[node.op_token + 2]) {
-            .identifier => .C,
-            else => .Many,
-        }),
-        else => unreachable,
-    });
-}
-
 fn ptrSliceType(mod: *Module, scope: *Scope, src: usize, ptr_info: *ast.PtrInfo, rhs: *ast.Node, size: std.builtin.TypeInfo.Pointer.Size) InnerError!*zir.Inst {
     const simple = ptr_info.allowzero_token == null and
         ptr_info.align_info == null and
src/Module.zig
@@ -1223,19 +1223,6 @@ fn astgenAndSemaFn(
             .{},
         );
     }
-    const opt_cc: ?*zir.Inst = if (fn_proto.ast.callconv_expr != 0) cc: {
-        // TODO instead of enum literal type, this needs to be the
-        // std.builtin.CallingConvention enum. We need to implement importing other files
-        // and enums in order to fix this.
-        const src = token_starts[tree.firstToken(fn_proto.ast.callconv_expr)];
-        const enum_lit_ty = try astgen.addZIRInstConst(mod, &fn_type_scope.base, src, .{
-            .ty = Type.initTag(.type),
-            .val = Value.initTag(.enum_literal_type),
-        });
-        break :cc try astgen.comptimeExpr(mod, &fn_type_scope.base, .{
-            .ty = enum_lit_ty,
-        }, fn_proto.ast.callconv_expr);
-    } else null;
 
     const maybe_bang = tree.firstToken(fn_proto.ast.return_type) - 1;
     if (token_tags[maybe_bang] == .bang) {
@@ -1247,13 +1234,24 @@ fn astgenAndSemaFn(
         type_type_rl,
         fn_proto.ast.return_type,
     );
-    const fn_type_inst = if (opt_cc) |cc|
-        try astgen.addZirInstTag(mod, &fn_type_scope.base, fn_src, .fn_type_cc, .{
+    const fn_type_inst = if (fn_proto.ast.callconv_expr != 0) cc: {
+        // TODO instead of enum literal type, this needs to be the
+        // std.builtin.CallingConvention enum. We need to implement importing other files
+        // and enums in order to fix this.
+        const src = token_starts[tree.firstToken(fn_proto.ast.callconv_expr)];
+        const enum_lit_ty = try astgen.addZIRInstConst(mod, &fn_type_scope.base, src, .{
+            .ty = Type.initTag(.type),
+            .val = Value.initTag(.enum_literal_type),
+        });
+        const cc = try astgen.comptimeExpr(mod, &fn_type_scope.base, .{
+            .ty = enum_lit_ty,
+        }, fn_proto.ast.callconv_expr);
+        break :cc try astgen.addZirInstTag(mod, &fn_type_scope.base, fn_src, .fn_type_cc, .{
             .return_type = return_type_inst,
             .param_types = param_types,
             .cc = cc,
-        })
-    else
+        });
+    } else
         try astgen.addZirInstTag(mod, &fn_type_scope.base, fn_src, .fn_type, .{
             .return_type = return_type_inst,
             .param_types = param_types,
@@ -1362,13 +1360,13 @@ fn astgenAndSemaFn(
             params_scope = &sub_scope.base;
         }
 
-        try astgen.expr(mod, params_scope, .none, body_node);
+        _ = try astgen.expr(mod, params_scope, .none, body_node);
 
         if (gen_scope.instructions.items.len == 0 or
             !gen_scope.instructions.items[gen_scope.instructions.items.len - 1].tag.isNoReturn())
         {
             const src = token_starts[tree.lastToken(body_node)];
-            _ = try astgen.addZIRNoOp(mod, &gen_scope.base, src, .returnvoid);
+            _ = try astgen.addZIRNoOp(mod, &gen_scope.base, src, .return_void);
         }
 
         if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
@@ -1542,6 +1540,7 @@ fn astgenAndSemaVarDecl(
             .decl = decl,
             .arena = &gen_scope_arena.allocator,
             .parent = &decl.container.base,
+            .force_comptime = true,
         };
         defer gen_scope.instructions.deinit(mod.gpa);
 
@@ -1600,7 +1599,7 @@ fn astgenAndSemaVarDecl(
     } else if (!is_extern) {
         return mod.failTok(
             &block_scope.base,
-            tree.firstToken(var_decl),
+            var_decl.ast.mut_token,
             "variables must be initialized",
             .{},
         );
src/zir_sema.zig
@@ -981,8 +981,8 @@ fn zirCall(mod: *Module, scope: *Scope, inst: *zir.Inst.Call) InnerError!*Inst {
     const ret_type = func.ty.fnReturnType();
 
     const b = try mod.requireFunctionBlock(scope, inst.base.src);
-    const is_comptime_call = b.is_comptime or inst.kw_args.modifier == .compile_time;
-    const is_inline_call = is_comptime_call or inst.kw_args.modifier == .always_inline or
+    const is_comptime_call = b.is_comptime or inst.positionals.modifier == .compile_time;
+    const is_inline_call = is_comptime_call or inst.positionals.modifier == .always_inline or
         func.ty.fnCallingConvention() == .Inline;
     if (is_inline_call) {
         const func_val = try mod.resolveConstValue(scope, func);
@@ -1668,13 +1668,13 @@ fn zirSwitchBr(mod: *Module, scope: *Scope, inst: *zir.Inst.SwitchBr, ref: bool)
 
 fn validateSwitch(mod: *Module, scope: *Scope, target: *Inst, inst: *zir.Inst.SwitchBr) InnerError!void {
     // validate usage of '_' prongs
-    if (inst.kw_args.special_prong == .underscore and target.ty.zigTypeTag() != .Enum) {
+    if (inst.positionals.special_prong == .underscore and target.ty.zigTypeTag() != .Enum) {
         return mod.fail(scope, inst.base.src, "'_' prong only allowed when switching on non-exhaustive enums", .{});
         // TODO notes "'_' prong here" inst.positionals.cases[last].src
     }
 
     // check that target type supports ranges
-    if (inst.kw_args.range) |range_inst| {
+    if (inst.positionals.range) |range_inst| {
         switch (target.ty.zigTypeTag()) {
             .Int, .ComptimeInt => {},
             else => {
@@ -1725,14 +1725,14 @@ fn validateSwitch(mod: *Module, scope: *Scope, target: *Inst, inst: *zir.Inst.Sw
                 const start = try target.ty.minInt(&arena, mod.getTarget());
                 const end = try target.ty.maxInt(&arena, mod.getTarget());
                 if (try range_set.spans(start, end)) {
-                    if (inst.kw_args.special_prong == .@"else") {
+                    if (inst.positionals.special_prong == .@"else") {
                         return mod.fail(scope, inst.base.src, "unreachable else prong, all cases already handled", .{});
                     }
                     return;
                 }
             }
 
-            if (inst.kw_args.special_prong != .@"else") {
+            if (inst.positionals.special_prong != .@"else") {
                 return mod.fail(scope, inst.base.src, "switch must handle all possibilities", .{});
             }
         },
@@ -1752,15 +1752,15 @@ fn validateSwitch(mod: *Module, scope: *Scope, target: *Inst, inst: *zir.Inst.Sw
                     return mod.fail(scope, item.src, "duplicate switch value", .{});
                 }
             }
-            if ((true_count + false_count < 2) and inst.kw_args.special_prong != .@"else") {
+            if ((true_count + false_count < 2) and inst.positionals.special_prong != .@"else") {
                 return mod.fail(scope, inst.base.src, "switch must handle all possibilities", .{});
             }
-            if ((true_count + false_count == 2) and inst.kw_args.special_prong == .@"else") {
+            if ((true_count + false_count == 2) and inst.positionals.special_prong == .@"else") {
                 return mod.fail(scope, inst.base.src, "unreachable else prong, all cases already handled", .{});
             }
         },
         .EnumLiteral, .Void, .Fn, .Pointer, .Type => {
-            if (inst.kw_args.special_prong != .@"else") {
+            if (inst.positionals.special_prong != .@"else") {
                 return mod.fail(scope, inst.base.src, "else prong required when switching on type '{}'", .{target.ty});
             }