Skip to content

generic_const_items: Generic function pointer leads to ICEs during codegen #136156

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
fmease opened this issue Jan 27, 2025 · 2 comments · Fixed by #136168
Closed

generic_const_items: Generic function pointer leads to ICEs during codegen #136156

fmease opened this issue Jan 27, 2025 · 2 comments · Fixed by #136168
Assignees
Labels
A-monomorphization Area: Monomorphization C-bug Category: This is a bug. F-generic_const_items `#![feature(generic_const_items)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-incomplete-features This issue requires the use of incomplete features. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@fmease
Copy link
Member

fmease commented Jan 27, 2025

Reproducer:

#![feature(generic_const_items)]

const _IDENTITY<T>: fn(T) -> T = |x| x;

fn main() {}

Start of compiler output (stopping before backtrace):

warning: the feature `generic_const_items` is incomplete and may not be safe to use and/or cause compiler crashes
 --> oi.rs:1:12
  |
1 | #![feature(generic_const_items)]
  |            ^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #113521 <https://github.com/rust-lang/rust/issues/113521> for more information
  = note: `#[warn(incomplete_features)]` on by default

error: internal compiler error: compiler/rustc_codegen_llvm/src/context.rs:1236:21: `fn_abi_of_instance(<{closure@oi.rs:3:34: 3:37} as FnOnce<(T,)>>::call_once - shim, [])` failed: Layout(Unknown(T/#0))
   --> /home/fmease/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
    |
250 |     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Rest of compiler output (includes backtrace)
warning: the feature `generic_const_items` is incomplete and may not be safe to use and/or cause compiler crashes
 --> oi.rs:1:12
  |
1 | #![feature(generic_const_items)]
  |            ^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #113521 <https://github.com/rust-lang/rust/issues/113521> for more information
  = note: `#[warn(incomplete_features)]` on by default

error: internal compiler error: compiler/rustc_codegen_llvm/src/context.rs:1236:21: `fn_abi_of_instance(<{closure@oi.rs:3:34: 3:37} as FnOnce<(T,)>>::call_once - shim, [])` failed: Layout(Unknown(T/#0))
   --> /home/fmease/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
    |
250 |     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


thread 'rustc' panicked at compiler/rustc_codegen_llvm/src/context.rs:1236:21:
Box<dyn Any>
stack backtrace:
   0:     0x74ca1ac8ea90 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h2f23fd9f9d9d249c
   1:     0x74ca1b414f26 - core::fmt::write::hda581013c22cc38e
   2:     0x74ca1c378bd1 - std::io::Write::write_fmt::h7b67a2c48701ad74
   3:     0x74ca1ac8e8f2 - std::sys::backtrace::BacktraceLock::print::hf557d5f06e408e4b
   4:     0x74ca1ac90d72 - std::panicking::default_hook::{{closure}}::h003adb2133b1767b
   5:     0x74ca1ac90bfa - std::panicking::default_hook::h16009a902eb48a3c
   6:     0x74ca19e42759 - std[b112ec976dab40f4]::panicking::update_hook::<alloc[fdb5804898039ca3]::boxed::Box<rustc_driver_impl[8694186ee707eb2f]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x74ca1ac918f3 - std::panicking::rust_panic_with_hook::h1d06543d16cce998
   8:     0x74ca19e7d421 - std[b112ec976dab40f4]::panicking::begin_panic::<rustc_errors[98d48f3eae9e9f2e]::ExplicitBug>::{closure#0}
   9:     0x74ca19e72326 - std[b112ec976dab40f4]::sys::backtrace::__rust_end_short_backtrace::<std[b112ec976dab40f4]::panicking::begin_panic<rustc_errors[98d48f3eae9e9f2e]::ExplicitBug>::{closure#0}, !>
  10:     0x74ca19e720df - std[b112ec976dab40f4]::panicking::begin_panic::<rustc_errors[98d48f3eae9e9f2e]::ExplicitBug>
  11:     0x74ca19e87351 - <rustc_errors[98d48f3eae9e9f2e]::diagnostic::BugAbort as rustc_errors[98d48f3eae9e9f2e]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x74ca1a3e646c - <rustc_errors[98d48f3eae9e9f2e]::DiagCtxtHandle>::span_bug::<rustc_span[863c7cf5cfba53d9]::span_encoding::Span, alloc[fdb5804898039ca3]::string::String>
  13:     0x74ca1a46c4b7 - rustc_middle[a35b023f906b1e15]::util::bug::opt_span_bug_fmt::<rustc_span[863c7cf5cfba53d9]::span_encoding::Span>::{closure#0}
  14:     0x74ca1a451bfa - rustc_middle[a35b023f906b1e15]::ty::context::tls::with_opt::<rustc_middle[a35b023f906b1e15]::util::bug::opt_span_bug_fmt<rustc_span[863c7cf5cfba53d9]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  15:     0x74ca1a451a8b - rustc_middle[a35b023f906b1e15]::ty::context::tls::with_context_opt::<rustc_middle[a35b023f906b1e15]::ty::context::tls::with_opt<rustc_middle[a35b023f906b1e15]::util::bug::opt_span_bug_fmt<rustc_span[863c7cf5cfba53d9]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  16:     0x74ca18ed1a57 - rustc_middle[a35b023f906b1e15]::util::bug::span_bug_fmt::<rustc_span[863c7cf5cfba53d9]::span_encoding::Span>
  17:     0x74ca19cc5786 - <rustc_codegen_llvm[5b2e353c6d9ab786]::context::CodegenCx as rustc_middle[a35b023f906b1e15]::ty::layout::FnAbiOfHelpers>::handle_fn_abi_err
  18:     0x74ca19c9c6c8 - <rustc_codegen_llvm[5b2e353c6d9ab786]::context::CodegenCx as rustc_middle[a35b023f906b1e15]::ty::layout::FnAbiOf>::fn_abi_of_instance::{closure#0}
  19:     0x74ca1c31a5ce - <rustc_codegen_llvm[5b2e353c6d9ab786]::context::CodegenCx as rustc_codegen_ssa[763a256790c9aaeb]::traits::declare::PreDefineCodegenMethods>::predefine_fn
  20:     0x74ca1c317b2e - rustc_codegen_llvm[5b2e353c6d9ab786]::base::compile_codegen_unit::module_codegen
  21:     0x74ca1c402008 - <rustc_codegen_llvm[5b2e353c6d9ab786]::LlvmCodegenBackend as rustc_codegen_ssa[763a256790c9aaeb]::traits::backend::ExtraBackendMethods>::compile_codegen_unit
  22:     0x74ca1c3fe371 - <rustc_codegen_llvm[5b2e353c6d9ab786]::LlvmCodegenBackend as rustc_codegen_ssa[763a256790c9aaeb]::traits::backend::CodegenBackend>::codegen_crate
  23:     0x74ca1c405074 - <rustc_interface[7b6200b50ffd4527]::queries::Linker>::codegen_and_build_linker
  24:     0x74ca1c388a5d - rustc_interface[7b6200b50ffd4527]::passes::create_and_enter_global_ctxt::<core[1c802f461cf195ce]::option::Option<rustc_interface[7b6200b50ffd4527]::queries::Linker>, rustc_driver_impl[8694186ee707eb2f]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  25:     0x74ca1c3c3dd3 - rustc_interface[7b6200b50ffd4527]::interface::run_compiler::<(), rustc_driver_impl[8694186ee707eb2f]::run_compiler::{closure#0}>::{closure#1}
  26:     0x74ca1c2857f5 - std[b112ec976dab40f4]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[7b6200b50ffd4527]::util::run_in_thread_with_globals<rustc_interface[7b6200b50ffd4527]::util::run_in_thread_pool_with_globals<rustc_interface[7b6200b50ffd4527]::interface::run_compiler<(), rustc_driver_impl[8694186ee707eb2f]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  27:     0x74ca1c2854d9 - <<std[b112ec976dab40f4]::thread::Builder>::spawn_unchecked_<rustc_interface[7b6200b50ffd4527]::util::run_in_thread_with_globals<rustc_interface[7b6200b50ffd4527]::util::run_in_thread_pool_with_globals<rustc_interface[7b6200b50ffd4527]::interface::run_compiler<(), rustc_driver_impl[8694186ee707eb2f]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[1c802f461cf195ce]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  28:     0x74ca1c284c6f - std::sys::pal::unix::thread::Thread::new::thread_start::hf2e53aa54a2bf93d
  29:     0x74ca166a339d - <unknown>
  30:     0x74ca1672849c - <unknown>
  31:                0x0 - <unknown>

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: please make sure that you have updated to the latest nightly

note: rustc 1.86.0-nightly (1e9b0177d 2025-01-24) running on x86_64-unknown-linux-gnu

query stack during panic:
end of query stack
error: aborting due to 1 previous error; 1 warning emitted

error: process exited unsuccessfully: exit status: 101

Interestingly(?), this ICE doesn't occur if we don't coerce the closure to a function pointer:

#![feature(generic_const_items, type_alias_impl_trait)]

type Fn<T> = impl FnOnce(T) -> T;
const _IDENTITY<T>: Fn<T> = |x| x;

fn main() { /* _IDENTITY/*::<i32>*/(23); */ }
@fmease fmease added A-closures Area: Closures (`|…| { … }`) A-codegen Area: Code generation A-layout Area: Memory layout of types C-bug Category: This is a bug. F-generic_const_items `#![feature(generic_const_items)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-incomplete-features This issue requires the use of incomplete features. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 27, 2025
@fmease fmease self-assigned this Jan 27, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 27, 2025
@fmease fmease changed the title ICE: generic_const_items: Generic closure leads to ICE as function pointer during LLVM codegen due to unknown layout when computing the fn ABI ICE: generic_const_items: Generic closure leads to ICE during LLVM codegen when coerced to function pointer due to unknown layout when computing the fn ABI Jan 27, 2025
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 27, 2025
@fmease fmease changed the title ICE: generic_const_items: Generic closure leads to ICE during LLVM codegen when coerced to function pointer due to unknown layout when computing the fn ABI generic_const_items: Generic function pointer leads to ICE during codegen due to unknown layout when computing the fn ABI Jan 28, 2025
@fmease fmease removed the A-closures Area: Closures (`|…| { … }`) label Jan 28, 2025
@fmease
Copy link
Member Author

fmease commented Jan 28, 2025

Closure is irrelevant:

#![feature(generic_const_items)]

const _IDENTITY<T>: fn(T) -> T = identity;
fn identity<T>(x: T) -> T { x }

fn main() {}

Furthermore, replacing the reference of identity with std::convert::identity prevents the ICE.

@fmease
Copy link
Member Author

fmease commented Jan 28, 2025

Ah, and the following leads to a different ICE:

#![feature(generic_const_items)]

const _CONSUME<T>: fn(T) = |_| (); // or the below
//const _CONSUME<T>: fn(T) = consume;
//fn consume<T>(_: T) {}

fn main() {}
error: internal compiler error: compiler/rustc_middle/src/ty/instance.rs:603:21: failed to resolve instance for std::ptr::drop_in_place::<T>

@fmease fmease removed the A-layout Area: Memory layout of types label Jan 28, 2025
@fmease fmease changed the title generic_const_items: Generic function pointer leads to ICE during codegen due to unknown layout when computing the fn ABI generic_const_items: Generic function pointer leads to ICEs during codegen Jan 28, 2025
@fmease fmease added A-monomorphization Area: Monomorphization and removed A-codegen Area: Code generation labels Jan 28, 2025
@bors bors closed this as completed in e37b744 Jan 29, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 29, 2025
Rollup merge of rust-lang#136168 - fmease:gci-fix-mono, r=compiler-errors

GCI: Don't try to eval / collect mono items inside overly generic free const items

Fixes rust-lang#136156. Thanks for the pointers, errs!

There's one (preexisting) thing of note (maybe?). There's a difference between `const _: () = panic!();` and `const _<'a>: () = panic!();`: The former is a pre-mono error, the latter is a post-mono error. For comparison, both `fn _f() { const { panic!() } }` and `fn _f<'a: 'a>() { const { panic!() } }` are post-mono errors.

cc `@oli-obk`
r? compiler-errors or reassign
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-monomorphization Area: Monomorphization C-bug Category: This is a bug. F-generic_const_items `#![feature(generic_const_items)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-incomplete-features This issue requires the use of incomplete features. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Development

Successfully merging a pull request may close this issue.

2 participants