authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-23 12:43:01+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-23 17:39:06+03:00
log3de5c3b5038d03759d8f5521627fdbbcad28c795
treebc15e22562a59f73c478454290fe4307d1a163be
parent8d1fdfc8ede94b1aea524041c1df10c42d4002e4

Sema: check for slices in packed and extern type validation

Closes #12930

3 files changed, 22 insertions(+), 4 deletions(-)

src/Sema.zig+4-4
......@@ -20841,9 +20841,9 @@ fn validateExternType(
2084120841 .Opaque,
2084220842 .Bool,
2084320843 .Float,
20844 .Pointer,
2084520844 .AnyFrame,
2084620845 => return true,
20846 .Pointer => return !ty.isSlice(),
2084720847 .Int => switch (ty.intInfo(sema.mod.getTarget()).bits) {
2084820848 8, 16, 32, 64, 128 => return true,
2084920849 else => return false,
......@@ -20886,7 +20886,6 @@ fn explainWhyTypeIsNotExtern(
2088620886 .Opaque,
2088720887 .Bool,
2088820888 .Float,
20889 .Pointer,
2089020889 .AnyFrame,
2089120890 => return,
2089220891
......@@ -20902,6 +20901,7 @@ fn explainWhyTypeIsNotExtern(
2090220901 .Frame,
2090320902 => return,
2090420903
20904 .Pointer => try mod.errNoteNonLazy(src_loc, msg, "slices have no guaranteed in-memory representation", .{}),
2090520905 .Void => try mod.errNoteNonLazy(src_loc, msg, "'void' is a zero bit type; for C 'void' use 'anyopaque'", .{}),
2090620906 .NoReturn => try mod.errNoteNonLazy(src_loc, msg, "'noreturn' is only allowed as a return type", .{}),
2090720907 .Int => if (ty.intInfo(sema.mod.getTarget()).bits > 128) {
......@@ -20960,11 +20960,11 @@ fn validatePackedType(ty: Type) bool {
2096020960 .Void,
2096120961 .Bool,
2096220962 .Float,
20963 .Pointer,
2096420963 .Int,
2096520964 .Vector,
2096620965 .Enum,
2096720966 => return true,
20967 .Pointer => return !ty.isSlice(),
2096820968 .Struct, .Union => return ty.containerLayout() == .Packed,
2096920969 }
2097020970}
......@@ -20980,7 +20980,6 @@ fn explainWhyTypeIsNotPacked(
2098020980 .Void,
2098120981 .Bool,
2098220982 .Float,
20983 .Pointer,
2098420983 .Int,
2098520984 .Vector,
2098620985 .Enum,
......@@ -21001,6 +21000,7 @@ fn explainWhyTypeIsNotPacked(
2100121000 .Optional,
2100221001 .Array,
2100321002 => try mod.errNoteNonLazy(src_loc, msg, "type has no guaranteed in-memory representation", .{}),
21003 .Pointer => try mod.errNoteNonLazy(src_loc, msg, "slices have no guaranteed in-memory representation", .{}),
2100421004 .Fn => {
2100521005 try mod.errNoteNonLazy(src_loc, msg, "type has no guaranteed in-memory representation", .{});
2100621006 try mod.errNoteNonLazy(src_loc, msg, "use '*const ' to make a function pointer type", .{});
test/cases/compile_errors/packed_struct_with_fields_of_not_allowed_types.zig+7
......@@ -60,6 +60,11 @@ const U = extern union {
6060 A: i32,
6161 B: u32,
6262};
63export fn entry12() void {
64 _ = @sizeOf(packed struct {
65 x: packed struct { a: []u8 },
66 });
67}
6368
6469// error
6570// backend=llvm
......@@ -82,3 +87,5 @@ const U = extern union {
8287// :38:9: error: packed structs cannot contain fields of type 'fn() void'
8388// :38:9: note: type has no guaranteed in-memory representation
8489// :38:9: note: use '*const ' to make a function pointer type
90// :65:28: error: packed structs cannot contain fields of type '[]u8'
91// :65:28: note: slices have no guaranteed in-memory representation
test/cases/compile_errors/slice_used_as_extern_fn_param.zig created+11
......@@ -0,0 +1,11 @@
1extern fn Text(str: []const u8, num: i32) callconv(.C) void;
2export fn entry() void {
3 _ = Text;
4}
5
6// error
7// backend=stage2
8// target=native
9//
10// :1:16: error: parameter of type '[]const u8' not allowed in function with calling convention 'C'
11// :1:16: note: slices have no guaranteed in-memory representation