Skip to content

Unexpected panic while compiling a a piece of code with destructuring #32153

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
astonbitecode opened this issue Mar 9, 2016 · 3 comments
Closed

Comments

@astonbitecode
Copy link

Hello,

I would like to report an issue, after the compiler prompted me to do so.

I am using toml-rs to get some configuration data, so I have a function like the following:

extern crate toml;

use toml::Value;

fn read(toml: &Value, path: &'static str, typ: &'static str) -> DefinitionResult {
    match toml.lookup(path) {
        Some(v) => {
            match typ {
                "String" => DefinitionResult::String(v.as_str().unwrap().to_string()),
                "Integer" => DefinitionResult::Integer(v.as_integer().unwrap()),
                _ => DefinitionResult::Err("Cannot handle type")
            }
        }
        None => {
            DefinitionResult::Err("Path does not exist in TOML")
        }
    }
}

With DefinitionResult being an enum:

pub enum DefinitionResult {
    String(String),
    Integer(i64),
    Long(u64),
    Err(&'static str)
}

I have created a Unit test like the following:

#[test]
fn get_string() {
    let toml_string = r#"
        [table1]
        key1 = "value1"
        key2 = "value2"
        [table2]
        key1 = "value1"
        key2 = "value2"
    "#.to_string();
    let toml = toml_string.parse().unwrap();

    let DefinitionResult::String(v1) = super::read(&toml, "table1.key1", "String");
    assert!(v1 == "value1");
}

and once I want to compile and run the test with cargo test I get the following error from the compiler:

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

I guess that has to do with the destructuring here:

DefinitionResult::String(v1) = super::read(&toml, "table1.key1", "String")

To be honest I am not sure if destructuring an enum like that using let is supported anyway.

If I re-write the destructure like:

match super::read(&toml, "table1.key1", "String") {
    DefinitionResult::String(v1) => assert!(v1 == "value1"),
    _ => assert!(true),
}

everything works great.

Thank you!

Meta

rustc --version --verbose:

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

stack backtrace:

1: 0x7fea61f9f340 - sys::backtrace::tracing:: imp::write::h2bb24970c1600d4fNyu
2: 0x7fea61fa86ab - panicking::default_handler::$u7b$$u7b$closure$u7d$$u7d$::closure.43727
3: 0x7fea61fa8203 - panicking::default_handler::hb760707c7feda3cdbez
4: 0x7fea61f70d7c - sys_common::unwind::begin_unwind_inner::h78583c314295e3926mt
5: 0x7fea61f71818 - sys_common::unwind::begin_unwind_fmt::h903be369b05a3ccccmt
6: 0x7fea5f2811d8 - middle::check_match::check_irrefutable::h8d1ba0bed3fbea2aQ4j
7: 0x7fea5f251f42 - middle::check_match::check_local::h2d216d4e5ead2573K2j
8: 0x7fea5f252197 - middle::check_match::check_fn::h082db3cfa9cb9bc8A3j
9: 0x7fea5f252901 - middle::check_match::check_crate::hc28c1fe732f94679kSi
10: 0x7fea624c142e - driver::phase_3_run_analysis_passes::
$u7b$$u7b$closure$u7d$$u7d$::closure.28446
11: 0x7fea624be1e5 - middle::ty::context::ctxt<'tcx>::create_and_enter::h6911830796927350595
12: 0x7fea624bad3f - driver::phase_3_run_analysis_passes::h10607465921210735652
13: 0x7fea6248c5b5 - driver::compile_input::h9417f7071f41b0b1Bca
14: 0x7fea6247c907 - run_compiler::h1d04446f72d95b52HOc
15: 0x7fea6247a0b1 - sys_common::unwind::try::try_fn::h2224149726337910613
16: 0x7fea61f9cd9b - __rust_try
17: 0x7fea61f9534d - sys_common::unwind::inner_try::hdfdf33e7068c9cb28jt
18: 0x7fea6247a900 - boxed::F.FnBox::call_box::h8331008289514682961
19: 0x7fea61fa6c79 - sys::thread::Thread:: new::thread_start::h1272204999bddc95vby
20: 0x7fea5a735423 - start_thread
21: 0x7fea61c25cbc - clone
22: 0x0 -

@mitaa
Copy link
Contributor

mitaa commented Mar 9, 2016

This should have been fixed recently (#31561)

@alexcrichton
Copy link
Member

Thanks @mitaa!

@astonbitecode
Copy link
Author

I would like to confirm that, after upgrading to the latest nightly (rustc 1.9.0-nightly (eabfc16 2016-03-08)), the compiler states:

src/main.rs:52:6: 52:34 error: refutable pattern in local binding: Integer(_) not covered [E0005]
src/main.rs:52 let DefinitionResult::String(v1) = super::read(&toml, "table1.key1", "String");

Thank you both!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants