diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-09-23 12:43:01 +0300 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2022-09-23 17:39:06 +0300 |
| commit | 3de5c3b5038d03759d8f5521627fdbbcad28c795 (patch) | |
| tree | bc15e22562a59f73c478454290fe4307d1a163be | |
| parent | 8d1fdfc8ede94b1aea524041c1df10c42d4002e4 (diff) | |
| download | zig-3de5c3b5038d03759d8f5521627fdbbcad28c795.tar.gz zig-3de5c3b5038d03759d8f5521627fdbbcad28c795.zip | |
Sema: check for slices in packed and extern type validation
Closes #12930
| -rw-r--r-- | src/Sema.zig | 8 | ||||
| -rw-r--r-- | test/cases/compile_errors/packed_struct_with_fields_of_not_allowed_types.zig | 7 | ||||
| -rw-r--r-- | test/cases/compile_errors/slice_used_as_extern_fn_param.zig | 11 |
3 files changed, 22 insertions, 4 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index be53c99f24..d0ddbdbfec 100644 --- a/src/Sema.zig +++ b/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", .{}); diff --git a/test/cases/compile_errors/packed_struct_with_fields_of_not_allowed_types.zig b/test/cases/compile_errors/packed_struct_with_fields_of_not_allowed_types.zig index 8fcd300629..fd98db04c8 100644 --- a/test/cases/compile_errors/packed_struct_with_fields_of_not_allowed_types.zig +++ b/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 diff --git a/test/cases/compile_errors/slice_used_as_extern_fn_param.zig b/test/cases/compile_errors/slice_used_as_extern_fn_param.zig new file mode 100644 index 0000000000..8391c3e21b --- /dev/null +++ b/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 |
