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

Async/.await and const-generics causes "Broken MIR:" #74906

Closed
rot256 opened this issue Jul 29, 2020 · 6 comments · Fixed by #77331
Closed

Async/.await and const-generics causes "Broken MIR:" #74906

rot256 opened this issue Jul 29, 2020 · 6 comments · Fixed by #77331
Assignees
Labels
A-async-await A-const-generics A-mir AsyncAwait-Triaged C-bug E-easy E-needs-test glacier I-ICE requires-nightly T-compiler

Comments

@rot256
Copy link

rot256 commented Jul 29, 2020

Code

The use of async/await and const generics can cause ICE (in the type checker?). Here is a minimal example which induces ICE:

#![feature(const_generics)]
#![allow(incomplete_features)]

const SIZE: usize = 16;

struct Bar<const H: usize> {}

struct Foo<const H: usize> {}

impl<const H: usize> Foo<H> {
    async fn biz(_: &[[u8; SIZE]]) -> Vec<()> {
        vec![]
    }

    pub async fn baz(&self) -> Bar<H> {
        Self::biz(&vec![]).await;
        Bar {}
    }
}

The behaviour is not triggered without the use of async/await.

The full library source.

Meta

I am running recent nightly compiler:

rustc --version --verbose:

rustc 1.47.0-nightly (6c8927b0c 2020-07-26)
binary: rustc
commit-hash: 6c8927b0cf80ceee19386026cf9d7fd4fd9d486f
commit-date: 2020-07-26
host: x86_64-unknown-linux-gnu
release: 1.47.0-nightly
LLVM version: 10.0

Error output

RUST_BACKTRACE=full cargo build
   Compiling bug v0.1.0 (/home/rot256/src/bug)
warning: constant is never used: `SIZE`
 --> src/lib.rs:4:1
   |
4 | const SIZE: usize = 16;
   | ^^^^^^^^^^^^^^^^^^^^^^^
   |
  = note: `#[warn(dead_code)]` on by default

warning: struct is never constructed: `Bar`
 --> src/lib.rs:6:8
   |
6 | struct Bar<const H: usize> {}
   |        ^^^

warning: struct is never constructed: `Foo`
 --> src/lib.rs:8:8
  |
8 | struct Foo<const H: usize> {}
   |        ^^^

warning: associated function is never used: `biz`
  --> src/lib.rs:11:14
     |
11 |     async fn biz(_: &[[u8; SIZE]]) -> Vec<()> {
     |              ^^^

warning: associated function is never used: `baz`
  --> src/lib.rs:15:18
     |
15 |     pub async fn baz(&self) -> Bar<H> {
     |                  ^^^

error: internal compiler error: src/librustc_mir/transform/generator.rs:739:13: Broken MIR: generator contains type std::vec::Vec<[u8; _]> in MIR, but typeck only knows about for<'r, 's, 't0, 't1> {std::future::ResumeTy, &'r Foo<H>, std::vec::Vec<[u8; _]>, &'s [[u8; _]], &'t0 std::vec::Vec<[u8; _]>, impl std::future::Future, ()}
  --> src/lib.rs:15:39
     |
15 |       pub async fn baz(&self) -> Bar<H> {
     |  _______________________________________^
16 | |         Self::biz(&vec![]).await;
17 | |         Bar {}
18 | |     }
     | |_____^

thread 'rustc' panicked at 'Box<Any>', /rustc/6c8927b0cf80ceee19386026cf9d7fd4fd9d486f/src/libstd/macros.rs:13:23
stack backtrace:
   0:     0x7f35c2d05215 - backtrace::backtrace::libunwind::trace::h75aedf5f78e5147f
                               at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/libunwind.rs:86
   1:     0x7f35c2d05215 - backtrace::backtrace::trace_unsynchronized::h18fb73c9ac9ae753
                               at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/mod.rs:66
   2:     0x7f35c2d05215 - std::sys_common::backtrace::_print_fmt::h65f97470ff13ec84
                               at src/libstd/sys_common/backtrace.rs:78
   3:     0x7f35c2d05215 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hee061c54ddc9f024
                               at src/libstd/sys_common/backtrace.rs:59
   4:     0x7f35c2d4141c - core::fmt::write::hfbd2baad61ed21a8
                               at src/libcore/fmt/mod.rs:1117
   5:     0x7f35c2cf6f52 - std::io::Write::write_fmt::h72f9bd227f40dc62
                               at src/libstd/io/mod.rs:1508
   6:     0x7f35c2d0a0b0 - std::sys_common::backtrace::_print::h2d2cd8fe02feb5fa
                               at src/libstd/sys_common/backtrace.rs:62
   7:     0x7f35c2d0a0b0 - std::sys_common::backtrace::print::h801b12991252ba7c
                               at src/libstd/sys_common/backtrace.rs:49
   8:     0x7f35c2d0a0b0 - std::panicking::default_hook::{{closure}}::h25fc1fbf3b63b5c8
                               at src/libstd/panicking.rs:198
   9:     0x7f35c2d09dfc - std::panicking::default_hook::h62c897957a5e0f26
                               at src/libstd/panicking.rs:217
  10:     0x7f35c348e719 - rustc_driver::report_ice::h54e3b417084b44b0
  11:     0x7f35c2d0a828 - std::panicking::rust_panic_with_hook::hb8a276f163c59810
                               at src/libstd/panicking.rs:530
  12:     0x7f35c4648a43 - std::panicking::begin_panic::h56ab19f2cedb9db6
  13:     0x7f35c46cf5ac - rustc_errors::HandlerInner::span_bug::h6725b1754e0b8350
  14:     0x7f35c46cf9f0 - rustc_errors::Handler::span_bug::h690acfedaaa39918
  15:     0x7f35c46d3ec4 - rustc_middle::util::bug::opt_span_bug_fmt::{{closure}}::h9ad20ff21dfdee15
  16:     0x7f35c46d2c2b - rustc_middle::ty::context::tls::with_opt::{{closure}}::h55dd76028893bf0b
  17:     0x7f35c46d2bd2 - rustc_middle::ty::context::tls::with_opt::ha4f5888994b68db9
  18:     0x7f35c46d3dc9 - rustc_middle::util::bug::opt_span_bug_fmt::hff1523241ff1d646
  19:     0x7f35c46d3d83 - rustc_middle::util::bug::span_bug_fmt::h6642bef07315747d
  20:     0x7f35c4677dbf - <rustc_mir::transform::generator::StateTransform as rustc_mir::transform::MirPass>::run_pass::ha0b6b0061f9616e8
  21:     0x7f35c4a076d5 - rustc_mir::transform::run_passes::h49d12d42b645883c
  22:     0x7f35c4a097ee - rustc_mir::transform::run_optimization_passes::h4808514d2a3dc8d0
  23:     0x7f35c4a09bc0 - rustc_mir::transform::inner_optimized_mir::h7bbfaea3bf718bc9
  24:     0x7f35c4a0997d - rustc_mir::transform::optimized_mir::h2c34b792ca19fc38
  25:     0x7f35c5e8e061 - rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps::h4c010a4768f0f01c
  26:     0x7f35c58f5b4f - rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl::h1d87895a9b5e641e
  27:     0x7f35c5c846b2 - rustc_data_structures::stack::ensure_sufficient_stack::hf48d61a9d17fd483
  28:     0x7f35c5acc793 - rustc_query_system::query::plumbing::get_query_impl::hef59961961c2f14e
  29:     0x7f35c59bb801 - rustc_middle::ty::layout::LayoutCx<rustc_middle::ty::context::TyCtxt>::layout_raw_uncached::h232eb1b291401511
  30:     0x7f35c59b6cff - rustc_middle::ty::layout::layout_raw::hf9eb140d1022d561
  31:     0x7f35c59e5c21 - rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::layout_raw>::compute::h9a2c6c02944bd2dd
  32:     0x7f35c5e92075 - rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps::he44c72eff6c9f466
  33:     0x7f35c590c0a0 - rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl::h7a291f01f92f5108
  34:     0x7f35c5c7762f - rustc_data_structures::stack::ensure_sufficient_stack::h353561557dc75241
  35:     0x7f35c5abd6e7 - rustc_query_system::query::plumbing::get_query_impl::hdc6080a3a10cbfb0
  36:     0x7f35c59c57f4 - <rustc_middle::ty::layout::LayoutCx<rustc_middle::ty::context::TyCtxt> as rustc_target::abi::LayoutOf>::layout_of::hdd5ae6209bb8c384
  37:     0x7f35c5f24fbe - <core::iter::adapters::ResultShunt<I,E> as core::iter::traits::iterator::Iterator>::next::h3dc9bd222e471f34
  38:     0x7f35c58b312c - <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T,I>>::from_iter::h02b7a95b20eda5b5
  39:     0x7f35c5f24dbd - <core::iter::adapters::ResultShunt<I,E> as core::iter::traits::iterator::Iterator>::next::h19c51e30891d4acc
  40:     0x7f35c58d73b0 - <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T,I>>::from_iter::hecfa2cc5545dc974
  41:     0x7f35c59bc2e2 - rustc_middle::ty::layout::LayoutCx<rustc_middle::ty::context::TyCtxt>::layout_raw_uncached::h232eb1b291401511
  42:     0x7f35c59b6cff - rustc_middle::ty::layout::layout_raw::hf9eb140d1022d561
  43:     0x7f35c59e5c21 - rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::layout_raw>::compute::h9a2c6c02944bd2dd
  44:     0x7f35c5e92075 - rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps::he44c72eff6c9f466
  45:     0x7f35c590c0a0 - rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl::h7a291f01f92f5108
  46:     0x7f35c5c7762f - rustc_data_structures::stack::ensure_sufficient_stack::h353561557dc75241
  47:     0x7f35c5abd6e7 - rustc_query_system::query::plumbing::get_query_impl::hdc6080a3a10cbfb0
  48:     0x7f35c59c6351 - <rustc_middle::ty::layout::LayoutCx<rustc_middle::ty::query::TyCtxtAt> as rustc_target::abi::LayoutOf>::layout_of::h249f6e77b4e78581
  49:     0x7f35c470fa5e - <rustc_mir::transform::const_prop::ConstProp as rustc_mir::transform::MirPass>::run_pass::heeb6bfd954ebfdb5
  50:     0x7f35c4a076d5 - rustc_mir::transform::run_passes::h49d12d42b645883c
  51:     0x7f35c4a097ee - rustc_mir::transform::run_optimization_passes::h4808514d2a3dc8d0
  52:     0x7f35c4a09bc0 - rustc_mir::transform::inner_optimized_mir::h7bbfaea3bf718bc9
  53:     0x7f35c4a0997d - rustc_mir::transform::optimized_mir::h2c34b792ca19fc38
  54:     0x7f35c4e721e2 - 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::hd8f3e45025769508
  55:     0x7f35c4f44a2a - rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps::hbbb1691ceb2a7378
  56:     0x7f35c5033abc - rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl::h2a116afb948656f3
  57:     0x7f35c4fb6de3 - rustc_data_structures::stack::ensure_sufficient_stack::h50171c7f6732d5e7
  58:     0x7f35c4fdab9a - rustc_query_system::query::plumbing::get_query_impl::h23e08d9912664785
  59:     0x7f35c4e900bd - rustc_metadata::rmeta::encoder::EncodeContext::encode_optimized_mir::h21e11081f3184770
  60:     0x7f35c4e9437b - <rustc_metadata::rmeta::encoder::EncodeContext as rustc_hir::intravisit::Visitor>::visit_item::hc5fad122cac52247
  61:     0x7f35c508fc67 - rustc_hir::hir::Crate::visit_all_item_likes::h57ce95c9da9b50ad
  62:     0x7f35c4e98da5 - rustc_metadata::rmeta::encoder::encode_metadata_impl::h2e797180c72c7de0
  63:     0x7f35c4fb437c - rustc_data_structures::sync::join::h26f229ec55e7a0f5
  64:     0x7f35c4eb2630 - rustc_metadata::rmeta::decoder::cstore_impl::<impl rustc_middle::middle::cstore::CrateStore for rustc_metadata::creader::CStore>::encode_metadata::hab1eceef04d8a455
  65:     0x7f35c5cdde50 - rustc_middle::ty::context::TyCtxt::encode_metadata::h6eaf9864b984a444
  66:     0x7f35c362e8f4 - rustc_interface::passes::start_codegen::h4cb47030bcada22e
  67:     0x7f35c36488e6 - rustc_middle::ty::context::tls::enter_global::hc8731f69dcd0bbcb
  68:     0x7f35c364445e - rustc_interface::queries::Queries::ongoing_codegen::ha8e667ca2563fa09
  69:     0x7f35c34498d3 - rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter::h3b5436b320054acc
  70:     0x7f35c34e8277 - rustc_span::with_source_map::hd3215c6d07150bbc
  71:     0x7f35c344afb6 - rustc_interface::interface::create_compiler_and_run::hc502b394ead637d1
  72:     0x7f35c347432d - scoped_tls::ScopedKey<T>::set::hb09c39dbfa5a4396
  73:     0x7f35c349a4a2 - std::sys_common::backtrace::__rust_begin_short_backtrace::h4fa4ac8c571754e6
  74:     0x7f35c345726e - core::ops::function::FnOnce::call_once{{vtable.shim}}::h23cc72206c0cf83b
  75:     0x7f35c2d18d3a - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h3e41d894970cea2f
                               at /rustc/6c8927b0cf80ceee19386026cf9d7fd4fd9d486f/src/liballoc/boxed.rs:1074
  76:     0x7f35c2d18d3a - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h9a318bb00b7f8871
                               at /rustc/6c8927b0cf80ceee19386026cf9d7fd4fd9d486f/src/liballoc/boxed.rs:1074
  77:     0x7f35c2d18d3a - std::sys::unix::thread::Thread::new::thread_start::hf522342530b04cb6
                               at src/libstd/sys/unix/thread.rs:87
  78:     0x7f35c2c48f27 - start_thread
                               at ./nptl/pthread_create.c:479
  79:     0x7f35c2b6831f - __clone
                               at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
  80:                0x0 - <unknown>

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.47.0-nightly (6c8927b0c 2020-07-26) running on x86_64-unknown-linux-gnu

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

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

query stack during panic:
#0 [optimized_mir] optimizing MIR for `Foo::<H>::baz::{{closure}}#0`
#1 [layout_raw] computing layout of `[static generator@src/lib.rs:15:39: 18:6 self:&Foo<H> for<'r, 's, 't0, 't1> {std::future::ResumeTy, &'r Foo<H>, std::vec::Vec<[u8; 16]>, &'s [[u8; 16]], &'t0 std::vec::Vec<[u8; 16]>, impl std::future::Future, ()}]`
#2 [layout_raw] computing layout of `std::future::from_generator::GenFuture<[static generator@src/lib.rs:15:39: 18:6 self:&Foo<H> for<'r, 's, 't0, 't1> {std::future::ResumeTy, &'r Foo<H>, std::vec::Vec<[u8; 16]>, &'s [[u8; 16]], &'t0 std::vec::Vec<[u8; 16]>, impl std::future::Future, ()}]>`
#3 [optimized_mir] optimizing MIR for `Foo::<H>::baz`
end of query stack
error: aborting due to previous error; 5 warnings emitted

error: could not compile `bug`.

To learn more, run the command again with --verbose.
@rot256 rot256 added C-bug I-ICE T-compiler labels Jul 29, 2020
@jonas-schievink jonas-schievink added A-async-await A-const-generics A-mir labels Jul 29, 2020
@jonas-schievink
Copy link
Member

jonas-schievink commented Jul 29, 2020

Guessing: It might be comparing an Unevaluated const to another Unevaluated const and expects them to compare equal.

Slightly minified:

#![feature(const_generics)]
#![allow(incomplete_features)]

async fn baz<const H: usize>() {
    biz(&Vec::new()).await;
}

const SIZE: usize = 16;

async fn biz(_: &[[u8; SIZE]]) {}

@wesleywiser wesleywiser added the requires-nightly label Jul 29, 2020
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier label Aug 3, 2020
@tmandry
Copy link
Contributor

tmandry commented Aug 4, 2020

Related to #72651. Not going to mark as a duplicate yet, since the MVCE is quite different.

@tmandry tmandry added this to On deck in wg-async work via automation Aug 4, 2020
@tmandry tmandry added the AsyncAwait-Triaged label Aug 4, 2020
@varkor
Copy link
Member

varkor commented Sep 13, 2020

Note that this ICEs with const_generics but not min_const_generics, where it compiles successfully. We should add a test.

@varkor varkor added const-generics-fixed-by-const_generics const-generics-needs-test-for-min_const_generics and removed const-generics-fixed-by-const_generics labels Sep 13, 2020
@JohnTitor
Copy link
Member

JohnTitor commented Sep 22, 2020

Triage: It's now compiled successfully with const_genetics and the latest nightly.

@tmandry tmandry added E-easy E-needs-test labels Sep 22, 2020
@tmandry
Copy link
Contributor

tmandry commented Sep 22, 2020

Great! Leaving open to track adding a regression test.

@hameerabbasi
Copy link
Contributor

hameerabbasi commented Sep 29, 2020

I'd like to add the test.
@rustbot claim

jonas-schievink added a commit to jonas-schievink/rust that referenced this issue Sep 29, 2020
Add test for async/await combined with const-generics.

Fixes rust-lang#74906.
jonas-schievink added a commit to jonas-schievink/rust that referenced this issue Sep 30, 2020
Add test for async/await combined with const-generics.

Fixes rust-lang#74906.
@bors bors closed this as completed in 4e51783 Sep 30, 2020
wg-async work automation moved this from On deck to Done Sep 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await A-const-generics A-mir AsyncAwait-Triaged C-bug E-easy E-needs-test glacier I-ICE requires-nightly T-compiler
Projects
Development

Successfully merging a pull request may close this issue.

8 participants