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(...@@ -20841,9 +20841,9 @@ fn validateExternType(
20841 .Opaque,20841 .Opaque,
20842 .Bool,20842 .Bool,
20843 .Float,20843 .Float,
20844 .Pointer,
20845 .AnyFrame,20844 .AnyFrame,
20846 => return true,20845 => return true,
20846 .Pointer => return !ty.isSlice(),
20847 .Int => switch (ty.intInfo(sema.mod.getTarget()).bits) {20847 .Int => switch (ty.intInfo(sema.mod.getTarget()).bits) {
20848 8, 16, 32, 64, 128 => return true,20848 8, 16, 32, 64, 128 => return true,
20849 else => return false,20849 else => return false,
...@@ -20886,7 +20886,6 @@ fn explainWhyTypeIsNotExtern(...@@ -20886,7 +20886,6 @@ fn explainWhyTypeIsNotExtern(
20886 .Opaque,20886 .Opaque,
20887 .Bool,20887 .Bool,
20888 .Float,20888 .Float,
20889 .Pointer,
20890 .AnyFrame,20889 .AnyFrame,
20891 => return,20890 => return,
2089220891
...@@ -20902,6 +20901,7 @@ fn explainWhyTypeIsNotExtern(...@@ -20902,6 +20901,7 @@ fn explainWhyTypeIsNotExtern(
20902 .Frame,20901 .Frame,
20903 => return,20902 => return,
2090420903
20904 .Pointer => try mod.errNoteNonLazy(src_loc, msg, "slices have no guaranteed in-memory representation", .{}),
20905 .Void => try mod.errNoteNonLazy(src_loc, msg, "'void' is a zero bit type; for C 'void' use 'anyopaque'", .{}),20905 .Void => try mod.errNoteNonLazy(src_loc, msg, "'void' is a zero bit type; for C 'void' use 'anyopaque'", .{}),
20906 .NoReturn => try mod.errNoteNonLazy(src_loc, msg, "'noreturn' is only allowed as a return type", .{}),20906 .NoReturn => try mod.errNoteNonLazy(src_loc, msg, "'noreturn' is only allowed as a return type", .{}),
20907 .Int => if (ty.intInfo(sema.mod.getTarget()).bits > 128) {20907 .Int => if (ty.intInfo(sema.mod.getTarget()).bits > 128) {
...@@ -20960,11 +20960,11 @@ fn validatePackedType(ty: Type) bool {...@@ -20960,11 +20960,11 @@ fn validatePackedType(ty: Type) bool {
20960 .Void,20960 .Void,
20961 .Bool,20961 .Bool,
20962 .Float,20962 .Float,
20963 .Pointer,
20964 .Int,20963 .Int,
20965 .Vector,20964 .Vector,
20966 .Enum,20965 .Enum,
20967 => return true,20966 => return true,
20967 .Pointer => return !ty.isSlice(),
20968 .Struct, .Union => return ty.containerLayout() == .Packed,20968 .Struct, .Union => return ty.containerLayout() == .Packed,
20969 }20969 }
20970}20970}
...@@ -20980,7 +20980,6 @@ fn explainWhyTypeIsNotPacked(...@@ -20980,7 +20980,6 @@ fn explainWhyTypeIsNotPacked(
20980 .Void,20980 .Void,
20981 .Bool,20981 .Bool,
20982 .Float,20982 .Float,
20983 .Pointer,
20984 .Int,20983 .Int,
20985 .Vector,20984 .Vector,
20986 .Enum,20985 .Enum,
...@@ -21001,6 +21000,7 @@ fn explainWhyTypeIsNotPacked(...@@ -21001,6 +21000,7 @@ fn explainWhyTypeIsNotPacked(
21001 .Optional,21000 .Optional,
21002 .Array,21001 .Array,
21003 => try mod.errNoteNonLazy(src_loc, msg, "type has no guaranteed in-memory representation", .{}),21002 => 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", .{}),
21004 .Fn => {21004 .Fn => {
21005 try mod.errNoteNonLazy(src_loc, msg, "type has no guaranteed in-memory representation", .{});21005 try mod.errNoteNonLazy(src_loc, msg, "type has no guaranteed in-memory representation", .{});
21006 try mod.errNoteNonLazy(src_loc, msg, "use '*const ' to make a function pointer type", .{});21006 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 {...@@ -60,6 +60,11 @@ const U = extern union {
60 A: i32,60 A: i32,
61 B: u32,61 B: u32,
62};62};
63export fn entry12() void {
64 _ = @sizeOf(packed struct {
65 x: packed struct { a: []u8 },
66 });
67}
6368
64// error69// error
65// backend=llvm70// backend=llvm
...@@ -82,3 +87,5 @@ const U = extern union {...@@ -82,3 +87,5 @@ const U = extern union {
82// :38:9: error: packed structs cannot contain fields of type 'fn() void'87// :38:9: error: packed structs cannot contain fields of type 'fn() void'
83// :38:9: note: type has no guaranteed in-memory representation88// :38:9: note: type has no guaranteed in-memory representation
84// :38:9: note: use '*const ' to make a function pointer type89// :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