From 2eef83e85f1f701fb67d410d3ffc1a1b344b4c13 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 29 Apr 2021 17:43:07 -0700 Subject: [PATCH] AstGen: fix comptime compile error source location --- src/AstGen.zig | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/AstGen.zig b/src/AstGen.zig index 8e653d4feb..c0ee500d1a 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -830,8 +830,7 @@ pub fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) Inn .char_literal => return charLiteral(gz, scope, rl, node), .error_set_decl => return errorSetDecl(gz, scope, rl, node), .array_access => return arrayAccess(gz, scope, rl, node), - // we use comptimeExprFromAst here as it is explicitly put there by the user, `comptimeExpr` can be used by the compiler, even in a comptime scope - .@"comptime" => return comptimeExprFromAst(gz, scope, rl, node_datas[node].lhs), + .@"comptime" => return comptimeExprAst(gz, scope, rl, node), .@"switch", .switch_comma => return switchExpr(gz, scope, rl, node), .@"nosuspend" => return nosuspendExpr(gz, scope, rl, node), @@ -1455,7 +1454,9 @@ pub fn structInitExprRlTy( return init_inst; } -pub fn comptimeExpr( +/// This calls expr in a comptime scope, and is intended to be called as a helper function. +/// The one that corresponds to `comptime` expression syntax is `comptimeExprAst`. +fn comptimeExpr( gz: *GenZir, scope: *Scope, rl: ResultLoc, @@ -1468,7 +1469,10 @@ pub fn comptimeExpr( return result; } -pub fn comptimeExprFromAst( +/// This one is for an actual `comptime` syntax, and will emit a compile error if +/// the scope already has `force_comptime=true`. +/// See `comptimeExpr` for the helper function for calling expr in a comptime scope. +fn comptimeExprAst( gz: *GenZir, scope: *Scope, rl: ResultLoc, @@ -1478,8 +1482,11 @@ pub fn comptimeExprFromAst( if (gz.force_comptime) { return astgen.failNode(node, "redundant comptime keyword in already comptime scope", .{}); } + const tree = &astgen.file.tree; + const node_datas = tree.nodes.items(.data); + const body_node = node_datas[node].lhs; gz.force_comptime = true; - const result = try expr(gz, scope, rl, node); + const result = try expr(gz, scope, rl, body_node); gz.force_comptime = false; return result; }