aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-12-02 20:16:47 +0200
committerVeikka Tuominen <git@vexu.eu>2022-12-03 00:09:23 +0200
commite2509ddbe69a56bb1f4a56b46946b2a706d5aabe (patch)
tree109fc0748d4e735b8a0624907bfb0610c06d8301 /src
parent0e38cc16d51178525e89774ce9151651b6a0e99a (diff)
downloadzig-e2509ddbe69a56bb1f4a56b46946b2a706d5aabe.tar.gz
zig-e2509ddbe69a56bb1f4a56b46946b2a706d5aabe.zip
AstGen: add error for invalid string comparisons
These operations are allowed because the string literals are just pointers but they produce unexpected results. These errors prevent beginners from shooting themselves in the foot while still allowing advanced users to circumvent them if they desire to do so. Closes #8290
Diffstat (limited to 'src')
-rw-r--r--src/AstGen.zig13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/AstGen.zig b/src/AstGen.zig
index b18d550347..1ec0cfa78f 100644
--- a/src/AstGen.zig
+++ b/src/AstGen.zig
@@ -5628,6 +5628,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;
@@ -6625,6 +6633,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 {