authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-31 18:38:26-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-31 18:39:34-07:00
loge8143f6cbe3d1bdaeda5cd5af13447f6639b80ad
tree954e032848a0abb638e1e0af45dc4ec3114e8f3e
parentcec766f73c298d287c20d1bd830c159958fe177d

stage2: compile error for duplicate switch value on sparse


3 files changed, 27 insertions(+), 19 deletions(-)

BRANCH_TODO-13
......@@ -36,16 +36,3 @@ Performance optimizations to look into:
3636 * look into not emitting redundant dbg stmts to TZIR
3737 * make decl references in ZIR be u32 indexes to the Decl dependencies array hash map
3838 instead of duplicating *Decl entries in zir.Code.
39
40
41 for (inst.positionals.items) |item| {
42 const resolved = try sema.resolveInst(item);
43 const casted = try sema.coerce(block, operand.ty, resolved);
44 const val = try sema.resolveConstValue(block, item_src, casted);
45
46 if (try seen_values.fetchPut(val, item.src)) |prev| {
47 return sema.mod.fail(&block.base, item.src, "duplicate switch value", .{});
48 // TODO notes "previous value here" prev.value
49 }
50 }
51
src/Sema.zig+11-6
......@@ -62,8 +62,6 @@ const LazySrcLoc = Module.LazySrcLoc;
6262const RangeSet = @import("RangeSet.zig");
6363const AstGen = @import("AstGen.zig");
6464
65const ValueSrcMap = std.HashMap(Value, LazySrcLoc, Value.hash, Value.eql, std.hash_map.DefaultMaxLoadPercentage);
66
6765pub fn root(sema: *Sema, root_block: *Scope.Block) !zir.Inst.Index {
6866 const inst_data = sema.code.instructions.items(.data)[0].pl_node;
6967 const extra = sema.code.extraData(zir.Inst.Block, inst_data.payload_index);
......@@ -2557,7 +2555,7 @@ fn analyzeSwitch(
25572555
25582556 var extra_index: usize = special.end;
25592557 {
2560 var scalar_i: usize = 0;
2558 var scalar_i: u32 = 0;
25612559 while (scalar_i < scalar_cases_len) : (scalar_i += 1) {
25622560 const item_ref = @intToEnum(zir.Inst.Ref, sema.code.extra[extra_index]);
25632561 extra_index += 1;
......@@ -2571,11 +2569,12 @@ fn analyzeSwitch(
25712569 &seen_values,
25722570 item_ref,
25732571 src_node_offset,
2572 .{ .scalar = scalar_i },
25742573 );
25752574 }
25762575 }
25772576 {
2578 var multi_i: usize = 0;
2577 var multi_i: u32 = 0;
25792578 while (multi_i < multi_cases_len) : (multi_i += 1) {
25802579 const items_len = sema.code.extra[extra_index];
25812580 extra_index += 1;
......@@ -2586,12 +2585,13 @@ fn analyzeSwitch(
25862585 const items = sema.code.refSlice(extra_index, items_len);
25872586 extra_index += items_len + body_len;
25882587
2589 for (items) |item_ref| {
2588 for (items) |item_ref, item_i| {
25902589 try sema.validateSwitchItemSparse(
25912590 block,
25922591 &seen_values,
25932592 item_ref,
25942593 src_node_offset,
2594 .{ .multi = .{ .prong = multi_i, .item = @intCast(u32, item_i) } },
25952595 );
25962596 }
25972597
......@@ -2981,14 +2981,19 @@ fn validateSwitchItemBool(
29812981 }
29822982}
29832983
2984const ValueSrcMap = std.HashMap(Value, AstGen.SwitchProngSrc, Value.hash, Value.eql, std.hash_map.DefaultMaxLoadPercentage);
2985
29842986fn validateSwitchItemSparse(
29852987 sema: *Sema,
29862988 block: *Scope.Block,
29872989 seen_values: *ValueSrcMap,
29882990 item_ref: zir.Inst.Ref,
29892991 src_node_offset: i32,
2992 switch_prong_src: AstGen.SwitchProngSrc,
29902993) InnerError!void {
2991 @panic("TODO");
2994 const item_val = try sema.resolveSwitchItemVal(block, item_ref, src_node_offset, switch_prong_src, .none);
2995 const entry = (try seen_values.fetchPut(item_val, switch_prong_src)) orelse return;
2996 return sema.validateSwitchDupe(block, entry.value, switch_prong_src, src_node_offset);
29922997}
29932998
29942999fn validateSwitchNoRange(
test/stage2/cbe.zig+16
......@@ -359,6 +359,22 @@ pub fn addCases(ctx: *TestContext) !void {
359359 , &.{
360360 ":6:9: error: duplicate switch value",
361361 });
362
363 // Sparse (no range capable) switch expression has duplicate case value.
364 case.addError(
365 \\export fn main() c_int {
366 \\ const A: type = i32;
367 \\ const b: c_int = switch (A) {
368 \\ i32 => 1,
369 \\ bool => 2,
370 \\ f64, i32 => 3,
371 \\ else => 4,
372 \\ };
373 \\}
374 , &.{
375 ":6:14: error: duplicate switch value",
376 ":4:9: note: previous value here",
377 });
362378 }
363379 //{
364380 // var case = ctx.exeFromCompiledC("optionals", .{});