Skip to content

ICE when calling mutation method on map value indexed with mutable reference #44405

Closed
@saghm

Description

@saghm

When calling a mutation method on an value obtained from a HashMap with a mutable reference index, an ICE occurs.

Code:

use std::collections::HashMap;

fn main() {
    let mut map: HashMap<i32, Vec<i32>> = HashMap::new();
    let mut i = 1;
    map.insert(i, Vec::new());

    map[&mut i].push(3); // ICE occurs here
}

Compiler output:

error: internal compiler error: re-trying op failed
 --> src/main.rs:8:5
  |
8 |     map[&mut i].push(3);
  |     ^^^^^^^^^^^

note: the compiler unexpectedly panicked. this is a bug.

Backtrace:

thread 'rustc' panicked at 'Box<Any>', /checkout/src/librustc_errors/lib.rs:437:8
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:
   0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
             at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at /checkout/src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at /checkout/src/libstd/sys_common/backtrace.rs:60
             at /checkout/src/libstd/panicking.rs:380
   3: std::panicking::default_hook
             at /checkout/src/libstd/panicking.rs:390
   4: std::panicking::rust_panic_with_hook
             at /checkout/src/libstd/panicking.rs:611
   5: std::panicking::begin_panic_new
   6: rustc_errors::Handler::abort_if_errors
   7: rustc_passes::consts::check_crate
   8: rustc_driver::driver::phase_3_run_analysis_passes::{{closure}}
   9: rustc_driver::driver::phase_3_run_analysis_passes
  10: rustc_driver::driver::compile_input
  11: rustc_driver::run_compiler

I believe that the correct behavior here would be for the compiler to report an error to the user on the indicated line due to IndexMut not being implemented for HashMap, which is what occurs when replacing &mut i with just &i.

rustc version:

rustc 1.20.0 (f3d6973f4 2017-08-27)
binary: rustc
commit-hash: f3d6973f41a7d1fb83029c9c0ceaf0f5d4fd7208
commit-date: 2017-08-27
host: x86_64-unknown-linux-gnu
release: 1.20.0
LLVM version: 4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️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