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

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

Closed
saghm opened this issue Sep 8, 2017 · 2 comments · Fixed by #63397
Closed

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

saghm opened this issue Sep 8, 2017 · 2 comments · Fixed by #63397
Labels
C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@saghm
Copy link

saghm commented Sep 8, 2017

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
@Aaron1011
Copy link
Member

Here's a self-contained version of the snippet:

use std::ops::Index;

struct Test;
struct Container(Test);

impl Test {
    fn test(&mut self) {
    }
}

impl<'a> Index<&'a bool> for Container {
    type Output = Test;

    fn index(&self, index: &'a bool) -> &Test {
        &self.0
    }

}


fn main() {
    let mut container = Container(Test);
    let mut val = true;
    container[&mut val].test();
}

Playground link

@alexcrichton alexcrichton added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 8, 2017
@jakubadamw
Copy link
Contributor

This doesn't ICE any more.

@JohnTitor JohnTitor added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Aug 8, 2019
Centril added a commit to Centril/rust that referenced this issue Aug 9, 2019
Centril added a commit to Centril/rust that referenced this issue Aug 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants