aboutsummaryrefslogtreecommitdiff
path: root/src/Sema.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-03-25 23:00:38 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-03-25 23:45:17 -0700
commitb2deaf80279aab1322036e55a9646ecbaaa47f44 (patch)
treef950b7d882299bab45a6d5ea8cd213d1850c2bb0 /src/Sema.zig
parent4bfcd105eff797aceb621d2c8b971c15fef6e450 (diff)
downloadzig-b2deaf80279aab1322036e55a9646ecbaaa47f44.tar.gz
zig-b2deaf80279aab1322036e55a9646ecbaaa47f44.zip
stage2: improve source locations of Decl access
* zir.Code: introduce a decls array. This is so that `decl_val` and `decl_ref` instructions can refer to a Decl with a u32 and therefore they can also store a source location. This is needed for proper compile error reporting. * astgen uses a hash map to avoid redundantly adding a Decl to the decls array. * fixed reporting "instruction illegal outside function body" instead of the desired message "unable to resolve comptime value". * astgen skips emitting dbg_stmt instructions in comptime scopes. * astgen has some logic to avoid adding unnecessary type coercion instructions for common values.
Diffstat (limited to 'src/Sema.zig')
-rw-r--r--src/Sema.zig19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index 6cd05135d4..f6ffd640ab 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -1102,10 +1102,15 @@ fn zirDbgStmtNode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE
const tracy = trace(@src());
defer tracy.end();
+ // We do not set sema.src here because dbg_stmt instructions are only emitted for
+ // ZIR code that possibly will need to generate runtime code. So error messages
+ // and other source locations must not rely on sema.src being set from dbg_stmt
+ // instructions.
if (block.is_comptime) return;
const src_node = sema.code.instructions.items(.data)[inst].node;
const src: LazySrcLoc = .{ .node_offset = src_node };
+
const src_loc = src.toSrcLoc(&block.base);
const abs_byte_off = try src_loc.byteOffset();
_ = try block.addDbgStmt(src, abs_byte_off);
@@ -1115,16 +1120,20 @@ fn zirDeclRef(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError
const tracy = trace(@src());
defer tracy.end();
- const decl = sema.code.instructions.items(.data)[inst].decl;
- return sema.analyzeDeclRef(block, .unneeded, decl);
+ const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
+ const src = inst_data.src();
+ const decl = sema.code.decls[inst_data.payload_index];
+ return sema.analyzeDeclRef(block, src, decl);
}
fn zirDeclVal(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
const tracy = trace(@src());
defer tracy.end();
- const decl = sema.code.instructions.items(.data)[inst].decl;
- return sema.analyzeDeclVal(block, .unneeded, decl);
+ const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
+ const src = inst_data.src();
+ const decl = sema.code.decls[inst_data.payload_index];
+ return sema.analyzeDeclVal(block, src, decl);
}
fn zirCallNone(
@@ -3211,10 +3220,10 @@ fn requireFunctionBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void
}
fn requireRuntimeBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void {
- try sema.requireFunctionBlock(block, src);
if (block.is_comptime) {
return sema.mod.fail(&block.base, src, "unable to resolve comptime value", .{});
}
+ try sema.requireFunctionBlock(block, src);
}
fn validateVarType(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) !void {