Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that non-overwrite accesses to downcast projections are dominated by variant checks. #59578

Open
eddyb opened this issue Mar 31, 2019 · 0 comments
Labels
A-codegen Area: Code generation A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@eddyb
Copy link
Member

eddyb commented Mar 31, 2019

Pattern-matching in Rust, e.g.:

fn f<T>(_: T) {}
fn g<T>(_: T) {}

// T and E have Copy bounds to reduce MIR verbosity. 
pub fn foo<T: Copy, E: Copy>(r: Result<T, E>) {
    match r {
        Ok(x) => f(x),
        Err(e) => g(e),
    }
}

turns into this MIR (slightly cleaned up):

fn foo(_1: std::result::Result<T, E>) -> () {
    let mut _0: ();                      // return place
    let mut _2: isize;
    let mut _3: T;
    let mut _4: E;

    bb0: {
        _2 = discriminant(_1);
        switchInt(move _2) -> [0isize: bb2, 1isize: bb3, otherwise: bb1];
    }

    bb1: {
        unreachable;
    }

    bb2: {
        _3 = ((_1 as Ok).0: T);
        _0 = const f(move _3) -> bb4;
    }

    bb3: {
        _4 = ((_1 as Err).0: E);
        _0 = const g(move _4) -> bb4;
    }

    bb4: {
        return;
    }
}

We already have a dominator tree for MIR, so we can build on top of that and compute the known variants for places (in this case, Ok and Err for _1).

Then we can just check that any read/borrow/etc. access (any access with does not fully overwrite the previous value, really) within a downcast (e.g. (_1 as Ok).0) is dominated by a variant check for that variant (i.e. _1 being Ok, via _2 being discriminant(_1)).

That said, the kind of dataflow borrowck already needs to do might easily include this too (e.g. treating (_1 as Ok) as initialized iff _1 is initialized and discriminant(_1) == 0 was checked).

(Also tempting: moving Discriminant into Operand to be able to get rid of the _2 and have switchInt(discriminant(_1)) directly)

cc @rust-lang/wg-compiler-nll @oli

@Centril Centril added A-codegen Area: Code generation A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 31, 2019
@jonas-schievink jonas-schievink added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Mar 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants