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

'index out of bounds: the len is 0 but the index is 0' on match { } block #12116

Closed
ghost opened this issue Feb 8, 2014 · 6 comments
Closed

Comments

@ghost
Copy link

ghost commented Feb 8, 2014

I suspect this is malformed typing and pointer mgmt on my part, but regardless, the compiler throws an error rather then tells me I made one.

This is in an attempt to expand on the linked list from the docs.

error output:

task 'rustc' failed at 'index out of bounds: the len is 0 but the index is 0', /build/rust/src/rust-0.9/src/librustc/middle/check_match.rs:245
error: internal compiler error: unexpected failure
This message reflects a bug in the Rust compiler. 
We would appreciate a bug report: https://github.com/mozilla/rust/wiki/HOWTO-submit-a-Rust-bug-report
note: the compiler hit an unexpected failure path. this is a bug
note: try running with RUST_LOG=rustc=1 to get further details and report the results to github.com/mozilla/rust/issues
task '<main>' failed at 'explicit failure', /build/rust/src/rust-0.9/src/librustc/lib.rs:453

Offending code:

enum IntList {
    Cons(int, ~IntList),
    Nil
}

fn tail(source_list: &IntList) -> IntList {
    match source_list {
        &Cons(val, ~ref next_list) => IntList::tail(next_list),
        &Cons(val, ~Nil)           => Cons(val, ~Nil),  //commenting this out allows compilation
        _                          => fail!()
    }
}

uname -a

Linux thinktank 3.12.9-2-ARCH #1 SMP PREEMPT Fri Jan 31 10:22:54 CET 2014 x86_64 GNU/Linux

rust is at 0.9-2 on this box, with the binary pkg from the archlinux repos (should be vanilla compiled.)

@alexcrichton
Copy link
Member

For me, I'm getting this output:

!!! (resolving module in lexical scope) module wasn't actually a module!
foo.rs:8:39: 8:52 error: unresolved name
foo.rs:8         &Cons(val, ~ref next_list) => IntList::tail(next_list),
                                               ^~~~~~~~~~~~~
foo.rs:8:39: 8:52 error: use of undeclared module `IntList`
foo.rs:8         &Cons(val, ~ref next_list) => IntList::tail(next_list),
                                               ^~~~~~~~~~~~~
!!! (resolving module in lexical scope) module wasn't actually a module!
foo.rs:8:39: 8:52 error: unresolved name `IntList::tail`.
foo.rs:8         &Cons(val, ~ref next_list) => IntList::tail(next_list),
                                               ^~~~~~~~~~~~~
error: aborting due to 3 previous errors

Which isn't great because there are these 'module wasn't actually a module' lines in the output, so something definitely needs to get fixed here. I'm not getting the exact error you're describing, though, so this bug may be fixed on master.

If you change IntList::tail to tail (IntList has no methods on it, tail is a free-standing function), I get an error like this:

error: internal compiler error: unexpected failure
This message reflects a bug in the Rust compiler. 
We would appreciate a bug report: http://static.rust-lang.org/doc/master/complement-bugreport.html
note: the compiler hit an unexpected failure path. this is a bug
Ok(task 'rustc' failed at 'index out of bounds: the len is 0 but the index is 0', /Users/acrichton/code/rust2/src/librustc/middle/check_match.rs:250
)

Which sounds like what you ran into (and should definitely be fixed!).

The fix for this bug is to move the ~Nil case above the ~ref next_list case. I'm not sure why the compiler doesn't disallow this (it should).

@hamham91
Copy link

hamham91 commented Feb 9, 2014

I received the same error when compiling the following socket server program (I have since fixed my code).

compiler-specific error output:

error: internal compiler error: unexpected failure
This message reflects a bug in the Rust compiler. 
We would appreciate a bug report: http://static.rust-lang.org/doc/master/complement-bugreport.html
note: the compiler hit an unexpected failure path. this is a bug
task 'rustc' failed at 'index out of bounds: the len is 0 but the index is 0', /home/lhamre/Documents/rust/src/librustc/middle/typeck/check/method.rs:455

code:

#[feature(globs)];
use std::io::*;
use std::io::File;
use std::io::net::tcp::TcpListener;
use std::io::net::tcp::TcpAcceptor;
use std::io::net::tcp::TcpStream;
use std::io::net::ip::SocketAddr;
use std::{str};

static IP: &'static str = "127.0.0.1";
static PORT:        int = 4000;


fn handle_connection(opt_stream: ~Option<net::tcp::TcpStream>) {

    let mut stream = opt_stream.unwrap();
    match(stream) {
        Some(ref mut s) => {
            match(s.peer_name()) {
                Some(pn) => {
                    println(format!("Received connection from: [{:s}]", pn.to_str()));
                }
                None => ()
            }
                           },
        None => ()
    }
    println!("Connection terminates.");
}

fn main() {
    let addr: SocketAddr = from_str::<SocketAddr>(format!("{:s}:{:d}", IP, PORT)).unwrap();
    let listener: TcpListener = TcpListener::bind(addr);

    // bind the listener to the specified address
    let mut acceptor: TcpAcceptor = listener.listen();

    // output the server's address and port
    println(format!("Listening on [{:s}] ...", addr.to_str()));

    // accept connections and process them in a thread
    for stream in acceptor.incoming() {
        do spawn {
            handle_connection(stream);
        }
    }

    // close the server
    drop(acceptor);
}

rustc -v:

rustc 0.10-pre (5512fb4 2014-01-19 05:56:35 -0800)
host: x86_64-unknown-linux-gnu

uname -a:

Linux nachos 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 17:37:58 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

@ghost
Copy link
Author

ghost commented Feb 9, 2014

I initially had that tail method wrapped up in impl IntList { }. Moving it outside of the IntList impl block still throws the error. Moving that ~Nil match case above the ~ref next_node, however, does circumvent the issue and I can get the code to pass tests.

@zenoamaro
Copy link

Some wrong code by me triggers this failure path on 0.10:

