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 {...@@ -2405,14 +2405,14 @@ pub const Type = struct {
24052405
2406 pub fn isValidParamType(self: Type, mod: *const Module) bool {2406 pub fn isValidParamType(self: Type, mod: *const Module) bool {
2407 return switch (self.zigTypeTagOrPoison(mod) catch return true) {2407 return switch (self.zigTypeTagOrPoison(mod) catch return true) {
2408 .Undefined, .Null, .Opaque, .NoReturn => false,2408 .Opaque, .NoReturn => false,
2409 else => true,2409 else => true,
2410 };2410 };
2411 }2411 }
24122412
2413 pub fn isValidReturnType(self: Type, mod: *const Module) bool {2413 pub fn isValidReturnType(self: Type, mod: *const Module) bool {
2414 return switch (self.zigTypeTagOrPoison(mod) catch return true) {2414 return switch (self.zigTypeTagOrPoison(mod) catch return true) {
2415 .Undefined, .Null, .Opaque => false,2415 .Opaque => false,
2416 else => true,2416 else => true,
2417 };2417 };
2418 }2418 }
test/behavior/fn.zig+14
...@@ -580,3 +580,17 @@ test "lazy values passed to anytype parameter" {...@@ -580,3 +580,17 @@ test "lazy values passed to anytype parameter" {
580 const D = struct {};580 const D = struct {};
581 try expect(@sizeOf(D) << 1 == 0);581 try expect(@sizeOf(D) << 1 == 0);
582}582}
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 {...@@ -4,11 +4,6 @@ export fn entry1() void {
4 _ = someFuncPtr;4 _ = someFuncPtr;
5}5}
66
7export fn entry2() void {
8 const someFuncPtr: fn (@TypeOf(null)) void = undefined;
9 _ = someFuncPtr;
10}
11
12fn foo(p: FooType) void {7fn foo(p: FooType) void {
13 _ = p;8 _ = p;
14}9}
...@@ -16,20 +11,11 @@ export fn entry3() void {...@@ -16,20 +11,11 @@ export fn entry3() void {
16 _ = foo;11 _ = foo;
17}12}
1813
19fn bar(p: @TypeOf(null)) void {
20 _ = p;
21}
22export fn entry4() void {
23 _ = bar;
24}
25
26// error14// error
27// backend=stage215// backend=stage2
28// target=native16// target=native
29//17//
30// :3:28: error: parameter of opaque type 'tmp.FooType' not allowed18// :3:28: error: parameter of opaque type 'tmp.FooType' not allowed
31// :1:17: note: opaque declared here19// :1:17: note: opaque declared here
32// :8:28: error: parameter of type '@TypeOf(null)' not allowed20// :7:8: error: parameter of opaque type 'tmp.FooType' not allowed
33// :12:8: error: parameter of opaque type 'tmp.FooType' not allowed
34// :1:17: note: opaque declared here21// :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 {};...@@ -2,12 +2,6 @@ const FooType = opaque {};
2export fn bar() FooType {2export fn bar() FooType {
3 return error.InvalidValue;3 return error.InvalidValue;
4}4}
5export fn bav() @TypeOf(null) {
6 return error.InvalidValue;
7}
8export fn baz() @TypeOf(undefined) {
9 return error.InvalidValue;
10}
115
12// error6// error
13// backend=stage27// backend=stage2
...@@ -15,5 +9,3 @@ export fn baz() @TypeOf(undefined) {...@@ -15,5 +9,3 @@ export fn baz() @TypeOf(undefined) {
15//9//
16// :2:17: error: opaque return type 'tmp.FooType' not allowed10// :2:17: error: opaque return type 'tmp.FooType' not allowed
17// :1:17: note: opaque declared here11// :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