Skip to content

[ICE] With closure on mutable ref #81700

@leonardo-m

Description

@leonardo-m
fn foo(x: &mut u32) {
    let bar = || { foo(x); };
    bar();
}
fn main() {}

Compiled with rustc bug2.rs gives:

error: internal compiler error: compiler\rustc_mir\src\borrow_check\diagnostics\mutability_errors.rs:525:22: upvar `x` borrowed, but not mutably

thread 'rustc' panicked at 'Box<Any>', compiler\rustc_errors\src\lib.rs:958:9
stack backtrace:
   0: std::panicking::begin_panic
   1: rustc_errors::HandlerInner::bug
   2: rustc_errors::Handler::bug
   3: rustc_middle::util::bug::opt_span_bug_fmt::{{closure}}
   4: rustc_middle::ty::context::tls::with_opt
   5: rustc_middle::util::bug::opt_span_bug_fmt
   6: rustc_middle::util::bug::bug_fmt
   7: rustc_mir::borrow_check::diagnostics::mutability_errors::<impl rustc_mir::borrow_check::MirBorrowckCtxt>::show_mutating_upvar
   8: rustc_mir::borrow_check::diagnostics::mutability_errors::<impl rustc_mir::borrow_check::MirBorrowckCtxt>::report_mutability_error
   9: rustc_mir::borrow_check::MirBorrowckCtxt::access_place
  10: <rustc_mir::borrow_check::MirBorrowckCtxt as rustc_mir::dataflow::framework::visitor::ResultsVisitor>::visit_statement_before_primary_effect
  11: <rustc_mir::dataflow::framework::direction::Forward as rustc_mir::dataflow::framework::direction::Direction>::visit_results_in_block
  12: rustc_mir::dataflow::framework::visitor::visit_results
  13: rustc_mir::borrow_check::do_mir_borrowck
  14: rustc_infer::infer::InferCtxtBuilder::enter
  15: rustc_mir::borrow_check::mir_borrowck
  16: core::ops::function::FnOnce::call_once
  17: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::mir_borrowck>::compute
  18: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  19: rustc_middle::ty::query::plumbing::<impl rustc_query_system::query::QueryContext for rustc_middle::ty::context::TyCtxt>::start_query::{{closure}}::{{closure}}::{{closure}}
  20: rustc_query_system::query::plumbing::force_query_with_job
  21: rustc_query_system::query::plumbing::get_query_impl
  22: rustc_query_system::query::plumbing::ensure_query_impl
  23: rustc_session::utils::<impl rustc_session::session::Session>::time
  24: rustc_interface::passes::analysis
  25: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::analysis>::compute
  26: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  27: rustc_middle::ty::query::plumbing::<impl rustc_query_system::query::QueryContext for rustc_middle::ty::context::TyCtxt>::start_query::{{closure}}::{{closure}}::{{closure}}
  28: rustc_query_system::query::plumbing::force_query_with_job
  29: rustc_query_system::query::plumbing::get_query_impl
  30: rustc_interface::passes::QueryContext::enter
  31: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  32: rustc_span::with_source_map
  33: rustc_interface::interface::create_compiler_and_run
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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.51.0-nightly (d4e3570db 2021-02-01) running on x86_64-pc-windows-gnu

query stack during panic:
#0 [mir_borrowck] borrow-checking `foo`
#1 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to previous error

Activity

matthiaskrgr

matthiaskrgr commented on Feb 3, 2021

@matthiaskrgr
Member

This was caused by 74500b9
@rustbot prioritize

edit:
in beta, this gives the following error:

error[E0596]: cannot borrow `bar` as mutable, as it is not declared as mutable
 --> src/main.rs:3:5
  |
2 |     let bar = || { foo(x); };
  |         --- help: consider changing this to be mutable: `mut bar`
3 |     bar();
  |     ^^^ cannot borrow as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
added
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
on Feb 3, 2021
added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Feb 3, 2021
apiraino

apiraino commented on Feb 3, 2021

@apiraino
Contributor

Assigning P-medium as discussed as part of the Prioritization Working Group procedure and removing I-prioritize.

camelid

camelid commented on Feb 3, 2021

@camelid
Member

Looks likely that #81158 is the cause. cc @Aaron1011

camelid

camelid commented on Feb 3, 2021

@camelid
Member

Re-prioritizing as P-high and removing I-prioritize as discussed in the prioritization working group.

@rustbot label: +regression-from-stable-to-nightly +P-high -P-medium -I-prioritize

added
P-highHigh priority
and removed
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
on Feb 3, 2021
self-assigned this
on Feb 3, 2021
added a commit that references this issue on Feb 3, 2021
bc84e21
added a commit that references this issue on Feb 3, 2021
e708cbd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-closuresArea: Closures (`|…| { … }`)C-bugCategory: This is a bug.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @matthiaskrgr@Aaron1011@apiraino@leonardo-m@camelid

    Issue actions

      [ICE] With closure on mutable ref · Issue #81700 · rust-lang/rust