Skip to content

Typestate doesn't track initialization through closures #1259

Closed
@jdm

Description

@jdm
Contributor
fn foo(bl: block(int)) {
    let val = 5;
    bl(val);
}

fn main() {
    let val;
    foo({|new_val| val = new_val;});
    log_err val;
}

yields this error:

/Users/jdm/src/closure.rs:8:8: 8:34 error: Unsatisfied precondition constraint (for example, init(val [/Users/jdm/src/closure.rs:7:8: 7:11])) for expression:
{|new_val| val = new_val; }
Precondition:
init(val [/Users/jdm/src/closure.rs:7:8: 7:11])
Prestate:
!init(val [/Users/jdm/src/closure.rs:7:8: 7:11]), !init(main [/Users/jdm/src/closure.rs:6:0: 10:0]), !init(main! [/Users/jdm/src/closure.rs:6:0: 10:0])
/Users/jdm/src/closure.rs:8     foo({|new_val| val = new_val;});
                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~
rust: upcall fail 'explicit failure', ../src/comp/driver/session.rs:66
rust: domain main @0x10202c400 root task failed

Activity

graydon

graydon commented on Dec 5, 2011

@graydon
Contributor

The type state system has no idea that the block gets run. It sees you using an uninitialized value in the log statement.

marijnh

marijnh commented on Dec 7, 2011

@marijnh
Contributor

Right. Calling a block zero times is valid, so the error message is correct in the above case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @graydon@jdm@marijnh

        Issue actions

          Typestate doesn't track initialization through closures · Issue #1259 · rust-lang/rust