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

"do not use optimized_mir for constants" panic when generating MIR cfg #81918

Closed
osa1 opened this issue Feb 9, 2021 · 5 comments · Fixed by #82362
Closed

"do not use optimized_mir for constants" panic when generating MIR cfg #81918

osa1 opened this issue Feb 9, 2021 · 5 comments · Fixed by #82362
Assignees
Labels
A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-mir-opt Area: MIR optimizations T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@osa1
Copy link
Contributor

osa1 commented Feb 9, 2021

The working example in #72956 builds fine, but if I add -Zunpretty=mir-cfg it panics. Repro:

use std::ops::Index;

struct Banana(*mut ());
unsafe impl Send for Banana {}

impl Banana {
    fn new() -> &'static mut Banana {
        static mut BANANA: Banana = Banana(std::ptr::null_mut());
        unsafe { &mut BANANA }
    }
}

struct Peach<'a>(&'a mut Banana);

impl<'a> std::ops::Index<usize> for Peach<'a> {
    type Output = ();
    fn index(&self, idx: usize) -> &() {
        &()
    }
}

async fn baz(v: &()) {}

async fn bar() -> () {
    let peach = Peach(Banana::new());
    let r: &() = &*std::ops::Index::index(&peach, 0);
    baz(r).await;
    peach.index(0);
}

fn assert_send<T: Send>(_: T) {}

pub fn main() {
    assert_send(bar())
}

It builds fine:

$ rustc +stage1 test_works.rs --edition=2018
warning: unused variable: `v`
  --> test_works.rs:22:14
   |
22 | async fn baz(v: &()) {}
   |              ^ help: if this is intentional, prefix it with an underscore: `_v`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `idx`
  --> test_works.rs:17:21
   |
