Skip to content

ICE (failed assertion) when taking mutable references to enum contents #23175

Closed
@bluecube

Description

@bluecube

I tried this code:

enum Foo {
    NotLast(Box<Foo>),
    Last(u32)
}

fn x(tree: &mut Foo) {
    let mut current = tree;

    loop {
        match current {
            &mut Foo::NotLast(ref mut inner) => {
                current = &mut *inner;
            },
            &mut Foo::Last(ref mut val) => {
                *val = 1;
            },
        }
    }
}

fn main() {}

And got the following message:

error.rs:11:31: 11:44 error: cannot borrow `current.0` as mutable more than once at a time
error.rs:11             &mut Foo::NotLast(ref mut inner) => {
                                          ^~~~~~~~~~~~~
error.rs:11:31: 11:44 note: previous borrow of `current.0` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `current.0` until the borrow ends
error.rs:11             &mut Foo::NotLast(ref mut inner) => {
                                          ^~~~~~~~~~~~~
error.rs:19:2: 19:2 note: previous borrow ends here
error.rs:6 fn x(tree: &mut Foo) {
...
error.rs:19 }
            ^
error.rs:12:17: 12:38 error: cannot assign to `current` because it is borrowed
error.rs:12                 current = &mut *inner;
                            ^~~~~~~~~~~~~~~~~~~~~
error.rs:11:31: 11:44 note: borrow of `current` occurs here
error.rs:11             &mut Foo::NotLast(ref mut inner) => {
                                          ^~~~~~~~~~~~~
error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
note: run with `RUST_BACKTRACE=1` for a backtrace
thread 'rustc' panicked at 'assertion failed: `(left == right) && (right == left)` (left: `TyS { sty: ty_uint(u32), flags: 0, region_depth: 0 }`, right: `TyS { sty: ty_uniq(TyS { sty: ty_enum(DefId { krate: 0, node: 4 }, Substs { types: VecPerParamSpace {TypeSpace: [], SelfSpace: [], FnSpace: [], }, regions: NonerasedRegions(VecPerParamSpace {TypeSpace: [], SelfSpace: [], FnSpace: [], }) }), flags: 0, region_depth: 0 }), flags: 0, region_depth: 0 }`)', /tmp/portage/dev-lang/rust-1.0.0_alpha2/work/rustc-1.0.0-alpha.2/src/librustc_borrowck/borrowck/mod.rs:402

The snippet is probably wrong, but the assert should not fail

Meta

rustc 1.0.0-dev (built 2015-02-28)
binary: rustc
commit-hash: unknown
commit-date: unknown
build-date: 2015-02-28
host: x86_64-unknown-linux-gnu
release: 1.0.0-dev

Backtrace:

   1:     0x7fe4742d3c20 - sys::backtrace::write::h6db1343660ca93a2KlC
   2:     0x7fe4742f9900 - <unknown>
   3:     0x7fe474244d80 - rt::unwind::begin_unwind_inner::h734e583390472682RBL
   4:     0x7fe474245220 - rt::unwind::begin_unwind_fmt::hdaef33c302b47274nAL
   5:     0x7fe473478670 - <unknown>
   6:     0x7fe473476f70 - <unknown>
   7:     0x7fe473476e90 - <unknown>
   8:     0x7fe473463640 - borrowck::check_loans::CheckLoanCtxt<'a, 'tcx>.euv..Delegate<'tcx>::borrow::h2a13ad241eee66d6cja
   9:     0x7fe47346e160 - <unknown>
  10:     0x7fe47346e160 - <unknown>
  11:     0x7fe47346e160 - <unknown>
  12:     0x7fe47346dff0 - <unknown>
  13:     0x7fe473472670 - <unknown>
  14:     0x7fe4734761d0 - <unknown>
  15:     0x7fe473472670 - <unknown>
  16:     0x7fe4734761d0 - <unknown>
  17:     0x7fe47348e560 - <unknown>
  18:     0x7fe47348fa00 - <unknown>
  19:     0x7fe47348f450 - borrowck::check_crate::hdf41b3c11cb3508fzRe
  20:     0x7fe4748df1b0 - driver::phase_3_run_analysis_passes::h025b35dbe058134fgHa
  21:     0x7fe4748c5610 - driver::compile_input::ha5d27daf21872ba2Gba
  22:     0x7fe474993b50 - run_compiler::he50deb5aedf688fcZbc
  23:     0x7fe474992480 - <unknown>
  24:     0x7fe474991370 - <unknown>
  25:     0x7fe474356bc0 - <unknown>
  26:     0x7fe474356bb0 - rust_try
  27:     0x7fe474991670 - <unknown>
  28:     0x7fe4742e7650 - <unknown>
  29:     0x7fe46ef4bf40 - <unknown>
  30:     0x7fe473eca7d9 - clone
  31:                0x0 - <unknown>

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions