Skip to content

ICE when iterating over string literal with manual newline insertions #59954

Closed
@snOm3ad

Description

@snOm3ad

The error message by the compiler was, thread 'rustc' panicked at 'index out of bounds: the len is 73 but the index is 74', /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/libcore/slice/mod.rs:2539:10.

The code:

pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
    let mut results = Vec::new();
    for line in contents.lines() {
        if line.contains(query) {
            results.push(line);
        }
    }
    results
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn one_result() {
        let query = "duct";
        let contents = "Rust:\nsafe, fast, productive.\nPick three.";
        assert_eq!(vec!["safe, fast, productive."], search(query, contents));
    }
}

Changing the contents string literal to be formatted in the conventional way, i.e. without the manually inserted newline characters makes the code compile again (see below).

        let contents = "\
Rust:
safe, fast, productive.
Pick three.";

Meta

The complete error message

$ cargo test
   Compiling minigrep v0.1.0 (/Users/snOm3ad/Desktop/rust/minigrep)
thread 'rustc' panicked at 'index out of bounds: the len is 73 but the index is 74', /rustc/91856ed52c58aa5ba66a015354d1cc69e9779bdf/src/libcore/slice/mod.rs:2539:10                                    
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

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: rustc 1.34.0 (91856ed52 2019-04-10) running on x86_64-apple-darwin

note: compiler flags: -C debuginfo=2 -C incremental

note: some of the compiler flags provided by cargo are hidden

error: Could not compile `minigrep`.
warning: build failed, waiting for other jobs to finish...
error: build failed

The backtrace:

stack backtrace:                                                                                                                                                                                          
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace                                                                                                                                           
   1: std::sys_common::backtrace::_print                                                                                                                                                                  
   2: std::panicking::default_hook::{{closure}}                                                                                                                                                           
   3: std::panicking::default_hook                                                                                                                                                                        
   4: rustc::util::common::panic_hook                                                                                                                                                                     
   5: std::panicking::rust_panic_with_hook                                                                                                                                                                
   6: std::panicking::continue_panic_fmt                                                                                                                                                                  
   7: rust_begin_unwind                                                                                                                                                                                   
   8: core::panicking::panic_fmt                                                                                                                                                                          
   9: core::panicking::panic_bounds_check                                                                                                                                                                 
  10: <rustc::ty::query::on_disk_cache::CacheDecoder<'a, 'tcx, 'x> as serialize::serialize::SpecializedDecoder<syntax_pos::span_encoding::Span>>::specialized_decode                                      
  11: serialize::serialize::Decoder::read_struct                                                                                                                                                          
  12: serialize::serialize::Decoder::read_seq                                                                                                                                                             
  13: serialize::serialize::Decoder::read_struct                                                                                                                                                          
  14: serialize::serialize::Decoder::read_seq                                                                                                                                                             
  15: <rustc::mir::Mir<'tcx> as serialize::serialize::Decodable>::decode::{{closure}}                                                                                                                     
  16: rustc::ty::query::on_disk_cache::OnDiskCache::try_load_query_result                                                                                                                                 
  17: <rustc::ty::query::queries::optimized_mir<'tcx> as rustc::ty::query::config::QueryDescription<'tcx>>::try_load_from_disk                                                                            
  18: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query                                                                                                            
  19: rustc_mir::monomorphize::collector::collect_items_rec                                                                                                                                               
  20: rustc_mir::monomorphize::collector::collect_items_rec                                                                                                                                               
  21: rustc_mir::monomorphize::collector::collect_crate_mono_items::{{closure}}                                                                                                                           
  22: rustc::util::common::time                                                                                                                                                                           
  23: rustc_mir::monomorphize::collector::collect_crate_mono_items                                                                                                                                        
  24: rustc::util::common::time                                                                                                                                                                           
  25: rustc_mir::monomorphize::partitioning::collect_and_partition_mono_items                                                                                                                             
  26: rustc::ty::query::__query_compute::collect_and_partition_mono_items                                                                                                                                 
  27: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::collect_and_partition_mono_items<'tcx>>::compute                                              
  28: rustc::dep_graph::graph::DepGraph::with_task_impl                                                                                                                                                   
  29: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query                                                                                                            
  30: rustc_codegen_ssa::base::codegen_crate                                                                                                                                                              
  31: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_utils::codegen_backend::CodegenBackend>::codegen_crate                                                                                     
  32: rustc::util::common::time                                                                                                                                                                           
  33: rustc_driver::driver::phase_4_codegen                                                                                                                                                               
  34: rustc_driver::driver::compile_input::{{closure}}                                                                                                                                                    
  35: <std::thread::local::LocalKey<T>>::with                                                                                                                                                             
  36: rustc::ty::context::TyCtxt::create_and_enter                                                                                                                                                        
  37: rustc_driver::driver::compile_input                                                                                                                                                                 
  38: rustc_driver::run_compiler_with_pool                                                                                                                                                                
  39: <scoped_tls::ScopedKey<T>>::set                                                                                                                                                                     
  40: rustc_driver::run_compiler                                                                                                                                                                          
  41: syntax::with_globals                                                                                                                                                                                
  42: __rust_maybe_catch_panic                                                                                                                                                                            
  43: <F as alloc::boxed::FnBox<A>>::call_box                                                                                                                                                             
  44: std::sys::unix::thread::Thread::new::thread_start                                                                                                                                                   
  45: _pthread_body                                                                                                                                                                                       
  46: _pthread_start                                                                                                                                                                                      
query stack during panic:                                                                                                                                                                                 
#0 [optimized_mir] processing `tests::one_result::{{closure}}`                                                                                                                                           
#1 [collect_and_partition_mono_items] collect_and_partition_mono_items                                                                                                                                   
end of query stack

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable ExampleI-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions