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

ICE: local variable without a name #63026

Closed
khernyo opened this issue Jul 26, 2019 · 1 comment · Fixed by #63051
Closed

ICE: local variable without a name #63026

khernyo opened this issue Jul 26, 2019 · 1 comment · Fixed by #63051
Labels
A-NLL Area: Non Lexical Lifetimes (NLL) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@khernyo
Copy link
Contributor

khernyo commented Jul 26, 2019

The following code causes an internal compiler error on 1.37.0-beta.6 and 1.38.0-nightly. On stable it's a simple error, though the message could be improved (#63027).

use std::collections::HashMap;
use std::hash::Hash;

fn group_by<I, F, T>(xs: &mut I, f: F) -> HashMap<T, Vec<&I::Item>>
where
    I: Iterator,
    F: Fn(&I::Item) -> T,
    T: Eq + Hash,
{
    let mut result = HashMap::new();
    for ref x in xs {
        let key = f(x);
        result.entry(key).or_insert(Vec::new()).push(x);
    }
    result
}

(Playground)

Output of rustup run nightly rustc --crate-type lib lib.rs:

error: internal compiler error: src/librustc_mir/borrow_check/conflict_errors.rs:1155: local variable without a name

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:646:9
stack backtrace:
   0:     0x7f1b61494ffb - backtrace::backtrace::libunwind::trace::hebd3abdfda411954
                               at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.29/src/backtrace/libunwind.rs:88
   1:     0x7f1b61494ffb - backtrace::backtrace::trace_unsynchronized::h99ec2b04a3a3fae2
                               at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.29/src/backtrace/mod.rs:66
   2:     0x7f1b61494ffb - std::sys_common::backtrace::_print::h4f8a08c3de6b2188
                               at src/libstd/sys_common/backtrace.rs:47
   3:     0x7f1b61494ffb - std::sys_common::backtrace::print::h577ed431dde2ed30
                               at src/libstd/sys_common/backtrace.rs:36
   4:     0x7f1b61494ffb - std::panicking::default_hook::{{closure}}::h8d03b1947aec3300
                               at src/libstd/panicking.rs:200
   5:     0x7f1b61494cd6 - std::panicking::default_hook::h339346c6666eb959
                               at src/libstd/panicking.rs:214
   6:     0x7f1b6323f151 - rustc::util::common::panic_hook::heb70e88bb67cd321
   7:     0x7f1b61495856 - std::panicking::rust_panic_with_hook::h379f5a37ec5e89b3
                               at src/libstd/panicking.rs:481
   8:     0x7f1b636fa1fd - std::panicking::begin_panic::h1bfa8dd80157a330
   9:     0x7f1b63717b0f - rustc_errors::Handler::bug::h77a54955c8e87e0c
  10:     0x7f1b62fcb883 - rustc::util::bug::opt_span_bug_fmt::{{closure}}::hb18fb2b1678fff1d
  11:     0x7f1b62fbfa63 - rustc::ty::context::tls::with_opt::{{closure}}::h71ce051429c1d0d3
  12:     0x7f1b62fbf9d3 - rustc::ty::context::tls::with_context_opt::hf63c72df770439a6
  13:     0x7f1b62fbfa17 - rustc::ty::context::tls::with_opt::h6e270c0412653eb9
  14:     0x7f1b62fcb798 - rustc::util::bug::opt_span_bug_fmt::h0b2ec00e2c3d7335
  15:     0x7f1b62fcb702 - rustc::util::bug::bug_fmt::h3c8524d7d8ffe695
  16:     0x7f1b6226a787 - rustc_mir::borrow_check::conflict_errors::<impl rustc_mir::borrow_check::MirBorrowckCtxt>::try_report_cannot_return_reference_to_local::h0d03c084a154cf79
  17:     0x7f1b62267bfd - rustc_mir::borrow_check::conflict_errors::<impl rustc_mir::borrow_check::MirBorrowckCtxt>::report_borrowed_value_does_not_live_long_enough::h6b10d8c35db41925
  18:     0x7f1b621d44b1 - rustc_mir::borrow_check::path_utils::each_borrow_involving_path::hcdc94227f89c260a
  19:     0x7f1b6227a3d1 - rustc_mir::borrow_check::MirBorrowckCtxt::access_place::hd484fc912b955c66
  20:     0x7f1b622799c3 - <rustc_mir::borrow_check::MirBorrowckCtxt as rustc_mir::dataflow::DataflowResultsConsumer>::visit_terminator_entry::h2834094bca128fad
  21:     0x7f1b62276427 - rustc_mir::borrow_check::do_mir_borrowck::hbf7ffdd9614e764c
  22:     0x7f1b62324936 - rustc::ty::context::GlobalCtxt::enter_local::hbdb8f2f2c2455e45
  23:     0x7f1b622745ee - rustc_mir::borrow_check::mir_borrowck::h9e91484bb6fbe128
  24:     0x7f1b61b078a6 - rustc::ty::query::__query_compute::mir_borrowck::h4a723163f585f3b5
  25:     0x7f1b61adcc2c - rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::mir_borrowck>::compute::hdac7dc2bf600a691
  26:     0x7f1b61b0aae4 - rustc::dep_graph::graph::DepGraph::with_task_impl::h306e313d4fc38690
  27:     0x7f1b61aea688 - rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query::h7273bdc528b59869
  28:     0x7f1b61adc99d - rustc::ty::<impl rustc::ty::context::TyCtxt>::par_body_owners::haee3db44174b2c94
  29:     0x7f1b61ab5cbc - rustc::util::common::time::hf8cd301d6dcfb12b
  30:     0x7f1b61a5ea59 - rustc_interface::passes::analysis::h611427e84b6d30de
  31:     0x7f1b619463a5 - rustc::ty::query::__query_compute::analysis::h13f044dafe7e51d2
  32:     0x7f1b6194760d - rustc::dep_graph::graph::DepGraph::with_task_impl::h3245cc04ae63d6c0
  33:     0x7f1b619587df - rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query::h59de3ec3903bd5d2
  34:     0x7f1b6196792a - rustc_interface::passes::BoxedGlobalCtxt::access::{{closure}}::he15abda5d3eb7dd4
  35:     0x7f1b61ab80ca - rustc_interface::passes::create_global_ctxt::{{closure}}::hdecf9455f9665b51
  36:     0x7f1b6196a257 - rustc_interface::interface::run_compiler_in_existing_thread_pool::h94c4f48e3489a9ef
  37:     0x7f1b619a3802 - std::thread::local::LocalKey<T>::with::h592677f58f0e6cf5
  38:     0x7f1b6197d94e - scoped_tls::ScopedKey<T>::set::h391138e22a7da238
  39:     0x7f1b619b9aa4 - syntax::with_globals::hfcf7c84f68aca2a1
  40:     0x7f1b6193bf02 - std::sys_common::backtrace::__rust_begin_short_backtrace::h9726e5336ccbb89e
  41:     0x7f1b614a60fa - __rust_maybe_catch_panic
                               at src/libpanic_unwind/lib.rs:82
  42:     0x7f1b6196b7c9 - core::ops::function::FnOnce::call_once{{vtable.shim}}::he23a0634afcd614f
  43:     0x7f1b61478d5f - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h85753735885edbd7
                               at /rustc/890881f8f4c77e8670d4b32104c0325fcfefc90f/src/liballoc/boxed.rs:766
  44:     0x7f1b614a4d80 - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h275915ff969a4972
                               at /rustc/890881f8f4c77e8670d4b32104c0325fcfefc90f/src/liballoc/boxed.rs:766
  45:     0x7f1b614a4d80 - std::sys_common::thread::start_thread::h633d1d20ef49db8f
                               at src/libstd/sys_common/thread.rs:13
  46:     0x7f1b614a4d80 - std::sys::unix::thread::Thread::new::thread_start::h71de64bdf6dee07c
                               at src/libstd/sys/unix/thread.rs:79
  47:     0x7f1b613d4fa3 - start_thread
  48:     0x7f1b612f44cf - clone
  49:                0x0 - <unknown>
query stack during panic:
#0 [mir_borrowck] processing `group_by`
#1 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to previous error


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.38.0-nightly (890881f8f 2019-07-25) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib

rustup run beta rustc --version --verbose:

rustc 1.37.0-beta.6 (61d1607e0 2019-07-22)
binary: rustc
commit-hash: 61d1607e0f6a18bb4897d6f9b10abeac9e11eb8e
commit-date: 2019-07-22
host: x86_64-unknown-linux-gnu
release: 1.37.0-beta.6
LLVM version: 8.0

rustup run nightly rustc --version --verbose:

rustc 1.38.0-nightly (890881f8f 2019-07-25)
binary: rustc
commit-hash: 890881f8f4c77e8670d4b32104c0325fcfefc90f
commit-date: 2019-07-25
host: x86_64-unknown-linux-gnu
release: 1.38.0-nightly
LLVM version: 9.0
@jonas-schievink jonas-schievink added A-NLL Area: Non Lexical Lifetimes (NLL) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. I-nominated labels Jul 26, 2019
@estebank
Copy link
Contributor

estebank commented Jul 27, 2019

Dumped HIR:

#[prelude_import]
use ::std::prelude::v1::*;
#[macro_use]
extern crate std;
use std::collections::HashMap;
use std::hash::Hash;

