aboutsummaryrefslogtreecommitdiff
path: root/src/AstGen.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-12-03 00:42:11 -0500
committerGitHub <noreply@github.com>2022-12-03 00:42:11 -0500
commitfdbb0fb7b9c08ebff1b7e45ef89f7160f350d44c (patch)
tree714f2766c64ace45df1f7d67ca70be0c88193184 /src/AstGen.zig
parentc43ac67f82cb5a022df67729aa1e6bebc22cfff2 (diff)
parentb500e0eb179218f5eb03408c09b5e5a928f0c46e (diff)
downloadzig-fdbb0fb7b9c08ebff1b7e45ef89f7160f350d44c.tar.gz
zig-fdbb0fb7b9c08ebff1b7e45ef89f7160f350d44c.zip
Merge pull request #13744 from Vexu/stage2-fixes
Improve error messages, fix dependency loops
Diffstat (limited to 'src/AstGen.zig')
-rw-r--r--src/AstGen.zig48
1 files changed, 41 insertions, 7 deletions
diff --git a/src/AstGen.zig b/src/AstGen.zig
index 009d05e5ed..4e571ffda9 100644
--- a/src/AstGen.zig
+++ b/src/AstGen.zig
@@ -2632,7 +2632,7 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
.compile_error,
.ret_node,
.ret_load,
- .ret_tok,
+ .ret_implicit,
.ret_err_value,
.@"unreachable",
.repeat,
@@ -3696,6 +3696,29 @@ fn fnDecl(
if (param.anytype_ellipsis3) |tok| {
return astgen.failTok(tok, "missing parameter name", .{});
} else {
+ ambiguous: {
+ if (tree.nodes.items(.tag)[param.type_expr] != .identifier) break :ambiguous;
+ const main_token = tree.nodes.items(.main_token)[param.type_expr];
+ const identifier_str = tree.tokenSlice(main_token);
+ if (isPrimitive(identifier_str)) break :ambiguous;
+ return astgen.failNodeNotes(
+ param.type_expr,
+ "missing parameter name or type",
+ .{},
+ &[_]u32{
+ try astgen.errNoteNode(
+ param.type_expr,
+ "if this is a name, annotate its type '{s}: T'",
+ .{identifier_str},
+ ),
+ try astgen.errNoteNode(
+ param.type_expr,
+ "if this is a type, give it a name '<name>: {s}'",
+ .{identifier_str},
+ ),
+ },
+ );
+ }
return astgen.failNode(param.type_expr, "missing parameter name", .{});
}
} else 0;
@@ -3891,9 +3914,8 @@ fn fnDecl(
// As our last action before the return, "pop" the error trace if needed
_ = try gz.addRestoreErrRetIndex(.ret, .always);
- // Since we are adding the return instruction here, we must handle the coercion.
- // We do this by using the `ret_tok` instruction.
- _ = try fn_gz.addUnTok(.ret_tok, .void_value, tree.lastToken(body_node));
+ // Add implicit return at end of function.
+ _ = try fn_gz.addUnTok(.ret_implicit, .void_value, tree.lastToken(body_node));
}
break :func try decl_gz.addFunc(.{
@@ -4311,9 +4333,8 @@ fn testDecl(
// As our last action before the return, "pop" the error trace if needed
_ = try gz.addRestoreErrRetIndex(.ret, .always);
- // Since we are adding the return instruction here, we must handle the coercion.
- // We do this by using the `ret_tok` instruction.
- _ = try fn_block.addUnTok(.ret_tok, .void_value, tree.lastToken(body_node));
+ // Add implicit return at end of function.
+ _ = try fn_block.addUnTok(.ret_implicit, .void_value, tree.lastToken(body_node));
}
const func_inst = try decl_block.addFunc(.{
@@ -5605,6 +5626,14 @@ fn simpleBinOp(
const tree = astgen.tree;
const node_datas = tree.nodes.items(.data);
+ if (op_inst_tag == .cmp_neq or op_inst_tag == .cmp_eq) {
+ const node_tags = tree.nodes.items(.tag);
+ const str = if (op_inst_tag == .cmp_eq) "==" else "!=";
+ if (node_tags[node_datas[node].lhs] == .string_literal or
+ node_tags[node_datas[node].rhs] == .string_literal)
+ return astgen.failNode(node, "cannot compare strings with {s}", .{str});
+ }
+
const lhs = try reachableExpr(gz, scope, .{ .rl = .none }, node_datas[node].lhs, node);
var line: u32 = undefined;
var column: u32 = undefined;
@@ -6602,6 +6631,11 @@ fn switchExpr(
continue;
}
+ for (case.ast.values) |val| {
+ if (node_tags[val] == .string_literal)
+ return astgen.failNode(val, "cannot switch on strings", .{});
+ }
+
if (case.ast.values.len == 1 and node_tags[case.ast.values[0]] != .switch_range) {
scalar_cases_len += 1;
} else {