Commit 3de5c3b503

Veikka Tuominen <git@vexu.eu>
2022-09-23 11:43:01
Sema: check for slices in packed and extern type validation
Closes #12930
1 parent 8d1fdfc
src/Sema.zig
@@ -20841,9 +20841,9 @@ fn validateExternType(
         .Opaque,
         .Bool,
         .Float,
-        .Pointer,
         .AnyFrame,
         => return true,
+        .Pointer => return !ty.isSlice(),
         .Int => switch (ty.intInfo(sema.mod.getTarget()).bits) {
             8, 16, 32, 64, 128 => return true,
             else => return false,
@@ -20886,7 +20886,6 @@ fn explainWhyTypeIsNotExtern(
         .Opaque,
         .Bool,
         .Float,
-        .Pointer,
         .AnyFrame,
         => return,
 
@@ -20902,6 +20901,7 @@ fn explainWhyTypeIsNotExtern(
         .Frame,
         => return,
 
+        .Pointer => try mod.errNoteNonLazy(src_loc, msg, "slices have no guaranteed in-memory representation", .{}),
         .Void => try mod.errNoteNonLazy(src_loc, msg, "'void' is a zero bit type; for C 'void' use 'anyopaque'", .{}),
         .NoReturn => try mod.errNoteNonLazy(src_loc, msg, "'noreturn' is only allowed as a return type", .{}),
         .Int => if (ty.intInfo(sema.mod.getTarget()).bits > 128) {
@@ -20960,11 +20960,11 @@ fn validatePackedType(ty: Type) bool {
         .Void,
         .Bool,
         .Float,
-        .Pointer,
         .Int,
         .Vector,
         .Enum,
         => return true,
+        .Pointer => return !ty.isSlice(),
         .Struct, .Union => return ty.containerLayout() == .Packed,
     }
 }
@@ -20980,7 +20980,6 @@ fn explainWhyTypeIsNotPacked(
         .Void,
         .Bool,
         .Float,
-        .Pointer,
         .Int,
         .Vector,
         .Enum,
@@ -21001,6 +21000,7 @@ fn explainWhyTypeIsNotPacked(
         .Optional,
         .Array,
         => try mod.errNoteNonLazy(src_loc, msg, "type has no guaranteed in-memory representation", .{}),
+        .Pointer => try mod.errNoteNonLazy(src_loc, msg, "slices have no guaranteed in-memory representation", .{}),
         .Fn => {
             try mod.errNoteNonLazy(src_loc, msg, "type has no guaranteed in-memory representation", .{});
             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
@@ -60,6 +60,11 @@ const U = extern union {
     A: i32,
     B: u32,
 };
+export fn entry12() void {
+    _ = @sizeOf(packed struct {
+        x: packed struct { a: []u8 },
+    });
+}
 
 // error
 // backend=llvm
@@ -82,3 +87,5 @@ const U = extern union {
 // :38:9: error: packed structs cannot contain fields of type 'fn() void'
 // :38:9: note: type has no guaranteed in-memory representation
 // :38:9: note: use '*const ' to make a function pointer type
+// :65:28: error: packed structs cannot contain fields of type '[]u8'
+// :65:28: note: slices have no guaranteed in-memory representation
test/cases/compile_errors/slice_used_as_extern_fn_param.zig
@@ -0,0 +1,11 @@
+extern fn Text(str: []const u8, num: i32) callconv(.C) void; 
+export fn entry() void {
+    _ = Text;
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :1:16: error: parameter of type '[]const u8' not allowed in function with calling convention 'C'
+// :1:16: note: slices have no guaranteed in-memory representation