authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-29 17:43:07-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-29 17:43:07-07:00
log2eef83e85f1f701fb67d410d3ffc1a1b344b4c13
tree9098c9e77b7dca5d79e123997b36fcb2e174251e
parent47585cbe12251ef8b32f2e711b5f2bb48d54cbe5

AstGen: fix comptime compile error source location


1 files changed, 12 insertions(+), 5 deletions(-)

src/AstGen.zig+12-5
...@@ -830,8 +830,7 @@ pub fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) Inn...@@ -830,8 +830,7 @@ pub fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) Inn
830 .char_literal => return charLiteral(gz, scope, rl, node),830 .char_literal => return charLiteral(gz, scope, rl, node),
831 .error_set_decl => return errorSetDecl(gz, scope, rl, node),831 .error_set_decl => return errorSetDecl(gz, scope, rl, node),
832 .array_access => return arrayAccess(gz, scope, rl, node),832 .array_access => return arrayAccess(gz, scope, rl, node),
833 // we use comptimeExprFromAst here as it is explicitly put there by the user, `comptimeExpr` can be used by the compiler, even in a comptime scope833 .@"comptime" => return comptimeExprAst(gz, scope, rl, node),
834 .@"comptime" => return comptimeExprFromAst(gz, scope, rl, node_datas[node].lhs),
835 .@"switch", .switch_comma => return switchExpr(gz, scope, rl, node),834 .@"switch", .switch_comma => return switchExpr(gz, scope, rl, node),
836835
837 .@"nosuspend" => return nosuspendExpr(gz, scope, rl, node),836 .@"nosuspend" => return nosuspendExpr(gz, scope, rl, node),
...@@ -1455,7 +1454,9 @@ pub fn structInitExprRlTy(...@@ -1455,7 +1454,9 @@ pub fn structInitExprRlTy(
1455 return init_inst;1454 return init_inst;
1456}1455}
14571456
1458pub fn comptimeExpr(1457/// This calls expr in a comptime scope, and is intended to be called as a helper function.
1458/// The one that corresponds to `comptime` expression syntax is `comptimeExprAst`.
1459fn comptimeExpr(
1459 gz: *GenZir,1460 gz: *GenZir,
1460 scope: *Scope,1461 scope: *Scope,
1461 rl: ResultLoc,1462 rl: ResultLoc,
...@@ -1468,7 +1469,10 @@ pub fn comptimeExpr(...@@ -1468,7 +1469,10 @@ pub fn comptimeExpr(
1468 return result;1469 return result;
1469}1470}
14701471
1471pub fn comptimeExprFromAst(1472/// This one is for an actual `comptime` syntax, and will emit a compile error if
1473/// the scope already has `force_comptime=true`.
1474/// See `comptimeExpr` for the helper function for calling expr in a comptime scope.
1475fn comptimeExprAst(
1472 gz: *GenZir,1476 gz: *GenZir,
1473 scope: *Scope,1477 scope: *Scope,
1474 rl: ResultLoc,1478 rl: ResultLoc,
...@@ -1478,8 +1482,11 @@ pub fn comptimeExprFromAst(...@@ -1478,8 +1482,11 @@ pub fn comptimeExprFromAst(
1478 if (gz.force_comptime) {1482 if (gz.force_comptime) {
1479 return astgen.failNode(node, "redundant comptime keyword in already comptime scope", .{});1483 return astgen.failNode(node, "redundant comptime keyword in already comptime scope", .{});
1480 }1484 }
1485 const tree = &astgen.file.tree;
1486 const node_datas = tree.nodes.items(.data);
1487 const body_node = node_datas[node].lhs;
1481 gz.force_comptime = true;1488 gz.force_comptime = true;
1482 const result = try expr(gz, scope, rl, node);1489 const result = try expr(gz, scope, rl, body_node);
1483 gz.force_comptime = false;1490 gz.force_comptime = false;
1484 return result;1491 return result;
1485}1492}