Closed
Description
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.