zig icon indicating copy to clipboard operation
zig copied to clipboard

return type 'null' not allowed

Open nektro opened this issue 11 months ago • 1 comments

Zig Version

0.11.0-dev.3620+c76ce25a6

Steps to Reproduce and Observed Behavior

const std = @import("std");

test {
    try std.testing.expect(foo() == null);
}

fn foo() @Type(.Null) {
    return null;
}
test.zig:7:10: error: return type '@TypeOf(null)' not allowed
fn foo() @Type(.Null) {
         ^~~~~~~~~~~~
referenced by:
    test_0: test.zig:4:28

Expected Behavior

null literal should behave as any other zero-sized type. I expect all 1 tests passed

nektro avatar Jun 18 '23 21:06 nektro

null literal should behave as any other zero-sized type

I think that means this should all pass too, just like if it was u0 or void:

test {
    var x: @TypeOf(null) = foo();
    try std.testing.expect(foo() == null);
    x = null;
    try std.testing.expect(foo() == null);
    bar(&x);
    try std.testing.expect(foo() == null);

    @compileLog(@sizeOf(@TypeOf(null)));
    @compileLog(@sizeOf(@Type(.Null)));
}

fn bar(x: *@TypeOf(null)) void {
    x.* = null;
}

fn foo() @Type(.Null) {
    return null;
}

Currently:

x.zig:4:12: error: variable of type '@TypeOf(null)' must be const or comptime
    var x: @TypeOf(null) = null;
           ^~~~~~~~~~~~~
x.zig:7:25: error: no size available for type '@TypeOf(null)'
    @compileLog(@sizeOf(@TypeOf(null))); // should be 0
                        ^~~~~~~~~~~~~
x.zig:13:8: error: parameter of type '*@TypeOf(null)' must be declared comptime
fn foo(x: *@TypeOf(null)) void {
       ^~~~~~~~~~~~~~~~~

Some relevant code is probably https://github.com/ziglang/zig/blob/423d7b848b1953173df99fde1f83166dc68c2a2c/src/type.zig#L2404-L2416

wooster0 avatar Jun 19 '23 06:06 wooster0