Commit 4a5628e730

Veikka Tuominen <git@vexu.eu>
2023-03-26 14:13:51
Module: fix lazy srcloc resolution for new for loop syntax
Closes #15081
1 parent 3a7fe0d
Changed files (2)
src
test
src/Module.zig
@@ -2459,7 +2459,7 @@ pub const SrcLoc = struct {
                 return nodeToSpan(tree, node_datas[asm_output].lhs);
             },
 
-            .node_offset_for_cond, .node_offset_if_cond => |node_off| {
+            .node_offset_if_cond => |node_off| {
                 const tree = try src_loc.file_scope.getTree(gpa);
                 const node = src_loc.declRelativeToNodeIndex(node_off);
                 const node_tags = tree.nodes.items(.tag);
@@ -2471,9 +2471,16 @@ pub const SrcLoc = struct {
                     .while_simple,
                     .while_cont,
                     .@"while",
+                    => tree.fullWhile(node).?.ast.cond_expr,
+
                     .for_simple,
                     .@"for",
-                    => tree.fullWhile(node).?.ast.cond_expr,
+                    => {
+                        const inputs = tree.fullFor(node).?.ast.inputs;
+                        const start = tree.firstToken(inputs[0]);
+                        const end = tree.lastToken(inputs[inputs.len - 1]);
+                        return tokensToSpan(tree, start, end, start);
+                    },
 
                     .@"orelse" => node,
                     .@"catch" => node,
@@ -2967,12 +2974,6 @@ pub const LazySrcLoc = union(enum) {
     /// The source location points to the initializer of a var decl.
     /// The Decl is determined contextually.
     node_offset_var_decl_init: i32,
-    /// The source location points to a for loop condition expression,
-    /// found by taking this AST node index offset from the containing
-    /// Decl AST node, which points to a for loop AST node. Next, navigate
-    /// to the condition expression.
-    /// The Decl is determined contextually.
-    node_offset_for_cond: i32,
     /// The source location points to the first parameter of a builtin
     /// function call, found by taking this AST node index offset from the containing
     /// Decl AST node, which points to a builtin call AST node. Next, navigate
@@ -3233,7 +3234,6 @@ pub const LazySrcLoc = union(enum) {
             .node_offset_var_decl_section,
             .node_offset_var_decl_addrspace,
             .node_offset_var_decl_init,
-            .node_offset_for_cond,
             .node_offset_builtin_call_arg0,
             .node_offset_builtin_call_arg1,
             .node_offset_builtin_call_arg2,
test/cases/compile_errors/comptime_if_inside_runtime_for.zig
@@ -0,0 +1,14 @@
+export fn entry() void {
+	var x: u32 = 0;
+	for(0..1, 1..2) |_, _| {
+		var y = x + if(x == 0) 1 else 0;
+		_ = y;
+	}
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :4:15: error: value with comptime-only type 'comptime_int' depends on runtime control flow
+// :3:6: note: runtime control flow here