fn group_by<I, F, T>(xs: &mut I, f: F) -> HashMap<T, Vec<&<I>::Item>> where
 I: Iterator, F: Fn(&<I>::Item) -> T, T: Eq +
 Hash {
          let mut result = <HashMap>::new();
          {
              let _t =
                  match ::std::iter::IntoIterator::into_iter(xs) {
                      mut iter =>
                      loop  {
                          let mut __next;
                          match ::std::iter::Iterator::next(&mut iter) {
                              ::std::option::Option::Some(val) =>
                              __next = val,
                              ::std::option::Option::None => break ,
                          }
                          let ref x = __next;
                          {
                              let key = f(x);
                              result.entry(key).or_insert(<Vec>::new()).push(x);
                          }
                      },
                  };
              _t
          };
          result
      }

fn main() { }

Error on it:

error[E0515]: cannot return value referencing local variable `__next`
  --> file9.rs:28:11
   |
19 |                           let ref x = __next;
   |                               ----- `__next` is borrowed here
...
28 |           result
   |           ^^^^^^ returns a value referencing data owned by the current function

The error once fixed should be closer to

error[E0515]: cannot return value referencing function parameter `xs`
  --> lib.rs:15:5
   |
11 |     for ref x in xs {
   |         ----- `xs` is borrowed here
...
15 |     result
   |     ^^^^^^ returns a value referencing data owned by the current function

This is caused because of this:

/// Appends end-user visible description of the `local` place to `buf`. If `local` doesn't have
/// a name, or its name was generated by the compiler, then `Err` is returned
fn append_local_to_string(&self, local_index: Local, buf: &mut String) -> Result<(), ()> {
let local = &self.body.local_decls[local_index];
match local.name {
Some(name) if !local.from_compiler_desugaring() => {
buf.push_str(name.as_str().get());
Ok(())
}
_ => Err(()),
}
}

It seems to me like its reasonable to remove the bug!() call and just refer to "a borrow happens here", although ideally we would carry the information of the original loan to be able to talk accurately about xs:

LocalKind::Var => bug!("local variable without a name"),

Centril added a commit to Centril/rust that referenced this issue Jul 28, 2019
Avoid ICE when referencing desugared local binding in borrow error

To avoid leaking the names of local bindings from expressions like for loops, rust-lang#60984 explicitly ignored them, but an assertion that `LocalKind::Var` *must* have a name would trigger an ICE.

Before this change, the binding generated by desugaring the for loop would leak into the diagnostic (rust-lang#63027):
```
error[E0515]: cannot return value referencing local variable `__next`
  --> return-local-binding-from-desugaring.rs:LL:CC
   |
LL |     for ref x in xs {
   |         ----- `__next` is borrowed here
...
LL |     result
   |     ^^^^^^ returns a value referencing data owned by the current function
```

Ideally `LocalKind` would carry more information to more accurately explain the problem, but for now, in order to avoid the ICE (fix rust-lang#63026), we accept `LocalKind::Var` without a name and produce the following output:

```
error[E0515]: cannot return value referencing local binding
  --> $DIR/return-local-binding-from-desugaring.rs:30:5
   |
LL |     for ref x in xs {
   |                  -- local binding introduced here
...
LL |     result
   |     ^^^^^^ returns a value referencing data owned by the current function
```
Centril added a commit to Centril/rust that referenced this issue Jul 28, 2019
Avoid ICE when referencing desugared local binding in borrow error

To avoid leaking the names of local bindings from expressions like for loops, rust-lang#60984 explicitly ignored them, but an assertion that `LocalKind::Var` *must* have a name would trigger an ICE.

Before this change, the binding generated by desugaring the for loop would leak into the diagnostic (rust-lang#63027):
```
error[E0515]: cannot return value referencing local variable `__next`
  --> return-local-binding-from-desugaring.rs:LL:CC
   |
LL |     for ref x in xs {
   |         ----- `__next` is borrowed here
...
LL |     result
   |     ^^^^^^ returns a value referencing data owned by the current function
```

Ideally `LocalKind` would carry more information to more accurately explain the problem, but for now, in order to avoid the ICE (fix rust-lang#63026), we accept `LocalKind::Var` without a name and produce the following output:

```
error[E0515]: cannot return value referencing local binding
  --> $DIR/return-local-binding-from-desugaring.rs:30:5
   |
LL |     for ref x in xs {
   |                  -- local binding introduced here
...
LL |     result
   |     ^^^^^^ returns a value referencing data owned by the current function
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-NLL Area: Non Lexical Lifetimes (NLL) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants