Skip to content

Unexpected evaluation order of assignments #65910

Closed
@matthewjasper

Description

@matthewjasper

Assignment expressions where the right-hand side is a FRU or has a reborrow adjustment are not evaluated in the strict right to left order that they usually are. The following example should compile and the assertions shouldn't be triggered

fn evaluate_reborrow_before_assign() {
    let mut x = &1;
    let y = &mut &2;
    let z = &3;
    // There's an implicit reborrow of `x` on the right-hand side of the
    // assignement. Note that writing an explicit reborrow would not show this
    // bug, as now there would be two reborrows on the right-hand side and at
    // least one of them would happen before the left-hand side is evaluated.
    *{ x = z; &mut *y } = x;
    assert_eq!(*x, 3);
    assert_eq!(**y, 1);                    // 3 on stable
}

fn evaluate_fru_to_temp_before_assign_box() {
    let x = Box::new(S(0));
    let y = &mut S(1);
    *{ drop(x); &mut *y } = S { ..*x };    // error here on stable
    assert_eq!(0, y.0);
}

fn main() {
    evaluate_reborrow_before_assign();
    evaluate_fru_to_temp_before_assign_box();
}

Metadata

Metadata

Assignees

Labels

A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions