rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
99.18k stars 12.8k forks source link

The codegen backend gets invoked even when we just generate MIR #128924

Open RalfJung opened 3 months ago

RalfJung commented 3 months ago

I tried this code

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

fn test() {

}

fn main() {
    become test()
}

with rustc +nightly --emit=mir become.rs I expect this to just produce MIR. Instead, it ICEs:

error: internal compiler error: /rustc/176e5452095444815207be02c16de0b1487a1b53/compiler/rustc_codegen_ssa/src/mir/block.rs:1392:17: `TailCall` terminator is not yet supported by `rustc_codegen_ssa`
 --> become.rs:9:5
  |
9 |     become test()
  |     ^^^^^^^^^^^^^

thread 'rustc' panicked at /rustc/176e5452095444815207be02c16de0b1487a1b53/compiler/rustc_codegen_ssa/src/mir/block.rs:1392:17:
Box<dyn Any>
stack backtrace:
   0:     0x7f8cc53d0f4d - std::backtrace_rs::backtrace::libunwind::trace::hddaf846e242943b6
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:     0x7f8cc53d0f4d - std::backtrace_rs::backtrace::trace_unsynchronized::hcd3688bf14d1cca6
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f8cc53d0f4d - std::sys::backtrace::_print_fmt::h02af5cd6c4544f01
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/std/src/sys/backtrace.rs:66:9
   3:     0x7f8cc53d0f4d - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h4f9f0817e56b2800
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/std/src/sys/backtrace.rs:39:26
   4:     0x7f8cc542163b - core::fmt::rt::Argument::fmt::hcad5edb9a0541baa
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/core/src/fmt/rt.rs:173:76
   5:     0x7f8cc542163b - core::fmt::write::h8555e7462120c82b
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/core/src/fmt/mod.rs:1178:21
   6:     0x7f8cc53c4cb3 - std::io::Write::write_fmt::h88e8d425fe2e683c
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/std/src/io/mod.rs:1823:15
   7:     0x7f8cc53d3742 - std::sys::backtrace::BacktraceLock::print::h8e59a45eca996cf4
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/std/src/sys/backtrace.rs:42:9
   8:     0x7f8cc53d3742 - std::panicking::default_hook::{{closure}}::h594c38f77836119c
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/std/src/panicking.rs:266:22
   9:     0x7f8cc53d33ae - std::panicking::default_hook::hccd160a0a5d55adf
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/std/src/panicking.rs:293:9
  10:     0x7f8cc1896677 - std[c4314996310caec6]::panicking::update_hook::<alloc[337169187422bdd4]::boxed::Box<rustc_driver_impl[6f1f866cc0168980]::install_ice_hook::{closure#0}>>::{closure#0}
  11:     0x7f8cc53d4132 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h329701e142ea769a
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/alloc/src/boxed.rs:2164:9
  12:     0x7f8cc53d4132 - std::panicking::rust_panic_with_hook::he59475a651e09a8f
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/std/src/panicking.rs:805:13
  13:     0x7f8cc18d1b41 - std[c4314996310caec6]::panicking::begin_panic::<rustc_errors[df59a31ecd01c26]::ExplicitBug>::{closure#0}
  14:     0x7f8cc18c4856 - std[c4314996310caec6]::sys::backtrace::__rust_end_short_backtrace::<std[c4314996310caec6]::panicking::begin_panic<rustc_errors[df59a31ecd01c26]::ExplicitBug>::{closure#0}, !>
  15:     0x7f8cc18c4806 - std[c4314996310caec6]::panicking::begin_panic::<rustc_errors[df59a31ecd01c26]::ExplicitBug>
  16:     0x7f8cc18dadb1 - <rustc_errors[df59a31ecd01c26]::diagnostic::BugAbort as rustc_errors[df59a31ecd01c26]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  17:     0x7f8cc16da84d - <rustc_errors[df59a31ecd01c26]::DiagCtxtHandle>::span_bug::<rustc_span[e39f355f3aad4b6e]::span_encoding::Span, alloc[337169187422bdd4]::string::String>
  18:     0x7f8cc16fc378 - rustc_middle[e06012d79be9a3f]::util::bug::opt_span_bug_fmt::<rustc_span[e39f355f3aad4b6e]::span_encoding::Span>::{closure#0}
  19:     0x7f8cc16fc43a - rustc_middle[e06012d79be9a3f]::ty::context::tls::with_opt::<rustc_middle[e06012d79be9a3f]::util::bug::opt_span_bug_fmt<rustc_span[e39f355f3aad4b6e]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  20:     0x7f8cc16ecd5b - rustc_middle[e06012d79be9a3f]::ty::context::tls::with_context_opt::<rustc_middle[e06012d79be9a3f]::ty::context::tls::with_opt<rustc_middle[e06012d79be9a3f]::util::bug::opt_span_bug_fmt<rustc_span[e39f355f3aad4b6e]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  21:     0x7f8cc16ec577 - rustc_middle[e06012d79be9a3f]::util::bug::span_bug_fmt::<rustc_span[e39f355f3aad4b6e]::span_encoding::Span>
  22:     0x7f8cc3cf31b8 - rustc_codegen_ssa[7773436e0ac6c0fa]::mir::codegen_mir::<rustc_codegen_llvm[b31d612c732226ef]::builder::Builder>
  23:     0x7f8cc3cc70d7 - rustc_codegen_llvm[b31d612c732226ef]::base::compile_codegen_unit::module_codegen
  24:     0x7f8cc3cc4282 - <rustc_codegen_llvm[b31d612c732226ef]::LlvmCodegenBackend as rustc_codegen_ssa[7773436e0ac6c0fa]::traits::backend::ExtraBackendMethods>::compile_codegen_unit
  25:     0x7f8cc3e500a6 - <rustc_codegen_llvm[b31d612c732226ef]::LlvmCodegenBackend as rustc_codegen_ssa[7773436e0ac6c0fa]::traits::backend::CodegenBackend>::codegen_crate
  26:     0x7f8cc3eb20d8 - <rustc_interface[fe42c70767b6e180]::queries::Linker>::codegen_and_build_linker
  27:     0x7f8cc3c8dbd3 - rustc_interface[fe42c70767b6e180]::interface::run_compiler::<core[83bad7981a5dd158]::result::Result<(), rustc_span[e39f355f3aad4b6e]::ErrorGuaranteed>, rustc_driver_impl[6f1f866cc0168980]::run_compiler::{closure#0}>::{closure#1}
  28:     0x7f8cc3c33a09 - std[c4314996310caec6]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[fe42c70767b6e180]::util::run_in_thread_with_globals<rustc_interface[fe42c70767b6e180]::util::run_in_thread_pool_with_globals<rustc_interface[fe42c70767b6e180]::interface::run_compiler<core[83bad7981a5dd158]::result::Result<(), rustc_span[e39f355f3aad4b6e]::ErrorGuaranteed>, rustc_driver_impl[6f1f866cc0168980]::run_compiler::{closure#0}>::{closure#1}, core[83bad7981a5dd158]::result::Result<(), rustc_span[e39f355f3aad4b6e]::ErrorGuaranteed>>::{closure#0}, core[83bad7981a5dd158]::result::Result<(), rustc_span[e39f355f3aad4b6e]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[83bad7981a5dd158]::result::Result<(), rustc_span[e39f355f3aad4b6e]::ErrorGuaranteed>>
  29:     0x7f8cc3c337b2 - <<std[c4314996310caec6]::thread::Builder>::spawn_unchecked_<rustc_interface[fe42c70767b6e180]::util::run_in_thread_with_globals<rustc_interface[fe42c70767b6e180]::util::run_in_thread_pool_with_globals<rustc_interface[fe42c70767b6e180]::interface::run_compiler<core[83bad7981a5dd158]::result::Result<(), rustc_span[e39f355f3aad4b6e]::ErrorGuaranteed>, rustc_driver_impl[6f1f866cc0168980]::run_compiler::{closure#0}>::{closure#1}, core[83bad7981a5dd158]::result::Result<(), rustc_span[e39f355f3aad4b6e]::ErrorGuaranteed>>::{closure#0}, core[83bad7981a5dd158]::result::Result<(), rustc_span[e39f355f3aad4b6e]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[83bad7981a5dd158]::result::Result<(), rustc_span[e39f355f3aad4b6e]::ErrorGuaranteed>>::{closure#1} as core[83bad7981a5dd158]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  30:     0x7f8cc53dde4b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hc39ab0f6d5b6300a
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/alloc/src/boxed.rs:2150:9
  31:     0x7f8cc53dde4b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h516fb9fcf7cbba8c
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/alloc/src/boxed.rs:2150:9
  32:     0x7f8cc53dde4b - std::sys::pal::unix::thread::Thread::new::thread_start::h5a12be27e50b2045
                               at /rustc/176e5452095444815207be02c16de0b1487a1b53/library/std/src/sys/pal/unix/thread.rs:105:17
  33:     0x7f8cbe2a36c2 - start_thread
                               at ./nptl/pthread_create.c:447:8
  34:     0x7f8cbe31e128 - clone3
                               at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:78
  35:                0x0 - <unknown>

This is very strange since the codegen backend should not even be invoked here -- I asked only for MIR, not for LLVM IR or a compiled binary.

--emit=metadata succeeds, so rustc is able to not call codegen... it just strangely decides to call codegen with --emit=mir, it seems?

bjorn3 commented 3 months ago

--emit=metadata without other --emit flags has a lot of special behavior. For example it omits MIR for any non-const function from the generated crate metadata, even when said MIR would be necessary for codegen in downstream crates.