Skip to content

Nightly compiler panics when attempting to mutate a shared reference. #91206

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Jonxslays opened this issue Nov 25, 2021 · 7 comments · Fixed by #91212
Closed

Nightly compiler panics when attempting to mutate a shared reference. #91206

Jonxslays opened this issue Nov 25, 2021 · 7 comments · Fixed by #91212
Assignees
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-high High priority regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Jonxslays
Copy link

Jonxslays commented Nov 25, 2021

Code

fn does_work() -> Vec<usize> {
    // Does work and returns a new Vector
    vec![1, 2, 3]
}

struct TestClient {
    inner: Vec<usize>,
}

impl TestClient {
    fn transform(&mut self) {
        self.inner.extend(does_work());
    }

    fn get_inner_ref(&self) -> &Vec<usize> {
        &self.inner
    }
}

fn main() {
    let mut client = TestClient { inner: vec![] };
    client.transform();

    let mut inner = client.get_inner_ref();
    inner.clear();
}

Meta

rustc --version --verbose:

rustc 1.58.0-nightly (65c55bf93 2021-11-23)
binary: rustc
commit-hash: 65c55bf931a55e6b1e5ed14ad8623814a7386424
commit-date: 2021-11-23
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0

In case it helps, when using the stable toolchain, the compiler does not panic but instead emits this error:

$ cargo build
   Compiling bug_test v0.1.0 (/home/projects/bug_test)
warning: variable does not need to be mutable
  --> src/main.rs:24:9
   |
24 |     let mut inner = c.get_inner_ref();
   |         ----^^^^^
   |         |
   |         help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default

error[E0596]: cannot borrow `*inner` as mutable, as it is behind a `&` reference
  --> src/main.rs:25:5
   |
24 |     let mut inner = c.get_inner_ref();
   |         --------- help: consider changing this to be a mutable reference: `&mut Vec<usize>`
25 |     inner.clear();
   |     ^^^^^ `inner` is a `&` reference, so the data it refers to cannot be borrowed as mutable

For more information about this error, try `rustc --explain E0596`.
warning: `bug_test` (bin "bug_test") generated 1 warning
error: could not compile `bug_test` due to previous error; 1 warning emitted

Stable version used in above output:

binary: rustc
commit-hash: 59eed8a2aac0230a8b53e89d4e99d55912ba6b35
commit-date: 2021-11-01
host: x86_64-unknown-linux-gnu
release: 1.56.1
LLVM version: 13.0.0

Error output

   Compiling bug_test v0.1.0 (/home/projects/bug_test)
thread 'rustc' panicked at 'index out of bounds: the len is 5 but the index is 5', compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs:450:53
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/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.58.0-nightly (65c55bf93 2021-11-23) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type bin

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

query stack during panic:
#0 [mir_borrowck] borrow-checking `main`
#1 [analysis] running analysis passes on this crate
end of query stack
Backtrace

