Skip to content

Commit 295b8ca

Browse files
kcbannerVexu
authored andcommitted
sema: add error for coercing a slice to an anyopaque pointer
1 parent 61236c2 commit 295b8ca

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Sema.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25093,6 +25093,13 @@ fn coerceExtra(
2509325093
} };
2509425094
break :pointer;
2509525095
}
25096+
if (inst_ty.isSlice()) {
25097+
in_memory_result = .{ .slice_to_anyopaque = .{
25098+
.actual = inst_ty,
25099+
.wanted = dest_ty,
25100+
} };
25101+
break :pointer;
25102+
}
2509625103
return sema.coerceCompatiblePtrs(block, dest_ty, inst, inst_src);
2509725104
}
2509825105

@@ -25603,6 +25610,7 @@ const InMemoryCoercionResult = union(enum) {
2560325610
ptr_bit_range: BitRange,
2560425611
ptr_alignment: IntPair,
2560525612
double_ptr_to_anyopaque: Pair,
25613+
slice_to_anyopaque: Pair,
2560625614

2560725615
const Pair = struct {
2560825616
actual: Type,
@@ -25901,6 +25909,13 @@ const InMemoryCoercionResult = union(enum) {
2590125909
});
2590225910
break;
2590325911
},
25912+
.slice_to_anyopaque => |pair| {
25913+
try sema.errNote(block, src, msg, "cannot implicitly cast slice '{}' to anyopaque pointer '{}'", .{
25914+
pair.actual.fmt(sema.mod), pair.wanted.fmt(sema.mod),
25915+
});
25916+
try sema.errNote(block, src, msg, "consider using '.ptr'", .{});
25917+
break;
25918+
},
2590425919
};
2590525920
}
2590625921
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export fn entry() void {
2+
const slice: []const u8 = "foo";
3+
const x = @as(*const anyopaque, slice);
4+
_ = x;
5+
}
6+
7+
// error
8+
// backend=stage2
9+
// target=native
10+
//
11+
// :3:37: error: expected type '*const anyopaque', found '[]const u8'
12+
// :3:37: note: cannot implicitly cast slice '[]const u8' to anyopaque pointer '*const anyopaque'
13+
// :3:37: note: consider using '.ptr'

0 commit comments

Comments
 (0)