| author | |
| committer | |
| log | a257e33fff8dd9efa7f43ea14e05eef65a7f3354 |
| tree | 776121d7bc05ef6189f83b819130b859502cc2c5 |
| parent | e2eabbbc5142f11defc56ad51cd5b8a5e97cdbda |
Opaque and `noreturn` makes sense since they don't represent real
values, but `null` and `undefined` are perfectly normal
comptime-only values.
Closes #160884 files changed, 17 insertions(+), 25 deletions(-)
src/type.zig+2-2| ... | ... | @@ -2405,14 +2405,14 @@ pub const Type = struct { |
| 2405 | 2405 | |
| 2406 | 2406 | pub fn isValidParamType(self: Type, mod: *const Module) bool { |
| 2407 | 2407 | return switch (self.zigTypeTagOrPoison(mod) catch return true) { |
| 2408 | .Undefined, .Null, .Opaque, .NoReturn => false, | |
| 2408 | .Opaque, .NoReturn => false, | |
| 2409 | 2409 | else => true, |
| 2410 | 2410 | }; |
| 2411 | 2411 | } |
| 2412 | 2412 | |
| 2413 | 2413 | pub fn isValidReturnType(self: Type, mod: *const Module) bool { |
| 2414 | 2414 | return switch (self.zigTypeTagOrPoison(mod) catch return true) { |
| 2415 | .Undefined, .Null, .Opaque => false, | |
| 2415 | .Opaque => false, | |
| 2416 | 2416 | else => true, |
| 2417 | 2417 | }; |
| 2418 | 2418 | } |
test/behavior/fn.zig+14| ... | ... | @@ -580,3 +580,17 @@ test "lazy values passed to anytype parameter" { |
| 580 | 580 | const D = struct {}; |
| 581 | 581 | try expect(@sizeOf(D) << 1 == 0); |
| 582 | 582 | } |
| 583 | ||
| 584 | test "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 | 4 | _ = someFuncPtr; |
| 5 | 5 | } |
| 6 | 6 | |
| 7 | export fn entry2() void { | |
| 8 | const someFuncPtr: fn (@TypeOf(null)) void = undefined; | |
| 9 | _ = someFuncPtr; | |
| 10 | } | |
| 11 | ||
| 12 | 7 | fn foo(p: FooType) void { |
| 13 | 8 | _ = p; |
| 14 | 9 | } |
| ... | ... | @@ -16,20 +11,11 @@ export fn entry3() void { |
| 16 | 11 | _ = foo; |
| 17 | 12 | } |
| 18 | 13 | |
| 19 | fn bar(p: @TypeOf(null)) void { | |
| 20 | _ = p; | |
| 21 | } | |
| 22 | export fn entry4() void { | |
| 23 | _ = bar; | |
| 24 | } | |
| 25 | ||
| 26 | 14 | // error |
| 27 | 15 | // backend=stage2 |
| 28 | 16 | // target=native |
| 29 | 17 | // |
| 30 | 18 | // :3:28: error: parameter of opaque type 'tmp.FooType' not allowed |
| 31 | 19 | // :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 | |
| 34 | 21 | // :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 | 2 | export fn bar() FooType { |
| 3 | 3 | return error.InvalidValue; |
| 4 | 4 | } |
| 5 | export fn bav() @TypeOf(null) { | |
| 6 | return error.InvalidValue; | |
| 7 | } | |
| 8 | export fn baz() @TypeOf(undefined) { | |
| 9 | return error.InvalidValue; | |
| 10 | } | |
| 11 | 5 | |
| 12 | 6 | // error |
| 13 | 7 | // backend=stage2 |
| ... | ... | @@ -15,5 +9,3 @@ export fn baz() @TypeOf(undefined) { |
| 15 | 9 | // |
| 16 | 10 | // :2:17: error: opaque return type 'tmp.FooType' not allowed |
| 17 | 11 | // :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 |