stack backtrace:
   0: rust_begin_unwind
             at /rustc/65c55bf931a55e6b1e5ed14ad8623814a7386424/library/std/src/panicking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/65c55bf931a55e6b1e5ed14ad8623814a7386424/library/core/src/panicking.rs:107:14
   2: core::panicking::panic_bounds_check
             at /rustc/65c55bf931a55e6b1e5ed14ad8623814a7386424/library/core/src/panicking.rs:75:5
   3: <core::option::Option<&rustc_middle::mir::Location>>::map::<rustc_span::span_encoding::Span, <rustc_borrowck::MirBorrowckCtxt>::report_mutability_error::{closure#2}>
   4: <rustc_borrowck::MirBorrowckCtxt>::report_mutability_error
   5: <rustc_borrowck::MirBorrowckCtxt>::access_place
   6: <rustc_borrowck::MirBorrowckCtxt as rustc_mir_dataflow::framework::visitor::ResultsVisitor>::visit_statement_before_primary_effect
   7: <rustc_mir_dataflow::framework::direction::Forward as rustc_mir_dataflow::framework::direction::Direction>::visit_results_in_block::<rustc_borrowck::dataflow::BorrowckAnalyses<rustc_index::bit_set::BitSet<rustc_borrowck::dataflow::BorrowIndex>, rustc_index::bit_set::BitSet<rustc_mir_dataflow::move_paths::MovePathIndex>, rustc_index::bit_set::BitSet<rustc_mir_dataflow::move_paths::InitIndex>>, rustc_borrowck::dataflow::BorrowckAnalyses<rustc_mir_dataflow::framework::engine::Results<rustc_borrowck::dataflow::Borrows>, rustc_mir_dataflow::framework::engine::Results<rustc_mir_dataflow::impls::MaybeUninitializedPlaces>, rustc_mir_dataflow::framework::engine::Results<rustc_mir_dataflow::impls::EverInitializedPlaces>>, rustc_borrowck::MirBorrowckCtxt>
   8: rustc_borrowck::do_mir_borrowck
   9: <rustc_infer::infer::InferCtxtBuilder>::enter::<rustc_middle::mir::query::BorrowCheckResult, rustc_borrowck::mir_borrowck::{closure#0}>
  10: rustc_borrowck::mir_borrowck
  11: <rustc_borrowck::provide::{closure#0} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::LocalDefId)>>::call_once
  12: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_span::def_id::LocalDefId, &rustc_middle::mir::query::BorrowCheckResult>
  13: rustc_data_structures::stack::ensure_sufficient_stack::<(&rustc_middle::mir::query::BorrowCheckResult, rustc_query_system::dep_graph::graph::DepNodeIndex), rustc_query_system::query::plumbing::execute_job<rustc_query_impl::plumbing::QueryCtxt, rustc_span::def_id::LocalDefId, &rustc_middle::mir::query::BorrowCheckResult>::{closure#3}>
  14: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::plumbing::QueryCtxt, rustc_query_system::query::caches::DefaultCache<rustc_span::def_id::LocalDefId, &rustc_middle::mir::query::BorrowCheckResult>>
  15: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::mir_borrowck
  16: <rustc_session::session::Session>::time::<(), rustc_interface::passes::analysis::{closure#2}>
  17: rustc_interface::passes::analysis
  18: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, (), core::result::Result<(), rustc_errors::ErrorReported>>
  19: rustc_data_structures::stack::ensure_sufficient_stack::<(core::result::Result<(), rustc_errors::ErrorReported>, rustc_query_system::dep_graph::graph::DepNodeIndex), rustc_query_system::query::plumbing::execute_job<rustc_query_impl::plumbing::QueryCtxt, (), core::result::Result<(), rustc_errors::ErrorReported>>::{closure#3}>
  20: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::plumbing::QueryCtxt, rustc_query_system::query::caches::DefaultCache<(), core::result::Result<(), rustc_errors::ErrorReported>>>
  21: rustc_query_system::query::plumbing::get_query::<rustc_query_impl::queries::analysis, rustc_query_impl::plumbing::QueryCtxt>
  22: <rustc_interface::passes::QueryContext>::enter::<rustc_driver::run_compiler::{closure#1}::{closure#2}::{closure#3}, core::result::Result<(), rustc_errors::ErrorReported>>
  23: <rustc_interface::interface::Compiler>::enter::<rustc_driver::run_compiler::{closure#1}::{closure#2}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_errors::ErrorReported>>
  24: rustc_span::with_source_map::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_interface::interface::create_compiler_and_run<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#1}>
  25: rustc_interface::interface::create_compiler_and_run::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>
  26: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

@Jonxslays Jonxslays added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 25, 2021
@hkratz
Copy link
Contributor

hkratz commented Nov 25, 2021

Bisected to #90352. CC @camsteffen @oli-obk

searched nightlies: from nightly-2021-10-01 to nightly-2021-11-25
regressed nightly: nightly-2021-11-23
searched commit range: 65f3f8b...936f260
regressed commit: cebd2dd

bisected with cargo-bisect-rustc v0.6.1

Host triple: aarch64-apple-darwin
Reproduce with:

cargo bisect-rustc 2021-10-01 --regress ice

@rustbot label regression-from-stable-to-nightly

@rustbot rustbot added regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Nov 25, 2021
@compiler-errors
Copy link
Member

I can fix this quickly -- seems like just changing an [index] to a get() since the statement_index can be out of bounds.

@rustbot claim

@compiler-errors
Copy link
Member

(only tangentially related) I also noticed a regression in error spans between stable and beta...

stable:

25 |     inner.clear();
   |     ^^^^^ `inner` is a `&` reference, so the data it refers to cannot be borrowed as mutable

beta:

25 |     inner.clear();
   |     ^^^^^^^^^^^^^ `inner` is a `&` reference, so the data it refers to cannot be borrowed as mutable

@Aaron1011
Copy link
Member

@compiler-errors That was an intentional change, meant to highlight the fact that the attempted mutable borrow is occurring because of the method call. However, it seems to be confusing for a lot of people, so I'll look into improving it.

@compiler-errors
Copy link
Member

@Aaron1011 Ah thanks for letting me know.

Maybe we can split the span up into the receiver and the method, and add a note on the latter like "due to this method which takes &mut self". Not sure if that much info bubbles down into borrowck. I'd be interested in making that change if you've got a lot on your plate and it's (relatively) easy to do.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 25, 2021
Fix ICE due to out-of-bounds statement index when reporting borrowck error

Replace an `[index]` with a `.get` when `statement_index` points to a basic-block terminator (and is therefore out-of-bounds in the statements list).

Fixes rust-lang#91206
Cc `@camsteffen`
r? `@oli-obk`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 25, 2021
Fix ICE due to out-of-bounds statement index when reporting borrowck error

Replace an `[index]` with a `.get` when `statement_index` points to a basic-block terminator (and is therefore out-of-bounds in the statements list).

Fixes rust-lang#91206
Cc ``@camsteffen``
r? ``@oli-obk``
@camelid
Copy link
Member

camelid commented Nov 25, 2021

Maybe we can split the span up into the receiver and the method, and add a note on the latter like "due to this method which takes &mut self". Not sure if that much info bubbles down into borrowck. I'd be interested in making that change if you've got a lot on your plate and it's (relatively) easy to do.

See #90286 for discussion about this exact idea. It'd be nice, but likely hard to implement.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 25, 2021
Fix ICE due to out-of-bounds statement index when reporting borrowck error

Replace an `[index]` with a `.get` when `statement_index` points to a basic-block terminator (and is therefore out-of-bounds in the statements list).

Fixes rust-lang#91206
Cc ```@camsteffen```
r? ```@oli-obk```
@apiraino
Copy link
Contributor

Assigning priority as discussed in the Zulip thread of the Prioritization Working Group.

@rustbot label -I-prioritize +P-high

@rustbot rustbot added P-high High priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Nov 26, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 26, 2021
Fix ICE due to out-of-bounds statement index when reporting borrowck error

Replace an `[index]` with a `.get` when `statement_index` points to a basic-block terminator (and is therefore out-of-bounds in the statements list).

Fixes rust-lang#91206
Cc `@camsteffen`
r? `@oli-obk`
@bors bors closed this as completed in 13c6066 Nov 26, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-high High priority regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. 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.

7 participants