17 |     fn index(&self, idx: usize) -> &() {
   |                     ^^^ help: if this is intentional, prefix it with an underscore: `_idx`

warning: 2 warnings emitted

Panics with -Zunpretty=mir-cfg:

$ rustc +stage1 test_works.rs --edition=2018 -Zunpretty=mir-cfg
warning: unused variable: `v`
  --> test_works.rs:22:14
   |
22 | async fn baz(v: &()) {}
   |              ^ help: if this is intentional, prefix it with an underscore: `_v`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `idx`
  --> test_works.rs:17:21
   |
17 |     fn index(&self, idx: usize) -> &() {
   |                     ^^^ help: if this is intentional, prefix it with an underscore: `_idx`

thread 'rustc' panicked at 'do not use `optimized_mir` for constants: Static(Mut)', compiler/rustc_mir/src/transform/mod.rs:579:24
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.52.0-dev running on x86_64-unknown-linux-gnu

note: compiler flags: -Z unpretty=mir-cfg

query stack during panic:
#0 [optimized_mir] optimizing MIR for `Banana::new::BANANA`
end of query stack
warning: 2 warnings emitted
Backtrace
   0: rust_begin_unwind
             at /home/omer/rust/rust/library/std/src/panicking.rs:493:5
   1: std::panicking::begin_panic_fmt
             at /home/omer/rust/rust/library/std/src/panicking.rs:435:5
   2: rustc_mir::transform::inner_optimized_mir
             at /home/omer/rust/rust/compiler/rustc_mir/src/transform/mod.rs:579:24
   3: rustc_mir::transform::optimized_mir
             at /home/omer/rust/rust/compiler/rustc_mir/src/transform/mod.rs:561:21
   4: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::optimized_mir>::compute
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/query/plumbing.rs:377:17
   5: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
             at /home/omer/rust/rust/compiler/rustc_query_system/src/dep_graph/graph.rs:363:14
   6: rustc_query_system::query::plumbing::force_query_with_job::{{closure}}::{{closure}}
   7: rustc_middle::ty::query::plumbing::<impl rustc_query_system::query::QueryContext for rustc_middle::ty::context::TyCtxt>::start_query::{{closure}}::{{closure}}::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/query/plumbing.rs:73:74
   8: stacker::maybe_grow
             at /home/omer/.cargo/registry/src/github.com-1ecc6299db9ec823/stacker-0.1.12/src/lib.rs:55:9
   9: rustc_data_structures::stack::ensure_sufficient_stack
             at /home/omer/rust/rust/compiler/rustc_data_structures/src/stack.rs:16:5
  10: rustc_middle::ty::query::plumbing::<impl rustc_query_system::query::QueryContext for rustc_middle::ty::context::TyCtxt>::start_query::{{closure}}::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/query/plumbing.rs:73:17
  11: rustc_middle::ty::context::tls::enter_context::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/context.rs:1712:50
  12: rustc_middle::ty::context::tls::set_tlv
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/context.rs:1696:9
  13: rustc_middle::ty::context::tls::enter_context
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/context.rs:1712:9
  14: rustc_middle::ty::query::plumbing::<impl rustc_query_system::query::QueryContext for rustc_middle::ty::context::TyCtxt>::start_query::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/query/plumbing.rs:72:13
  15: rustc_middle::ty::context::tls::with_related_context::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/context.rs:1756:13
  16: rustc_middle::ty::context::tls::with_context::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/context.rs:1740:40
  17: rustc_middle::ty::context::tls::with_context_opt
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/context.rs:1729:22
  18: rustc_middle::ty::context::tls::with_context
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/context.rs:1740:9
  19: rustc_middle::ty::context::tls::with_related_context
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/context.rs:1753:9
  20: rustc_middle::ty::query::plumbing::<impl rustc_query_system::query::QueryContext for rustc_middle::ty::context::TyCtxt>::start_query
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/query/plumbing.rs:61:9
  21: rustc_query_system::query::plumbing::force_query_with_job::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_query_system/src/query/plumbing.rs:598:9
  22: rustc_query_system::query::plumbing::with_diagnostics
             at /home/omer/rust/rust/compiler/rustc_query_system/src/query/plumbing.rs:303:18
  23: rustc_query_system::query::plumbing::force_query_with_job
             at /home/omer/rust/rust/compiler/rustc_query_system/src/query/plumbing.rs:597:51
  24: rustc_query_system::query::plumbing::try_execute_query
             at /home/omer/rust/rust/compiler/rustc_query_system/src/query/plumbing.rs:427:16
  25: rustc_query_system::query::plumbing::get_query_impl::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_query_system/src/query/plumbing.rs:645:23
  26: <rustc_query_system::query::caches::DefaultCache<K,V> as rustc_query_system::query::caches::QueryCache>::lookup
             at /home/omer/rust/rust/compiler/rustc_query_system/src/query/caches.rs:114:79
  27: rustc_query_system::query::plumbing::try_get_cached
             at /home/omer/rust/rust/compiler/rustc_query_system/src/query/plumbing.rs:380:5
  28: rustc_query_system::query::plumbing::get_query_impl
             at /home/omer/rust/rust/compiler/rustc_query_system/src/query/plumbing.rs:637:5
  29: rustc_query_system::query::plumbing::get_query
             at /home/omer/rust/rust/compiler/rustc_query_system/src/query/plumbing.rs:739:5
  30: rustc_middle::ty::query::TyCtxtAt::optimized_mir
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/query/plumbing.rs:487:17
  31: rustc_middle::ty::query::<impl rustc_middle::ty::context::TyCtxt>::optimized_mir
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/query/plumbing.rs:448:17
  32: rustc_mir::util::graphviz::write_mir_graphviz
             at /home/omer/rust/rust/compiler/rustc_mir/src/util/graphviz.rs:25:21
  33: rustc_driver::pretty::print_with_analysis
             at /home/omer/rust/rust/compiler/rustc_driver/src/pretty.rs:499:26
  34: rustc_driver::pretty::print_after_hir_lowering
             at /home/omer/rust/rust/compiler/rustc_driver/src/pretty.rs:431:22
  35: rustc_driver::run_compiler::{{closure}}::{{closure}}::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_driver/src/lib.rs:349:25
  36: rustc_interface::passes::QueryContext::enter::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_interface/src/passes.rs:749:42
  37: rustc_middle::ty::context::tls::enter_context::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/context.rs:1712:50
  38: rustc_middle::ty::context::tls::set_tlv
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/context.rs:1696:9
  39: rustc_middle::ty::context::tls::enter_context
             at /home/omer/rust/rust/compiler/rustc_middle/src/ty/context.rs:1712:9
  40: rustc_interface::passes::QueryContext::enter
             at /home/omer/rust/rust/compiler/rustc_interface/src/passes.rs:749:9
  41: rustc_driver::run_compiler::{{closure}}::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_driver/src/lib.rs:347:21
  42: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
             at /home/omer/rust/rust/compiler/rustc_interface/src/queries.rs:418:19
  43: rustc_driver::run_compiler::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_driver/src/lib.rs:341:22
  44: rustc_interface::interface::create_compiler_and_run::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_interface/src/interface.rs:197:13
  45: rustc_span::with_source_map
             at /home/omer/rust/rust/compiler/rustc_span/src/lib.rs:789:5
  46: rustc_interface::interface::create_compiler_and_run
             at /home/omer/rust/rust/compiler/rustc_interface/src/interface.rs:191:5
  47: rustc_interface::interface::run_compiler::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_interface/src/interface.rs:213:12
  48: rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals::{{closure}}::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_interface/src/util.rs:152:13
  49: scoped_tls::ScopedKey<T>::set
             at /home/omer/.cargo/registry/src/github.com-1ecc6299db9ec823/scoped-tls-1.0.0/src/lib.rs:137:9
  50: rustc_span::with_session_globals
             at /home/omer/rust/rust/compiler/rustc_span/src/lib.rs:105:5
  51: rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_interface/src/util.rs:150:9
  52: rustc_interface::util::scoped_thread::{{closure}}
             at /home/omer/rust/rust/compiler/rustc_interface/src/util.rs:125:24
@osa1
Copy link
Contributor Author

osa1 commented Feb 9, 2021

@oli-obk I think the panic was added by you. Could you say a few words on what the issue here? I'd be happy to investigate further and fix, but I have no idea where to start.

@oli-obk
Copy link
Contributor

oli-obk commented Feb 9, 2021

That's an oversight by me. In

let body = &tcx.optimized_mir(def_id);
you instead need to do something similar to mir printing. That is, use instance_mir instead of optimized_mir, but also create a second dump for const fn by additionally using mir_for_ctfe to show the graph of the ctfe version of the const fn

@oli-obk
Copy link
Contributor

oli-obk commented Feb 9, 2021

#81200 is somewhat related, as we should probably first fix that one and then deduplicate the mir selection logic between cfg printing and regular mir dumps.

@estebank estebank added A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-mir-opt Area: MIR optimizations T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 11, 2021
@osa1
Copy link
Contributor Author

osa1 commented Feb 20, 2021

#82326 has a smaller repro.

@osa1
Copy link
Contributor Author

osa1 commented Feb 21, 2021

@rustbot claim

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Feb 22, 2021
Fix mir-cfg dumps

Fixes rust-lang#81918
Fixes rust-lang#82326 (duplicate)
Fixes rust-lang#82325

---

r? `@oli-obk`
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 23, 2021
Fix mir-cfg dumps

Fixes rust-lang#81918
Fixes rust-lang#82326 (duplicate)
Fixes rust-lang#82325

---

r? ``@oli-obk``
@bors bors closed this as completed in 2145a87 Feb 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-mir-opt Area: MIR optimizations 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