Repository: rust
Issue: ICE: no MIR available for DefId when compiling `--crate-type lib` with rmeta dependency
Labels: I-ICE,T-compiler,C-bug

```rust
$ cat foo.rs
pub fn foo() {}
$ cat bar.rs
pub fn bar() {
    foo::foo();
}
$ rustc --crate-type lib --emit metadata foo.rs
$ rustc --extern foo=libfoo.rmeta --crate-type lib bar.rs
error: internal compiler error: compiler/rustc_monomorphize/src/collector.rs:1012:9: no MIR available for DefId(20:3 ~ foo[bcc9]::foo)
```


### Meta
Reproduces on both stable and nightly as of 4/6/23:

`rustc +nightly --version --verbose`:
```
rustc 1.70.0-nightly (cf7ada217 2023-04-03)
binary: rustc
commit-hash: cf7ada217c8ac63367b184afd9fffaff30f6ed44
commit-date: 2023-04-03
host: x86_64-unknown-linux-gnu
release: 1.70.0-nightly
LLVM version: 16.0.0
```

`rustc +stable --version --verbose`:
```
$ rustc +stable --version --verbose
rustc 1.68.2 (9eb3afe9e 2023-03-27)
binary: rustc
commit-hash: 9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0
commit-date: 2023-03-27
host: x86_64-unknown-linux-gnu
release: 1.68.2
LLVM version: 15.0.6
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary><strong>Backtrace</strong></summary>
<p>

```
error: internal compiler error: compiler/rustc_monomorphize/src/collector.rs:1012:9: no MIR available for DefId(20:3 ~ foo[bcc9]::foo)

thread 'rustc' panicked at 'Box<dyn Any>', /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/compiler/rustc_errors/src/lib.rs:1609:9
stack backtrace:
   0:     0x7f7e04f6659a - std::backtrace_rs::backtrace::libunwind::trace::ha271a8a7e1f3d4ef
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7f7e04f6659a - std::backtrace_rs::backtrace::trace_unsynchronized::h85739da0352c791a
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f7e04f6659a - std::sys_common::backtrace::_print_fmt::hbc6ebcfb2910b329
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys_common/backtrace.rs:65:5
   3:     0x7f7e04f6659a - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::he1c117e52d53614f
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f7e04fc839e - core::fmt::write::h25eb51b9526b8e0c
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/core/src/fmt/mod.rs:1213:17
   5:     0x7f7e04f56be5 - std::io::Write::write_fmt::ha9edec5fb1621933
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/io/mod.rs:1682:15
   6:     0x7f7e04f66365 - std::sys_common::backtrace::_print::hf8657cd429fc3452
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys_common/backtrace.rs:47:5
   7:     0x7f7e04f66365 - std::sys_common::backtrace::print::h41b9b18ed86f86bd
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys_common/backtrace.rs:34:9
   8:     0x7f7e04f6912f - std::panicking::default_hook::{{closure}}::h22a91871f4454152
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/panicking.rs:267:22
   9:     0x7f7e04f68e6b - std::panicking::default_hook::h21ddc36de0cd4ae7
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/panicking.rs:286:9
  10:     0x7f7e082b9324 - rustc_driver[70f63b52fde826b7]::DEFAULT_HOOK::{closure#0}::{closure#0}
  11:     0x7f7e04f6996a - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h6f7e3c94ecc52e2f
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/alloc/src/boxed.rs:2002:9
  12:     0x7f7e04f6996a - std::panicking::rust_panic_with_hook::h5059419d6d59b3d0
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/panicking.rs:692:13
  13:     0x7f7e087df711 - std[f8b79e573431a86c]::panicking::begin_panic::<rustc_errors[53e74ce803854a4d]::ExplicitBug>::{closure#0}
  14:     0x7f7e087de136 - std[f8b79e573431a86c]::sys_common::backtrace::__rust_end_short_backtrace::<std[f8b79e573431a86c]::panicking::begin_panic<rustc_errors[53e74ce803854a4d]::ExplicitBug>::{closure#0}, !>
  15:     0x7f7e0884aad6 - std[f8b79e573431a86c]::panicking::begin_panic::<rustc_errors[53e74ce803854a4d]::ExplicitBug>
  16:     0x7f7e0880bcb6 - std[f8b79e573431a86c]::panic::panic_any::<rustc_errors[53e74ce803854a4d]::ExplicitBug>
  17:     0x7f7e08806586 - <rustc_errors[53e74ce803854a4d]::HandlerInner>::bug::<&alloc[b346c7f99e9347e5]::string::String>
  18:     0x7f7e08804e90 - <rustc_errors[53e74ce803854a4d]::Handler>::bug::<&alloc[b346c7f99e9347e5]::string::String>
  19:     0x7f7e0881a4cb - rustc_middle[6dfce017f6b7786d]::util::bug::opt_span_bug_fmt::<rustc_span[7c23fb27ec020b97]::span_encoding::Span>::{closure#0}
  20:     0x7f7e0881a2fa - rustc_middle[6dfce017f6b7786d]::ty::context::tls::with_opt::<rustc_middle[6dfce017f6b7786d]::util::bug::opt_span_bug_fmt<rustc_span[7c23fb27ec020b97]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  21:     0x7f7e0881a2ca - rustc_middle[6dfce017f6b7786d]::ty::context::tls::with_context_opt::<rustc_middle[6dfce017f6b7786d]::ty::context::tls::with_opt<rustc_middle[6dfce017f6b7786d]::util::bug::opt_span_bug_fmt<rustc_span[7c23fb27ec020b97]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  22:     0x7f7e0881a416 - rustc_middle[6dfce017f6b7786d]::util::bug::opt_span_bug_fmt::<rustc_span[7c23fb27ec020b97]::span_encoding::Span>
  23:     0x7f7e0650cbe3 - rustc_middle[6dfce017f6b7786d]::util::bug::bug_fmt
  24:     0x7f7e070dc73c - rustc_monomorphize[aa5fb922692f5618]::collector::collect_neighbours
  25:     0x7f7e070ccaf3 - rustc_monomorphize[aa5fb922692f5618]::collector::collect_items_rec
  26:     0x7f7e076cfdbb - <rustc_session[cd7c78827427c6d6]::session::Session>::time::<(), rustc_monomorphize[aa5fb922692f5618]::collector::collect_crate_mono_items::{closure#1}>
  27:     0x7f7e076cf904 - rustc_monomorphize[aa5fb922692f5618]::collector::collect_crate_mono_items
  28:     0x7f7e076cdd03 - rustc_monomorphize[aa5fb922692f5618]::partitioning::collect_and_partition_mono_items
  29:     0x7f7e07a2a68c - rustc_query_system[59807289ca97d521]::query::plumbing::try_execute_query::<rustc_query_impl[81153385010d7886]::queries::collect_and_partition_mono_items, rustc_query_impl[81153385010d7886]::plumbing::QueryCtxt>
  30:     0x7f7e07c79a62 - <rustc_query_impl[81153385010d7886]::Queries as rustc_middle[6dfce017f6b7786d]::ty::query::QueryEngine>::collect_and_partition_mono_items
  31:     0x7f7e07a48c49 - rustc_codegen_ssa[356018be9b9ac562]::back::symbol_export::exported_symbols_provider_local
  32:     0x7f7e06b21e1f - rustc_query_system[59807289ca97d521]::query::plumbing::try_execute_query::<rustc_query_impl[81153385010d7886]::queries::exported_symbols, rustc_query_impl[81153385010d7886]::plumbing::QueryCtxt>
  33:     0x7f7e07698722 - rustc_query_system[59807289ca97d521]::query::plumbing::get_query::<rustc_query_impl[81153385010d7886]::queries::exported_symbols, rustc_query_impl[81153385010d7886]::plumbing::QueryCtxt, rustc_middle[6dfce017f6b7786d]::dep_graph::dep_node::DepKind>
  34:     0x7f7e06ccf698 - <rustc_metadata[8ec84d9deeba008d]::rmeta::encoder::EncodeContext>::encode_crate_root
  35:     0x7f7e0750c3e8 - rustc_metadata[8ec84d9deeba008d]::rmeta::encoder::encode_metadata_impl
  36:     0x7f7e074f904e - rustc_data_structures[560608da72c35763]::sync::join::<rustc_metadata[8ec84d9deeba008d]::rmeta::encoder::encode_metadata::{closure#0}, rustc_metadata[8ec84d9deeba008d]::rmeta::encoder::encode_metadata::{closure#1}, (), ()>
  37:     0x7f7e074f8d61 - rustc_metadata[8ec84d9deeba008d]::rmeta::encoder::encode_metadata
  38:     0x7f7e074f81e3 - rustc_metadata[8ec84d9deeba008d]::fs::encode_and_write_metadata
  39:     0x7f7e0749e7ce - rustc_interface[698bad460dbd68d3]::passes::start_codegen
  40:     0x7f7e0749ddba - <rustc_interface[698bad460dbd68d3]::queries::Queries>::ongoing_codegen
  41:     0x7f7e07497ab9 - rustc_span[7c23fb27ec020b97]::with_source_map::<core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>, rustc_interface[698bad460dbd68d3]::interface::run_compiler<core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>, rustc_driver[70f63b52fde826b7]::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
  42:     0x7f7e074902d4 - <scoped_tls[1044df3f3db9be03]::ScopedKey<rustc_span[7c23fb27ec020b97]::SessionGlobals>>::set::<rustc_interface[698bad460dbd68d3]::interface::run_compiler<core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>, rustc_driver[70f63b52fde826b7]::run_compiler::{closure#1}>::{closure#0}, core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>>
  43:     0x7f7e0748f9d2 - std[f8b79e573431a86c]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[698bad460dbd68d3]::util::run_in_thread_pool_with_globals<rustc_interface[698bad460dbd68d3]::interface::run_compiler<core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>, rustc_driver[70f63b52fde826b7]::run_compiler::{closure#1}>::{closure#0}, core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>>
  44:     0x7f7e0748f77a - <<std[f8b79e573431a86c]::thread::Builder>::spawn_unchecked_<rustc_interface[698bad460dbd68d3]::util::run_in_thread_pool_with_globals<rustc_interface[698bad460dbd68d3]::interface::run_compiler<core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>, rustc_driver[70f63b52fde826b7]::run_compiler::{closure#1}>::{closure#0}, core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>>::{closure#1} as core[1d432356d8e1e9f1]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  45:     0x7f7e04f73823 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h3205ec2d7fc232c5
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/alloc/src/boxed.rs:1988:9
  46:     0x7f7e04f73823 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h3bb5daec8177f56b
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/alloc/src/boxed.rs:1988:9
  47:     0x7f7e04f73823 - std::sys::unix::thread::Thread::new::thread_start::had7b8061e306bb5c
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys/unix/thread.rs:108:17
  48:     0x7f7e04d03bb5 - <unknown>
  49:     0x7f7e04d85d90 - <unknown>
  50:                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.68.2 (9eb3afe9e 2023-03-27) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib

query stack during panic:
#0 [collect_and_partition_mono_items] collect_and_partition_mono_items
#1 [exported_symbols] collecting exported symbols for crate `0`
end of query stack
error: aborting due to previous error
```
</p>
</details>