Skip to content

Functions should be able to initialize their arguments #793

Closed
@eholk

Description

@eholk

This came up in the course of writing comms as a library. I want to be able to write this:

fn recv_into(v : &T) {
    rustrt::port_recv(unsafe::reinterpret_cast(ptr::addr_of(v)),
                      **raw_port);
}

fn recv() -> T {
    let x : T;
    self.recv_into(x);
    ret x;
}

recv_into has the form it does because it calls into the runtime library, which needs a pointer to the place where the data should go. Ideally, we won't always want to initialize this, and in fact in some cases we may not be able to (for example, a tag type is exported from a module, but not its constructor).

What would be nice is some time of typestate annotation that says a parameter may not be initialized (or perhaps even stronger, is not initialized), and then declare as a postcondition that its argument is initialized.

I'm not sure what ramifications this might have as far as safety and typestate analysis, but this pattern is relatively common in a lot of systems code I've seen.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions