authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-06-20 13:12:15-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-20 21:51:01-07:00
loga257e33fff8dd9efa7f43ea14e05eef65a7f3354
tree776121d7bc05ef6189f83b819130b859502cc2c5
parente2eabbbc5142f11defc56ad51cd5b8a5e97cdbda

Type: remove arbitrary restrictions on param and return types

Opaque and `noreturn` makes sense since they don't represent real values, but `null` and `undefined` are perfectly normal comptime-only values. Closes #16088

4 files changed, 17 insertions(+), 25 deletions(-)

src/type.zig+2-2
......@@ -2405,14 +2405,14 @@ pub const Type = struct {
24052405
24062406 pub fn isValidParamType(self: Type, mod: *const Module) bool {
24072407 return switch (self.zigTypeTagOrPoison(mod) catch return true) {
2408 .Undefined, .Null, .Opaque, .NoReturn => false,
2408 .Opaque, .NoReturn => false,
24092409 else => true,
24102410 };
24112411 }
24122412
24132413 pub fn isValidReturnType(self: Type, mod: *const Module) bool {
24142414 return switch (self.zigTypeTagOrPoison(mod) catch return true) {
2415 .Undefined, .Null, .Opaque => false,
2415 .Opaque => false,
24162416 else => true,
24172417 };
24182418 }
test/behavior/fn.zig+14
......@@ -580,3 +580,17 @@ test "lazy values passed to anytype parameter" {
580580 const D = struct {};
581581 try expect(@sizeOf(D) << 1 == 0);
582582}
583
584test "pass and return comptime-only types" {
585 const S = struct {
586 fn returnNull(comptime x: @Type(.Null)) @Type(.Null) {
587 return x;
588 }
589 fn returnUndefined(comptime x: @Type(.Undefined)) @Type(.Undefined) {
590 return x;
591 }
592 };
593
594 try expectEqual(null, S.returnNull(null));
595 try expectEqual(@as(u0, 0), S.returnUndefined(undefined));
596}
test/cases/compile_errors/function_parameter_is_opaque.zig+1-15
......@@ -4,11 +4,6 @@ export fn entry1() void {
44 _ = someFuncPtr;
55}
66
7export fn entry2() void {
8 const someFuncPtr: fn (@TypeOf(null)) void = undefined;
9 _ = someFuncPtr;
10}
11
127fn foo(p: FooType) void {
138 _ = p;
149}
......@@ -16,20 +11,11 @@ export fn entry3() void {
1611 _ = foo;
1712}
1813
19fn bar(p: @TypeOf(null)) void {
20 _ = p;
21}
22export fn entry4() void {
23 _ = bar;
24}
25
2614// error
2715// backend=stage2
2816// target=native
2917//
3018// :3:28: error: parameter of opaque type 'tmp.FooType' not allowed
3119// :1:17: note: opaque declared here
32// :8:28: error: parameter of type '@TypeOf(null)' not allowed
33// :12:8: error: parameter of opaque type 'tmp.FooType' not allowed
20// :7:8: error: parameter of opaque type 'tmp.FooType' not allowed
3421// :1:17: note: opaque declared here
35// :19:8: error: parameter of type '@TypeOf(null)' not allowed
test/cases/compile_errors/function_returning_opaque_type.zig-8
......@@ -2,12 +2,6 @@ const FooType = opaque {};
22export fn bar() FooType {
33 return error.InvalidValue;
44}
5export fn bav() @TypeOf(null) {
6 return error.InvalidValue;
7}
8export fn baz() @TypeOf(undefined) {
9 return error.InvalidValue;
10}
115
126// error
137// backend=stage2
......@@ -15,5 +9,3 @@ export fn baz() @TypeOf(undefined) {
159//
1610// :2:17: error: opaque return type 'tmp.FooType' not allowed
1711// :1:17: note: opaque declared here
18// :5:17: error: return type '@TypeOf(null)' not allowed
19// :8:17: error: return type '@TypeOf(undefined)' not allowed