Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Inconsistent evaluation order for assignment operations #27868

Closed

Description

@eefriedman

UPDATE: This is fixed by the MIR-based borrow checker and just needs a test. See the mentoring instructions below.


Currently, the borrow checker thinks that the RHS of += is evaluated before the LHS. trans thinks that the LHS is evaluated before the RHS. The disagreement leads to bad results.

fn main() {
    let x = Box::new(0);
    let mut y = 0;
    *{ drop(x); &mut y } += *x;
    assert_eq!(y, 0);
}

Activity

  1. nrc commented on Nov 19, 2015

    @nrc
    Member

    triage: P-medium

    Also fixed by MIR (almost a dup of #28160, but not quite)/

  2. added
    E-hard
    A-borrow-checker
    on Aug 4, 2016
  3. nikomatsakis commented on Oct 21, 2016

    @nikomatsakis
    Contributor
  4. added
    T-lang
    and removed on Mar 24, 2017
  5. Mark-Simulacrum commented on May 6, 2017

    @Mark-Simulacrum
    Member

    So the internals thread says this: "before MIR, we were actually quite inconsistent here, in that borrowck and trans disagreed on some of the particulars. MIR eliminated that inconsistency, but there are still some matters that we should try to resolve -- and quick!" If that's the case, then this should be closed. @nikomatsakis Is this the case?

  6. nikomatsakis commented on May 8, 2017

    @nikomatsakis
    Contributor

    Well, we're not yet running the borrowck on MIR, so I'm not sure.

  7. nikomatsakis commented on May 8, 2017

    @nikomatsakis
    Contributor

    But certainly the example seems to work ok now.

  8. RalfJung commented on Jun 15, 2017

    @RalfJung
    Member

    Assignment operators are still wrong, which can lead to use-after-free:

    use std::ops::AddAssign;
    
    struct MyVec<T>(Vec<T>);
    
    impl <T> Drop for MyVec<T> {
        fn drop(&mut self) {
            println!("Being dropped.");
        }
    }
    
    impl<T> AddAssign<T> for MyVec<T> {
        fn add_assign(&mut self, _elem: T) {
            println!("In add_assign.");
        }
    }
    
    fn main() {
        let mut vec = MyVec(vec![0]);
        let mut vecvec = vec![vec];
    
        vecvec[0] += {
            vecvec = vec![];
            0
        };
    }

    Prints

    Being dropped.
    In add_assign.
    

    Credit to @xfix for bringing this up in rust-lang/rfcs#2025 (comment), I just weaponized it.

  9. added
    I-unsound
    on Jun 15, 2017
  10. added
    C-bug
    on Jul 22, 2017
  11. 14 remaining items

  12. added
    fixed-by-NLL
    and removed
    E-needs-test
    on Oct 26, 2018
  13. pnkfelix commented on Apr 29, 2019

    @pnkfelix
    Contributor

    NLL (migrate mode) is enabled in all editions as of PR #59114.

    The only policy question is that, under migrate mode, we only emit a warning on this (unsound) test case. Therefore, I am not 100% sure whether we should close this until that warning has been turned into a hard-error under our (still in development) future-compatibility lint policy.

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

    A-NLLA-borrow-checkerC-bugE-mentorI-unsoundP-mediumT-langfixed-by-NLL

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions