zig icon indicating copy to clipboard operation
zig copied to clipboard

missing 'defined here' notes on equality compile error

Open nektro opened this issue 1 year ago • 4 comments

Zig Version

0.12.0-dev.1828+225fe6ddb

Steps to Reproduce and Observed Behavior

const std = @import("std");

const E = enum(u32) {
    a,
    b,
    c,
    _,
};

const S = struct {
    a: u32,
    b: u32,
    c: u32,
};

test {
    var x: S = std.mem.zeroes(S);
    _ = &x;

    var y: bool = x.a == E.b;
    _ = &y;
}
test.zig:20:23: error: incompatible types: 'u32' and 'test.E'
    var y: bool = x.a == E.b;
                  ~~~~^~~~~~
test.zig:20:20: note: type 'u32' here
    var y: bool = x.a == E.b;
                  ~^~
test.zig:20:27: note: type 'test.E' here
    var y: bool = x.a == E.b;
                         ~^~

Expected Behavior

same as above but with the addition of test.zig:10:1: note: 'S' defined here and test.zig:3:1: note: 'E' defined here

nektro avatar Mar 08 '24 20:03 nektro

(oops, meant to file this under error message)

nektro avatar Mar 08 '24 20:03 nektro

I see no purpose in providing a "defined here" note. The error message is focused on the fact that you cannot compare u32 to an enum. Which enum is at fault has nothing to do with it.

Rexicon226 avatar Mar 09 '24 23:03 Rexicon226

perhaps, but the u32 came from a struct field and i might wanna go update the type of that field, which isnt trivial to know which one in a case where S is from a generic function. this was a reduction after all.

nektro avatar Mar 10 '24 04:03 nektro

I found a small addition that can partially address this issue. Unfortunately, for the example above it only adds a note for the source location of E.

Output with additional note:

src/main.zig:20:23: error: incompatible types: 'u32' and 'main.E'
    var y: bool = x.a == E.b;
                  ~~~~^~~~~~
src/main.zig:20:20: note: type 'u32' here
    var y: bool = x.a == E.b;
                  ~^~
src/main.zig:20:27: note: type 'main.E' here
    var y: bool = x.a == E.b;
                         ~^~
src/main.zig:3:11: note: type 'main.E' declared here
const E = enum(u32) {
          ^~~~
Diff
diff --git a/src/Sema.zig b/src/Sema.zig
index e9b6615f14..1ddf3244b8 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -33909,7 +33909,20 @@ const PeerResolveResult = union(enum) {
             };
 
             if (conflict_srcs[0]) |src_loc| try sema.errNote(src_loc, msg, "type '{}' here", .{conflict_tys[0].fmt(pt)});
+            if (conflict_tys[conflict_idx[0]].srcLocOrNull(pt.zcu)) |loc| try sema.errNote(
+                loc,
+                msg,
+                "type '{}' declared here",
+                .{conflict_tys[0].fmt(pt)},
+            );
+
             if (conflict_srcs[1]) |src_loc| try sema.errNote(src_loc, msg, "type '{}' here", .{conflict_tys[1].fmt(pt)});
+            if (conflict_tys[conflict_idx[1]].srcLocOrNull(pt.zcu)) |loc| try sema.errNote(
+                loc,
+                msg,
+                "type '{}' declared here",
+                .{conflict_tys[1].fmt(pt)},
+            );
 
             // No child error
             break;

WillLillis avatar Jul 21 '24 05:07 WillLillis