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

Panic in nightly rustc when compiling code that tries to borrow *v as mutable behind & reference #82032

Closed
KerfuffleV2 opened this issue Feb 12, 2021 · 8 comments · Fixed by #82056
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ 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

@KerfuffleV2
Copy link

KerfuffleV2 commented Feb 12, 2021

Code

use std::collections::HashMap;
use async_std::{fs, io, prelude::*};

type Handle = io::BufWriter<fs::File>;
struct Thing(HashMap<String, Handle>);

impl Thing {
    pub async fn die_horribly(&mut self) {
        for v in self.0.values() {
            v.flush().await;
        }
    }
}

fn main() {
    println!("Hello, world!");
}

Dependencies from Cargo.toml

[dependencies]
async-std = "1.9.0"

Meta

rustc --version --verbose:

rustc 1.52.0-nightly (e9920ef77 2021-02-11)
binary: rustc
commit-hash: e9920ef7749d11fc71cc32ca4ba055bcfeaab945
commit-date: 2021-02-11
host: x86_64-unknown-linux-gnu
release: 1.52.0-nightly
LLVM version: 11.0.1

The internal compiler error does not occur in stable (rustc 1.50.0 (cb75ad5db 2021-02-10)):

% cargo build
   Compiling rusterr v0.1.0 (/home/user/devel/rusterr)
error[E0596]: cannot borrow `*v` as mutable, as it is behind a `&` reference
  --> src/main.rs:10:13
   |
9  |         for v in self.0.values() {
   |                  --------------- this iterator yields `&` references
10 |             v.flush().await;
   |             ^ `v` is a `&` reference, so the data it refers to cannot be borrowed as mutable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
error: could not compile `rusterr`

The fix for compilation is to use values_mut() instead of values() and this also prevents the panic in the nightly version of rustc.

Error output

% cargo build
   Compiling rusterr v0.1.0 (/home/user/devel/rusterr)
error: internal compiler error: compiler/rustc_middle/src/hir/map/mod.rs:306:18: impossible case reached

thread 'rustc' panicked at 'Box<Any>', /rustc/e9920ef7749d11fc71cc32ca4ba055bcfeaab945/library/std/src/panic.rs:59:5
note: run with `RUST_BACKTRACE=1` environment variable to display a 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.52.0-nightly (e9920ef77 2021-02-11) 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 `Thing::die_horribly::{closure#0}`
#1 [mir_borrowck] borrow-checking `Thing::die_horribly`
end of query stack
error: aborting due to previous error

error: could not compile `rusterr`
Backtrace

% RUST_BACKTRACE=1 cargo build
   Compiling rusterr v0.1.0 (/home/user/devel/rusterr)
error: internal compiler error: compiler/rustc_middle/src/hir/map/mod.rs:306:18: impossible case reached

thread 'rustc' panicked at 'Box<Any>', /rustc/e9920ef7749d11fc71cc32ca4ba055bcfeaab945/library/std/src/panic.rs:59:5
stack backtrace:
   0: std::panicking::begin_panic
   1: std::panic::panic_any
   2: rustc_errors::HandlerInner::bug
   3: rustc_errors::Handler::bug
   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_middle::hir::map::Map::item
   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: 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
  17: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  18: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  19: rustc_data_structures::stack::ensure_sufficient_stack
  20: rustc_query_system::query::plumbing::force_query_with_job
  21: rustc_query_system::query::plumbing::get_query_impl
  22: rustc_mir::borrow_check::type_check::TypeChecker::check_rvalue
  23: rustc_mir::borrow_check::type_check::TypeChecker::typeck_mir
  24: rustc_mir::borrow_check::type_check::type_check
  25: rustc_mir::borrow_check::nll::compute_regions
  26: rustc_mir::borrow_check::do_mir_borrowck
  27: rustc_infer::infer::InferCtxtBuilder::enter
  28: rustc_mir::borrow_check::mir_borrowck
  29: 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
  30: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  31: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  32: rustc_data_structures::stack::ensure_sufficient_stack
  33: rustc_query_system::query::plumbing::force_query_with_job
  34: rustc_query_system::query::plumbing::get_query_impl
  35: rustc_typeck::collect::type_of::type_of
  36: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::type_of>::compute
  37: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  38: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  39: rustc_data_structures::stack::ensure_sufficient_stack
  40: rustc_query_system::query::plumbing::force_query_with_job
  41: rustc_query_system::query::plumbing::get_query_impl
  42: rustc_typeck::check::check::check_item_type
  43: rustc_middle::hir::map::Map::visit_item_likes_in_module
  44: rustc_typeck::check::check::check_mod_item_types
  45: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::check_mod_item_types>::compute
  46: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  47: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  48: rustc_data_structures::stack::ensure_sufficient_stack
  49: rustc_query_system::query::plumbing::force_query_with_job
  50: rustc_query_system::query::plumbing::get_query_impl
  51: rustc_query_system::query::plumbing::ensure_query_impl
  52: rustc_session::utils::<impl rustc_session::session::Session>::time
  53: rustc_typeck::check_crate
  54: rustc_interface::passes::analysis
  55: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::analysis>::compute
  56: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  57: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  58: rustc_data_structures::stack::ensure_sufficient_stack
  59: rustc_query_system::query::plumbing::force_query_with_job
  60: rustc_query_system::query::plumbing::get_query_impl
  61: rustc_interface::passes::QueryContext::enter
  62: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  63: rustc_span::with_source_map
  64: rustc_interface::interface::create_compiler_and_run
  65: scoped_tls::ScopedKey<T>::set
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.52.0-nightly (e9920ef77 2021-02-11) 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 `Thing::die_horribly::{closure#0}`
#1 [mir_borrowck] borrow-checking `Thing::die_horribly`
#2 [type_of] computing type of `Thing::die_horribly::{opaque#0}`
#3 [check_mod_item_types] checking item types in top-level module
#4 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to previous error

error: could not compile `rusterr`

@KerfuffleV2 KerfuffleV2 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 Feb 12, 2021
@jonas-schievink jonas-schievink added the regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. label Feb 12, 2021
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Feb 12, 2021
@jyn514 jyn514 added the E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc label Feb 12, 2021
@jyn514
Copy link
Member

jyn514 commented Feb 12, 2021

It would be nice to reproduce this without the async-std dependency.

@jyn514 jyn514 added the E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example label Feb 12, 2021
@jyn514
Copy link
Member

jyn514 commented Feb 12, 2021

Slightly smaller:

use async_std::{fs, io, prelude::*};

type Handle = io::BufWriter<fs::File>;

async fn die_horribly(thing: &[Handle]) {
    for v in thing {
        v.flush().await;
    }
}

fn main() {}

@jyn514
Copy link
Member

jyn514 commented Feb 12, 2021

I bisected far enough to find it regressed between the 9th and 10th, then ran out of disk space 🤦

installing nightly-2021-02-10
testing...
RESULT: nightly-2021-02-10, ===> Yes

installing nightly-2021-02-08
installing nightly-2021-02-09
searched toolchains nightly-2020-10-01 through nightly-2021-02-10
installing nightly-2021-02-10
uninstalling nightly-2021-02-10
error: The regression was not found. Expanding the bounds may help.
fetching https://static.rust-lang.org/dist/2021-02-09/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2021-02-09: 40 B / 40 B [=============================================================================================================] 100.00 % 319.37 KB/s converted 2021-02-09 to 0fc6756b42e0556cc2e18079f5fc6b4d58f4e81a
fetching https://static.rust-lang.org/dist/2021-02-10/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2021-02-10: 40 B / 40 B [=============================================================================================================] 100.00 % 313.29 KB/s converted 2021-02-10 to 097bc6a84f2280a889b9ab4b544f27851a978927
looking for regression commit between 2021-02-09 and 2021-02-10
cloning rust repository
ERROR: failed to make directory './rust.git': No space left on device; class=Os (2)

@Stupremee
Copy link
Member

pub struct Handle {}
impl Handle {
    async fn flush(&mut self) {}
}

async fn die_horribly(thing: &[Handle]) {
    for v in thing {
        v.flush().await;
    }
}

Smaller example without async_std

@Stupremee Stupremee removed the E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example label Feb 12, 2021
@KerfuffleV2
Copy link
Author

It would be nice to reproduce this without the async-std dependency.

Here you go.

The async part doesn't seem important and the issue still occurs with the std versions of File and BufWriter and async-std removed from dependencies:

use std::{fs, io::*};
use std::collections::HashMap;

type Handle = BufWriter<fs::File>;
struct Thing(HashMap<String, Handle>);

impl Thing {
    pub fn die_horribly(&mut self) {
        for v in self.0.values() {
            v.flush();
        }
    }
}

fn main() {
}

It could probably be reduced in size further but I'm not much of a Rust expert. I suspect anything which triggers that particular compiler error would do the trick.

@jyn514
Copy link
Member

jyn514 commented Feb 12, 2021

searched nightlies: from nightly-2021-02-10 to nightly-2021-02-11
regressed nightly: nightly-2021-02-11
searched commits: from 097bc6a to 07194ff
regressed commit: f7534b5

bisected with cargo-bisect-rustc v0.6.0

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc --preserve --regress ice --start 2021-02-10 --end 2021-02-11 

@jyn514 jyn514 removed the E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc label Feb 12, 2021
@jyn514
Copy link
Member

jyn514 commented Feb 12, 2021

Probably #81466. cc @sasurau4

In particular https://github.com/rust-lang/rust/pull/81466/files#diff-d312b694654d74c5ee17332e226377520e7a0b67f29f823ffb68df6ca883844aR547 looks suspect.

@jyn514 jyn514 added A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints labels Feb 12, 2021
@b-naber
Copy link
Contributor

b-naber commented Feb 12, 2021

Are async functions registered as items in the hir?

edit: nvm just saw that someone posted a non-async example

bors added a commit to rust-lang-ci/rust that referenced this issue Feb 14, 2021
Rollup of 11 pull requests

Successful merges:

 - rust-lang#80523 (#[doc(inline)] sym_generated)
 - rust-lang#80920 (Visit more targets when validating attributes)
 - rust-lang#81720 (Updated smallvec version due to RUSTSEC-2021-0003)
 - rust-lang#81891 ([rustdoc-json] Make `header` a vec of modifiers, and FunctionPointer consistent)
 - rust-lang#81912 (Implement the precise analysis pass for lint `disjoint_capture_drop_reorder`)
 - rust-lang#81914 (Fixing bad suggestion for `_` in `const` type when a function rust-lang#81885)
 - rust-lang#81919 (BTreeMap: fix internal comments)
 - rust-lang#81927 (Add a regression test for rust-lang#32498)
 - rust-lang#81965 (Fix MIR pretty printer for non-local DefIds)
 - rust-lang#82029 (Use debug log level for developer oriented logs)
 - rust-lang#82056 (fix ice (rust-lang#82032))

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in bcb1403 Feb 15, 2021
@jyn514 jyn514 removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Feb 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ 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.

6 participants