Skip to content

Unclear when Fn.return_type should be null #23075

@MasonRemaley

Description

@MasonRemaley

Sometimes, Fn.return_type is set to null. It's unclear what the the rule is here, there's currently a TODO in the docs:

TODO change the language spec to make this not optional.

I figured it was worth filing an issue to track either resolving this TODO, or replacing it with an explanation of when the value will be null and when it won't.

Feel free to close if this is a dup--I searched a bit but wasn't able to find anything, but there were a lot of false positives so I could've missed it.


Here's an example of a case where it ends up being null right now:

pub fn temp(a: anytype) @TypeOf(a) {
    std.debug.assert(@typeInfo(@TypeOf(temp)).@"fn".return_type.? == f32);
    return 0.0;
}

test temp {
    _ = temp(@as(f32, 0.0));
}

The real use case that lead to me wanting to do this is here. I was trying to avoid duplicating the logic that decides the return type, but pulling it out into a function is a bit messy since @TypeOf (for good reason) takes values not types so I'd end up having to do @TypeOf(@as(undefined, T)) etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions