Skip to content

Destructuring irrefutable enums with ref inside match causes a panic #31872

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

Closed
mkroman opened this issue Feb 24, 2016 · 3 comments
Closed

Destructuring irrefutable enums with ref inside match causes a panic #31872

mkroman opened this issue Feb 24, 2016 · 3 comments
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@mkroman
Copy link

mkroman commented Feb 24, 2016

When destructuring an irrefutable enum inside a destructured match pattern with references, the compiler crashes. I tried replicating the case with as little code as possible, but it seems to only happen when I use the enum from the irc crate.

This is the code I managed to narrow it down to:

extern crate irc;

use irc::client::data::command::Command;

pub enum Event<'a> {
    Command(&'a str, Vec<&'a str>)
}

struct Plugin;

impl Plugin {
    fn process(&self, command: Command, event: Event) {
        match event {
            Event::Command(ref cmd, ref args) => {
                let Command::PRIVMSG(ref target, ref msg) = command; // <--
            }
        }
    }
}

fn main() {
    let plugin = Plugin;
    let event = Event::Command("hello", vec!["world"]);
    let cmd = Command::PRIVMSG("foo".to_string(), "bar".to_string());

    plugin.process(cmd, event);
}

Note that it is destructuring from the command argument to the process function, and not the destructured reference cmd.

I expected cmd and args to both be references to the types in the Event::Command enum, and expected target and msg to be &strs referencing the Strings defined in the Command::PRIVMSG enum type in the irc crate.

Instead, the compiler panics with the following error:

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)` (left: `64`, right: `1`)', ../src/librustc/middle/check_match.rs:1051
note: Run with `RUST_BACKTRACE=1` for a backtrace.

Could not compile `plugins`.

Meta

Rust version:

rustc 1.8.0-nightly (c8fc481 2016-02-22)
binary: rustc
commit-hash: c8fc481
commit-date: 2016-02-22
host: x86_64-unknown-linux-gnu
release: 1.8.0-nightly

Backtrace:

   Compiling compiler-crash v0.1.0 (file:///home/mk/Projects/Rust/compiler-crash)
     Running `rustc src/main.rs --crate-name compiler_crash --crate-type bin -g --out-dir /home/mk/Projects/Rust/compiler-crash/target/debug --emit=dep-info,link -L dependency=/home/mk/Projects/Rust/compiler-crash/target/debug -L dependency=/home/mk/Projects/Rust/compiler-crash/target/debug/deps --extern irc=/home/mk/Projects/Rust/compiler-crash/target/debug/deps/libirc-cc59e736e2ba9456.rlib -L native=/home/mk/Projects/Rust/compiler-crash/target/debug/build/openssl-09576f2f9776fa80/out -L native=/home/mk/Projects/Rust/compiler-crash/target/debug/build/openssl-sys-extras-52d5315fb71d3c6d/out -L native=/usr/lib`
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
thread 'rustc' panicked at 'assertion failed: `(left == right)` (left: `64`, right: `1`)', ../src/librustc/middle/check_match.rs:1051
stack backtrace:
   1:     0x7f0f59186340 - sys::backtrace::tracing::imp::write::h2bb24970c1600d4fNyu
   2:     0x7f0f5918f6ab - panicking::default_handler::_$u7b$$u7b$closure$u7d$$u7d$::closure.43727
   3:     0x7f0f5918f203 - panicking::default_handler::hb760707c7feda3cdbez
   4:     0x7f0f59157d7c - sys_common::unwind::begin_unwind_inner::h78583c314295e3926mt
   5:     0x7f0f59158818 - sys_common::unwind::begin_unwind_fmt::h903be369b05a3ccccmt
   6:     0x7f0f564371d8 - middle::check_match::check_irrefutable::h8d1ba0bed3fbea2aQ4j
   7:     0x7f0f56407f42 - middle::check_match::check_local::h2d216d4e5ead2573K2j
   8:     0x7f0f56405571 - middle::check_match::check_expr::h52b2bafd753fd3e2ESi
   9:     0x7f0f5640a3b1 - intravisit::walk_arm::h17326268766873259457
  10:     0x7f0f5640529e - middle::check_match::check_expr::h52b2bafd753fd3e2ESi
  11:     0x7f0f564081ba - middle::check_match::check_fn::h082db3cfa9cb9bc8A3j
  12:     0x7f0f5640919d - middle::check_match::check_crate::hc28c1fe732f94679kSi
  13:     0x7f0f596ad42e - driver::phase_3_run_analysis_passes::_$u7b$$u7b$closure$u7d$$u7d$::closure.28446
  14:     0x7f0f596aa1e5 - middle::ty::context::ctxt<'tcx>::create_and_enter::h6911830796927350595
  15:     0x7f0f596a6d3f - driver::phase_3_run_analysis_passes::h10607465921210735652
  16:     0x7f0f596785b5 - driver::compile_input::h9417f7071f41b0b1Bca
  17:     0x7f0f59668907 - run_compiler::h1d04446f72d95b52HOc
  18:     0x7f0f596660b1 - sys_common::unwind::try::try_fn::h2224149726337910613
  19:     0x7f0f59183d9b - __rust_try
  20:     0x7f0f5917c34d - sys_common::unwind::inner_try::hdfdf33e7068c9cb28jt
  21:     0x7f0f59666900 - boxed::F.FnBox<A>::call_box::h8331008289514682961
  22:     0x7f0f5918dc79 - sys::thread::Thread::new::thread_start::h1272204999bddc95vby
  23:     0x7f0f518c5423 - start_thread
  24:     0x7f0f58e05cbc - clone
  25:                0x0 - <unknown>

Could not compile `compiler-crash`.

Caused by:
  Process didn't exit successfully: `rustc src/main.rs --crate-name compiler_crash --crate-type bin -g --out-dir /home/mk/Projects/Rust/compiler-crash/target/debug --emit=dep-info,link -L dependency=/home/mk/Projects/Rust/compiler-crash/target/debug -L dependency=/home/mk/Projects/Rust/compiler-crash/target/debug/deps --extern irc=/home/mk/Projects/Rust/compiler-crash/target/debug/deps/libirc-cc59e736e2ba9456.rlib -L native=/home/mk/Projects/Rust/compiler-crash/target/debug/build/openssl-09576f2f9776fa80/out -L native=/home/mk/Projects/Rust/compiler-crash/target/debug/build/openssl-sys-extras-52d5315fb71d3c6d/out -L native=/usr/lib` (exit code: 101)

Repository

I uploaded the code as a repository, with Cargo.lock for easier replication.
https://github.com/mkroman/compiler-crash

@durka
Copy link
Contributor

durka commented Feb 25, 2016

Note that it's not actually expected to compile, because Command::PRIVMSG(ref target, ref msg) is refutable. But it shouldn't crash.

@steveklabnik steveklabnik added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Mar 11, 2016
@joelself
Copy link

This looks to be the exact same compiler panic I encountered. I've made a branch with code that panics the compiler here. The specific line of code that is causing the panic is this one. Here's a minimal snippet that replicates the panic:

enum MakePanic {
  Panic(usize),
  Calm,
  Anxious,
}

#[test]
fn test_compile_panic() {
  let mkpnc = MakePanic::Panic(3);
  let MakePanic::Panic(val) = mkpnc;
  println!("Use val {}", val);
}

Note that it doesn't panic with fewer than 3 enum options. I tried Panic(usize), Panic(String), and Panic(Rc<RefCell<(String, usize)>>) and they all cause a panic. It also panics when the other options have values in them. It panics with a lifetime declared on the enum and used in one of the options. It'll panic if mkpnc is destructured into val by value and by reference.

Version beta:
rustc 1.8.0-beta.1 (facbfdd 2016-03-02)
binary: rustc
commit-hash: facbfdd
commit-date: 2016-03-02
host: x86_64-apple-darwin
release: 1.8.0-beta.1

Version nightly:
rustc 1.9.0-nightly (7b0b80a 2016-03-02)
binary: rustc
commit-hash: 7b0b80a
commit-date: 2016-03-02
host: x86_64-apple-darwin
release: 1.9.0-nightly

Backtrace beta:

thread 'rustc' panicked at 'assertion failed: `(left == right)` (left: `2`, right: `1`)', ../src/librustc/middle/check_match.rs:1051
stack backtrace:
   1:        0x107ebe3a8 - sys::backtrace::tracing::imp::write::h8c3fbdc55f1abdcag4u
   2:        0x107ec6d15 - panicking::default_handler::_$u7b$$u7b$closure$u7d$$u7d$::closure.44007
   3:        0x107ec6817 - panicking::default_handler::h6775aa517c5d030aFvz
   4:        0x107e89fc6 - sys_common::unwind::begin_unwind_inner::h9a1b0d9dd0c062e122t
   5:        0x107e8aabe - sys_common::unwind::begin_unwind_fmt::h99d6622b969d640e81t
   6:        0x104d7731e - middle::check_match::check_irrefutable::h037ce85fe9e0719fP4j
   7:        0x104d46440 - middle::check_match::check_local::h57d1a825afa7d07fJ2j
   8:        0x104d466a7 - middle::check_match::check_fn::h02fdb8e29648c14az3j
   9:        0x104d46e8e - middle::check_match::check_crate::h5c6040bd71f7b985jSi
  10:        0x103e8090f - driver::phase_3_run_analysis_passes::_$u7b$$u7b$closure$u7d$$u7d$::closure.28271
  11:        0x103e7d869 - middle::ty::context::ctxt<'tcx>::create_and_enter::h8855598638456051733
  12:        0x103e7a2e2 - driver::phase_3_run_analysis_passes::h7337521525707930622
  13:        0x103e4e0bd - driver::compile_input::h467e81e458addce4Hca
  14:        0x103e3d788 - run_compiler::h971a7f9ccb1ca35e6Oc
  15:        0x103e3ade5 - sys_common::unwind::try::try_fn::h8709370279840092323
  16:        0x107ebbd7b - __rust_try
  17:        0x107eb3fa3 - sys_common::unwind::inner_try::h8630ed5c10186d0a4Zt
  18:        0x103e3b699 - boxed::F.FnBox<A>::call_box::h4993598626702227198
  19:        0x107ec5d0c - sys::thread::Thread::new::thread_start::h2721df3f64ee52b36Jy
  20:     0x7fff81f6f059 - _pthread_body
  21:     0x7fff81f6efd6 - _pthread_start

Backtrace nightly:

thread 'rustc' panicked at 'assertion failed: `(left == right)` (left: `2`, right: `1`)', ../src/librustc/middle/check_match.rs:1051
stack backtrace:
   1:        0x113bc43a8 - sys::backtrace::tracing::imp::write::h7add7fdda97786a1s4u
   2:        0x113bccd15 - panicking::default_handler::_$u7b$$u7b$closure$u7d$$u7d$::closure.44030
   3:        0x113bcc817 - panicking::default_handler::h72a4f6df9637bc80Rvz
   4:        0x113b8ffc6 - sys_common::unwind::begin_unwind_inner::hf798769ad97c097fe3t
   5:        0x113b90abe - sys_common::unwind::begin_unwind_fmt::hb2ee19b4aa59ba8ak2t
   6:        0x1105cd7be - middle::check_match::check_irrefutable::hb3dbd95dda6de55c92j
   7:        0x11059c980 - middle::check_match::check_local::h61b952193c791ff530j
   8:        0x11059cbe7 - middle::check_match::check_fn::hab6c47340d646ea4T1j
   9:        0x11059d3ce - middle::check_match::check_crate::h4de6707235db8a8dDQi
  10:        0x10f6dea1f - driver::phase_3_run_analysis_passes::_$u7b$$u7b$closure$u7d$$u7d$::closure.28283
  11:        0x10f6db979 - middle::ty::context::ctxt<'tcx>::create_and_enter::h17676359018619823038
  12:        0x10f6d83f2 - driver::phase_3_run_analysis_passes::h9214455933593547357
  13:        0x10f6ac19d - driver::compile_input::h26b63276678a06cfHca
  14:        0x10f699ce8 - run_compiler::hd54b7109a035c12b6Oc
  15:        0x10f697345 - sys_common::unwind::try::try_fn::h18306268176533686141
  16:        0x113bc1d7b - __rust_try
  17:        0x113bb9fa3 - sys_common::unwind::inner_try::hdaa7ae415a983f2ag0t
  18:        0x10f697bf9 - boxed::F.FnBox<A>::call_box::h14172178064022666043
  19:        0x113bcbd0c - sys::thread::Thread::new::thread_start::h45441f586afbe2dciKy
  20:     0x7fff81f6f059 - _pthread_body
  21:     0x7fff81f6efd6 - _pthread_start

@mitaa
Copy link
Contributor

mitaa commented Mar 14, 2016

I think this should be fixed (#31561)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

6 participants