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

panicked at 'Box<Any>' when running Cargo test #41032

Closed
jmelion opened this issue Apr 3, 2017 · 1 comment
Closed

panicked at 'Box<Any>' when running Cargo test #41032

jmelion opened this issue Apr 3, 2017 · 1 comment

Comments

@jmelion
Copy link

jmelion commented Apr 3, 2017

My code snippet throws panicked at 'Box<Any>' when running cargo test on the following code:

fn main() {
    println!("Hello, world!");
}

fn to_bab(aba: &str) -> Option<String>
{
    if aba.len() != 3
    {
        return None
    }
    let mut result = String::new();
    result.push(aba.chars().nth(1).unwrap());
    result.push(aba.chars().nth(0).unwrap());
    result.push(aba.chars().nth(1).unwrap());
    Some(result)
}

pub fn contains_matching_babs(abas: &[&str], babs: &[&str]) -> bool
{
    if abas.is_empty() || babs.is_empty()
    {
        return false;
    }
    for aba in abas.iter()
    {
       if babs.contains(&(to_bab(aba).unwrap_or(String::new()).as_str()))
       {
           return true;
       }
    }
    false
}

mod test
{
    use super::contains_matching_babs;

    #[test]
    fn test_contains_matching_babs()
    {
        let abas = vec!["aba", "cdc", "pop" ];
        let babs = vec!["kdk", "psp", "dcd" ];
        let cdcs = vec!["idi", "aha", "bjb" ];

        assert!(contains_matching_babs(&abas, &babs), true);
        assert!(contains_matching_babs(&babs, &abas), true);
        assert!(contains_matching_babs(&abas, &cdcs), false);
        assert!(contains_matching_babs(&cdcs, &abas), false);
    }
}

I am new to Rust so I might be missing something, and I realise this code is messy but I don't really see any instance where the code would panic. If I fill it with print statements, the panic seems to come upon returning from the contains_matching_babs function.

If I comment out the last two assert statements the code runs and the tests pass fine.

This seems related to issue 32783 however it seemed to happen with an older nightly version of rustc, whereas this is with a supposedly stable version.

Meta

rustc --version --verbose:

rustc 1.15.1 (021bd294c 2017-02-08)
binary: rustc
commit-hash: 021bd294c039bd54aa5c4aa85bcdffb0d24bc892
commit-date: 2017-02-08
host: x86_64-unknown-linux-gnu
release: 1.15.1
LLVM version: 3.9

Backtrace:

---- test::test_contains_matching_babs stdout ----
	thread 'test::test_contains_matching_babs' panicked at 'Box<Any>', src/main.rs:47
stack backtrace:
   1:     0x7fc377ccb8ba - std::sys::imp::backtrace::tracing::imp::write::h3188f035833a2635
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:42
   2:     0x7fc377cce95f - std::panicking::default_hook::{{closure}}::h6385b6959a2dd25b
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panicking.rs:349
   3:     0x7fc377cce4fd - std::panicking::default_hook::he4f3b61755d7fa95
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panicking.rs:359
   4:     0x7fc377ccee07 - std::panicking::rust_panic_with_hook::hf00b8130f73095ec
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panicking.rs:553
   5:     0x7fc377c8ab02 - std::panicking::begin_panic::h9099384c43e50561
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panicking.rs:515
   6:     0x7fc377c8e8c8 - bug_report::test::test_contains_matching_babs::h43b2441a3a256fde
                        at /path/to/project/src/main.rs:47
   7:     0x7fc377c9ccce - <F as test::FnBox<T>>::call_box::h04563d623b7ebdf9
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libtest/lib.rs:1349
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libtest/lib.rs:140
   8:     0x7fc377cd5d1a - __rust_maybe_catch_panic
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libpanic_unwind/lib.rs:98
   9:     0x7fc377c9125a - std::panicking::try::do_call::h092f707f35bffc28
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panicking.rs:434
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panic.rs:351
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libtest/lib.rs:1294
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panic.rs:295
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panicking.rs:458
  10:     0x7fc377cd5d1a - __rust_maybe_catch_panic
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libpanic_unwind/lib.rs:98
  11:     0x7fc377c97e32 - <F as alloc::boxed::FnBox<A>>::call_box::h23358fa8847261cf
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panicking.rs:434
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panic.rs:351
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/thread/mod.rs:301
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/liballoc/boxed.rs:605
  12:     0x7fc377ccdc64 - std::sys::imp::thread::Thread::new::thread_start::he018521f53b24939
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/liballoc/boxed.rs:615
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/sys_common/thread.rs:21
                        at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/sys/unix/thread.rs:84
  13:     0x7fc377434183 - start_thread
  14:     0x7fc376f4b37c - clone
  15:                0x0 - <unknown>

@stephaneyfx
Copy link
Contributor

The second argument to assert! should be a custom panic message (doc). Your assertions should be written as follows:

assert!(contains_matching_babs(&abas, &babs));
assert!(contains_matching_babs(&babs, &abas));
assert!(!contains_matching_babs(&abas, &cdcs));
assert!(!contains_matching_babs(&cdcs, &abas));

With your current code, the third assertion does not hold and the second argument (false) is used to panic in a way similar to what is described in the panic! doc, wrapping the boolean value in Box<Any>.

@jmelion jmelion closed this as completed Apr 3, 2017
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

2 participants