$ env RUST_BACKTRACE=1 rustc commands.rs
commands.rs:17:20: 17:25 error: mismatched types: expected `uint` but found `int` (expected uint but found int)
commands.rs:17      match tokens.get(index) {
                                     ^~~~~
commands.rs:18:4: 18:13 error: mismatched types: expected `std::option::Option<&&str>` but found `std::result::Result<<generic #17>,<generic #18>>` (expected enum std::option::Option but found enum std::result::Result)
commands.rs:18          Ok(value) => int::parse_bytes(value.as_bytes(), 10),
                        ^~~~~~~~~
error: internal compiler error: unexpected failure
note: the compiler hit an unexpected failure path. this is a bug.
note: we would appreciate a bug report: http://static.rust-lang.org/doc/master/complement-bugreport.html
note: run with `RUST_BACKTRACE=1` for a backtrace
task 'rustc' failed at 'index out of bounds: the len is 0 but the index is 0', /private/tmp/rust-Q8uz/rust-0.10/src/librustc/lib.rs:1
stack backtrace:
   1:        0x107cb28e4 - rt::backtrace::imp::write::h2a8a3b7117b6a3bag8b::v0.10
   2:        0x107c16b56 - rt::unwind::begin_unwind_inner::h13ee063055871ef1KIb::v0.10
   3:        0x107c15fc8 - rt::unwind::begin_unwind::hcf46aa8743077363kIb::v0.10
   4:        0x107cb23b9 - rt::unwind::begin_unwind_raw::h9f0f430a7522466dTFb::v0.10
   5:        0x107c1523e - rt::unwind::fail_::hd92116ac6b4e151axDb::v0.10
   6:        0x107cb2402 - rt::unwind::fail_bounds_check::closure.41732
   7:        0x107c172ce - rt::unwind::fail_bounds_check::h608f2f2d28d48d89UDb::v0.10
   8:        0x1055f23ec - middle::typeck::check::method::LookupContext$LT$$x27a$GT$::push_bound_candidates::closure.66574
   9:        0x1055f0f01 - middle::typeck::check::autoderef::hf4fdb94b1d06a293Oz9::v0.10
  10:        0x1055edf93 - middle::typeck::check::method::LookupContext$LT$$x27a$GT$::push_bound_candidates::h4eebbd30dc096d68cf6::v0.10
  11:        0x1055ee1f0 - middle::typeck::check::method::lookup_in_trait::h4fe82a71d0abe009ZX5::v0.10
  12:        0x1055eec39 - middle::typeck::check::try_overloaded_deref::h9be89e6dcd449c3aKD9::v0.10
  13:        0x1055f1044 - middle::typeck::check::autoderef::hf4fdb94b1d06a293Oz9::v0.10
  14:        0x1055ec9f0 - middle::typeck::check::method::lookup::h91adc5fe6eb35f8b3Q5::v0.10
  15:        0x105627335 - middle::typeck::check::check_expr_with_unifier::h9611897cf402e0e1X79::v0.10
  16:        0x10563042c - middle::typeck::check::check_expr_with_unifier::check_argument_types::h91f9c529b5bd0134Zba::v0.10
  17:        0x10562b0a8 - middle::typeck::check::check_expr_with_unifier::h9611897cf402e0e1X79::v0.10
  18:        0x10559353d - middle::typeck::check::_match::check_match::hdddd2803abbbbf71Oe0::v0.10
  19:        0x105627c5e - middle::typeck::check::check_expr_with_unifier::h9611897cf402e0e1X79::v0.10
  20:        0x105606c39 - middle::typeck::check::check_block_with_expected::h2c7bb960e076eb42Rrc::v0.10
  21:        0x10560275d - middle::typeck::check::check_fn::h1c2cafb90241ad96p67::v0.10
  22:        0x105601f42 - middle::typeck::check::check_bare_fn::hf0b73f6491c4bb03hW7::v0.10
  23:        0x10560b6fb - middle::typeck::check::check_method_body::h49c9b749692cc863kr8::v0.10
  24:        0x1055fdaf6 - middle::typeck::check::check_item::hb8facf87dab582d4Pg8::v0.10
  25:        0x105601dbe - middle::typeck::check::check_item_types::hdb126557beb409a1RV7::v0.10
  26:        0x10573013f - util::common::time::h9acad76c21817c9aXHh::v0.10
  27:        0x10572f049 - middle::typeck::check_crate::hf3137d246c3f533atTu::v0.10
  28:        0x105b3f775 - driver::driver::phase_3_run_analysis_passes::h605fda952e28c418vxf::v0.10
  29:        0x105b45a63 - driver::driver::compile_input::haa7c9f4729d6243csWf::v0.10
  30:        0x105b6b6c4 - run_compiler::hfd5ecc9785222053Yzn::v0.10
  31:        0x105b7f36d - main_args::closure.92066
  32:        0x105b7db82 - monitor::closure.91951
  33:        0x105b7972b - task::TaskBuilder::try::closure.91726
  34:        0x10533d49c - task::spawn_opts::closure.7957
  35:        0x107cadd08 - rt::task::Task::run::closure.41637
  36:        0x107cb85fc - rust_try
  37:        0x107cadb87 - rt::task::Task::run::h544c274b867f177dNB9::v0.10
  38:        0x10533d31f - task::spawn_opts::closure.7929
  39:        0x107cb12c6 - rt::thread::thread_start::hc06c53d926f6e6a3uga::v0.10
  40:     0x7fff90a76899 - _pthread_body
  41:     0x7fff90a7672a - _pthread_struct_init
$ rustc -v
rustc 0.10
host: x86_64-apple-darwin

@nathantypanski
Copy link
Contributor

I ran into this same issue when porting some code of mine to 0.10:

[... lots of compiler errors ...]
shell/shellprocess.rs:94:37: 94:73 error: this function takes 2 parameters but 3 parameters were supplied
shell/shellprocess.rs:94                 let maybe_process = Process::new(command, args, options);
                                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
shell/shellprocess.rs:96:21: 96:38 error: mismatched types: expected `std::result::Result<std::io::process::Process,std::io::IoError>` but found `std::option::Option<<generic #228>>` (expected enum std::result::Result but found enum std::option::Option)
shell/shellprocess.rs:96                     Some(mut process) => {
                                             ^~~~~~~~~~~~~~~~~
shell/shellprocess.rs:101:21: 101:25 error: mismatched types: expected `std::result::Result<std::io::process::Process,std::io::IoError>` but found `std::option::Option<<generic #229>>` (expected enum std::result::Result but found enum std::option::Option)
shell/shellprocess.rs:101                     None => {
                                              ^~~~
shell/shellprocess.rs:98:25: 98:74 error: type `std::comm::Sender<std::option::Option<i32>>` does not implement any method in scope named `try_send_deferred`
shell/shellprocess.rs:98                         pidchan.try_send_deferred(Some(process.get_id()));
error: internal compiler error: unexpected failure
note: the compiler hit an unexpected failure path. this is a bug.
note: we would appreciate a bug report: http://static.rust-lang.org/doc/master/complement-bugreport.html
note: run with `RUST_BACKTRACE=1` for a backtrace
task 'rustc' failed at 'index out of bounds: the len is 0 but the index is 0', /home/nathan/devel/rust/rust/src/librustc/lib.rs:1
   1:     0x2b5d181b5570 - rt::backtrace::imp::write::h76f9f964662fed8egZb::v0.10
   2:     0x2b5d18116d60 - rt::unwind::begin_unwind_inner::h20c6fc89a138044dJzb::v0.10
   3:     0x2b5d18116cd0 - rt::unwind::begin_unwind::hf5d4f38c5a824766jzb::v0.10
   4:     0x2b5d181b5180 - rt::unwind::begin_unwind_raw::h095c05a4a6f76230Swb::v0.10
   5:     0x2b5d18116000 - rt::unwind::fail_::h64b5ce58e240f918wub::v0.10
   6:     0x2b5d181b51f0 - rt::unwind::fail_bounds_check::closure.41722
   7:     0x2b5d18117f30 - rt::unwind::fail_bounds_check::hdfd835c252494181Tub::v0.10
   8:     0x2b5d15db4b80 - middle::typeck::check::method::LookupContext$LT$$x27a$GT$::push_bound_candidates::closure.66575
   9:     0x2b5d15db3a80 - middle::typeck::check::autoderef::h209b1a78eff615ceOz9::v0.10
  10:     0x2b5d15db07b0 - middle::typeck::check::method::LookupContext$LT$$x27a$GT$::push_bound_candidates::h2c320d4e0304d1c1cf6::v0.10
  11:     0x2b5d15db0940 - middle::typeck::check::method::lookup_in_trait::h9a4368c2c22d8c14ZX5::v0.10
  12:     0x2b5d15db1340 - middle::typeck::check::try_overloaded_deref::h3555006258421935KD9::v0.10
  13:     0x2b5d15db3a80 - middle::typeck::check::autoderef::h209b1a78eff615ceOz9::v0.10
  14:     0x2b5d15daed50 - middle::typeck::check::method::lookup::h6fca92561b19e46f3Q5::v0.10
  15:     0x2b5d15de9b50 - middle::typeck::check::check_expr_with_unifier::h3dc9d4d7d7b503ffX79::v0.10
  16:     0x2b5d15df3e70 - middle::typeck::check::check_expr_with_unifier::check_argument_types::hf94095d0f2af9975Zba::v0.10
  17:     0x2b5d15de9b50 - middle::typeck::check::check_expr_with_unifier::h3dc9d4d7d7b503ffX79::v0.10
  18:     0x2b5d15df3e70 - middle::typeck::check::check_expr_with_unifier::check_argument_types::hf94095d0f2af9975Zba::v0.10
  19:     0x2b5d15df3a40 - middle::typeck::check::check_expr_with_unifier::check_method_argument_types::h59f6e45bf2ccf005I99::v0.10
  20:     0x2b5d15de9b50 - middle::typeck::check::check_expr_with_unifier::h3dc9d4d7d7b503ffX79::v0.10
  21:     0x2b5d15e06440 - middle::typeck::check::check_stmt::h8de0f29e39556877Cnc::v0.10
  22:     0x2b5d15dc9b00 - middle::typeck::check::check_block_with_expected::h617a7d1f64ccc363Rrc::v0.10
  23:     0x2b5d15de9b50 - middle::typeck::check::check_expr_with_unifier::h3dc9d4d7d7b503ffX79::v0.10
  24:     0x2b5d15d54550 - middle::typeck::check::_match::check_match::h3798f0743e099fdaOe0::v0.10
  25:     0x2b5d15de9b50 - middle::typeck::check::check_expr_with_unifier::h3dc9d4d7d7b503ffX79::v0.10
  26:     0x2b5d15dc9b00 - middle::typeck::check::check_block_with_expected::h617a7d1f64ccc363Rrc::v0.10
  27:     0x2b5d15de9b50 - middle::typeck::check::check_expr_with_unifier::h3dc9d4d7d7b503ffX79::v0.10
  28:     0x2b5d15dc9b00 - middle::typeck::check::check_block_with_expected::h617a7d1f64ccc363Rrc::v0.10
  29:     0x2b5d15dc5210 - middle::typeck::check::check_fn::hedc9e5e9af7872e2p67::v0.10
  30:     0x2b5d15df7dd0 - middle::typeck::check::check_expr_with_unifier::check_expr_fn::h392bddc8bae422eapWa::v0.10
  31:     0x2b5d15de9b50 - middle::typeck::check::check_expr_with_unifier::h3dc9d4d7d7b503ffX79::v0.10
  32:     0x2b5d15df3e70 - middle::typeck::check::check_expr_with_unifier::check_argument_types::hf94095d0f2af9975Zba::v0.10
  33:     0x2b5d15de9b50 - middle::typeck::check::check_expr_with_unifier::h3dc9d4d7d7b503ffX79::v0.10
  34:     0x2b5d15e06440 - middle::typeck::check::check_stmt::h8de0f29e39556877Cnc::v0.10
  35:     0x2b5d15dc9b00 - middle::typeck::check::check_block_with_expected::h617a7d1f64ccc363Rrc::v0.10
  36:     0x2b5d15dc5210 - middle::typeck::check::check_fn::hedc9e5e9af7872e2p67::v0.10
  37:     0x2b5d15dc4fb0 - middle::typeck::check::check_bare_fn::hd29dcfcb90e3097bhW7::v0.10
  38:     0x2b5d15dce900 - middle::typeck::check::check_method_body::h9ac46b731aeb38bfkr8::v0.10
  39:     0x2b5d15dc0370 - middle::typeck::check::check_item::h8a242bd677d2cb0cPg8::v0.10
  40:     0x2b5d15dc1de0 - visit::walk_item::hc01793bd70dfd3f2Ltx::v0.10
  41:     0x2b5d15dc1de0 - visit::walk_item::hc01793bd70dfd3f2Ltx::v0.10
  42:     0x2b5d15dc1de0 - visit::walk_item::hc01793bd70dfd3f2Ltx::v0.10
  43:     0x2b5d15dc4e90 - middle::typeck::check::check_item_types::h743c31ff8fd17826RV7::v0.10
  44:     0x2b5d15efcb50 - util::common::time::h7248430f34fd512dXHh::v0.10
  45:     0x2b5d15efb720 - middle::typeck::check_crate::h9a4f17addda92887tTu::v0.10
  46:     0x2b5d1632f6d0 - driver::driver::phase_3_run_analysis_passes::hac32a0798c64c5ffvxf::v0.10
  47:     0x2b5d163360d0 - driver::driver::compile_input::h82298c25e912b787sWf::v0.10
  48:     0x2b5d1635a6c0 - run_compiler::h5ede87f710b5db3dYzn::v0.10
  49:     0x2b5d16371c10 - main_args::closure.92068
  50:     0x2b5d16370540 - monitor::closure.91953
  51:     0x2b5d1636be70 - task::TaskBuilder::try::closure.91728
  52:     0x2b5d155cd040 - task::spawn_opts::closure.7828
  53:     0x2b5d181b0a00 - rt::task::Task::run::closure.41627
  54:     0x2b5d181bc010 - rust_try
  55:     0x2b5d181b0840 - rt::task::Task::run::h5215c3a91f0146e3fs9::v0.10
  56:     0x2b5d155cce10 - task::spawn_opts::closure.7801
  57:     0x2b5d181b40b0 - rt::thread::thread_start::h90f81afe93891a62W69::v0.10
  58:     0x2b5d1b1e3fa0 - start_thread
  59:     0x2b5d18900a09 - __clone
  60:                0x0 - <unknown>
$ rustc -v
rustc 0.10 (46867cc 2014-04-02 16:59:39 -0700)
host: x86_64-unknown-linux-gnu

@cptroot
Copy link

cptroot commented May 1, 2014

I just got this same error on Windows 7 with rustc -v:

rustc 0.11-pre-nightly (7a19a82 2014-04-27 23:06:41 -0700)
host: i686-pc-mingw32

The error I got was

error: internal compiler error: unexpected failure
note: the compiler hit an unexpected failure path. this is a bug.
note: we would appreciate a bug report: http://static.rust-lang.org/doc/master/c
omplement-bugreport.html
note: run with `RUST_BACKTRACE=1` for a backtrace
task 'rustc' failed at 'index out of bounds: the len is 0 but the index is 0', C
:\bot\slave\nightly-win\build\src\librustc\middle\check_match.rs:246

And it appears the offending code was a match with out of order statements, similar to what @nathanisaac-s reported.

match tail {
    &Cons(_, ~ref mut next) => { tail = next; },
    &Cons(_, ~Nil) => { break; },
}

Hope this helps.

flip1995 pushed a commit to flip1995/rust that referenced this issue Feb 27, 2024
fix suggestion error in [`useless_vec`]

fixes: rust-lang#12101

---

changelog: fix suggestion error in [`useless_vec`]

r+ `@matthiaskrgr` since they opened the issue?
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

5 participants